init: --android-home for forcing the path to the Android SDK

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.
This commit is contained in:
Hans-Christoph Steiner 2014-04-23 19:21:22 -04:00
parent cc089b49b1
commit 66df02d5f8
4 changed files with 131 additions and 33 deletions

1
.gitignore vendored
View file

@ -10,3 +10,4 @@ dist/
env/ env/
fdroidserver.egg-info/ fdroidserver.egg-info/
pylint.parseable pylint.parseable
/.testfiles/

View file

@ -123,8 +123,8 @@ def read_config(opts, config_file='config.py'):
def test_sdk_exists(c): def test_sdk_exists(c):
if c['sdk_path'] == None: if c['sdk_path'] == 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.critical("Neither ANDROID_HOME nor sdk_path is set, no Android SDK found!") logging.critical('No Android SDK found! ANDROID_HOME is not set and sdk_path is not in config.py!')
logging.info('Set ANDROID_HOME to the path to your SDK, i.e.:') logging.info('You can use ANDROID_HOME to set the path to your SDK, i.e.:')
logging.info('\texport ANDROID_HOME=/opt/android-sdk') logging.info('\texport ANDROID_HOME=/opt/android-sdk')
return False return False
if not os.path.exists(c['sdk_path']): 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']): if not os.path.isdir(c['sdk_path']):
logging.critical('Android SDK path "' + c['sdk_path'] + '" is not a directory!') logging.critical('Android SDK path "' + c['sdk_path'] + '" is not a directory!')
return False 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 return True
def write_password_file(pwtype, password=None): def write_password_file(pwtype, password=None):

View file

@ -103,10 +103,10 @@ def main():
help="Path to the keystore for the repo signing key") help="Path to the keystore for the repo signing key")
parser.add_option("--repo-keyalias", default=None, parser.add_option("--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_option("--android-home", default=None,
help="Path to the Android SDK (sometimes set in ANDROID_HOME)")
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
common.test_sdk_exists(common.get_default_config())
# find root install prefix # find root install prefix
tmp = os.path.dirname(sys.argv[0]) tmp = os.path.dirname(sys.argv[0])
if os.path.basename(tmp) == 'bin': if os.path.basename(tmp) == 'bin':
@ -118,6 +118,25 @@ def main():
examplesdir = prefix + '/examples' examplesdir = prefix + '/examples'
fdroiddir = os.getcwd() 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'): if not os.path.exists('config.py'):
# 'metadata' and 'tmp' are created in fdroid # 'metadata' and 'tmp' are created in fdroid
@ -126,6 +145,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'])
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.')
@ -134,29 +154,8 @@ def main():
# now that we have a local config.py, read configuration... # now that we have a local config.py, read configuration...
config = common.read_config(options) 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 # 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 = []
aaptdirs.append(os.path.join(build_tools, config['build_tools'])) aaptdirs.append(os.path.join(build_tools, config['build_tools']))
aaptdirs.append(build_tools) aaptdirs.append(build_tools)
@ -255,7 +254,7 @@ def main():
logging.info('Built repo based in "' + fdroiddir + '"') logging.info('Built repo based in "' + fdroiddir + '"')
logging.info('with this config:') 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 SDK Build Tools:\t' + os.path.dirname(aapt))
logging.info(' Android NDK (optional):\t' + ndk_path) logging.info(' Android NDK (optional):\t' + ndk_path)
logging.info(' Keystore for signing key:\t' + keystore) logging.info(' Keystore for signing key:\t' + keystore)

View file

@ -13,6 +13,22 @@ copy_apks_into_repo() {
done 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 if [ -z $WORKSPACE ]; then
WORKSPACE=`dirname $(pwd)` WORKSPACE=`dirname $(pwd)`
echo "Setting Workspace to $WORKSPACE" echo "Setting Workspace to $WORKSPACE"
@ -24,9 +40,9 @@ if [ -z $fdroid ]; then
fi 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 cd $REPOROOT
$fdroid init $fdroid init
copy_apks_into_repo $REPOROOT 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=`mktemp --directory --tmpdir=$WORKSPACE` 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=`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 KEYSTORE=$REPOROOT/keystore.jks
cd $REPOROOT cd $REPOROOT
$fdroid init --keystore $KEYSTORE $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 cd $REPOROOT
$fdroid init --keystore NONE $fdroid init --keystore NONE
test -e opensc-fdroid.cfg test -e opensc-fdroid.cfg
test ! -e NONE test ! -e NONE
echo SUCCESS