Don't pass config variables as arguments - much simpler code

This commit is contained in:
Daniel Martí 2013-11-08 20:44:27 +01:00
parent 72151355b1
commit 6e0ad365d0
4 changed files with 48 additions and 64 deletions

View file

@ -63,7 +63,7 @@ def got_valid_builder_vm():
# Note that 'force' here also implies test mode. # 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.""" """Do a build on the build server."""
import ssh import ssh
@ -256,7 +256,7 @@ def build_server(app, thisbuild, vcs, build_dir, output_dir, sdk_path, force):
name, _ = lib.split('@') name, _ = lib.split('@')
if options.verbose: if options.verbose:
print "Processing srclib '" + name + "'" 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. # If one was used for the main source, add that too.
basesrclib = vcs.getsrclib() basesrclib = vcs.getsrclib()
if basesrclib: if basesrclib:
@ -342,8 +342,7 @@ def build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_d
# Prepare the source code... # Prepare the source code...
root_dir, srclibpaths = common.prepare_source(vcs, app, thisbuild, root_dir, srclibpaths = common.prepare_source(vcs, app, thisbuild,
build_dir, srclib_dir, extlib_dir, config['sdk_path'], config['ndk_path'], build_dir, srclib_dir, extlib_dir, onserver)
config['javacc_path'], config['mvn3'], onserver)
# We need to clean via the build tool in case the binary dirs are # We need to clean via the build tool in case the binary dirs are
# different from the default ones # 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... # Run a build command if one is required...
if 'build' in thisbuild: if 'build' in thisbuild:
cmd = thisbuild['build'] cmd = common.replace_config_vars(thisbuild['build'])
# Substitute source library paths into commands... # Substitute source library paths into commands...
for name, libpath in srclibpaths: for name, libpath in srclibpaths:
libpath = os.path.relpath(libpath, root_dir) libpath = os.path.relpath(libpath, root_dir)
cmd = cmd.replace('$$' + name + '$$', libpath) 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: if options.verbose:
print "Running 'build' commands in %s" % root_dir print "Running 'build' commands in %s" % root_dir
p = FDroidPopen(['bash', '-x', '-c', cmd], p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=root_dir)
cwd=root_dir)
if p.returncode != 0: if p.returncode != 0:
raise BuildException("Error running build command for %s:%s" % 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. # grabbing the source now.
vcs.gotorevision(thisbuild['commit']) 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: else:
build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, install, force, onserver) build_local(app, thisbuild, vcs, build_dir, output_dir, srclib_dir, extlib_dir, tmp_dir, install, force, onserver)
return True return True
@ -856,7 +851,7 @@ def main():
if options.verbose: if options.verbose:
print "Getting {0} vcs interface for {1}".format( print "Getting {0} vcs interface for {1}".format(
app['Repo Type'], app['Repo']) 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 first = False

View file

@ -82,7 +82,7 @@ def check_http(app):
# caution, because it's inappropriate for many projects. # caution, because it's inappropriate for many projects.
# Returns (None, "a message") if this didn't work, or (version, vercode) for # Returns (None, "a message") if this didn't work, or (version, vercode) for
# the details of the current version. # the details of the current version.
def check_tags(app, sdk_path): def check_tags(app):
try: 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) 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... # 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) vcs.gotorevision(None)
@ -146,7 +146,7 @@ def check_tags(app, sdk_path):
# caution, because it's inappropriate for many projects. # caution, because it's inappropriate for many projects.
# Returns (None, "a message") if this didn't work, or (version, vercode) for # Returns (None, "a message") if this didn't work, or (version, vercode) for
# the details of the current version. # the details of the current version.
def check_repomanifest(app, sdk_path, branch=None): def check_repomanifest(app, branch=None):
try: try:
@ -158,7 +158,7 @@ def check_repomanifest(app, sdk_path, branch=None):
repotype = app['Repo Type'] repotype = app['Repo Type']
# Set up vcs interface and make sure we have the latest code... # 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 repotype == 'git':
if branch: 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()) msg = "Could not scan app %s due to unknown error: %s" % (app['id'], traceback.format_exc())
return (None, msg) return (None, msg)
def check_repotrunk(app, sdk_path, branch=None): def check_repotrunk(app, branch=None):
try: try:
if app['Repo Type'] == 'srclib': 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') 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... # 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) vcs.gotorevision(None)
@ -340,13 +340,13 @@ def main():
vercode = None vercode = None
mode = app['Update Check Mode'] mode = app['Update Check Mode']
if mode == 'Tags': if mode == 'Tags':
(version, vercode, tag) = check_tags(app, config['sdk_path']) (version, vercode, tag) = check_tags(app)
elif mode == 'RepoManifest': elif mode == 'RepoManifest':
(version, vercode) = check_repomanifest(app, config['sdk_path']) (version, vercode) = check_repomanifest(app)
elif mode.startswith('RepoManifest/'): elif mode.startswith('RepoManifest/'):
(version, vercode) = check_repomanifest(app, config['sdk_path'], mode[13:]) (version, vercode) = check_repomanifest(app, mode[13:])
elif mode == 'RepoTrunk': elif mode == 'RepoTrunk':
(version, vercode) = check_repotrunk(app, config['sdk_path']) (version, vercode) = check_repotrunk(app)
elif mode == 'HTTP': elif mode == 'HTTP':
(version, vercode) = check_http(app) (version, vercode) = check_http(app)
elif mode == 'Static': elif mode == 'Static':
@ -384,8 +384,7 @@ def main():
else: else:
app_dir = os.path.join('build/', app['id']) app_dir = os.path.join('build/', app['id'])
vcs = common.getvcs(app["Repo Type"], app["Repo"], app_dir, vcs = common.getvcs(app["Repo Type"], app["Repo"], app_dir)
config['sdk_path'])
vcs.gotorevision(tag) vcs.gotorevision(tag)
flavour = None flavour = None

View file

@ -67,21 +67,21 @@ def read_config(opts, config_file='config.py'):
return config return config
def getvcs(vcstype, remote, local, sdk_path): def getvcs(vcstype, remote, local):
if vcstype == 'git': if vcstype == 'git':
return vcs_git(remote, local, sdk_path) return vcs_git(remote, local)
if vcstype == 'svn': if vcstype == 'svn':
return vcs_svn(remote, local, sdk_path) return vcs_svn(remote, local)
if vcstype == 'git-svn': if vcstype == 'git-svn':
return vcs_gitsvn(remote, local, sdk_path) return vcs_gitsvn(remote, local)
if vcstype == 'hg': if vcstype == 'hg':
return vcs_hg(remote, local, sdk_path) return vcs_hg(remote, local)
if vcstype == 'bzr': if vcstype == 'bzr':
return vcs_bzr(remote, local, sdk_path) return vcs_bzr(remote, local)
if vcstype == 'srclib': if vcstype == 'srclib':
if local != 'build/srclib/' + remote: if local != 'build/srclib/' + remote:
raise VCSException("Error: srclib paths are hard-coded!") 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) raise VCSException("Invalid vcs type " + vcstype)
def getsrclibvcs(name): def getsrclibvcs(name):
@ -91,9 +91,7 @@ def getsrclibvcs(name):
return parse_srclib(srclib_path)['Repo Type'] return parse_srclib(srclib_path)['Repo Type']
class vcs: class vcs:
def __init__(self, remote, local, sdk_path): def __init__(self, remote, local):
self.sdk_path = sdk_path
# svn, git-svn and bzr may require auth # svn, git-svn and bzr may require auth
self.username = None 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 # 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 # it, which may be a subdirectory of the actual project. If you want the base
# directory of the project, pass 'basepath=True'. # 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: if raw:
name = spec 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) sdir = os.path.join(srclib_dir, name)
if not preponly: 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) vcs.srclib = (name, sdir)
if ref: if ref:
vcs.gotorevision(ref) vcs.gotorevision(ref)
@ -1311,8 +1309,7 @@ def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False,
if prepare: if prepare:
if srclib["Prepare"] is not None: if srclib["Prepare"] is not None:
cmd = srclib["Prepare"].replace('$$SDK$$', sdk_path) cmd = replace_config_vars(srclib["Prepare"])
cmd = cmd.replace('$$NDK$$', ndk_path).replace('$$MVN$$', mvn3)
p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir) p = FDroidPopen(['bash', '-x', '-c', cmd], cwd=libdir)
if p.returncode != 0: 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": if srclib["Update Project"] == "Yes":
print "Updating srclib %s at path %s" % (name, libdir) 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: 'update', 'project', '-p', libdir]) != 0:
raise BuildException( 'Error updating ' + name + ' project') raise BuildException( 'Error updating ' + name + ' project')
@ -1340,15 +1337,11 @@ def getsrclib(spec, srclib_dir, sdk_path, ndk_path="", mvn3="", basepath=False,
# 'build/srclib' # 'build/srclib'
# 'extlib_dir' - the path to the external libraries directory, usually # 'extlib_dir' - the path to the external libraries directory, usually
# 'build/extlib' # '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: # Returns the (root, srclibpaths) where:
# 'root' is the root directory, which may be the same as 'build_dir' or may # 'root' is the root directory, which may be the same as 'build_dir' or may
# be a subdirectory of it. # be a subdirectory of it.
# 'srclibpaths' is information on the srclibs being used # '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... # Optionally, the actual app source can be in a subdirectory...
if 'subdir' in build: 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... # Run an init command if one is required...
if 'init' in build: if 'init' in build:
cmd = build['init'] cmd = replace_config_vars(build['init'])
cmd = cmd.replace('$$SDK$$', sdk_path)
cmd = cmd.replace('$$NDK$$', ndk_path)
cmd = cmd.replace('$$MVN$$', mvn3)
if options.verbose: if options.verbose:
print "Running 'init' commands in %s" % root_dir 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' if (updatemode != 'no'
and build.get('maven', 'no') == 'no' and build.get('maven', 'no') == 'no'
and build.get('gradle', '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'] 'update', 'project']
if 'target' in build and build['target']: if 'target' in build and build['target']:
parms += ['-t', 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) re.S|re.M).group(1)
props += "sdk-location=%s\n" % sdkloc props += "sdk-location=%s\n" % sdkloc
else: else:
props += "sdk.dir=%s\n" % sdk_path props += "sdk.dir=%s\n" % config['sdk_path']
props += "sdk-location=%s\n" % sdk_path props += "sdk-location=%s\n" % ['sdk_path']
# Add ndk location... # Add ndk location...
props += "ndk.dir=%s\n" % ndk_path props += "ndk.dir=%s\n" % config['ndk_path']
props += "ndk-location=%s\n" % ndk_path props += "ndk-location=%s\n" % config['ndk_path']
# Add java.encoding if necessary... # Add java.encoding if necessary...
if 'encoding' in build: if 'encoding' in build:
props += "java.encoding=%s\n" % build['encoding'] 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(';'): for lib in build['srclibs'].split(';'):
lib = lib.strip() lib = lib.strip()
name, _ = lib.split('@') 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() basesrclib = vcs.getsrclib()
# If one was used for the main source, add that too. # If one was used for the main source, add that too.
if basesrclib: 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... # Run a pre-build command if one is required...
if 'prebuild' in build: if 'prebuild' in build:
cmd = build['prebuild'] cmd = replace_config_vars(build['prebuild'])
# Substitute source library paths into prebuild commands... # Substitute source library paths into prebuild commands...
for name, libpath in srclibpaths: for name, libpath in srclibpaths:
libpath = os.path.relpath(libpath, root_dir) libpath = os.path.relpath(libpath, root_dir)
cmd = cmd.replace('$$' + name + '$$', libpath) 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: if options.verbose:
print "Running 'prebuild' commands in %s" % root_dir 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 ',)): elif not any(s in line for s in (' signingConfig ',)):
o.write(line) 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

View file

@ -84,8 +84,7 @@ def main():
build_dir = 'build/' + app['id'] build_dir = 'build/' + app['id']
# Set up vcs interface and make sure we have the latest code... # Set up vcs interface and make sure we have the latest code...
vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir, vcs = common.getvcs(app['Repo Type'], app['Repo'], build_dir)
config['sdk_path'])
for thisbuild in app['builds']: for thisbuild in app['builds']:
@ -97,10 +96,7 @@ def main():
# Prepare the source code... # Prepare the source code...
root_dir, _ = common.prepare_source(vcs, app, thisbuild, root_dir, _ = common.prepare_source(vcs, app, thisbuild,
build_dir, srclib_dir, extlib_dir, build_dir, srclib_dir, extlib_dir, False)
config['sdk_path'], config['ndk_path'],
config['javacc_path'], config['mvn3'],
False)
# Do the scan... # Do the scan...
buildprobs = common.scan_source(build_dir, root_dir, thisbuild) buildprobs = common.scan_source(build_dir, root_dir, thisbuild)