chiark / gitweb /
refactor env handling for FDroidPopen to support .fdroid.* metadata
authorHans-Christoph Steiner <hans@eds.org>
Wed, 5 Aug 2015 12:39:58 +0000 (14:39 +0200)
committerHans-Christoph Steiner <hans@eds.org>
Wed, 23 Mar 2016 16:16:27 +0000 (17:16 +0100)
The start up sequence of processes that are based on the .fdroid.* metadata
is a bit different, so this ensures that the environment variables get
properly initialized in all cases.

This also creates a single function where the environment is set.  Before
it was being set in multiple places across multiple files.

fdroidserver/build.py
fdroidserver/common.py
tests/common.TestCase
tests/import.TestCase
tests/install.TestCase
tests/update.TestCase

index b059124dd0ecd82b99982ef459cb48244fad5167..b64cc9207952146bf0c596f4a94a3f80bc0c2d4a 100644 (file)
@@ -472,13 +472,7 @@ def build_local(app, build, vcs, build_dir, output_dir, srclib_dir, extlib_dir,
             logging.critical("Android NDK '%s' is not a directory!" % ndk_path)
             sys.exit(3)
 
-    # Set up environment vars that depend on each build
-    for n in ['ANDROID_NDK', 'NDK', 'ANDROID_NDK_HOME']:
-        common.env[n] = ndk_path
-
-    common.reset_env_path()
-    # Set up the current NDK to the PATH
-    common.add_to_env_path(ndk_path)
+    common.set_FDroidPopen_env(build)
 
     # Prepare the source code...
     root_dir, srclibpaths = common.prepare_source(vcs, app, build,
index 725c79613fac98b6c62f14cddf9c79940fb0b45f..8b676cc5279af99f7b62e67b39de8c69eaf4e8e7 100644 (file)
@@ -197,7 +197,7 @@ def read_config(opts, config_file='config.py'):
     The config is read from config_file, which is in the current directory when
     any of the repo management commands are used.
     """
-    global config, options, env, orig_path
+    global config, options, orig_path
 
     if config is not None:
         return config
@@ -268,6 +268,21 @@ def read_config(opts, config_file='config.py'):
     return config
 
 
+def get_ndk_path(version):
+    if config is None or 'ndk_paths' not in config:
+        ndk_path = os.getenv('ANDROID_NDK_HOME')
+        if ndk_path is None:
+            logging.error('No NDK found! Either set ANDROID_NDK_HOME or add ndk_path to your config.py')
+        else:
+            return ndk_path
+    if version is None:
+        version = 'r10e'  # falls back to latest
+    paths = config['ndk_paths']
+    if version not in paths:
+        return ''
+    return paths[version] or ''
+
+
 def find_sdk_tools_cmd(cmd):
     '''find a working path to a tool from the Android SDK'''
 
@@ -1639,6 +1654,8 @@ def FDroidPopenBytes(commands, cwd=None, output=True, stderr_to_stdout=True):
     """
 
     global env
+    if env is None:
+        set_FDroidPopen_env()
 
     if cwd:
         cwd = os.path.normpath(cwd)
@@ -1780,25 +1797,32 @@ def remove_signing_keys(build_dir):
                     logging.info("Cleaned %s of keysigning configs at %s" % (propfile, path))
 
 
-def reset_env_path():
+def set_FDroidPopen_env(build=None):
+    # There is only a weak standard, the variables used by gradle, so also set
+    # up the most commonly used environment variables for SDK and NDK
     global env, orig_path
-    env['PATH'] = orig_path
+    if env is None:
+        env = os.environ
+        orig_path = env['PATH']
+        for n in ['ANDROID_HOME', 'ANDROID_SDK']:
+            env[n] = config['sdk_path']
 
+    # Set up environment vars that depend on each build
+    if build is not None:
+        path = build.ndk_path()
+        paths = orig_path.split(os.pathsep)
+        if path in paths:
+            return
+        paths.append(path)
+        env['PATH'] = os.pathsep.join(paths)
 
-def add_to_env_path(path):
-    global env
-    paths = env['PATH'].split(os.pathsep)
-    if path in paths:
-        return
-    paths.append(path)
-    env['PATH'] = os.pathsep.join(paths)
+        for n in ['ANDROID_NDK', 'NDK', 'ANDROID_NDK_HOME']:
+            env[n] = build.ndk_path()
 
 
 def replace_config_vars(cmd, build):
-    global env
     cmd = cmd.replace('$$SDK$$', config['sdk_path'])
-    # env['ANDROID_NDK'] is set in build_local right before prepare_source
-    cmd = cmd.replace('$$NDK$$', env['ANDROID_NDK'])
+    cmd = cmd.replace('$$NDK$$', get_ndk_path(build['ndk']))
     cmd = cmd.replace('$$MVN3$$', config['mvn3'])
     if build is not None:
         cmd = cmd.replace('$$COMMIT$$', build.commit)
index 48e1d29d71f568f3b088a150f5f02597176b1a05..d7dca14ea50c0c5489fed28a722d3707d4958c25 100755 (executable)
@@ -64,7 +64,7 @@ class CommonTest(unittest.TestCase):
 
     def testIsApkDebuggable(self):
         config = dict()
-        config['sdk_path'] = os.getenv('ANDROID_HOME')
+        fdroidserver.common.fill_config_defaults(config)
         fdroidserver.common.config = config
         self._set_build_tools()
         config['aapt'] = fdroidserver.common.find_sdk_tools_cmd('aapt')
index cce85d68d6828ba66c3a486d254c92fdc36551ad..c53b53d46446739e7376629e1059bb39f1d7d317 100755 (executable)
@@ -25,8 +25,9 @@ class ImportTest(unittest.TestCase):
 
     def test_import_gitlab(self):
         # FDroidPopen needs some config to work
-        fdroidserver.common.config = dict()
-        fdroidserver.common.config['sdk_path'] = '/fake/path/to/android-sdk'
+        config = dict()
+        fdroidserver.common.fill_config_defaults(config)
+        fdroidserver.common.config = config
 
         url = 'https://gitlab.com/fdroid/fdroidclient'
         app = fdroidserver.metadata.get_default_app_info()
index d1ed93fff1f4c9ef8e40bcacb21d1ea8c22c6e34..ce516117d4533d26e7aaba040c72e32d7da49a3d 100755 (executable)
@@ -23,7 +23,7 @@ class InstallTest(unittest.TestCase):
 
     def test_devices(self):
         config = dict()
-        config['sdk_path'] = os.getenv('ANDROID_HOME')
+        fdroidserver.common.fill_config_defaults(config)
         fdroidserver.common.config = config
         config['adb'] = fdroidserver.common.find_sdk_tools_cmd('adb')
         self.assertTrue(os.path.exists(config['adb']))
index 83349f5e74593d93c5f4603c14915ca7c4ff7e8b..0cc93e5c8b8880dd0c2200587b98b3310944f488 100755 (executable)
@@ -29,6 +29,10 @@ class UpdateTest(unittest.TestCase):
         if not os.path.exists(getsig_dir + "/getsig.class"):
             logging.critical("getsig.class not found. To fix: cd '%s' && ./make.sh" % getsig_dir)
             sys.exit(1)
+        # FDroidPopen needs some config to work
+        config = dict()
+        fdroidserver.common.fill_config_defaults(config)
+        fdroidserver.common.config = config
         p = FDroidPopen(['java', '-cp', os.path.join(os.path.dirname(__file__), 'getsig'),
                          'getsig', os.path.join(os.getcwd(), apkfile)])
         sig = None