Offline Server  1.6
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
generatesnippet.py
Go to the documentation of this file.
1 # ***** BEGIN LICENSE BLOCK *****
2 # Version: MPL 1.1/GPL 2.0/LGPL 2.1
3 #
4 # The contents of this file are subject to the Mozilla Public License Version
5 # 1.1 (the "License"); you may not use this file except in compliance with
6 # the License. You may obtain a copy of the License at
7 # http://www.mozilla.org/MPL/
8 #
9 # Software distributed under the License is distributed on an "AS IS" basis,
10 # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 # for the specific language governing rights and limitations under the
12 # License.
13 #
14 # The Original Code is the Mozilla build system
15 #
16 # The Initial Developer of the Original Code is Mozilla.
17 # Portions created by the Initial Developer are Copyright (C) 2009
18 # the Initial Developer. All Rights Reserved.
19 #
20 # Contributor(s):
21 # Armen Zambrano Gasparnian <armenzg@mozilla.com>
22 #
23 # Alternatively, the contents of this file may be used under the terms of
24 # either the GNU General Public License Version 2 or later (the "GPL"), or
25 # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 # in which case the provisions of the GPL or the LGPL are applicable instead
27 # of those above. If you wish to allow use of your version of this file only
28 # under the terms of either the GPL or the LGPL, and not to allow others to
29 # use your version of this file under the terms of the MPL, indicate your
30 # decision by deleting the provisions above and replace them with the notice
31 # and other provisions required by the GPL or the LGPL. If you do not delete
32 # the provisions above, a recipient may use your version of this file under
33 # the terms of any one of the MPL, the GPL or the LGPL.
34 #
35 # ***** END LICENSE BLOCK *****
36 
37 """
38 This script generates the complete snippet for a given locale or en-US
39 Most of the parameters received are to generate the MAR's download URL
40 and determine the MAR's filename
41 """
42 import sys, os, platform, sha
43 from optparse import OptionParser
44 from ConfigParser import ConfigParser
45 from stat import ST_SIZE
46 
47 def main():
48  error = False
49  parser = OptionParser(
50  usage="%prog [options]")
51  parser.add_option("--mar-path",
52  action="store",
53  dest="marPath",
54  help="[Required] Specify the absolute path where the MAR file is found.")
55  parser.add_option("--application-ini-file",
56  action="store",
57  dest="applicationIniFile",
58  help="[Required] Specify the absolute path to the application.ini file.")
59  parser.add_option("-l",
60  "--locale",
61  action="store",
62  dest="locale",
63  help="[Required] Specify which locale we are generating the snippet for.")
64  parser.add_option("-p",
65  "--product",
66  action="store",
67  dest="product",
68  help="[Required] This option is used to generate the URL to download the MAR file.")
69  parser.add_option("--platform",
70  action="store",
71  dest="platform",
72  help="[Required] This option is used to indicate which target platform.")
73  parser.add_option("--branch",
74  action="store",
75  dest="branch",
76  help="This option is used to indicate which branch name to use for FTP file names.")
77  parser.add_option("--download-base-URL",
78  action="store",
79  dest="downloadBaseURL",
80  help="This option indicates under which.")
81  parser.add_option("-v",
82  "--verbose",
83  action="store_true",
84  dest="verbose",
85  default=False,
86  help="This option increases the output of the script.")
87  (options, args) = parser.parse_args()
88  for req, msg in (('marPath', "the absolute path to the where the MAR file is"),
89  ('applicationIniFile', "the absolute path to the application.ini file."),
90  ('locale', "a locale."),
91  ('product', "specify a product."),
92  ('platform', "specify the platform.")):
93  if not hasattr(options, req):
94  parser.error('You must specify %s' % msg)
95 
96  if not options.downloadBaseURL or options.downloadBaseURL == '':
97  options.downloadBaseURL = 'http://ftp.mozilla.org/pub/mozilla.org/%s/nightly' % options.product
98 
99  if not options.branch or options.branch == '':
100  options.branch = None
101 
102  snippet = generateSnippet(options.marPath,
103  options.applicationIniFile,
104  options.locale,
105  options.downloadBaseURL,
106  options.product,
107  options.platform,
108  options.branch)
109  f = open(os.path.join(options.marPath, 'complete.update.snippet'), 'wb')
110  f.write(snippet)
111  f.close()
112 
113  if options.verbose:
114  # Show in our logs what the contents of the snippet are
115  print snippet
116 
117 def generateSnippet(abstDistDir, applicationIniFile, locale,
118  downloadBaseURL, product, platform, branch):
119  # Let's extract information from application.ini
120  c = ConfigParser()
121  try:
122  c.readfp(open(applicationIniFile))
123  except IOError, (stderror):
124  sys.exit(stderror)
125  buildid = c.get("App", "BuildID")
126  appVersion = c.get("App", "Version")
127  branchName = branch or c.get("App", "SourceRepository").split('/')[-1]
128 
129  marFileName = '%s-%s.%s.%s.complete.mar' % (
130  product,
131  appVersion,
132  locale,
133  platform)
134  # Let's determine the hash and the size of the MAR file
135  # This function exits the script if the file does not exist
136  (completeMarHash, completeMarSize) = getFileHashAndSize(
137  os.path.join(abstDistDir, marFileName))
138  # Construct the URL to where the MAR file will exist
139  interfix = ''
140  if locale == 'en-US':
141  interfix = ''
142  else:
143  interfix = '-l10n'
144  marDownloadURL = "%s/%s%s/%s" % (downloadBaseURL,
145  datedDirPath(buildid, branchName),
146  interfix,
147  marFileName)
148 
149  snippet = """complete
150 %(marDownloadURL)s
151 sha1
152 %(completeMarHash)s
153 %(completeMarSize)s
154 %(buildid)s
155 %(appVersion)s
156 %(appVersion)s
157 """ % dict( marDownloadURL=marDownloadURL,
158  completeMarHash=completeMarHash,
159  completeMarSize=completeMarSize,
160  buildid=buildid,
161  appVersion=appVersion)
162 
163  return snippet
164 
165 def getFileHashAndSize(filepath):
166  sha1Hash = 'UNKNOWN'
167  size = 'UNKNOWN'
168 
169  try:
170  # open in binary mode to make sure we get consistent results
171  # across all platforms
172  f = open(filepath, "rb")
173  shaObj = sha.new(f.read())
174  sha1Hash = shaObj.hexdigest()
175  size = os.stat(filepath)[ST_SIZE]
176  except IOError, (stderror):
177  sys.exit(stderror)
178 
179  return (sha1Hash, size)
180 
181 def datedDirPath(buildid, milestone):
182  """
183  Returns a string that will look like:
184  2009/12/2009-12-31-09-mozilla-central
185  """
186  year = buildid[0:4]
187  month = buildid[4:6]
188  day = buildid[6:8]
189  hour = buildid[8:10]
190  datedDir = "%s-%s-%s-%s-%s" % (year,
191  month,
192  day,
193  hour,
194  milestone)
195  return "%s/%s/%s" % (year, month, datedDir)
196 
197 if __name__ == '__main__':
198  main()
← centre documentaire © anakeen - published under CC License - Dynacase