demx8as6 | b5ec030 | 2023-07-06 14:09:11 +0000 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ################################################################################ |
| 3 | # Copyright 2023 highstreet technologies GmbH |
| 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 | |
| 18 | import os |
| 19 | import argparse |
| 20 | |
| 21 | default_ip_address = 'aaa.bbb.ccc.ddd' |
| 22 | default_http_domain = 'smo.o-ran-sc.org' |
| 23 | |
| 24 | script_name = os.path.basename(__file__) |
| 25 | directory_path = os.path.dirname(__file__) |
| 26 | file_extensions = ['.env', '.yaml', '.json'] |
| 27 | |
| 28 | parser = argparse.ArgumentParser(script_name) |
| 29 | required = parser.add_argument_group('required named arguments') |
| 30 | required.add_argument("-i", "--ip_address", help="The remote accessable IP address of this system.", type=str, required=True) |
| 31 | parser.add_argument("-d", "--http_domain", help="The http domain. Default is " + default_http_domain + ".", type=str, default=default_http_domain) |
| 32 | parser.add_argument("-r", "--revert", help="Reverts the previous made changes.", action='store_true') |
| 33 | args = parser.parse_args() |
| 34 | |
| 35 | def find_replace(directory, find_text, replace_text, extensions): |
| 36 | for root, dirs, files in os.walk(directory): |
| 37 | for file_name in files: |
| 38 | file_path = os.path.join(root, file_name) |
| 39 | file_ext = os.path.splitext(file_name)[1] |
| 40 | if file_ext in extensions or file_name in extensions: |
| 41 | with open(file_path, 'r') as file: |
| 42 | content = file.read() |
| 43 | if find_text in content: |
| 44 | updated_content = content.replace(find_text, replace_text) |
| 45 | with open(file_path, 'w') as file: |
| 46 | file.write(updated_content) |
| 47 | print(f"Replaced '{find_text}' with '{replace_text}' in '{file_path}'") |
| 48 | |
| 49 | if args.revert == False: |
| 50 | # replace ip |
| 51 | find_replace(directory_path, default_ip_address, args.ip_address, file_extensions) |
| 52 | |
| 53 | # replace domain |
| 54 | if not args.http_domain == default_http_domain: |
| 55 | find_replace(directory_path, default_http_domain, args.http_domain, file_extensions) |
| 56 | else: |
| 57 | # revert back ip |
| 58 | find_replace(directory_path, args.ip_address, default_ip_address, file_extensions) |
| 59 | |
| 60 | # revert back domain |
| 61 | if not args.http_domain == default_http_domain: |
| 62 | find_replace(directory_path, args.http_domain, default_http_domain, file_extensions) |