diff options
author | Ilya Zverev <zverik@textual.ru> | 2016-01-20 19:16:36 +0300 |
---|---|---|
committer | Ilya Zverev <zverik@textual.ru> | 2016-01-20 19:21:19 +0300 |
commit | 9d2f64d7bb4949445977e903e195b3b24dc7f038 (patch) | |
tree | 2a27f52f0a09e4e199dcf0e31338823d05e0e2be /tools/android | |
parent | ef2f26e5f0eb1ea70493629d9e21b4d3483c81cb (diff) |
[android] Rewrite set_up_android.py script
Diffstat (limited to 'tools/android')
-rwxr-xr-x | tools/android/set_up_android.py | 174 |
1 files changed, 87 insertions, 87 deletions
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 -import shutil -import getopt -import sys - - -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 +import os, sys, shutil, collections +from optparse import OptionParser + +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 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) - - curDir = os.getcwd() - omimRoot = os.path.dirname(os.path.dirname(curDir)) - androidRoot = os.path.join(omimRoot, 'android') - locPropsOrigin = os.path.join(androidRoot, 'local.properties') - - file = open(locPropsOrigin, 'w') - file.write(locPropsContent) - file.close() + 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) + ]]) + + # 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) + + # 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) + +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() + + 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']) - # copy files to folders - subfolders = ['YoPme', - 'YoPme2', - 'UnitTests'] - - 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 - - -def usage(): - print "Usage: " + sys.argv[0] + " --sdk <SDK path> --ndk <NDK path>" - - -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() |