chiark / gitweb /
init: --android-home for forcing the path to the Android SDK
authorHans-Christoph Steiner <hans@eds.org>
Wed, 23 Apr 2014 23:21:22 +0000 (19:21 -0400)
committerHans-Christoph Steiner <hans@eds.org>
Wed, 23 Apr 2014 23:33:10 +0000 (19:33 -0400)
This allows the user to set the path to their Android SDK from the command
line. This option is named after the standard env var ANDROID_HOME, as used
in the build.xml generated by `android update project`.  --android-home
takes precendence over the ANDROID_HOME env var if it is set.

.gitignore
fdroidserver/common.py
fdroidserver/init.py
tests/run-tests

index 2a4e5024ffe039c26f706c764a67098e4342da80..277ca28042a299270c9534936622936c5f55ec89 100644 (file)
@@ -10,3 +10,4 @@ dist/
 env/
 fdroidserver.egg-info/
 pylint.parseable
+/.testfiles/
index 0c1d7dd3976f733c6dc3a866e23470df8200e5e3..dc6ac6974a18333259ef6833229a0d8908021f58 100644 (file)
@@ -123,8 +123,8 @@ def read_config(opts, config_file='config.py'):
 def test_sdk_exists(c):
     if c['sdk_path'] == None:
         # c['sdk_path'] is set to the value of ANDROID_HOME by default
-        logging.critical("Neither ANDROID_HOME nor sdk_path is set, no Android SDK found!")
-        logging.info('Set ANDROID_HOME to the path to your SDK, i.e.:')
+        logging.critical('No Android SDK found! ANDROID_HOME is not set and sdk_path is not in config.py!')
+        logging.info('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
         logging.info('\texport ANDROID_HOME=/opt/android-sdk')
         return False
     if not os.path.exists(c['sdk_path']):
@@ -133,6 +133,9 @@ def test_sdk_exists(c):
     if not os.path.isdir(c['sdk_path']):
         logging.critical('Android SDK path "' + c['sdk_path'] + '" is not a directory!')
         return False
+    if not os.path.isdir(os.path.join(c['sdk_path'], 'build-tools')):
+        logging.critical('Android SDK path "' + c['sdk_path'] + '" does not contain "build-tools/"!')
+        return False
     return True
 
 def write_password_file(pwtype, password=None):
index a6f53c6b1c41a266dc753d39fa59c204c38be6a8..390a7c7d51e02e8f26c620fee9e069d56e25592e 100644 (file)
@@ -103,10 +103,10 @@ def main():
                       help="Path to the keystore for the repo signing key")
     parser.add_option("--repo-keyalias", default=None,
                       help="Alias of the repo signing key in the keystore")
+    parser.add_option("--android-home", default=None,
+                      help="Path to the Android SDK (sometimes set in ANDROID_HOME)")
     (options, args) = parser.parse_args()
 
-    common.test_sdk_exists(common.get_default_config())
-
     # find root install prefix
     tmp = os.path.dirname(sys.argv[0])
     if os.path.basename(tmp) == 'bin':
@@ -118,6 +118,25 @@ def main():
         examplesdir = prefix + '/examples'
 
     fdroiddir = os.getcwd()
+    test_config = common.get_default_config()
+
+    # track down where the Android SDK is, the default is to use the path set
+    # in ANDROID_HOME if that exists, otherwise None
+    if options.android_home != None:
+        test_config['sdk_path'] = options.android_home
+    elif not common.test_sdk_exists(test_config):
+        # if neither --android-home nor the default sdk_path exist, prompt the user
+        default_sdk_path = '/opt/android-sdk'
+        while True:
+            s = raw_input('Enter the path to the Android SDK (' + default_sdk_path + ') here:\n> ')
+            if re.match('^\s*$', s) != None:
+                test_config['sdk_path'] = default_sdk_path
+            else:
+                test_config['sdk_path'] = s
+            if common.test_sdk_exists(test_config):
+                break
+    if not common.test_sdk_exists(test_config):
+        sys.exit(3)
 
     if not os.path.exists('config.py'):
         # 'metadata' and 'tmp' are created in fdroid
@@ -126,6 +145,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'])
     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.')
@@ -134,29 +154,8 @@ def main():
     # now that we have a local config.py, read configuration...
     config = common.read_config(options)
 
-    # track down where the Android SDK is
-    if os.path.isdir(config['sdk_path']):
-        logging.info('Using "' + config['sdk_path'] + '" for the Android SDK')
-        sdk_path = config['sdk_path']
-    elif 'ANDROID_HOME' in os.environ.keys():
-        sdk_path = os.environ['ANDROID_HOME']
-    else:
-        default_sdk_path = '/opt/android-sdk'
-        while True:
-            s = raw_input('Enter the path to the Android SDK (' + default_sdk_path + '): ')
-            if re.match('^\s*$', s) != None:
-                sdk_path = default_sdk_path
-            else:
-                sdk_path = s
-            if os.path.isdir(os.path.join(sdk_path, 'build-tools')):
-                break
-            else:
-                logging.info('"' + s + '" does not contain the Android SDK! Try again...')
-    if os.path.isdir(sdk_path):
-        write_to_config('sdk_path', sdk_path)
-
     # try to find a working aapt, in all the recent possible paths
-    build_tools = os.path.join(sdk_path, 'build-tools')
+    build_tools = os.path.join(config['sdk_path'], 'build-tools')
     aaptdirs = []
     aaptdirs.append(os.path.join(build_tools, config['build_tools']))
     aaptdirs.append(build_tools)
@@ -255,7 +254,7 @@ def main():
 
     logging.info('Built repo based in "' + fdroiddir + '"')
     logging.info('with this config:')
-    logging.info('  Android SDK:\t\t\t' + sdk_path)
+    logging.info('  Android SDK:\t\t\t' + config['sdk_path'])
     logging.info('  Android SDK Build Tools:\t' + os.path.dirname(aapt))
     logging.info('  Android NDK (optional):\t' + ndk_path)
     logging.info('  Keystore for signing key:\t' + keystore)
index 09e7c94b25dab2a2f591ebd0d946f9922916812d..b4ff47f6fb32b2a35813e92bd988bb9166d456d9 100755 (executable)
@@ -13,6 +13,22 @@ copy_apks_into_repo() {
     done
 }
 
+create_fake_android_home() {
+    mkdir $1/build-tools
+    mkdir $1/build-tools/19.0.1
+    touch $1/build-tools/19.0.1/aapt
+}
+
+create_test_dir() {
+    test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
+    mktemp --directory --tmpdir=$WORKSPACE/.testfiles
+}
+
+create_test_file() {
+    test -e $WORKSPACE/.testfiles || mkdir $WORKSPACE/.testfiles
+    mktemp --tmpdir=$WORKSPACE/.testfiles
+}
+
 if [ -z $WORKSPACE ]; then
     WORKSPACE=`dirname $(pwd)`
     echo "Setting Workspace to $WORKSPACE"
@@ -24,9 +40,9 @@ if [ -z $fdroid ]; then
 fi
 
 #------------------------------------------------------------------------------#
-# setup a new repo from scratch
+echo "setup a new repo from scratch using ANDROID_HOME"
 
-REPOROOT=`mktemp --directory --tmpdir=$WORKSPACE`
+REPOROOT=`create_test_dir`
 cd $REPOROOT
 $fdroid init
 copy_apks_into_repo $REPOROOT
@@ -35,9 +51,85 @@ $fdroid update
 
 
 #------------------------------------------------------------------------------#
-# setup a new repo from scratch and generate a keystore
+# check that --android-home fails when dir does not exist or is not a dir
+
+REPOROOT=`create_test_dir`
+KEYSTORE=$REPOROOT/keystore.jks
+cd $REPOROOT
+set +e
+$fdroid init --keystore $KEYSTORE --android-home /opt/fakeandroidhome
+if [ $? -eq 0 ]; then
+    echo "This should have failed because /opt/fakeandroidhome does not exist!"
+    exit 1
+else
+    echo "testing android-home path checker passed"
+fi
+TESTFILE=`create_test_file`
+$fdroid init --keystore $KEYSTORE --android-home $TESTFILE
+if [ $? -eq 0 ]; then
+    echo "This should have failed because $TESTFILE is a file not a dir!"
+    exit 1
+else
+    echo "testing android-home not-dir checker passed"
+fi
+set -e
+
+
+#------------------------------------------------------------------------------#
+echo "check that --android-home overrides ANDROID_HOME"
+
+REPOROOT=`create_test_dir`
+FAKE_ANDROID_HOME=`create_test_dir`
+create_fake_android_home $FAKE_ANDROID_HOME
+KEYSTORE=$REPOROOT/keystore.jks
+cd $REPOROOT
+$fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
+set +e
+grep $FAKE_ANDROID_HOME $REPOROOT/config.py
+if [ $? -ne 0 ]; then
+    echo "the value set in --android-home '$FAKE_ANDROID_HOME' should override ANDROID_HOME '$ANDROID_HOME'"
+    exit 1
+fi
+set -e
+
+
+#------------------------------------------------------------------------------#
+echo "setup a new repo from scratch with keystore and android-home set on cmd line"
+
+REPOROOT=`create_test_dir`
+KEYSTORE=$REPOROOT/keystore.jks
+FAKE_ANDROID_HOME=`create_test_dir`
+create_fake_android_home $FAKE_ANDROID_HOME
+STORED_ANDROID_HOME=$ANDROID_HOME
+unset ANDROID_HOME
+echo "ANDROID_HOME: $ANDROID_HOME"
+cd $REPOROOT
+$fdroid init --keystore $KEYSTORE --android-home $FAKE_ANDROID_HOME
+test -e $KEYSTORE
+copy_apks_into_repo $REPOROOT
+$fdroid update -c
+$fdroid update
+test -e repo/index.xml
+test -e repo/index.jar
+export ANDROID_HOME=$STORED_ANDROID_HOME
+
+
+#------------------------------------------------------------------------------#
+echo "setup new repo from scratch using ANDROID_HOME, putting APKs in repo first"
 
-REPOROOT=`mktemp --directory --tmpdir=$WORKSPACE`
+REPOROOT=`create_test_dir`
+cd $REPOROOT
+mkdir repo
+copy_apks_into_repo $REPOROOT
+$fdroid init
+$fdroid update -c
+$fdroid update
+
+
+#------------------------------------------------------------------------------#
+echo "setup a new repo from scratch and generate a keystore"
+
+REPOROOT=`create_test_dir`
 KEYSTORE=$REPOROOT/keystore.jks
 cd $REPOROOT
 $fdroid init --keystore $KEYSTORE
@@ -50,10 +142,13 @@ test -e repo/index.jar
 
 
 #------------------------------------------------------------------------------#
-# setup a new repo from scratch with a HSM/smartcard
+echo "setup a new repo from scratch with a HSM/smartcard"
 
-REPOROOT=`mktemp --directory --tmpdir=$WORKSPACE`
+REPOROOT=`create_test_dir`
 cd $REPOROOT
 $fdroid init --keystore NONE
 test -e opensc-fdroid.cfg
 test ! -e NONE
+
+
+echo SUCCESS