chiark / gitweb /
Don't override built-in 'dir'
[fdroidserver.git] / fdroidserver / init.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
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>
8 #
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.
13 #
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.
18 #
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/>.
21
22 import os
23 import re
24 import shutil
25 import sys
26 from optparse import OptionParser
27
28 import common
29
30
31 config = {}
32 options = None
33
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:
37         data = f.read()
38     pattern = key + '\s*=.*'
39     repl = key + ' = "' + value + '"'
40     data = re.sub(pattern, repl, data)
41     with open('config.py', 'w') as f:
42         f.writelines(data)
43
44 def main():
45     # Parse command line...
46     global options
47     parser = OptionParser()
48
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'
54     else:
55         # we're running straight out of the git repo
56         prefix = tmp
57         examplesdir = prefix
58
59     repodir = os.getcwd()
60
61     if not os.path.exists('config.py') and not os.path.exists('repo'):
62         # 'metadata' and 'tmp' are created in fdroid
63         os.mkdir('repo')
64         shutil.copy(os.path.join(examplesdir, 'fdroid-icon.png'), repodir)
65         shutil.copyfile(os.path.join(examplesdir, 'config.sample.py'), 'config.py')
66     else:
67         print('Looks like this is already an F-Droid repo, cowardly refusing to overwrite it...')
68         sys.exit()
69
70     # now that we have a local config.py, read configuration...
71     common.read_config(config)
72
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']
79     else:
80         default_sdk_path = '/opt/android-sdk'
81         while True:
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
85             else:
86                 sdk_path = s
87             if os.path.isdir(os.path.join(sdk_path, 'build-tools')):
88                 break
89             else:
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)
93
94     # try to find a working aapt, in all the recent possible paths
95     build_tools = os.path.join(sdk_path, 'build-tools')
96     aaptdirs = []
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))
102     for d in aaptdirs:
103         if os.path.isfile(os.path.join(d, 'aapt')):
104             aapt = os.path.join(d, 'aapt')
105             break
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', '')
111         else:
112             write_to_config('build_tools', dirname)
113
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
124
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') + '"' +
131 '''
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
134 a signing key.
135
136 For more info: https://f-droid.org/manual/fdroid.html#Simple-Binary-Repository
137 ''')