conf: method for getting combined cmdline/conffile options
authorMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Fri, 7 Feb 2014 12:27:47 +0000 (14:27 +0200)
committerMarkus Lehtonen <markus.lehtonen@linux.intel.com>
Tue, 11 Feb 2014 08:32:27 +0000 (10:32 +0200)
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 <markus.lehtonen@linux.intel.com>
gitbuildsys/cmd_clone.py
gitbuildsys/cmd_export.py
gitbuildsys/cmd_import.py
gitbuildsys/cmd_pull.py
gitbuildsys/conf.py

index 438aeb2..d7fd7ec 100644 (file)
@@ -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',
index ef8725d..3f26d98 100644 (file)
@@ -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 = ""
index 1ade47c..47b5f2d 100644 (file)
@@ -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)
 
index 378b7d7..c7bc927 100644 (file)
@@ -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]',
index 5d8a060..43d1ee8 100644 (file)
@@ -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'