chiark / gitweb /
Basic completion for init, add --verbose to it
[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
46     global options, config
47
48     # Parse command line...
49     parser = OptionParser()
50     parser.add_option("-v", "--verbose", action="store_true", default=False,
51                       help="Spew out even more information than normal")
52     (options, args) = parser.parse_args()
53
54     # find root install prefix
55     tmp = os.path.dirname(sys.argv[0])
56     if os.path.basename(tmp) == 'bin':
57         prefix = os.path.dirname(tmp)
58         examplesdir = prefix + '/share/doc/fdroidserver/examples'
59     else:
60         # we're running straight out of the git repo
61         prefix = tmp
62         examplesdir = prefix
63
64     repodir = os.getcwd()
65
66     if not os.path.exists('config.py') and not os.path.exists('repo'):
67         # 'metadata' and 'tmp' are created in fdroid
68         os.mkdir('repo')
69         shutil.copy(os.path.join(examplesdir, 'fdroid-icon.png'), repodir)
70         shutil.copyfile(os.path.join(examplesdir, 'config.sample.py'), 'config.py')
71     else:
72         print('Looks like this is already an F-Droid repo, cowardly refusing to overwrite it...')
73         sys.exit()
74
75     # now that we have a local config.py, read configuration...
76     config = common.read_config(options)
77
78     # track down where the Android SDK is
79     if os.path.isdir(config['sdk_path']):
80         print('Using "' + config['sdk_path'] + '" for the Android SDK')
81         sdk_path = config['sdk_path']
82     elif 'ANDROID_HOME' in os.environ.keys():
83         sdk_path = os.environ['ANDROID_HOME']
84     else:
85         default_sdk_path = '/opt/android-sdk'
86         while True:
87             s = raw_input('Enter the path to the Android SDK (' + default_sdk_path + '): ')
88             if re.match('^\s*$', s) != None:
89                 sdk_path = default_sdk_path
90             else:
91                 sdk_path = s
92             if os.path.isdir(os.path.join(sdk_path, 'build-tools')):
93                 break
94             else:
95                 print('"' + s + '" does not contain the Android SDK! Try again...')
96     if os.path.isdir(sdk_path):
97         write_to_config('sdk_path', sdk_path)
98
99     # try to find a working aapt, in all the recent possible paths
100     build_tools = os.path.join(sdk_path, 'build-tools')
101     aaptdirs = []
102     aaptdirs.append(os.path.join(build_tools, config['build_tools']))
103     aaptdirs.append(build_tools)
104     for f in sorted(os.listdir(build_tools), reverse=True):
105         if os.path.isdir(os.path.join(build_tools, f)):
106             aaptdirs.append(os.path.join(build_tools, f))
107     for d in aaptdirs:
108         if os.path.isfile(os.path.join(d, 'aapt')):
109             aapt = os.path.join(d, 'aapt')
110             break
111     if os.path.isfile(aapt):
112         dirname = os.path.basename(os.path.dirname(aapt))
113         if dirname == 'build-tools':
114             # this is the old layout, before versioned build-tools
115             write_to_config('build_tools', '')
116         else:
117             write_to_config('build_tools', dirname)
118
119     # track down where the Android NDK is
120     ndk_path = '/opt/android-ndk'
121     if os.path.isdir(config['ndk_path']):
122         ndk_path = config['ndk_path']
123     elif 'ANDROID_NDK' in os.environ.keys():
124         print('using ANDROID_NDK')
125         ndk_path = os.environ['ANDROID_NDK']
126     if os.path.isdir(ndk_path):
127         write_to_config('ndk_path', ndk_path)
128     # the NDK is optional so we don't prompt the user for it if its not found
129
130     print('Built repo in "' + repodir + '" with this config:')
131     print('  Android SDK:\t\t\t' + sdk_path)
132     print('  Android SDK Build Tools:\t' + os.path.dirname(aapt))
133     print('  Android NDK (optional):\t' + ndk_path)
134     print('\nTo complete the setup, add your APKs to "' +
135           os.path.join(repodir, 'repo') + '"' +
136 '''
137 then run "fdroid update -c; fdroid update".  You might also want to edit
138 "config.py" to set the URL, repo name, and more.  You should also set up
139 a signing key.
140
141 For more info: https://f-droid.org/manual/fdroid.html#Simple-Binary-Repository
142 ''')