generatesnippet.py 6.1 KB

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