2 # -*- coding: utf-8 -*-
4 # update.py - part of the FDroid server tools
5 # Copyright (C) 2010-2013, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013 Daniel Martà <mvdan@mvdan.cc>
7 # Copyright (C) 2013 Hans-Christoph Steiner <hans@eds.org>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU Affero General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU Affero General Public License for more details.
19 # You should have received a copy of the GNU Affero General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 from optparse import OptionParser
34 def write_to_config(key, value):
35 '''write a key/value to the local config.py'''
36 with open('config.py', 'r') as f:
38 pattern = key + '\s*=.*'
39 repl = key + ' = "' + value + '"'
40 data = re.sub(pattern, repl, data)
41 with open('config.py', 'w') as f:
45 # Parse command line...
47 parser = OptionParser()
49 # find root install prefix
50 tmp = os.path.dirname(sys.argv[0])
51 if os.path.basename(tmp) == 'bin':
52 prefix = os.path.dirname(tmp)
53 examplesdir = prefix + '/share/doc/fdroidserver/examples'
55 # we're running straight out of the git repo
61 if not os.path.exists('config.py') and not os.path.exists('repo'):
62 # 'metadata' and 'tmp' are created in fdroid
64 shutil.copy(os.path.join(examplesdir, 'fdroid-icon.png'), repodir)
65 shutil.copyfile(os.path.join(examplesdir, 'config.sample.py'), 'config.py')
67 print('Looks like this is already an F-Droid repo, cowardly refusing to overwrite it...')
70 # now that we have a local config.py, read configuration...
71 common.read_config(config)
73 # track down where the Android SDK is
74 if os.path.isdir(config['sdk_path']):
75 print('Using "' + config['sdk_path'] + '" for the Android SDK')
76 sdk_path = config['sdk_path']
77 elif 'ANDROID_HOME' in os.environ.keys():
78 sdk_path = os.environ['ANDROID_HOME']
80 default_sdk_path = '/opt/android-sdk'
82 s = raw_input('Enter the path to the Android SDK (' + default_sdk_path + '): ')
83 if re.match('^\s*$', s) != None:
84 sdk_path = default_sdk_path
87 if os.path.isdir(os.path.join(sdk_path, 'build-tools')):
90 print('"' + s + '" does not contain the Android SDK! Try again...')
91 if os.path.isdir(sdk_path):
92 write_to_config('sdk_path', sdk_path)
94 # try to find a working aapt, in all the recent possible paths
95 build_tools = os.path.join(sdk_path, 'build-tools')
97 aaptdirs.append(os.path.join(build_tools, config['build_tools']))
98 aaptdirs.append(build_tools)
99 for f in sorted(os.listdir(build_tools), reverse=True):
100 if os.path.isdir(os.path.join(build_tools, f)):
101 aaptdirs.append(os.path.join(build_tools, f))
103 if os.path.isfile(os.path.join(d, 'aapt')):
104 aapt = os.path.join(d, 'aapt')
106 if os.path.isfile(aapt):
107 dirname = os.path.basename(os.path.dirname(aapt))
108 if dirname == 'build-tools':
109 # this is the old layout, before versioned build-tools
110 write_to_config('build_tools', '')
112 write_to_config('build_tools', dirname)
114 # track down where the Android NDK is
115 ndk_path = '/opt/android-ndk'
116 if os.path.isdir(config['ndk_path']):
117 ndk_path = config['ndk_path']
118 elif 'ANDROID_NDK' in os.environ.keys():
119 print('using ANDROID_NDK')
120 ndk_path = os.environ['ANDROID_NDK']
121 if os.path.isdir(ndk_path):
122 write_to_config('ndk_path', ndk_path)
123 # the NDK is optional so we don't prompt the user for it if its not found
125 print('Built repo in "' + repodir + '" with this config:')
126 print(' Android SDK:\t\t\t' + sdk_path)
127 print(' Android SDK Build Tools:\t' + os.path.dirname(aapt))
128 print(' Android NDK (optional):\t' + ndk_path)
129 print('\nTo complete the setup, add your APKs to "' +
130 os.path.join(repodir, 'repo') + '"' +
132 then run "fdroid update -c; fdroid update". You might also want to edit
133 "config.py" to set the URL, repo name, and more. You should also set up
136 For more info: https://f-droid.org/manual/fdroid.html#Simple-Binary-Repository