chiark / gitweb /
Added myself to copyright notices
[fdroidserver.git] / fdroidserver / publish.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # publish.py - part of the FDroid server tools
5 # Copyright (C) 2010-12, 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 from common import BuildException
30
31 def main():
32
33     #Read configuration...
34     execfile('config.py', globals())
35
36     # Parse command line...
37     parser = OptionParser()
38     parser.add_option("-v", "--verbose", action="store_true", default=False,
39                       help="Spew out even more information than normal")
40     parser.add_option("-p", "--package", default=None,
41                       help="Publish only the specified package")
42     (options, args) = parser.parse_args()
43
44     log_dir = 'logs'
45     if not os.path.isdir(log_dir):
46         print "Creating log directory"
47         os.makedirs(log_dir)
48
49     tmp_dir = 'tmp'
50     if not os.path.isdir(tmp_dir):
51         print "Creating temporary directory"
52         os.makedirs(tmp_dir)
53
54     output_dir = 'repo'
55     if not os.path.isdir(output_dir):
56         print "Creating output directory"
57         os.makedirs(output_dir)
58
59     unsigned_dir = 'unsigned'
60     if not os.path.isdir(unsigned_dir):
61         print "No unsigned directory - nothing to do"
62         sys.exit(0)
63
64     for apkfile in sorted(glob.glob(os.path.join(unsigned_dir, '*.apk'))):
65
66         apkfilename = os.path.basename(apkfile)
67         i = apkfilename.rfind('_')
68         if i == -1:
69             raise BuildException("Invalid apk name")
70         appid = apkfilename[:i]
71         print "Processing " + appid
72
73         if not options.package or options.package == appid:
74
75             # Figure out the key alias name we'll use. Only the first 8
76             # characters are significant, so we'll use the first 8 from
77             # the MD5 of the app's ID and hope there are no collisions.
78             # If a collision does occur later, we're going to have to
79             # come up with a new alogrithm, AND rename all existing keys
80             # in the keystore!
81             if appid in keyaliases:
82                 # For this particular app, the key alias is overridden...
83                 keyalias = keyaliases[appid]
84                 if keyalias.startswith('@'):
85                     m = md5.new()
86                     m.update(keyalias[1:])
87                     keyalias = m.hexdigest()[:8]
88             else:
89                 m = md5.new()
90                 m.update(appid)
91                 keyalias = m.hexdigest()[:8]
92             print "Key alias: " + keyalias
93
94             # See if we already have a key for this application, and
95             # if not generate one...
96             p = subprocess.Popen(['keytool', '-list',
97                 '-alias', keyalias, '-keystore', keystore,
98                 '-storepass', keystorepass], stdout=subprocess.PIPE)
99             output = p.communicate()[0]
100             if p.returncode !=0:
101                 print "Key does not exist - generating..."
102                 p = subprocess.Popen(['keytool', '-genkey',
103                     '-keystore', keystore, '-alias', keyalias,
104                     '-keyalg', 'RSA', '-keysize', '2048',
105                     '-validity', '10000',
106                     '-storepass', keystorepass, '-keypass', keypass,
107                     '-dname', keydname], stdout=subprocess.PIPE)
108                 output = p.communicate()[0]
109                 print output
110                 if p.returncode != 0:
111                     raise BuildException("Failed to generate key")
112
113             # Sign the application...
114             p = subprocess.Popen(['jarsigner', '-keystore', keystore,
115                 '-storepass', keystorepass, '-keypass', keypass, '-sigalg',
116                 'MD5withRSA', '-digestalg', 'SHA1',
117                     apkfile, keyalias], stdout=subprocess.PIPE)
118             output = p.communicate()[0]
119             print output
120             if p.returncode != 0:
121                 raise BuildException("Failed to sign application")
122
123             # Zipalign it...
124             p = subprocess.Popen([os.path.join(sdk_path,'tools','zipalign'),
125                                 '-v', '4', apkfile,
126                                 os.path.join(output_dir, apkfilename)],
127                                 stdout=subprocess.PIPE)
128             output = p.communicate()[0]
129             print output
130             if p.returncode != 0:
131                 raise BuildException("Failed to align application")
132             os.remove(apkfile)
133
134             # Move the source tarball into the output directory...
135             tarfilename = apkfilename[:-4] + '_src.tar.gz'
136             shutil.move(os.path.join(unsigned_dir, tarfilename),
137                     os.path.join(output_dir, tarfilename))
138
139             print 'Published ' + apkfilename
140
141
142 if __name__ == "__main__":
143     main()
144