From 56186c2933cd507f6c31659f9ae4ee1fb4a83a0e Mon Sep 17 00:00:00 2001 From: Markus Lehtonen Date: Fri, 7 Feb 2014 14:27:47 +0200 Subject: [PATCH] conf: method for getting combined cmdline/conffile options Add a new method for getting options which the user can give through command line options and/or in config file(s). Command line option will take precedence over what is configured in config file(s). Removes a lot of duplicate code. Change-Id: I06f5b1b6c231829ca490553025c2e4e81aa0abe2 Signed-off-by: Markus Lehtonen --- gitbuildsys/cmd_clone.py | 10 ++-------- gitbuildsys/cmd_export.py | 30 ++++++------------------------ gitbuildsys/cmd_import.py | 10 ++-------- gitbuildsys/cmd_pull.py | 5 +---- gitbuildsys/conf.py | 10 ++++++++++ 5 files changed, 21 insertions(+), 44 deletions(-) diff --git a/gitbuildsys/cmd_clone.py b/gitbuildsys/cmd_clone.py index 438aeb2..d7fd7ec 100644 --- a/gitbuildsys/cmd_clone.py +++ b/gitbuildsys/cmd_clone.py @@ -36,14 +36,8 @@ def main(args): """gbs clone entry point.""" # Determine upstream branch - if args.upstream_branch: - upstream_branch = args.upstream_branch - else: - upstream_branch = configmgr.get('upstream_branch', 'general') - if args.packaging_branch: - packaging_branch = args.packaging_branch - else: - packaging_branch = configmgr.get('packaging_branch', 'general') + upstream_branch = configmgr.get_arg_conf(args, 'upstream_branch') + packaging_branch = configmgr.get_arg_conf(args, 'packaging_branch') # Construct GBP cmdline arguments gbp_args = ['dummy argv[0]', '--color-scheme=magenta:green:yellow:red', diff --git a/gitbuildsys/cmd_export.py b/gitbuildsys/cmd_export.py index ef8725d..3f26d98 100644 --- a/gitbuildsys/cmd_export.py +++ b/gitbuildsys/cmd_export.py @@ -53,20 +53,14 @@ def is_native_pkg(repo, args): """ Determine if the package is "native" """ - if args.upstream_branch: - upstream_branch = args.upstream_branch - else: - upstream_branch = configmgr.get('upstream_branch', 'general') + upstream_branch = configmgr.get_arg_conf(args, 'upstream_branch') return not repo.has_branch(upstream_branch) def get_packaging_dir(args): """ Determine the packaging dir to be used """ - if args.packaging_dir: - path = args.packaging_dir - else: - path = configmgr.get('packaging_dir', 'general') + path = configmgr.get_arg_conf(args, 'packaging_dir') return path.rstrip(os.sep) def check_export_branches(repo, args): @@ -77,10 +71,7 @@ def check_export_branches(repo, args): remote_branches = {} for branch in repo.get_remote_branches(): remote_branches[branch.split('/')[-1]] = branch - if args.upstream_branch: - upstream_branch = args.upstream_branch - else: - upstream_branch = configmgr.get('upstream_branch', 'general') + upstream_branch = configmgr.get_arg_conf(args, 'upstream_branch') # track upstream/pristine-tar branch for br in [upstream_branch, 'pristine-tar']: @@ -93,14 +84,8 @@ def create_gbp_export_args(repo, commit, export_dir, tmp_dir, spec, args, """ Construct the cmdline argument list for git-buildpackage export """ - if args.upstream_branch: - upstream_branch = args.upstream_branch - else: - upstream_branch = configmgr.get('upstream_branch', 'general') - if args.upstream_tag: - upstream_tag = args.upstream_tag - else: - upstream_tag = configmgr.get('upstream_tag', 'general') + upstream_branch = configmgr.get_arg_conf(args, 'upstream_branch') + upstream_tag = configmgr.get_arg_conf(args, 'upstream_tag') # transform variables from shell to python convention ${xxx} -> %(xxx)s upstream_tag = re.sub(r'\$\{([^}]+)\}', r'%(\1)s', upstream_tag) @@ -108,10 +93,7 @@ def create_gbp_export_args(repo, commit, export_dir, tmp_dir, spec, args, log.debug("Using upstream tag format: '%s'" % upstream_tag) # Get patch squashing option - if args.squash_patches_until: - squash_patches_until = args.squash_patches_until - else: - squash_patches_until = configmgr.get('squash_patches_until', 'general') + squash_patches_until = configmgr.get_arg_conf(args, 'squash_patches_until') # Determine the remote repourl reponame = "" diff --git a/gitbuildsys/cmd_import.py b/gitbuildsys/cmd_import.py index 1ade47c..47b5f2d 100644 --- a/gitbuildsys/cmd_import.py +++ b/gitbuildsys/cmd_import.py @@ -44,14 +44,8 @@ def main(args): dirn=configmgr.get('tmpdir', 'general'), directory=True) - if args.upstream_branch: - upstream_branch = args.upstream_branch - else: - upstream_branch = configmgr.get('upstream_branch', 'general') - if args.upstream_tag: - upstream_tag = args.upstream_tag - else: - upstream_tag = configmgr.get('upstream_tag', 'general') + upstream_branch = configmgr.get_arg_conf(args, 'upstream_branch') + upstream_tag = configmgr.get_arg_conf(args, 'upstream_tag') # transform variables from shell to python convention ${xxx} -> %(xxx)s upstream_tag = re.sub(r'\$\{([^}]+)\}', r'%(\1)s', upstream_tag) diff --git a/gitbuildsys/cmd_pull.py b/gitbuildsys/cmd_pull.py index 378b7d7..c7bc927 100644 --- a/gitbuildsys/cmd_pull.py +++ b/gitbuildsys/cmd_pull.py @@ -36,10 +36,7 @@ def main(args): """gbs pull entry point.""" # Determine upstream branch - if args.upstream_branch: - upstream_branch = args.upstream_branch - else: - upstream_branch = configmgr.get('upstream_branch', 'general') + upstream_branch = configmgr.get_arg_conf(args, 'upstream_branch') # Construct GBP cmdline arguments gbp_args = ['dummy argv[0]', diff --git a/gitbuildsys/conf.py b/gitbuildsys/conf.py index 5d8a060..43d1ee8 100644 --- a/gitbuildsys/conf.py +++ b/gitbuildsys/conf.py @@ -407,6 +407,16 @@ url = http://download.tizen.org/releases/daily/trunk/ivi/latest/ else: return self._get(opt, section) + def get_arg_conf(self, args, opt, section='general'): + """get value from command line arguments if found there, otherwise fall + back to config + """ + if hasattr(args, opt): + value = getattr(args, opt) + if value is not None: + return value + return self.get(opt, section) + @staticmethod def update(cfgparsers): 'update changed values into files on disk' -- 2.7.4