From 8b3d6a2c2a3aa54de517856dc90cb55402f7a852 Mon Sep 17 00:00:00 2001 From: Gang Li Date: Mon, 23 Sep 2024 10:32:54 +0800 Subject: [PATCH] Add config option to be able to use non-default charon configuration --- .github/workflows/unittests.yaml | 2 +- charon/cmd/cmd_cache.py | 26 ++++++++++++++++++++++---- charon/cmd/cmd_checksum.py | 11 ++++++++++- charon/cmd/cmd_delete.py | 11 ++++++++++- charon/cmd/cmd_index.py | 11 ++++++++++- charon/cmd/cmd_upload.py | 14 ++++++++++++-- charon/config.py | 6 ++++-- charon/pkgs/maven.py | 5 +++-- charon/pkgs/npm.py | 5 +++-- tests/test_config.py | 22 ++++++++++++++++++++++ 10 files changed, 97 insertions(+), 16 deletions(-) diff --git a/.github/workflows/unittests.yaml b/.github/workflows/unittests.yaml index bee62848..b1eae31b 100644 --- a/.github/workflows/unittests.yaml +++ b/.github/workflows/unittests.yaml @@ -31,7 +31,7 @@ jobs: run: python -m tox -e test - name: Upload pytest html report - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 if: failure() with: path: __pytest_reports/charon-unit-tests.html diff --git a/charon/cmd/cmd_cache.py b/charon/cmd/cmd_cache.py index 65cf87fb..e7f55282 100644 --- a/charon/cmd/cmd_cache.py +++ b/charon/cmd/cmd_cache.py @@ -58,6 +58,14 @@ format of CF defining too, and each path should be in a single line. """ ) +@option( + "--config", + "-c", + help=""" + The charon configuration yaml file path. Default is + $HOME/.charon/charon.yaml + """ +) @option( "--debug", "-D", @@ -79,6 +87,7 @@ def invalidate( target: str, paths: List[str], path_file: str, + config: str = None, quiet: bool = False, debug: bool = False ): @@ -110,7 +119,7 @@ def invalidate( break try: - (buckets, aws_profile) = _init_cmd(target) + (buckets, aws_profile) = _init_cmd(target, config) for b in buckets: cf_client = CFClient(aws_profile=aws_profile) @@ -144,6 +153,14 @@ def invalidate( """, required=True ) +@option( + "--config", + "-c", + help=""" + The charon configuration yaml file path. Default is + $HOME/.charon/charon.yaml + """ +) @option( "--debug", "-D", @@ -164,6 +181,7 @@ def invalidate( def check( invalidation_id: str, target: str, + config: str = None, quiet: bool = False, debug: bool = False ): @@ -175,7 +193,7 @@ def check( is_quiet=quiet, is_debug=debug, use_log_file=False ) try: - (buckets, aws_profile) = _init_cmd(target) + (buckets, aws_profile) = _init_cmd(target, config) if not buckets: sys.exit(1) @@ -203,8 +221,8 @@ def check( sys.exit(2) -def _init_cmd(target: str) -> Tuple[List[Tuple[str, str, str, str, str]], str]: - conf = get_config() +def _init_cmd(target: str, config: str) -> Tuple[List[Tuple[str, str, str, str, str]], str]: + conf = get_config(config) if not conf: sys.exit(1) diff --git a/charon/cmd/cmd_checksum.py b/charon/cmd/cmd_checksum.py index e628d825..6940798f 100644 --- a/charon/cmd/cmd_checksum.py +++ b/charon/cmd/cmd_checksum.py @@ -191,11 +191,20 @@ def validate( """, required=True ) +@option( + "--config", + "-c", + help=""" + The charon configuration yaml file path. Default is + $HOME/.charon/charon.yaml + """ +) @command() def refresh( target: str, paths: List[str], path_file: str, + config: str = None, quiet: bool = False, debug: bool = False ): @@ -219,7 +228,7 @@ def refresh( if paths: work_paths.extend(paths) - conf = get_config() + conf = get_config(config) aws_profile = os.getenv("AWS_PROFILE") or conf.get_aws_profile() if not aws_profile: logger.error("No AWS profile specified!") diff --git a/charon/cmd/cmd_delete.py b/charon/cmd/cmd_delete.py index d4752f26..7d9ae5f0 100644 --- a/charon/cmd/cmd_delete.py +++ b/charon/cmd/cmd_delete.py @@ -96,6 +96,14 @@ be extracted, when needed. """, ) +@option( + "--config", + "-c", + help=""" + The charon configuration yaml file path. Default is + $HOME/.charon/charon.yaml + """ +) @option( "--debug", "-D", @@ -120,6 +128,7 @@ def delete( root_path="maven-repository", ignore_patterns: List[str] = None, work_dir: str = None, + config: str = None, debug=False, quiet=False, dryrun=False @@ -136,7 +145,7 @@ def delete( "no files will be deleted.") if not _validate_prod_key(product, version): return - conf = get_config() + conf = get_config(config) if not conf: sys.exit(1) diff --git a/charon/cmd/cmd_index.py b/charon/cmd/cmd_index.py index e27c5033..14e0deaa 100644 --- a/charon/cmd/cmd_index.py +++ b/charon/cmd/cmd_index.py @@ -42,6 +42,14 @@ """, required=True ) +@option( + "--config", + "-c", + help=""" + The charon configuration yaml file path. Default is + $HOME/.charon/charon.yaml + """ +) @option( "--debug", "-D", @@ -61,6 +69,7 @@ def index( path: str, target: str, + config: str = None, debug: bool = False, quiet: bool = False, dryrun: bool = False @@ -73,7 +82,7 @@ def index( is_quiet=quiet, is_debug=debug, use_log_file=False ) try: - conf = get_config() + conf = get_config(config) if not conf: sys.exit(1) diff --git a/charon/cmd/cmd_upload.py b/charon/cmd/cmd_upload.py index 55696c2e..a55c5e0f 100644 --- a/charon/cmd/cmd_upload.py +++ b/charon/cmd/cmd_upload.py @@ -97,6 +97,14 @@ be extracted, when needed. """, ) +@option( + "--config", + "-c", + help=""" + The charon configuration yaml file path. Default is + $HOME/.charon/charon.yaml + """ +) @option( "--contain_signature", "-s", @@ -137,6 +145,7 @@ def upload( root_path="maven-repository", ignore_patterns: List[str] = None, work_dir: str = None, + config: str = None, contain_signature: bool = False, sign_key: str = "redhatdevel", debug=False, @@ -155,7 +164,7 @@ def upload( "no files will be uploaded.") if not _validate_prod_key(product, version): return - conf = get_config() + conf = get_config(config) if not conf: sys.exit(1) @@ -211,7 +220,8 @@ def upload( cf_enable=conf.is_aws_cf_enable(), key=sign_key, dry_run=dryrun, - manifest_bucket_name=manifest_bucket_name + manifest_bucket_name=manifest_bucket_name, + config=config ) if not succeeded: sys.exit(1) diff --git a/charon/config.py b/charon/config.py index f9b6403c..84b4aec4 100644 --- a/charon/config.py +++ b/charon/config.py @@ -68,8 +68,10 @@ def is_aws_cf_enable(self) -> bool: return self.__aws_cf_enable -def get_config() -> Optional[CharonConfig]: - config_file_path = os.path.join(os.getenv("HOME"), ".charon", CONFIG_FILE) +def get_config(cfgPath=None) -> Optional[CharonConfig]: + config_file_path = cfgPath + if not config_file_path or not os.path.isfile(config_file_path): + config_file_path = os.path.join(os.getenv("HOME"), ".charon", CONFIG_FILE) data = read_yaml_from_file_path(config_file_path, 'schemas/charon.json') return CharonConfig(data) diff --git a/charon/pkgs/maven.py b/charon/pkgs/maven.py index f8ba8abc..f93d7ba1 100644 --- a/charon/pkgs/maven.py +++ b/charon/pkgs/maven.py @@ -272,7 +272,8 @@ def handle_maven_uploading( cf_enable=False, key=None, dry_run=False, - manifest_bucket_name=None + manifest_bucket_name=None, + config=None ) -> Tuple[str, bool]: """ Handle the maven product release tarball uploading process. * repo is the location of the tarball in filesystem @@ -406,7 +407,7 @@ def handle_maven_uploading( # 10. Generate signature file if contain_signature is set to True if gen_sign: - conf = get_config() + conf = get_config(config) if not conf: sys.exit(1) suffix_list = __get_suffix(PACKAGE_TYPE_MAVEN, conf) diff --git a/charon/pkgs/npm.py b/charon/pkgs/npm.py index 2fd6bee3..33d0c961 100644 --- a/charon/pkgs/npm.py +++ b/charon/pkgs/npm.py @@ -87,7 +87,8 @@ def handle_npm_uploading( cf_enable=False, key=None, dry_run=False, - manifest_bucket_name=None + manifest_bucket_name=None, + config=None ) -> Tuple[str, bool]: """ Handle the npm product release tarball uploading process. For NPM uploading, tgz file and version metadata will be relocated based @@ -189,7 +190,7 @@ def handle_npm_uploading( logger.info("package.json uploading done") if gen_sign: - conf = get_config() + conf = get_config(config) if not conf: sys.exit(1) suffix_list = __get_suffix(PACKAGE_TYPE_NPM, conf) diff --git a/tests/test_config.py b/tests/test_config.py index 25bd4649..ef55f362 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -22,8 +22,10 @@ import charon.config as config import re +import tempfile from charon.constants import DEFAULT_REGISTRY +from charon.utils.files import overwrite_file from tests.base import BaseTest @@ -49,6 +51,26 @@ def test_no_config(self): with pytest.raises(FileNotFoundError): config.get_config() + def test_non_default_config(self): + self.__base.setUp() + config_content = """ +ignore_patterns: + - ".*^(redhat).*" + +targets: + changed: + - bucket: changed-bucket + prefix: changed-prefix + """ + _, tmp_config_file = tempfile.mkstemp(prefix="charon-test-config", suffix=".yaml") + overwrite_file(tmp_config_file, config_content) + conf = config.get_config(tmp_config_file) + self.assertIsNotNone(conf) + self.assertEqual("changed-bucket", conf.get_target("changed")[0].get('bucket', '')) + self.assertEqual("changed-prefix", conf.get_target("changed")[0].get('prefix', '')) + self.assertEqual([], conf.get_target("ga")) + os.remove(tmp_config_file) + def test_config_missing_targets(self): content_missing_targets = """ ignore_patterns: