chiark / gitweb /
Don't pass config variables as arguments - much simpler code
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 8 Nov 2013 19:44:27 +0000 (20:44 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Fri, 8 Nov 2013 19:44:27 +0000 (20:44 +0100)
fdroidserver/build.py
fdroidserver/checkupdates.py
fdroidserver/common.py
fdroidserver/scanner.py

index 3c0343adabd7644862fd4c2b5c3039ddde33aeb9..6af132ed1c13f02e78728fafb8c96ebef3d47217 100644 (file)
@@ -63,7 +63,7 @@ def got_valid_builder_vm():
 
 
 # Note that 'force' here also implies test mode.
-def build_server(app, thisbuild, vcs, build_dir, output_dir, sdk_path, force):
+def build_server(app, thisbuild, vcs, build_dir, output_dir, force):
     """Do a build on the build server."""
 
     import ssh
@@ -256,7 +256,7 @@ def build_server(app, thisbuild, vcs, build_dir, output_dir, sdk_path, force):
                 name, _ = lib.split('@')
                 if options.verbose:
                     print "Processing srclib '" + name + "'"
-                srclibpaths.append((name, common.getsrclib(lib, 'build/srclib', sdk_path, basepath=True, prepare=False)))
+                srclibpaths.append((name, common.getsrclib(lib, 'build/srclib', basepath=True, prepare=False)))
         # If one was used for the main source, add that too.
         basesrclib = vcs.getsrclib()
         if basesrclib:
@@ -342,8 +342,7 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
 
     # Prepare the source code...
     root_dir, srclibpaths = common.prepare_source(vcs, app, thisbuild,
-            build_dir, srclib_dir, extlib_dir, config['sdk_path'], config['ndk_path'],
-            config['javacc_path'], config['mvn3'], onserver)
+            build_dir, srclib_dir, extlib_dir, onserver)
 
     # We need to clean via the build tool in case the binary dirs are
     # different from the default ones
@@ -412,19 +411,15 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
 
     # Run a build command if one is required...
     if 'build' in thisbuild:
-        cmd = thisbuild['build']
+        cmd = common.replace_config_vars(thisbuild['build'])
         # Substitute source library paths into commands...
         for name, libpath in srclibpaths:
             libpath = os.path.relpath(libpath, root_dir)
             cmd = cmd.replace('$$' + name + '$$', libpath)
-        cmd = cmd.replace('$$SDK$$', config['sdk_path'])
-        cmd = cmd.replace('$$NDK$$', config['ndk_path'])
-        cmd = cmd.replace('$$MVN3$$', config['mvn3'])
         if options.verbose:
             print "Running 'build' commands in %s" % root_dir
 
-        p = FDroidPopen(['bash', '-x', '-c', cmd],
-                cwd=root_dir)
+        p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
         
         if p.returncode != 0:
             raise BuildException("Error running build command for %s:%s" %
@@ -691,7 +686,7 @@ def trybuild(app, thisbuild, build_dir, output_dir, also_check_dir, srclib_dir,
         # grabbing the source now.
         vcs.gotorevision(thisbuild['commit'])
 
-        build_server(app, thisbuild, vcs, build_dir, output_dir, config['sdk_path'], force)
+        build_server(app, thisbuild, vcs, build_dir, output_dir, force)
     else:
         build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, install, force, onserver)
     return True
@@ -856,7 +851,7 @@ def main():
                     if options.verbose:
                         print "Getting {0} vcs interface for {1}".format(
                                 app['Repo Type'], app['Repo'])
-                    vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir, config['sdk_path'])
+                    vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
 
                     first = False
 
index 1e908391c7ef74ce983511fbe2de247f20d59753..5b74fa370104b5b028c9bb197f7c3323ed66664d 100644 (file)
@@ -82,7 +82,7 @@ def check_http(app):
 # caution, because it's inappropriate for many projects.
 # Returns (None, "a message") if this didn't work, or (version, vercode) for
 # the details of the current version.
-def check_tags(app, sdk_path):
+def check_tags(app):
 
     try:
 
@@ -97,7 +97,7 @@ def check_tags(app, sdk_path):
             return (None, 'Tags update mode only works for git, hg, bzr and git-svn repositories currently', None)
 
         # Set up vcs interface and make sure we have the latest code...
-        vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir, sdk_path)
+        vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
 
         vcs.gotorevision(None)
 
@@ -146,7 +146,7 @@ def check_tags(app, sdk_path):
 # caution, because it's inappropriate for many projects.
 # Returns (None, "a message") if this didn't work, or (version, vercode) for
 # the details of the current version.
-def check_repomanifest(app, sdk_path, branch=None):
+def check_repomanifest(app, branch=None):
 
     try:
 
@@ -158,7 +158,7 @@ def check_repomanifest(app, sdk_path, branch=None):
             repotype = app['Repo Type']
 
         # Set up vcs interface and make sure we have the latest code...
-        vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir, sdk_path)
+        vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
 
         if repotype == 'git':
             if branch:
@@ -208,7 +208,7 @@ def check_repomanifest(app, sdk_path, branch=None):
         msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
         return (None, msg)
 
-def check_repotrunk(app, sdk_path, branch=None):
+def check_repotrunk(app, branch=None):
 
     try:
         if app['Repo Type'] == 'srclib':
@@ -222,7 +222,7 @@ def check_repotrunk(app, sdk_path, branch=None):
             return (None, 'RepoTrunk update mode only makes sense in svn and git-svn repositories')
 
         # Set up vcs interface and make sure we have the latest code...
-        vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir, sdk_path)
+        vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
 
         vcs.gotorevision(None)
 
@@ -340,13 +340,13 @@ def main():
         vercode = None
         mode = app['Update Check Mode']
         if mode == 'Tags':
-            (version, vercode, tag) = check_tags(app, config['sdk_path'])
+            (version, vercode, tag) = check_tags(app)
         elif mode == 'RepoManifest':
-            (version, vercode) = check_repomanifest(app, config['sdk_path'])
+            (version, vercode) = check_repomanifest(app)
         elif mode.startswith('RepoManifest/'):
-            (version, vercode) = check_repomanifest(app, config['sdk_path'], mode[13:])
+            (version, vercode) = check_repomanifest(app, mode[13:])
         elif mode == 'RepoTrunk':
-            (version, vercode) = check_repotrunk(app, config['sdk_path'])
+            (version, vercode) = check_repotrunk(app)
         elif mode == 'HTTP':
             (version, vercode) = check_http(app)
         elif mode == 'Static':
@@ -384,8 +384,7 @@ def main():
                 else:
                     app_dir = os.path.join('build/', app['id'])
 
-                vcs = common.getvcs(app["Repo Type"], app["Repo"], app_dir,
-                        config['sdk_path'])
+                vcs = common.getvcs(app["Repo Type"], app["Repo"], app_dir)
                 vcs.gotorevision(tag)
 
                 flavour = None
index a697154f36690d44b453dde0e9bf3b9af15051b8..6bc3b41ec89b54e66692aa299003557b5b67cbb0 100644 (file)
@@ -67,21 +67,21 @@ def read_config(opts, config_file='config.py'):
     return config
 
 
-def getvcs(vcstype, remote, local, sdk_path):
+def getvcs(vcstype, remote, local):
     if vcstype == 'git':
-        return vcs_git(remote, local, sdk_path)
+        return vcs_git(remote, local)
     if vcstype == 'svn':
-        return vcs_svn(remote, local, sdk_path)
+        return vcs_svn(remote, local)
     if vcstype == 'git-svn':
-        return vcs_gitsvn(remote, local, sdk_path)
+        return vcs_gitsvn(remote, local)
     if vcstype == 'hg':
-        return vcs_hg(remote, local, sdk_path)
+        return vcs_hg(remote, local)
     if vcstype == 'bzr':
-        return vcs_bzr(remote, local, sdk_path)
+        return vcs_bzr(remote, local)
     if vcstype == 'srclib':
         if local != 'build/srclib/' + remote:
             raise VCSException("Error: srclib paths are hard-coded!")
-        return getsrclib(remote, 'build/srclib', sdk_path, raw=True)
+        return getsrclib(remote, 'build/srclib', raw=True)
     raise VCSException("Invalid vcs type " + vcstype)
 
 def getsrclibvcs(name):
@@ -91,9 +91,7 @@ def getsrclibvcs(name):
     return parse_srclib(srclib_path)['Repo Type']
 
 class vcs:
-    def __init__(self, remote, local, sdk_path):
-
-        self.sdk_path = sdk_path
+    def __init__(self, remote, local):
 
         # svn, git-svn and bzr may require auth
         self.username = None
@@ -1270,7 +1268,7 @@ def parse_srclib(metafile, **kw):
 # Returns the path to it. Normally this is the path to be used when referencing
 # it, which may be a subdirectory of the actual project. If you want the base
 # directory of the project, pass 'basepath=True'.
-def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False, raw=False, prepare=True, preponly=False):
+def getsrclib(spec, srclib_dir, basepath=False, raw=False, prepare=True, preponly=False):
 
     if raw:
         name = spec
@@ -1288,7 +1286,7 @@ def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False,
     sdir = os.path.join(srclib_dir, name)
 
     if not preponly:
-        vcs = getvcs(srclib["Repo Type"], srclib["Repo"], sdir, sdk_path)
+        vcs = getvcs(srclib["Repo Type"], srclib["Repo"], sdir)
         vcs.srclib = (name, sdir)
         if ref:
             vcs.gotorevision(ref)
@@ -1311,8 +1309,7 @@ def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False,
     if prepare:
 
         if srclib["Prepare"] is not None:
-            cmd = srclib["Prepare"].replace('$$SDK$$', sdk_path)
-            cmd = cmd.replace('$$NDK$$', ndk_path).replace('$$MVN$$', mvn3)
+            cmd = replace_config_vars(srclib["Prepare"])
 
             p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir)
             if p.returncode != 0:
@@ -1321,7 +1318,7 @@ def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False,
         
         if srclib["Update Project"] == "Yes":
             print "Updating srclib %s at path %s" % (name, libdir)
-            if subprocess.call([os.path.join(sdk_path, 'tools', 'android'),
+            if subprocess.call([os.path.join(config['sdk_path'], 'tools', 'android'),
                 'update', 'project', '-p', libdir]) != 0:
                     raise BuildException( 'Error updating ' + name + ' project')
 
@@ -1340,15 +1337,11 @@ def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False,
 #                   'build/srclib'
 #  'extlib_dir'  - the path to the external libraries directory, usually
 #                   'build/extlib'
-#  'sdk_path'    - the path to the Android SDK
-#  'ndk_path'    - the path to the Android NDK
-#  'javacc_path' - the path to javacc
-#  'mvn3'        - the path to the maven 3 executable
 # Returns the (root, srclibpaths) where:
 #   'root' is the root directory, which may be the same as 'build_dir' or may
 #          be a subdirectory of it.
 #   'srclibpaths' is information on the srclibs being used
-def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, sdk_path, ndk_path, javacc_path, mvn3, onserver=False):
+def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, onserver=False):
 
     # Optionally, the actual app source can be in a subdirectory...
     if 'subdir' in build:
@@ -1373,10 +1366,7 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, sdk_path,
 
     # Run an init command if one is required...
     if 'init' in build:
-        cmd = build['init']
-        cmd = cmd.replace('$$SDK$$', sdk_path)
-        cmd = cmd.replace('$$NDK$$', ndk_path)
-        cmd = cmd.replace('$$MVN$$', mvn3)
+        cmd = replace_config_vars(build['init'])
         if options.verbose:
             print "Running 'init' commands in %s" % root_dir
 
@@ -1390,7 +1380,7 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, sdk_path,
     if (updatemode != 'no'
             and build.get('maven', 'no') == 'no'
             and build.get('gradle', 'no') == 'no'):
-        parms = [os.path.join(sdk_path, 'tools', 'android'),
+        parms = [os.path.join(config['sdk_path'], 'tools', 'android'),
                 'update', 'project']
         if 'target' in build and build['target']:
             parms += ['-t', build['target']]
@@ -1464,11 +1454,11 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, sdk_path,
                 re.S|re.M).group(1)
             props += "sdk-location=%s\n" % sdkloc
         else:
-            props += "sdk.dir=%s\n" % sdk_path
-            props += "sdk-location=%s\n" % sdk_path
+            props += "sdk.dir=%s\n" % config['sdk_path']
+            props += "sdk-location=%s\n" % ['sdk_path']
         # Add ndk location...
-        props += "ndk.dir=%s\n" % ndk_path
-        props += "ndk-location=%s\n" % ndk_path
+        props += "ndk.dir=%s\n" % config['ndk_path']
+        props += "ndk-location=%s\n" % config['ndk_path']
         # Add java.encoding if necessary...
         if 'encoding' in build:
             props += "java.encoding=%s\n" % build['encoding']
@@ -1602,7 +1592,7 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, sdk_path,
         for lib in build['srclibs'].split(';'):
             lib = lib.strip()
             name, _ = lib.split('@')
-            srclibpaths.append((name, getsrclib(lib, srclib_dir, sdk_path, ndk_path, mvn3, preponly=onserver)))
+            srclibpaths.append((name, getsrclib(lib, srclib_dir, preponly=onserver)))
     basesrclib = vcs.getsrclib()
     # If one was used for the main source, add that too.
     if basesrclib:
@@ -1620,15 +1610,13 @@ def prepare_source(vcs, app, build, build_dir, srclib_dir, extlib_dir, sdk_path,
 
     # Run a pre-build command if one is required...
     if 'prebuild' in build:
-        cmd = build['prebuild']
+        cmd = replace_config_vars(build['prebuild'])
 
         # Substitute source library paths into prebuild commands...
         for name, libpath in srclibpaths:
             libpath = os.path.relpath(libpath, root_dir)
             cmd = cmd.replace('$$' + name + '$$', libpath)
-        cmd = cmd.replace('$$SDK$$', sdk_path)
-        cmd = cmd.replace('$$NDK$$', ndk_path)
-        cmd = cmd.replace('$$MVN3$$', mvn3)
+
         if options.verbose:
             print "Running 'prebuild' commands in %s" % root_dir
 
@@ -1965,3 +1953,9 @@ def clean_gradle_keys(path):
             elif not any(s in line for s in (' signingConfig ',)):
                 o.write(line)
 
+def replace_config_vars(cmd):
+    cmd = cmd.replace('$$SDK$$', config['sdk_path'])
+    cmd = cmd.replace('$$NDK$$', config['ndk_path'])
+    cmd = cmd.replace('$$MVN3$$', config['mvn3'])
+    return cmd
+
index 745010f3dc9d4718b96c19661eb95fb90ca2e5ad..a949825432501921e3c6129f1c5c527627754ed6 100644 (file)
@@ -84,8 +84,7 @@ def main():
                 build_dir = 'build/' + app['id']
 
                 # Set up vcs interface and make sure we have the latest code...
-                vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir,
-                        config['sdk_path'])
+                vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
 
                 for thisbuild in app['builds']:
 
@@ -97,10 +96,7 @@ def main():
 
                         # Prepare the source code...
                         root_dir, _ = common.prepare_source(vcs, app, thisbuild,
-                                build_dir, srclib_dir, extlib_dir,
-                                config['sdk_path'], config['ndk_path'],
-                                config['javacc_path'], config['mvn3'],
-                                False)
+                                build_dir, srclib_dir, extlib_dir, False)
 
                         # Do the scan...
                         buildprobs = common.scan_source(build_dir, root_dir, thisbuild)