Fix dmaap-datarouter test to run with py36
[integration/csit.git] / scripts / dmaap-datarouter / remove_cert_from_ca.py
1 #
2 # ============LICENSE_START=======================================================
3 #  Copyright (C) 2019 Nordix Foundation.
4 # ================================================================================
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17 # SPDX-License-Identifier: Apache-2.0
18 # ============LICENSE_END=========================================================
19 #
20
21 import certifi
22 import os
23
24 cafile = certifi.where()
25 number_of_lines_to_delete = 39
26 count = 0
27 dr_cert_exists = False
28
29 with open(cafile, 'r+b', buffering=0) as outfile:
30     for line in outfile.readlines()[-35:-34]:
31         if '# Serial: 0x9EAEEDC0A7CEB59D'.encode() in line:
32             dr_cert_exists = True
33     if dr_cert_exists:
34         outfile.seek(0, os.SEEK_END)
35         end = outfile.tell()
36         while outfile.tell() > 0:
37             outfile.seek(-1, os.SEEK_CUR)
38             char = outfile.read(1)
39             if char == b'\n':
40                 count += 1
41             if count == number_of_lines_to_delete:
42                 outfile.truncate()
43                 print("Removed " + str(number_of_lines_to_delete) + " lines from end of CA File")
44                 exit(0)
45             outfile.seek(-1, os.SEEK_CUR)
46     else:
47         print("No DR cert in CA File to remove")
48
49 if count < number_of_lines_to_delete + 1:
50     print("Number of lines in file less than number of lines to delete. Exiting...")
51     exit(1)