build_archive.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. # <pep8 compliant>
  3. import os
  4. import shutil
  5. import subprocess
  6. import sys
  7. # todo:
  8. # strip executables
  9. # get parameters
  10. if len(sys.argv) < 5:
  11. sys.stderr.write('Excepted arguments: ./build_archive.py name extension install_dir output_dir')
  12. sys.exit(1)
  13. package_name = sys.argv[1]
  14. extension = sys.argv[2]
  15. install_dir = sys.argv[3]
  16. output_dir = sys.argv[4]
  17. package_archive = os.path.join(output_dir, package_name + '.' + extension)
  18. package_dir = package_name
  19. # remove existing package with the same name
  20. try:
  21. if os.path.exists(package_archive):
  22. os.remove(package_archive)
  23. if os.path.exists(package_dir):
  24. shutil.rmtree(package_dir)
  25. except Exception as ex:
  26. sys.stderr.write('Failed to clean up old package files: ' + str(ex) + '\n')
  27. sys.exit(1)
  28. # create temporary package dir
  29. try:
  30. shutil.copytree(install_dir, package_dir)
  31. for f in os.listdir(package_dir):
  32. if f.startswith('makes'):
  33. os.remove(os.path.join(package_dir, f))
  34. except Exception as ex:
  35. sys.stderr.write('Failed to copy install directory: ' + str(ex) + '\n')
  36. sys.exit(1)
  37. # create archive
  38. try:
  39. if not os.path.exists(output_dir):
  40. os.mkdir(output_dir)
  41. if extension == 'zip':
  42. archive_cmd = ['zip', '-9', '-r', package_archive, package_dir]
  43. elif extension == 'tar.bz2':
  44. archive_cmd = ['tar', 'cjf', package_archive, package_dir]
  45. else:
  46. sys.stderr.write('Unknown archive extension: ' + extension)
  47. sys.exit(-1)
  48. subprocess.call(archive_cmd)
  49. except Exception as ex:
  50. sys.stderr.write('Failed to create package archive: ' + str(ex) + '\n')
  51. sys.exit(1)
  52. # empty temporary package dir
  53. try:
  54. shutil.rmtree(package_dir)
  55. except Exception as ex:
  56. sys.stderr.write('Failed to clean up package directory: ' + str(ex) + '\n')
  57. sys.exit(1)