chiark / gitweb /
Merge branch 'master' into verbose-rewrite
[fdroidserver.git] / fdroidserver / publish.py
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3 #
4 # publish.py - part of the FDroid server tools
5 # Copyright (C) 2010-13, Ciaran Gultnieks, ciaran@ciarang.com
6 # Copyright (C) 2013 Daniel Martí <mvdan@mvdan.cc>
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU Affero General Public License for more details.
17 #
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
21 import sys
22 import os
23 import shutil
24 import subprocess
25 import md5
26 import glob
27 from optparse import OptionParser
28
29 import common
30 from common import BuildException
31
32 config = None
33 options = None
34
35 def main():
36
37     global config, options
38
39     # Parse command line...
40     parser = OptionParser()
41     parser.add_option("-v", "--verbose", action="store_true", default=False,
42                       help="Spew out even more information than normal")
43     parser.add_option("-p", "--package", default=None,
44                       help="Publish only the specified package")
45     (options, args) = parser.parse_args()
46
47     config = common.read_config(options)
48
49     log_dir = 'logs'
50     if not os.path.isdir(log_dir):
51         print "Creating log directory"
52         os.makedirs(log_dir)
53
54     tmp_dir = 'tmp'
55     if not os.path.isdir(tmp_dir):
56         print "Creating temporary directory"
57         os.makedirs(tmp_dir)
58
59     output_dir = 'repo'
60     if not os.path.isdir(output_dir):
61         print "Creating output directory"
62         os.makedirs(output_dir)
63
64     unsigned_dir = 'unsigned'
65     if not os.path.isdir(unsigned_dir):
66         print "No unsigned directory - nothing to do"
67         sys.exit(0)
68
69     for apkfile in sorted(glob.glob(os.path.join(unsigned_dir, '*.apk'))):
70
71         apkfilename = os.path.basename(apkfile)
72         i = apkfilename.rfind('_')
73         if i == -1:
74             raise BuildException("Invalid apk name")
75         appid = apkfilename[:i]
76         print "Processing " + appid
77
78         if not options.package or options.package == appid:
79
80             # Figure out the key alias name we'll use. Only the first 8
81             # characters are significant, so we'll use the first 8 from
82             # the MD5 of the app's ID and hope there are no collisions.
83             # If a collision does occur later, we're going to have to
84             # come up with a new alogrithm, AND rename all existing keys
85             # in the keystore!
86             if appid in config['keyaliases']:
87                 # For this particular app, the key alias is overridden...
88                 keyalias = config['keyaliases'][appid]
89                 if keyalias.startswith('@'):
90                     m = md5.new()
91                     m.update(keyalias[1:])
92                     keyalias = m.hexdigest()[:8]
93             else:
94                 m = md5.new()
95                 m.update(appid)
96                 keyalias = m.hexdigest()[:8]
97             print "Key alias: " + keyalias
98
99             # See if we already have a key for this application, and
100             # if not generate one...
101             p = subprocess.Popen(['keytool', '-list',
102                 '-alias', keyalias, '-keystore', config['keystore'],
103                 '-storepass', config['keystorepass']], stdout=subprocess.PIPE)
104             output = p.communicate()[0]
105             if p.returncode !=0:
106                 print "Key does not exist - generating..."
107                 p = subprocess.Popen(['keytool', '-genkey',
108                     '-keystore', config['keystore'], '-alias', keyalias,
109                     '-keyalg', 'RSA', '-keysize', '2048',
110                     '-validity', '10000',
111                     '-storepass', config['keystorepass'],
112                     '-keypass', config['keypass'],
113                     '-dname', config['keydname']], stdout=subprocess.PIPE)
114                 output = p.communicate()[0]
115                 print output
116                 if p.returncode != 0:
117                     raise BuildException("Failed to generate key")
118
119             # Sign the application...
120             p = subprocess.Popen(['jarsigner', '-keystore', config['keystore'],
121                 '-storepass', config['keystorepass'],
122                 '-keypass', config['keypass'], '-sigalg',
123                 'MD5withRSA', '-digestalg', 'SHA1',
124                     apkfile, keyalias], stdout=subprocess.PIPE)
125             output = p.communicate()[0]
126             print output
127             if p.returncode != 0:
128                 raise BuildException("Failed to sign application")
129
130             # Zipalign it...
131             p = subprocess.Popen([os.path.join(config['sdk_path'],'tools','zipalign'),
132                                 '-v', '4', apkfile,
133                                 os.path.join(output_dir, apkfilename)],
134                                 stdout=subprocess.PIPE)
135             output = p.communicate()[0]
136             print output
137             if p.returncode != 0:
138                 raise BuildException("Failed to align application")
139             os.remove(apkfile)
140
141             # Move the source tarball into the output directory...
142             tarfilename = apkfilename[:-4] + '_src.tar.gz'
143             shutil.move(os.path.join(unsigned_dir, tarfilename),
144                     os.path.join(output_dir, tarfilename))
145
146             print 'Published ' + apkfilename
147
148
149 if __name__ == "__main__":
150     main()
151