2 # -*- coding: utf-8 -*-
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>
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.
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.
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/>.
27 from optparse import OptionParser
29 from common import BuildException
33 #Read configuration...
34 execfile('config.py', globals())
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()
45 if not os.path.isdir(log_dir):
46 print "Creating log directory"
50 if not os.path.isdir(tmp_dir):
51 print "Creating temporary directory"
55 if not os.path.isdir(output_dir):
56 print "Creating output directory"
57 os.makedirs(output_dir)
59 unsigned_dir = 'unsigned'
60 if not os.path.isdir(unsigned_dir):
61 print "No unsigned directory - nothing to do"
64 for apkfile in sorted(glob.glob(os.path.join(unsigned_dir, '*.apk'))):
66 apkfilename = os.path.basename(apkfile)
67 i = apkfilename.rfind('_')
69 raise BuildException("Invalid apk name")
70 appid = apkfilename[:i]
71 print "Processing " + appid
73 if not options.package or options.package == appid:
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
81 if appid in keyaliases:
82 # For this particular app, the key alias is overridden...
83 keyalias = keyaliases[appid]
84 if keyalias.startswith('@'):
86 m.update(keyalias[1:])
87 keyalias = m.hexdigest()[:8]
91 keyalias = m.hexdigest()[:8]
92 print "Key alias: " + keyalias
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]
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]
110 if p.returncode != 0:
111 raise BuildException("Failed to generate key")
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]
120 if p.returncode != 0:
121 raise BuildException("Failed to sign application")
124 p = subprocess.Popen([os.path.join(sdk_path,'tools','zipalign'),
126 os.path.join(output_dir, apkfilename)],
127 stdout=subprocess.PIPE)
128 output = p.communicate()[0]
130 if p.returncode != 0:
131 raise BuildException("Failed to align application")
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))
139 print 'Published ' + apkfilename
142 if __name__ == "__main__":