Keep consistency in config defaults and writes

* 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
This commit is contained in:
Daniel Martí 2014-09-15 12:34:40 +02:00
parent 8d1c4c1d60
commit 88dc0b2bb5
2 changed files with 70 additions and 60 deletions

View file

@ -39,10 +39,9 @@ options = None
env = None env = None
def get_default_config(): default_config = {
return { 'sdk_path': "$ANDROID_HOME",
'sdk_path': os.getenv("ANDROID_HOME") or "", 'ndk_path': "$ANDROID_NDK",
'ndk_path': os.getenv("ANDROID_NDK") or "",
'build_tools': "20.0.0", 'build_tools': "20.0.0",
'ant': "ant", 'ant': "ant",
'mvn3': "mvn", 'mvn3': "mvn",
@ -55,7 +54,7 @@ def get_default_config():
'stats_to_carbon': False, 'stats_to_carbon': False,
'repo_maxage': 0, 'repo_maxage': 0,
'build_server_always': False, 'build_server_always': False,
'keystore': os.path.join(os.getenv("HOME"), '.local', 'share', 'fdroidserver', 'keystore.jks'), 'keystore': os.path.join("$HOME", '.local', 'share', 'fdroidserver', 'keystore.jks'),
'smartcardoptions': [], 'smartcardoptions': [],
'char_limits': { 'char_limits': {
'Summary': 50, 'Summary': 50,
@ -72,7 +71,24 @@ def get_default_config():
using the tools on https://gitlab.com/u/fdroid. using the tools on https://gitlab.com/u/fdroid.
''', ''',
'archive_older': 0, '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'): 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: 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)) logging.warn("unsafe permissions on {0} (should be 0600)!".format(config_file))
defconfig = get_default_config() fill_config_defaults(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)
if not test_sdk_exists(config): if not test_sdk_exists(config):
sys.exit(3) sys.exit(3)
@ -193,7 +199,7 @@ def read_config(opts, config_file='config.py'):
def test_sdk_exists(c): def test_sdk_exists(c):
if c['sdk_path'] is None: if c['sdk_path'] is None:
# c['sdk_path'] is set to the value of ANDROID_HOME by default # 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('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
logging.error('\texport ANDROID_HOME=/opt/android-sdk') logging.error('\texport ANDROID_HOME=/opt/android-sdk')
return False return False

View file

@ -36,8 +36,11 @@ config = {}
options = None 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''' '''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: with open('config.py', 'r') as f:
data = f.read() data = f.read()
pattern = '\n[\s#]*' + key + '\s*=\s*"[^"]*"' pattern = '\n[\s#]*' + key + '\s*=\s*"[^"]*"'
@ -122,7 +125,8 @@ def main():
examplesdir = prefix + '/examples' examplesdir = prefix + '/examples'
fdroiddir = os.getcwd() 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 # track down where the Android SDK is, the default is to use the path set
# in ANDROID_HOME if that exists, otherwise None # 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.copy(os.path.join(examplesdir, 'fdroid-icon.png'), fdroiddir)
shutil.copyfile(os.path.join(examplesdir, 'config.py'), 'config.py') shutil.copyfile(os.path.join(examplesdir, 'config.py'), 'config.py')
os.chmod('config.py', 0o0600) os.chmod('config.py', 0o0600)
write_to_config('sdk_path', test_config['sdk_path']) write_to_config(test_config, 'sdk_path')
else: else:
logging.warn('Looks like this is already an F-Droid repo, cowardly refusing to overwrite it...') 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.') logging.info('Try running `fdroid init` in an empty directory.')
@ -179,7 +183,7 @@ def main():
test_config['build_tools'] = '' test_config['build_tools'] = ''
else: else:
test_config['build_tools'] = dirname 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): if not common.test_build_tools_exists(test_config):
sys.exit(3) sys.exit(3)
@ -194,7 +198,7 @@ def main():
logging.info('using ANDROID_NDK') logging.info('using ANDROID_NDK')
ndk_path = os.environ['ANDROID_NDK'] ndk_path = os.environ['ANDROID_NDK']
if os.path.isdir(ndk_path): 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 # 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 # 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): if not os.path.exists(keystore):
logging.info('"' + keystore logging.info('"' + keystore
+ '" does not exist, creating a new keystore there.') + '" does not exist, creating a new keystore there.')
write_to_config('keystore', keystore) write_to_config(test_config, 'keystore', keystore)
repo_keyalias = None repo_keyalias = None
if options.repo_keyalias: if options.repo_keyalias:
repo_keyalias = 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: if options.distinguished_name:
keydname = 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 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') disable_in_config('keypass', 'never used with smartcard')
write_to_config('smartcardoptions', write_to_config(test_config, 'smartcardoptions',
('-storetype PKCS11 -providerName SunPKCS11-OpenSC ' ('-storetype PKCS11 -providerName SunPKCS11-OpenSC '
+ '-providerClass sun.security.pkcs11.SunPKCS11 ' + '-providerClass sun.security.pkcs11.SunPKCS11 '
+ '-providerArg opensc-fdroid.cfg')) + '-providerArg opensc-fdroid.cfg'))
@ -254,14 +258,14 @@ def main():
if not os.path.exists(keystoredir): if not os.path.exists(keystoredir):
os.makedirs(keystoredir, mode=0o700) os.makedirs(keystoredir, mode=0o700)
password = genpassword() password = genpassword()
write_to_config('keystorepass', password) write_to_config(test_config, 'keystorepass', password)
write_to_config('keypass', password) write_to_config(test_config, 'keypass', password)
if options.repo_keyalias is None: if options.repo_keyalias is None:
repo_keyalias = socket.getfqdn() 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: if not options.distinguished_name:
keydname = 'CN=' + repo_keyalias + ', OU=F-Droid' 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) genkey(keystore, repo_keyalias, password, keydname)
logging.info('Built repo based in "' + fdroiddir + '"') logging.info('Built repo based in "' + fdroiddir + '"')