chore(misc): Add standard python script guard to charts-local.py

This adds a `if __name__ == "__main__"` guard to the charts-local.py script.
The guard should always be used to make it obvious to developers that this is
the entrypoint of the script and to prevent accidental side effects when handling
the script as a python module in any way (e.g. during interactive debugging).
This commit is contained in:
Lilly Sell
2025-12-03 10:23:27 +01:00
parent 1b9843d579
commit 3d7d538626

View File

@@ -21,40 +21,6 @@ import configargparse
from pathlib import Path from pathlib import Path
from git import Repo from git import Repo
p = configargparse.ArgParser()
p.add('--branch', env_var='CHART_DEV_BRANCH', help='The branch you want to work with. Will be created by the script if it does not exist yet.')
p.add('--git_hostname', env_var='GIT_HOSTNAME', default='git@gitlab.opencode.de', help='Set the hostname for the chart git checkouts.')
p.add('--revert', default=False, action='store_true', help='Set this parameter if you want to revert the referencing of the local helm chart checkout paths in the helmfiles.')
p.add('--match', default='', help="Clone/pull only charts that contain the given string in their name.")
p.add('--loglevel', env_var='LOGLEVEL', default='DEBUG', help='Set the loglevel: DEBUG, INFO, WARNING, ERROR, CRITICAL-')
options = p.parse_args()
script_path = os.path.dirname(os.path.realpath(__file__))
# some static definitions
log_path = script_path+'/../logs'
charts_yaml = script_path+'/../helmfile/environments/default/charts.yaml.gotmpl'
base_repo_path = script_path+'/..'
base_helmfile = base_repo_path+'/helmfile_generic.yaml.gotmpl'
helmfile_backup_extension = '.bak'
Path(log_path).mkdir(parents=True, exist_ok=True)
logFormatter = logging.Formatter("%(asctime)s %(levelname)-5.5s %(message)s")
rootLogger = logging.getLogger()
rootLogger.setLevel(options.loglevel)
fileHandler = logging.FileHandler("{0}/{1}.log".format(log_path, os.path.basename(__file__)))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
logging.debug(f"Working with relative paths from script location: {script_path}")
logging.debug(f"Log directory: {log_path}")
logging.debug(f"charts.yaml.gotmpl: {charts_yaml}")
def create_or_switch_branch_base_repo(): def create_or_switch_branch_base_repo():
base_repo = Repo(path=base_repo_path) base_repo = Repo(path=base_repo_path)
@@ -196,11 +162,50 @@ def revert_the_helmfiles():
## ##
## Main program ## Main program
## ##
if options.revert: if __name__ == "__main__":
revert_the_helmfiles() p = configargparse.ArgParser()
else: p.add('--branch', env_var='CHART_DEV_BRANCH',
branch = create_or_switch_branch_base_repo() help='The branch you want to work with. Will be created by the script if it does not exist yet.')
with open(charts_yaml, 'r') as file: p.add('--git_hostname', env_var='GIT_HOSTNAME', default='git@gitlab.opencode.de',
charts = yaml.safe_load(file) help='Set the hostname for the chart git checkouts.')
charts_dict = clone_charts_locally(branch, charts) p.add('--revert', default=False, action='store_true',
process_the_helmfiles(charts_dict, charts) help='Set this parameter if you want to revert the referencing of the local helm chart checkout paths in the helmfiles.')
p.add('--match', default='', help="Clone/pull only charts that contain the given string in their name.")
p.add('--loglevel', env_var='LOGLEVEL', default='DEBUG',
help='Set the loglevel: DEBUG, INFO, WARNING, ERROR, CRITICAL-')
options = p.parse_args()
script_path = os.path.dirname(os.path.realpath(__file__))
# some static definitions
log_path = script_path + '/../logs'
charts_yaml = script_path + '/../helmfile/environments/default/charts.yaml.gotmpl'
base_repo_path = script_path + '/..'
base_helmfile = base_repo_path + '/helmfile_generic.yaml.gotmpl'
helmfile_backup_extension = '.bak'
Path(log_path).mkdir(parents=True, exist_ok=True)
logFormatter = logging.Formatter("%(asctime)s %(levelname)-5.5s %(message)s")
rootLogger = logging.getLogger()
rootLogger.setLevel(options.loglevel)
fileHandler = logging.FileHandler("{0}/{1}.log".format(log_path, os.path.basename(__file__)))
fileHandler.setFormatter(logFormatter)
rootLogger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
rootLogger.addHandler(consoleHandler)
logging.debug(f"Working with relative paths from script location: {script_path}")
logging.debug(f"Log directory: {log_path}")
logging.debug(f"charts.yaml.gotmpl: {charts_yaml}")
if options.revert:
revert_the_helmfiles()
else:
branch = create_or_switch_branch_base_repo()
with open(charts_yaml, 'r') as file:
charts = yaml.safe_load(file)
charts_dict = clone_charts_locally(branch, charts)
process_the_helmfiles(charts_dict, charts)