diff --git a/tools/android/set_up_android.py b/tools/android/set_up_android.py index a638a46bf1..1a74d342da 100755 --- a/tools/android/set_up_android.py +++ b/tools/android/set_up_android.py @@ -1,93 +1,93 @@ #!/usr/bin/python +import os, sys, shutil, collections +from optparse import OptionParser -import os -import shutil -import getopt -import sys +def find_recursive(root, subpath, maxdepth=4): + queue = collections.deque([(root, 0)]) + while len(queue) > 0: + item = queue.popleft() + if os.path.isfile(os.path.join(item[0], *subpath)): + return item[0] + if item[1] < maxdepth: + for name in os.listdir(item[0]): + fullname = os.path.join(item[0], name) + if os.path.isdir(fullname) and '.' not in name: + queue.append((fullname, item[1] + 1)) + return None +def read_local_properties(): + androidRoot = os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'android') + propsFile = os.path.join(androidRoot, 'local.properties') + sdkDir = None + ndkDir = None + if os.path.exists(propsFile): + with open(propsFile, 'r') as f: + for line in f: + if line[:8] == 'sdk.dir=': + sdkDir = line[8:].strip() + elif line[:8] == 'ndk.dir=': + ndkDir = line[8:].strip() + return (sdkDir, ndkDir) -def locate_sdk(sdkPath): - if (not sdkPath): - sdkPath = raw_input('Enter Android SDK path: ').strip() - # check if directory is correct - androidPath = os.path.join(sdkPath, 'tools', 'android') - if os.path.exists(androidPath): - print 'ANDROID SDK path assigned: %s' % sdkPath - return sdkPath +def query_path(title, option, default, subpath): + default = '' if not default else os.path.abspath(default) + searchHint = ', "s" to search' + while True: + path = raw_input('Path to {0}{1} [{2}]:'.format(title, searchHint, default)) or default + if len(searchHint) > 0 and path.lower().strip() == 's': + found = find_recursive(os.path.expanduser('~'), subpath) + if found: + default = found + searchHint = '' + else: + break + test = os.path.join(path, *subpath) + if path and os.path.isfile(test): + return os.path.abspath(path) else: - print 'Incorrect SDK path.' - exit(1) - - -def locate_ndk(ndkPath): - if (not ndkPath): - ndkPath = raw_input('Enter Anroid NDK path: ').strip() - # check if directory is correct - ndkBuildPath = os.path.join(ndkPath, 'ndk-build') - if os.path.exists(ndkBuildPath): - print 'ANDROID NDK path assigned: %s' % ndkPath - return ndkPath - else: - print 'Incorrect NDK path.' - exit(1) - + print 'Could not find {0}, not an {1} path.'.format(test, title) + sys.exit(1) def write_local_properties(sdkDir, ndkDir): - locPropsContent = ('# Autogenerated file\n' + - '# Do not add it to version control\n' + - 'sdk.dir=%s\n' % sdkDir + - 'ndk.dir=%s\n' % ndkDir) + content = ''.join([x + '\n' for x in [ + '# Autogenerated file', + '# Do not add it to version control', + 'sdk.dir={0}'.format(sdkDir), + 'ndk.dir={0}'.format(ndkDir) + ]]) - curDir = os.getcwd() - omimRoot = os.path.dirname(os.path.dirname(curDir)) - androidRoot = os.path.join(omimRoot, 'android') - locPropsOrigin = os.path.join(androidRoot, 'local.properties') + # Create omim/android/local.properties + androidRoot = os.path.join(os.path.dirname(sys.argv[0]), '..', '..', 'android') + propsFile = os.path.join(androidRoot, 'local.properties') + print 'Writing {0}'.format(propsFile) + with open(propsFile, 'w') as f: + f.write(content) - file = open(locPropsOrigin, 'w') - file.write(locPropsContent) - file.close() + # Copy files to folders + for folder in ['YoPme', 'YoPme2', 'UnitTests']: + destFolder = os.path.join(androidRoot, folder) + if not os.path.exists(destFolder): + os.makedirs(destFolder) + dst = os.path.join(destFolder, 'local.properties') + print 'Copying to {0}'.format(dst) + shutil.copy(propsFile, dst) - # copy files to folders - subfolders = ['YoPme', - 'YoPme2', - 'UnitTests'] +if __name__ == '__main__': + parser = OptionParser() + parser.add_option('-s', '--sdk', help='Path to Android SDK') + parser.add_option('-n', '--ndk', help='Path to Android NDK') + options, _ = parser.parse_args() - for folder in subfolders: - dstFold = os.path.join(androidRoot, folder) - if not os.path.exists(dstFold): - os.makedirs(dstFold) - dst = os.path.join(dstFold, 'local.properties') - shutil.copy(locPropsOrigin, dst) - print 'File created:', dst + sdkDir = options.sdk + ndkDir = options.ndk + if not options.sdk or not options.ndk: + sdkDirOld, ndkDirOld = read_local_properties() + if not sdkDir: + sdkDir = sdkDirOld + if not ndkDir: + ndkDir = ndkDirOld + sdkDir = query_path('Android SDK', options.sdk, sdkDir, ['platform-tools', 'adb']) + ndkDir = query_path('Android NDK', options.ndk, ndkDir, ['ndk-build']) -def usage(): - print "Usage: " + sys.argv[0] + " --sdk --ndk " - - -def run(): - try: - opts, args = getopt.getopt(sys.argv[1:], "h", ["sdk=", "ndk=", "help"]) - except getopt.GetoptError as err: - print str(err) - usage() - exit(2) - - sdk = None - ndk = None - for o, a in opts: - if o == "--sdk": - sdk = a - elif o == "--ndk": - ndk = a - elif o in ("-h", "--help"): - usage() - sys.exit() - - sdkDir = locate_sdk(sdk) - ndkDir = locate_ndk(ndk) - print '>>> Writing local.properties' write_local_properties(sdkDir, ndkDir) - print '>>> Done!' - -run()