chiark / gitweb /
update: make icon extraction less dependent on aapt
[fdroidserver.git] / fdroidserver / init.py
index f29b2d53891057e33bf117363c177092f677865f..b47d65b6219fd4b6433828eaed8c4f012ff6fa15 100644 (file)
@@ -27,7 +27,9 @@ import sys
 from argparse import ArgumentParser
 import logging
 
+from . import _
 from . import common
+from .exception import FDroidException
 
 config = {}
 options = None
@@ -52,38 +54,21 @@ def main():
     parser = ArgumentParser()
     common.setup_global_opts(parser)
     parser.add_argument("-d", "--distinguished-name", default=None,
-                        help="X.509 'Distiguished Name' used when generating keys")
+                        help=_("X.509 'Distinguished Name' used when generating keys"))
     parser.add_argument("--keystore", default=None,
-                        help="Path to the keystore for the repo signing key")
+                        help=_("Path to the keystore for the repo signing key"))
     parser.add_argument("--repo-keyalias", default=None,
-                        help="Alias of the repo signing key in the keystore")
+                        help=_("Alias of the repo signing key in the keystore"))
     parser.add_argument("--android-home", default=None,
-                        help="Path to the Android SDK (sometimes set in ANDROID_HOME)")
+                        help=_("Path to the Android SDK (sometimes set in ANDROID_HOME)"))
     parser.add_argument("--no-prompt", action="store_true", default=False,
-                        help="Do not prompt for Android SDK path, just fail")
+                        help=_("Do not prompt for Android SDK path, just fail"))
     options = parser.parse_args()
 
-    # find root install prefix
-    tmp = os.path.dirname(sys.argv[0])
-    examplesdir = None
-    if os.path.basename(tmp) == 'bin':
-        egg_link = os.path.join(tmp, '..', 'local/lib/python2.7/site-packages/fdroidserver.egg-link')
-        if os.path.exists(egg_link):
-            # installed from local git repo
-            examplesdir = os.path.join(open(egg_link).readline().rstrip(), 'examples')
-        else:
-            # try .egg layout
-            examplesdir = os.path.dirname(os.path.dirname(__file__)) + '/share/doc/fdroidserver/examples'
-            if not os.path.exists(examplesdir):  # use UNIX layout
-                examplesdir = os.path.dirname(tmp) + '/share/doc/fdroidserver/examples'
-    else:
-        # we're running straight out of the git repo
-        prefix = os.path.normpath(os.path.join(os.path.dirname(__file__), '..'))
-        examplesdir = prefix + '/examples'
-
     aapt = None
     fdroiddir = os.getcwd()
     test_config = dict()
+    examplesdir = common.get_examples_dir()
     common.fill_config_defaults(test_config)
 
     # track down where the Android SDK is, the default is to use the path set
@@ -98,15 +83,24 @@ def main():
             # make sure at least aapt is found, since this can't do anything without it
             test_config['aapt'] = common.find_sdk_tools_cmd('aapt')
         else:
-            # if neither --android-home nor the default sdk_path exist, prompt the user
+            # if neither --android-home nor the default sdk_path
+            # exist, prompt the user using platform-specific default
             default_sdk_path = '/opt/android-sdk'
             if sys.platform == 'win32' or sys.platform == 'cygwin':
-                default_sdk_path = os.path.join(os.getenv('USERPROFILE'),
-                                                'AppData', 'Local', 'Android', 'android-sdk')
+                p = os.path.join(os.getenv('USERPROFILE'),
+                                 'AppData', 'Local', 'Android', 'android-sdk')
+            elif sys.platform == 'darwin':
+                # on OSX, Homebrew is common and has an easy path to detect
+                p = '/usr/local/opt/android-sdk'
+            else:
+                # if the Debian packages are installed, suggest them
+                p = '/usr/lib/android-sdk'
+            if os.path.exists(p):
+                default_sdk_path = p
+
             while not options.no_prompt:
                 try:
-                    s = input('Enter the path to the Android SDK ('
-                              + default_sdk_path + ') here:\n> ')
+                    s = input(_('Enter the path to the Android SDK (%s) here:\n> ') % default_sdk_path)
                 except KeyboardInterrupt:
                     print('')
                     sys.exit(1)
@@ -117,7 +111,7 @@ def main():
                 if common.test_sdk_exists(test_config):
                     break
     if not common.test_sdk_exists(test_config):
-        sys.exit(3)
+        raise FDroidException("Android SDK not found.")
 
     if not os.path.exists('config.py'):
         # 'metadata' and 'tmp' are created in fdroid
@@ -135,9 +129,11 @@ def main():
     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.')
-        sys.exit()
+        raise FDroidException('Repository already exists.')
 
-    if 'aapt' not in test_config or not os.path.isfile(test_config['aapt']):
+    if common.use_androguard():
+        pass
+    elif 'aapt' not in test_config or not os.path.isfile(test_config['aapt']):
         # try to find a working aapt, in all the recent possible paths
         build_tools = os.path.join(test_config['sdk_path'], 'build-tools')
         aaptdirs = []
@@ -150,7 +146,7 @@ def main():
             if os.path.isfile(os.path.join(d, 'aapt')):
                 aapt = os.path.join(d, 'aapt')
                 break
-        if os.path.isfile(aapt):
+        if aapt and os.path.isfile(aapt):
             dirname = os.path.basename(os.path.dirname(aapt))
             if dirname == 'build-tools':
                 # this is the old layout, before versioned build-tools
@@ -184,6 +180,7 @@ def main():
                              + '" does not exist, creating a new keystore there.')
     common.write_to_config(test_config, 'keystore', keystore)
     repo_keyalias = None
+    keydname = None
     if options.repo_keyalias:
         repo_keyalias = options.repo_keyalias
         common.write_to_config(test_config, 'repo_keyalias', repo_keyalias)
@@ -217,7 +214,16 @@ def main():
                                    flags=re.MULTILINE)
             with open('opensc-fdroid.cfg', 'w') as f:
                 f.write(opensc_fdroid)
-    elif not os.path.exists(keystore):
+    elif os.path.exists(keystore):
+        to_set = ['keystorepass', 'keypass', 'repo_keyalias', 'keydname']
+        if repo_keyalias:
+            to_set.remove('repo_keyalias')
+        if keydname:
+            to_set.remove('keydname')
+        logging.warning('\n' + _('Using existing keystore "{path}"').format(path=keystore)
+                        + '\n' + _('Now set these in config.py:') + ' '
+                        + ', '.join(to_set) + '\n')
+    else:
         password = common.genpassword()
         c = dict(test_config)
         c['keystorepass'] = password
@@ -230,21 +236,20 @@ def main():
         common.write_to_config(test_config, 'keydname', c['keydname'])
         common.genkeystore(c)
 
-    logging.info('Built repo based in "' + fdroiddir + '"')
-    logging.info('with this config:')
-    logging.info('  Android SDK:\t\t\t' + config['sdk_path'])
+    msg = '\n'
+    msg += _('Built repo based in "%s" with this config:') % fdroiddir
+    msg += '\n\n  Android SDK:\t\t\t' + config['sdk_path']
     if aapt:
-        logging.info('  Android SDK Build Tools:\t' + os.path.dirname(aapt))
-    logging.info('  Android NDK r12b (optional):\t$ANDROID_NDK')
-    logging.info('  Keystore for signing key:\t' + keystore)
+        msg += '\n  Android SDK Build Tools:\t' + os.path.dirname(aapt)
+    msg += '\n  Android NDK r12b (optional):\t$ANDROID_NDK'
+    msg += '\n  ' + _('Keystore for signing key:\t') + keystore
     if repo_keyalias is not None:
-        logging.info('  Alias for key in store:\t' + repo_keyalias)
-    logging.info('\nTo complete the setup, add your APKs to "' +
-                 os.path.join(fdroiddir, 'repo') + '"' + '''
+        msg += '\n  Alias for key in store:\t' + repo_keyalias
+    msg += '\n\n' + '''To complete the setup, add your APKs to "%s"
 then run "fdroid update -c; fdroid update".  You might also want to edit
 "config.py" to set the URL, repo name, and more.  You should also set up
 a signing key (a temporary one might have been automatically generated).
 
-For more info: https://f-droid.org/manual/fdroid.html#Simple-Binary-Repository
-and https://f-droid.org/manual/fdroid.html#Signing
-''')
+For more info: https://f-droid.org/docs/Setup_an_F-Droid_App_Repo
+and https://f-droid.org/docs/Signing_Process''' % os.path.join(fdroiddir, 'repo')
+    logging.info(msg)