chiark / gitweb /
Keep consistency in config defaults and writes
authorDaniel Martí <mvdan@mvdan.cc>
Mon, 15 Sep 2014 10:34:40 +0000 (12:34 +0200)
committerDaniel Martí <mvdan@mvdan.cc>
Mon, 15 Sep 2014 10:34:40 +0000 (12:34 +0200)
* Default paths are not expanded
* When writing to config.py in "fdroid init", don't write expanded paths
  either
* Support changes in e.g. $ANDROID_HOME after a config.py is generated thanks
  to the fix above

fdroidserver/common.py
fdroidserver/init.py

index b18b0ef12d239cee1c1a0feef7bbba7c0ba5b809..373eefc714c5c4bc8d75ab47e370dcfed795b85a 100644 (file)
@@ -39,40 +39,56 @@ options = None
 env = None
 
 
-def get_default_config():
-    return {
-        'sdk_path': os.getenv("ANDROID_HOME") or "",
-        'ndk_path': os.getenv("ANDROID_NDK") or "",
-        'build_tools': "20.0.0",
-        'ant': "ant",
-        'mvn3': "mvn",
-        'gradle': 'gradle',
-        'sync_from_local_copy_dir': False,
-        'update_stats': False,
-        'stats_ignore': [],
-        'stats_server': None,
-        'stats_user': None,
-        'stats_to_carbon': False,
-        'repo_maxage': 0,
-        'build_server_always': False,
-        'keystore': os.path.join(os.getenv("HOME"), '.local', 'share', 'fdroidserver', 'keystore.jks'),
-        'smartcardoptions': [],
-        'char_limits': {
-            'Summary': 50,
-            'Description': 1500
-        },
-        'keyaliases': {},
-        'repo_url': "https://MyFirstFDroidRepo.org/fdroid/repo",
-        'repo_name': "My First FDroid Repo Demo",
-        'repo_icon': "fdroid-icon.png",
-        'repo_description': '''
-            This is a repository of apps to be used with FDroid. Applications in this
-            repository are either official binaries built by the original application
-            developers, or are binaries built from source by the admin of f-droid.org
-            using the tools on https://gitlab.com/u/fdroid.
-            ''',
-        'archive_older': 0,
-    }
+default_config = {
+    'sdk_path': "$ANDROID_HOME",
+    'ndk_path': "$ANDROID_NDK",
+    'build_tools': "20.0.0",
+    'ant': "ant",
+    'mvn3': "mvn",
+    'gradle': 'gradle',
+    'sync_from_local_copy_dir': False,
+    'update_stats': False,
+    'stats_ignore': [],
+    'stats_server': None,
+    'stats_user': None,
+    'stats_to_carbon': False,
+    'repo_maxage': 0,
+    'build_server_always': False,
+    'keystore': os.path.join("$HOME", '.local', 'share', 'fdroidserver', 'keystore.jks'),
+    'smartcardoptions': [],
+    'char_limits': {
+        'Summary': 50,
+        'Description': 1500
+    },
+    'keyaliases': {},
+    'repo_url': "https://MyFirstFDroidRepo.org/fdroid/repo",
+    'repo_name': "My First FDroid Repo Demo",
+    'repo_icon': "fdroid-icon.png",
+    'repo_description': '''
+        This is a repository of apps to be used with FDroid. Applications in this
+        repository are either official binaries built by the original application
+        developers, or are binaries built from source by the admin of f-droid.org
+        using the tools on https://gitlab.com/u/fdroid.
+        ''',
+    'archive_older': 0,
+}
+
+
+def fill_config_defaults(config):
+    for k, v in default_config.items():
+        if k not in config:
+            config[k] = v
+
+    # Expand environment variables
+    for k, v in config.items():
+        if type(v) != str:
+            continue
+        orig = v
+        v = os.path.expanduser(v)
+        v = os.path.expandvars(v)
+        if orig != v:
+            config[k] = v
+            config[k + '_orig'] = orig
 
 
 def read_config(opts, config_file='config.py'):
@@ -111,17 +127,7 @@ def read_config(opts, config_file='config.py'):
         if st.st_mode & stat.S_IRWXG or st.st_mode & stat.S_IRWXO:
             logging.warn("unsafe permissions on {0} (should be 0600)!".format(config_file))
 
-    defconfig = get_default_config()
-    for k, v in defconfig.items():
-        if k not in config:
-            config[k] = v
-
-    # Expand environment variables
-    for k, v in config.items():
-        if type(v) != str:
-            continue
-        v = os.path.expanduser(v)
-        config[k] = os.path.expandvars(v)
+    fill_config_defaults(config)
 
     if not test_sdk_exists(config):
         sys.exit(3)
@@ -193,7 +199,7 @@ def read_config(opts, config_file='config.py'):
 def test_sdk_exists(c):
     if c['sdk_path'] is None:
         # c['sdk_path'] is set to the value of ANDROID_HOME by default
-        logging.error('No Android SDK found! ANDROID_HOME is not set and sdk_path is not in config.py!')
+        logging.error('No Android SDK found!')
         logging.error('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
         logging.error('\texport ANDROID_HOME=/opt/android-sdk')
         return False
index a3eded5b3819e7be225f9cc04642540c986f83d7..e656256ab58c8a37072cb4ec94afbdcbec79536d 100644 (file)
@@ -36,8 +36,11 @@ config = {}
 options = None
 
 
-def write_to_config(key, value):
+def write_to_config(config, key, value=None):
     '''write a key/value to the local config.py'''
+    if value is None:
+        origkey = key + '_orig'
+        value = config[origkey] if origkey in config else config[key]
     with open('config.py', 'r') as f:
         data = f.read()
     pattern = '\n[\s#]*' + key + '\s*=\s*"[^"]*"'
@@ -122,7 +125,8 @@ def main():
         examplesdir = prefix + '/examples'
 
     fdroiddir = os.getcwd()
-    test_config = common.get_default_config()
+    test_config = dict()
+    common.fill_config_defaults(test_config)
 
     # track down where the Android SDK is, the default is to use the path set
     # in ANDROID_HOME if that exists, otherwise None
@@ -154,7 +158,7 @@ def main():
         shutil.copy(os.path.join(examplesdir, 'fdroid-icon.png'), fdroiddir)
         shutil.copyfile(os.path.join(examplesdir, 'config.py'), 'config.py')
         os.chmod('config.py', 0o0600)
-        write_to_config('sdk_path', test_config['sdk_path'])
+        write_to_config(test_config, 'sdk_path')
     else:
         logging.warn('Looks like this is already an F-Droid repo, cowardly refusing to overwrite it...')
         logging.info('Try running `fdroid init` in an empty directory.')
@@ -179,7 +183,7 @@ def main():
             test_config['build_tools'] = ''
         else:
             test_config['build_tools'] = dirname
-        write_to_config('build_tools', test_config['build_tools'])
+        write_to_config(test_config, 'build_tools')
     if not common.test_build_tools_exists(test_config):
         sys.exit(3)
 
@@ -194,7 +198,7 @@ def main():
         logging.info('using ANDROID_NDK')
         ndk_path = os.environ['ANDROID_NDK']
     if os.path.isdir(ndk_path):
-        write_to_config('ndk_path', ndk_path)
+        write_to_config(test_config, 'ndk_path')
     # the NDK is optional so we don't prompt the user for it if its not found
 
     # find or generate the keystore for the repo signing key. First try the
@@ -213,18 +217,18 @@ def main():
             if not os.path.exists(keystore):
                 logging.info('"' + keystore
                              + '" does not exist, creating a new keystore there.')
-    write_to_config('keystore', keystore)
+    write_to_config(test_config, 'keystore', keystore)
     repo_keyalias = None
     if options.repo_keyalias:
         repo_keyalias = options.repo_keyalias
-        write_to_config('repo_keyalias', repo_keyalias)
+        write_to_config(test_config, 'repo_keyalias', repo_keyalias)
     if options.distinguished_name:
         keydname = options.distinguished_name
-        write_to_config('keydname', keydname)
+        write_to_config(test_config, 'keydname', keydname)
     if keystore == 'NONE':  # we're using a smartcard
-        write_to_config('repo_keyalias', '1')  # seems to be the default
+        write_to_config(test_config, 'repo_keyalias', '1')  # seems to be the default
         disable_in_config('keypass', 'never used with smartcard')
-        write_to_config('smartcardoptions',
+        write_to_config(test_config, 'smartcardoptions',
                         ('-storetype PKCS11 -providerName SunPKCS11-OpenSC '
                          + '-providerClass sun.security.pkcs11.SunPKCS11 '
                          + '-providerArg opensc-fdroid.cfg'))
@@ -254,14 +258,14 @@ def main():
         if not os.path.exists(keystoredir):
             os.makedirs(keystoredir, mode=0o700)
         password = genpassword()
-        write_to_config('keystorepass', password)
-        write_to_config('keypass', password)
+        write_to_config(test_config, 'keystorepass', password)
+        write_to_config(test_config, 'keypass', password)
         if options.repo_keyalias is None:
             repo_keyalias = socket.getfqdn()
-            write_to_config('repo_keyalias', repo_keyalias)
+            write_to_config(test_config, 'repo_keyalias', repo_keyalias)
         if not options.distinguished_name:
             keydname = 'CN=' + repo_keyalias + ', OU=F-Droid'
-            write_to_config('keydname', keydname)
+            write_to_config(test_config, 'keydname', keydname)
         genkey(keystore, repo_keyalias, password, keydname)
 
     logging.info('Built repo based in "' + fdroiddir + '"')