upload-node-checksums.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python
  2. import argparse
  3. import hashlib
  4. import os
  5. import shutil
  6. import tempfile
  7. from lib.config import s3_config
  8. from lib.util import download, rm_rf, s3put, safe_mkdir
  9. DIST_URL = 'https://atom.io/download/electron/'
  10. def main():
  11. args = parse_args()
  12. dist_url = args.dist_url
  13. if dist_url[-1] != "/":
  14. dist_url += "/"
  15. url = dist_url + args.version + '/'
  16. directory, files = download_files(url, get_files_list(args.version))
  17. checksums = [
  18. create_checksum('sha1', directory, 'SHASUMS.txt', files),
  19. create_checksum('sha256', directory, 'SHASUMS256.txt', files)
  20. ]
  21. if args.target_dir is None:
  22. bucket, access_key, secret_key = s3_config()
  23. s3put(bucket, access_key, secret_key, directory,
  24. 'atom-shell/dist/{0}'.format(args.version), checksums)
  25. else:
  26. copy_files(checksums, args.target_dir)
  27. rm_rf(directory)
  28. def parse_args():
  29. parser = argparse.ArgumentParser(description='upload sumsha file')
  30. parser.add_argument('-v', '--version', help='Specify the version',
  31. required=True)
  32. parser.add_argument('-u', '--dist-url',
  33. help='Specify the dist url for downloading',
  34. required=False, default=DIST_URL)
  35. parser.add_argument('-t', '--target-dir',
  36. help='Specify target dir of checksums',
  37. required=False)
  38. return parser.parse_args()
  39. def get_files_list(version):
  40. return [
  41. { "filename": 'node-{0}.tar.gz'.format(version), "required": True },
  42. { "filename": 'iojs-{0}.tar.gz'.format(version), "required": True },
  43. { "filename": 'iojs-{0}-headers.tar.gz'.format(version), "required": True },
  44. { "filename": 'node.lib', "required": False },
  45. { "filename": 'x64/node.lib', "required": False },
  46. { "filename": 'win-x86/iojs.lib', "required": False },
  47. { "filename": 'win-x64/iojs.lib', "required": False }
  48. ]
  49. def download_files(url, files):
  50. directory = tempfile.mkdtemp(prefix='electron-tmp')
  51. result = []
  52. for optional_f in files:
  53. required = optional_f['required']
  54. f = optional_f['filename']
  55. try:
  56. result.append(download(f, url + f, os.path.join(directory, f)))
  57. except Exception:
  58. if required:
  59. raise
  60. return directory, result
  61. def create_checksum(algorithm, directory, filename, files):
  62. lines = []
  63. for path in files:
  64. h = hashlib.new(algorithm)
  65. with open(path, 'r') as f:
  66. h.update(f.read())
  67. lines.append(h.hexdigest() + ' ' + os.path.relpath(path, directory))
  68. checksum_file = os.path.join(directory, filename)
  69. with open(checksum_file, 'w') as f:
  70. f.write('\n'.join(lines) + '\n')
  71. return checksum_file
  72. def copy_files(source_files, output_dir):
  73. for source_file in source_files:
  74. output_path = os.path.join(output_dir, os.path.basename(source_file))
  75. safe_mkdir(os.path.dirname(output_path))
  76. shutil.copy2(source_file, output_path)
  77. if __name__ == '__main__':
  78. import sys
  79. sys.exit(main())