artifacts.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. from urllib.request import urlopen
  3. import json
  4. import os
  5. import subprocess
  6. import sys
  7. import getpass
  8. def get_json(url):
  9. return json.loads(urlopen(url).read().decode('utf-8'))
  10. def download_file(src_url, dest_path):
  11. print(dest_path)
  12. subprocess.call(
  13. ['curl', '-L', '-#', '-o', dest_path, src_url])
  14. def download_appveyor_artifacts():
  15. api_url = 'https://ci.appveyor.com/api'
  16. builds = get_json(
  17. '{}/projects/etrepum/simplejson'.format(api_url))
  18. for job in builds['build']['jobs']:
  19. url = '{api_url}/buildjobs/{jobId}/artifacts'.format(
  20. api_url=api_url, **job)
  21. for artifact in get_json(url):
  22. download_file(
  23. '{url}/{fileName}'.format(url=url, **artifact),
  24. artifact['fileName'])
  25. def download_github_artifacts():
  26. release = get_json(
  27. 'https://api.github.com/repos/simplejson/simplejson/releases/latest')
  28. for asset in release['assets']:
  29. download_file(asset['browser_download_url'], 'dist/{name}'.format(**asset))
  30. def get_version():
  31. return subprocess.check_output(
  32. [sys.executable, 'setup.py', '--version'],
  33. encoding='utf8'
  34. ).strip()
  35. def artifact_matcher(version):
  36. prefix = 'simplejson-{}'.format(version)
  37. def matches(fn):
  38. return (
  39. fn.startswith(prefix) and
  40. os.path.splitext(fn)[1] in ('.exe', '.whl') and
  41. not fn.endswith('-none-any.whl')
  42. ) or fn == '{}.tar.gz'.format(prefix)
  43. return matches
  44. def sign_artifacts(version):
  45. artifacts = set(os.listdir('dist'))
  46. matches = artifact_matcher(version)
  47. passphrase = getpass.getpass('\nGPG Passphrase:')
  48. for fn in artifacts:
  49. if matches(fn) and '{}.asc'.format(fn) not in artifacts:
  50. sign_artifact(os.path.join('dist', fn), passphrase)
  51. def sign_artifact(path, passphrase):
  52. cmd = [
  53. 'gpg',
  54. '--detach-sign',
  55. '--batch',
  56. '--passphrase-fd', '0',
  57. '--armor',
  58. path
  59. ]
  60. print(' '.join(cmd))
  61. subprocess.run(cmd, check=True, input=passphrase, encoding='utf8')
  62. def upload_artifacts(version):
  63. artifacts = set(os.listdir('dist'))
  64. matches = artifact_matcher(version)
  65. args = ['twine', 'upload']
  66. for fn in artifacts:
  67. if matches(fn):
  68. filename = os.path.join('dist', fn)
  69. args.extend([filename, filename + '.asc'])
  70. subprocess.check_call(args)
  71. def main():
  72. try:
  73. os.makedirs('dist')
  74. except OSError:
  75. pass
  76. download_appveyor_artifacts()
  77. download_github_artifacts()
  78. version = get_version()
  79. sign_artifacts(version)
  80. upload_artifacts(version)
  81. if __name__ == '__main__':
  82. main()