waf-unpack 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/python2
  2. # -*- mode: python; coding: utf-8 -*-
  3. #
  4. # Most of this code was stolen from Waf which is:
  5. # Thomas Nagy, 2005-2010
  6. # Copyright © 2011, 2016 Jonas Smedegaard <dr@jones.dk>
  7. # Adapted for CDBS purposes by Rémi Thebault <remi.thebault@gmail.com>
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 3, or (at your option)
  12. # any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful, but
  15. # WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. # General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. usage = '''
  22. python waf-unpack --waf=WAF_FILE --dest=DEST_FOLDER
  23. Unpacks the WAF_FILE structure into DEST_FOLDER destination'''
  24. import sys, os
  25. from optparse import OptionParser
  26. C1='#*'
  27. C2='#&'
  28. cwd = os.getcwd()
  29. join = os.path.join
  30. WAF='waf'
  31. def b(x):
  32. return x
  33. if sys.hexversion>0x300000f:
  34. WAF='waf3'
  35. def b(x):
  36. return x.encode()
  37. def err(m):
  38. print(('\033[91mError: %s\033[0m' % m))
  39. sys.exit(1)
  40. def unpack_waf (waf, dest):
  41. f = open(waf,'rb')
  42. c = "corrupted waf (%d)"
  43. while 1:
  44. line = f.readline()
  45. if not line: err("run waf-light from a folder containing wafadmin")
  46. if line == b('#==>\n'):
  47. txt = f.readline()
  48. if not txt: err(c % 1)
  49. if f.readline()!=b('#<==\n'): err(c % 2)
  50. break
  51. if not txt: err(c % 3)
  52. txt = txt[1:-1].replace(b(C1), b('\n')).replace(b(C2), b('\r'))
  53. import shutil, tarfile
  54. try: shutil.rmtree(dest)
  55. except OSError: pass
  56. try:
  57. for x in ['Tools', '3rdparty']:
  58. os.makedirs(join(dest, 'wafadmin', x))
  59. except OSError:
  60. err("Cannot unpack waf lib into %s\nMove waf into a writeable directory" % dest)
  61. os.chdir(dest)
  62. tmp = 't.bz2'
  63. t = open(tmp,'wb')
  64. t.write(txt)
  65. t.close()
  66. t = None
  67. try:
  68. t = tarfile.open(tmp)
  69. except:
  70. try:
  71. os.system('bunzip2 t.bz2')
  72. t = tarfile.open('t')
  73. except:
  74. os.chdir(cwd)
  75. try: shutil.rmtree(dest)
  76. except OSError: pass
  77. err("Waf cannot be unpacked, check that bzip2 support is present")
  78. for x in t: t.extract(x)
  79. t.close()
  80. for x in ['Tools', '3rdparty']:
  81. os.chmod(join('wafadmin',x), 493)
  82. if sys.hexversion>0x300000f:
  83. sys.path = [join(dest, 'wafadmin')] + sys.path
  84. import py3kfixes
  85. py3kfixes.fixdir(dest)
  86. os.chdir(cwd)
  87. if __name__ == '__main__':
  88. parser = OptionParser(usage)
  89. #parser.add_option('-h', '--help', action='store_true', dest='help', default=False, help='Print this help and exits')
  90. parser.add_option('-w', '--waf', action='store', dest='waf', help='The Waf file structure', metavar='WAF_FILE')
  91. parser.add_option('-d', '--dest', action='store', dest='dest', help='The destination folder. Created if needed', metavar='DEST_FOLDER')
  92. (options, args) = parser.parse_args()
  93. if not options.waf and not options.dest:
  94. print '--waf and --dest options are mandatory'
  95. parser.print_help()
  96. sys.exit(1)
  97. print 'Unpacking ' + options.waf + ' to ' + options.dest + ' ...'
  98. unpack_waf(options.waf, options.dest)
  99. print 'Done'