tools/update-packaging/generatesnippet.py

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 # This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 # License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4
michael@0 5 """
michael@0 6 This script generates the complete snippet for a given locale or en-US
michael@0 7 Most of the parameters received are to generate the MAR's download URL
michael@0 8 and determine the MAR's filename
michael@0 9 """
michael@0 10 import sys, os, platform, sha
michael@0 11 from optparse import OptionParser
michael@0 12 from ConfigParser import ConfigParser
michael@0 13 from stat import ST_SIZE
michael@0 14
michael@0 15 def main():
michael@0 16 error = False
michael@0 17 parser = OptionParser(
michael@0 18 usage="%prog [options]")
michael@0 19 parser.add_option("--mar-path",
michael@0 20 action="store",
michael@0 21 dest="marPath",
michael@0 22 help="[Required] Specify the absolute path where the MAR file is found.")
michael@0 23 parser.add_option("--application-ini-file",
michael@0 24 action="store",
michael@0 25 dest="applicationIniFile",
michael@0 26 help="[Required] Specify the absolute path to the application.ini file.")
michael@0 27 parser.add_option("-l",
michael@0 28 "--locale",
michael@0 29 action="store",
michael@0 30 dest="locale",
michael@0 31 help="[Required] Specify which locale we are generating the snippet for.")
michael@0 32 parser.add_option("-p",
michael@0 33 "--product",
michael@0 34 action="store",
michael@0 35 dest="product",
michael@0 36 help="[Required] This option is used to generate the URL to download the MAR file.")
michael@0 37 parser.add_option("--platform",
michael@0 38 action="store",
michael@0 39 dest="platform",
michael@0 40 help="[Required] This option is used to indicate which target platform.")
michael@0 41 parser.add_option("--branch",
michael@0 42 action="store",
michael@0 43 dest="branch",
michael@0 44 help="This option is used to indicate which branch name to use for FTP file names.")
michael@0 45 parser.add_option("--download-base-URL",
michael@0 46 action="store",
michael@0 47 dest="downloadBaseURL",
michael@0 48 help="This option indicates under which.")
michael@0 49 parser.add_option("-v",
michael@0 50 "--verbose",
michael@0 51 action="store_true",
michael@0 52 dest="verbose",
michael@0 53 default=False,
michael@0 54 help="This option increases the output of the script.")
michael@0 55 (options, args) = parser.parse_args()
michael@0 56 for req, msg in (('marPath', "the absolute path to the where the MAR file is"),
michael@0 57 ('applicationIniFile', "the absolute path to the application.ini file."),
michael@0 58 ('locale', "a locale."),
michael@0 59 ('product', "specify a product."),
michael@0 60 ('platform', "specify the platform.")):
michael@0 61 if not hasattr(options, req):
michael@0 62 parser.error('You must specify %s' % msg)
michael@0 63
michael@0 64 if not options.downloadBaseURL or options.downloadBaseURL == '':
michael@0 65 options.downloadBaseURL = 'http://ftp.mozilla.org/pub/mozilla.org/%s/nightly' % options.product
michael@0 66
michael@0 67 if not options.branch or options.branch == '':
michael@0 68 options.branch = None
michael@0 69
michael@0 70 snippet = generateSnippet(options.marPath,
michael@0 71 options.applicationIniFile,
michael@0 72 options.locale,
michael@0 73 options.downloadBaseURL,
michael@0 74 options.product,
michael@0 75 options.platform,
michael@0 76 options.branch)
michael@0 77 f = open(os.path.join(options.marPath, 'complete.update.snippet'), 'wb')
michael@0 78 f.write(snippet)
michael@0 79 f.close()
michael@0 80
michael@0 81 if options.verbose:
michael@0 82 # Show in our logs what the contents of the snippet are
michael@0 83 print snippet
michael@0 84
michael@0 85 def generateSnippet(abstDistDir, applicationIniFile, locale,
michael@0 86 downloadBaseURL, product, platform, branch):
michael@0 87 # Let's extract information from application.ini
michael@0 88 c = ConfigParser()
michael@0 89 try:
michael@0 90 c.readfp(open(applicationIniFile))
michael@0 91 except IOError, (stderror):
michael@0 92 sys.exit(stderror)
michael@0 93 buildid = c.get("App", "BuildID")
michael@0 94 appVersion = c.get("App", "Version")
michael@0 95 branchName = branch or c.get("App", "SourceRepository").split('/')[-1]
michael@0 96
michael@0 97 marFileName = '%s-%s.%s.%s.complete.mar' % (
michael@0 98 product,
michael@0 99 appVersion,
michael@0 100 locale,
michael@0 101 platform)
michael@0 102 # Let's determine the hash and the size of the MAR file
michael@0 103 # This function exits the script if the file does not exist
michael@0 104 (completeMarHash, completeMarSize) = getFileHashAndSize(
michael@0 105 os.path.join(abstDistDir, marFileName))
michael@0 106 # Construct the URL to where the MAR file will exist
michael@0 107 interfix = ''
michael@0 108 if locale == 'en-US':
michael@0 109 interfix = ''
michael@0 110 else:
michael@0 111 interfix = '-l10n'
michael@0 112 marDownloadURL = "%s/%s%s/%s" % (downloadBaseURL,
michael@0 113 datedDirPath(buildid, branchName),
michael@0 114 interfix,
michael@0 115 marFileName)
michael@0 116
michael@0 117 snippet = """complete
michael@0 118 %(marDownloadURL)s
michael@0 119 sha1
michael@0 120 %(completeMarHash)s
michael@0 121 %(completeMarSize)s
michael@0 122 %(buildid)s
michael@0 123 %(appVersion)s
michael@0 124 %(appVersion)s
michael@0 125 """ % dict( marDownloadURL=marDownloadURL,
michael@0 126 completeMarHash=completeMarHash,
michael@0 127 completeMarSize=completeMarSize,
michael@0 128 buildid=buildid,
michael@0 129 appVersion=appVersion)
michael@0 130
michael@0 131 return snippet
michael@0 132
michael@0 133 def getFileHashAndSize(filepath):
michael@0 134 sha1Hash = 'UNKNOWN'
michael@0 135 size = 'UNKNOWN'
michael@0 136
michael@0 137 try:
michael@0 138 # open in binary mode to make sure we get consistent results
michael@0 139 # across all platforms
michael@0 140 f = open(filepath, "rb")
michael@0 141 shaObj = sha.new(f.read())
michael@0 142 sha1Hash = shaObj.hexdigest()
michael@0 143 size = os.stat(filepath)[ST_SIZE]
michael@0 144 except IOError, (stderror):
michael@0 145 sys.exit(stderror)
michael@0 146
michael@0 147 return (sha1Hash, size)
michael@0 148
michael@0 149 def datedDirPath(buildid, milestone):
michael@0 150 """
michael@0 151 Returns a string that will look like:
michael@0 152 2009/12/2009-12-31-09-mozilla-central
michael@0 153 """
michael@0 154 year = buildid[0:4]
michael@0 155 month = buildid[4:6]
michael@0 156 day = buildid[6:8]
michael@0 157 hour = buildid[8:10]
michael@0 158 datedDir = "%s-%s-%s-%s-%s" % (year,
michael@0 159 month,
michael@0 160 day,
michael@0 161 hour,
michael@0 162 milestone)
michael@0 163 return "%s/%s/%s" % (year, month, datedDir)
michael@0 164
michael@0 165 if __name__ == '__main__':
michael@0 166 main()

mercurial