2 # -*- coding: utf-8 -*-
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>
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
30 from common import BuildException
36 # Read configuration...
37 common.read_config(config)
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()
48 if not os.path.isdir(log_dir):
49 print "Creating log directory"
53 if not os.path.isdir(tmp_dir):
54 print "Creating temporary directory"
58 if not os.path.isdir(output_dir):
59 print "Creating output directory"
60 os.makedirs(output_dir)
62 unsigned_dir = 'unsigned'
63 if not os.path.isdir(unsigned_dir):
64 print "No unsigned directory - nothing to do"
67 for apkfile in sorted(glob.glob(os.path.join(unsigned_dir, '*.apk'))):
69 apkfilename = os.path.basename(apkfile)
70 i = apkfilename.rfind('_')
72 raise BuildException("Invalid apk name")
73 appid = apkfilename[:i]
74 print "Processing " + appid
76 if not options.package or options.package == appid:
78 # Figure out the key alias name we'll use. Only the first 8
79 # characters are significant, so we'll use the first 8 from
80 # the MD5 of the app's ID and hope there are no collisions.
81 # If a collision does occur later, we're going to have to
82 # come up with a new alogrithm, AND rename all existing keys
84 if appid in config['keyaliases']:
85 # For this particular app, the key alias is overridden...
86 keyalias = config['keyaliases'][appid]
87 if keyalias.startswith('@'):
89 m.update(keyalias[1:])
90 keyalias = m.hexdigest()[:8]
94 keyalias = m.hexdigest()[:8]
95 print "Key alias: " + keyalias
97 # See if we already have a key for this application, and
98 # if not generate one...
99 p = subprocess.Popen(['keytool', '-list',
100 '-alias', keyalias, '-keystore', config['keystore'],
101 '-storepass', config['keystorepass']], stdout=subprocess.PIPE)
102 output = p.communicate()[0]
104 print "Key does not exist - generating..."
105 p = subprocess.Popen(['keytool', '-genkey',
106 '-keystore', config['keystore'], '-alias', keyalias,
107 '-keyalg', 'RSA', '-keysize', '2048',
108 '-validity', '10000',
109 '-storepass', config['keystorepass'],
110 '-keypass', config['keypass'],
111 '-dname', config['keydname']], stdout=subprocess.PIPE)
112 output = p.communicate()[0]
114 if p.returncode != 0:
115 raise BuildException("Failed to generate key")
117 # Sign the application...
118 p = subprocess.Popen(['jarsigner', '-keystore', config['keystore'],
119 '-storepass', config['keystorepass'],
120 '-keypass', config['keypass'], '-sigalg',
121 'MD5withRSA', '-digestalg', 'SHA1',
122 apkfile, keyalias], stdout=subprocess.PIPE)
123 output = p.communicate()[0]
125 if p.returncode != 0:
126 raise BuildException("Failed to sign application")
129 p = subprocess.Popen([os.path.join(config['sdk_path'],'tools','zipalign'),
131 os.path.join(output_dir, apkfilename)],
132 stdout=subprocess.PIPE)
133 output = p.communicate()[0]
135 if p.returncode != 0:
136 raise BuildException("Failed to align application")
139 # Move the source tarball into the output directory...
140 tarfilename = apkfilename[:-4] + '_src.tar.gz'
141 shutil.move(os.path.join(unsigned_dir, tarfilename),
142 os.path.join(output_dir, tarfilename))
144 print 'Published ' + apkfilename
147 if __name__ == "__main__":