master_unpack.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # ##### BEGIN GPL LICENSE BLOCK #####
  2. #
  3. # This program is free software; you can redistribute it and/or
  4. # modify it under the terms of the GNU General Public License
  5. # as published by the Free Software Foundation; either version 2
  6. # of the License, or (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the Free Software Foundation,
  15. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. #
  17. # ##### END GPL LICENSE BLOCK #####
  18. # Runs on Buildbot master, to unpack incoming unload.zip into latest
  19. # builds directory and remove older builds.
  20. # <pep8 compliant>
  21. import os
  22. import shutil
  23. import sys
  24. import zipfile
  25. # extension stripping
  26. def strip_extension(filename):
  27. extensions = '.zip', '.tar', '.bz2', '.gz', '.tgz', '.tbz', '.exe'
  28. for ext in extensions:
  29. if filename.endswith(ext):
  30. filename = filename[:-len(ext)]
  31. return filename
  32. # extract platform from package name
  33. def get_platform(filename):
  34. # name is blender-version-platform.extension. we want to get the
  35. # platform out, but there may be some variations, so we fiddle a
  36. # bit to handle current and hopefully future names
  37. filename = strip_extension(filename)
  38. filename = strip_extension(filename)
  39. tokens = filename.split("-")
  40. platforms = ('osx', 'mac', 'bsd',
  41. 'win', 'linux', 'source',
  42. 'irix', 'solaris')
  43. platform_tokens = []
  44. found = False
  45. for i, token in enumerate(tokens):
  46. if not found:
  47. for platform in platforms:
  48. if platform in token.lower():
  49. found = True
  50. break
  51. if found:
  52. platform_tokens += [token]
  53. return '-'.join(platform_tokens)
  54. def get_branch(filename):
  55. if filename.startswith("blender-2.8"):
  56. return "blender2.8"
  57. tokens = filename.split("-")
  58. branch = ""
  59. for token in tokens:
  60. if token == "blender":
  61. return branch
  62. if branch == "":
  63. branch = token
  64. else:
  65. branch = branch + "-" + token
  66. return ""
  67. # get filename
  68. if len(sys.argv) < 2:
  69. sys.stderr.write("Not enough arguments, expecting file to unpack\n")
  70. sys.exit(1)
  71. filename = sys.argv[1]
  72. # open zip file
  73. if not os.path.exists(filename):
  74. sys.stderr.write("File %r not found.\n" % filename)
  75. sys.exit(1)
  76. try:
  77. z = zipfile.ZipFile(filename, "r")
  78. except Exception as ex:
  79. sys.stderr.write('Failed to open zip file: %s\n' % str(ex))
  80. sys.exit(1)
  81. if len(z.namelist()) != 1:
  82. sys.stderr.write("Expected one file in %r." % filename)
  83. sys.exit(1)
  84. package = z.namelist()[0]
  85. packagename = os.path.basename(package)
  86. # detect platform and branch
  87. platform = get_platform(packagename)
  88. branch = get_branch(packagename)
  89. if platform == '':
  90. sys.stderr.write('Failed to detect platform ' +
  91. 'from package: %r\n' % packagename)
  92. sys.exit(1)
  93. # extract
  94. if not branch or branch == 'master':
  95. directory = 'public_html/download'
  96. elif branch == 'experimental-build':
  97. directory = 'public_html/download/experimental'
  98. else:
  99. directory = 'public_html/download'
  100. try:
  101. filename = os.path.join(directory, packagename)
  102. zf = z.open(package)
  103. f = file(filename, "wb")
  104. shutil.copyfileobj(zf, f)
  105. os.chmod(filename, 0644)
  106. zf.close()
  107. z.close()
  108. except Exception as ex:
  109. sys.stderr.write('Failed to unzip package: %s\n' % str(ex))
  110. sys.exit(1)
  111. # remove other files from the same platform and branch
  112. try:
  113. for f in os.listdir(directory):
  114. if get_platform(f) == platform and get_branch(f) == branch:
  115. if f != packagename:
  116. os.remove(os.path.join(directory, f))
  117. except Exception as ex:
  118. sys.stderr.write('Failed to remove old packages: %s\n' % str(ex))
  119. sys.exit(1)