release.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #! /usr/bin/env python3
  2. import argparse, base64, common, glob, os, platform, re, subprocess, sys, urllib.request, zipfile
  3. def log_errors(name):
  4. def wrap(f):
  5. def result(*args, **kwargs):
  6. try:
  7. f(*args, **kwargs)
  8. except Exception as e:
  9. print(f"{name}: Failed {e}")
  10. return result
  11. return wrap
  12. def package(version):
  13. zip = f"Fira_Code_v{version}.zip"
  14. print('Package:', zip)
  15. with zipfile.ZipFile(zip, 'w', compression = zipfile.ZIP_DEFLATED, compresslevel = 9) as archive:
  16. for f in glob.glob("distr/**", recursive = True):
  17. arcname = f[len("distr/"):]
  18. if arcname and not os.path.basename(arcname).startswith("."):
  19. archive.write(f, arcname)
  20. def github_headers():
  21. if os.environ.get('GITHUB_BASIC'):
  22. auth = 'Basic ' + base64.b64encode(os.environ.get('GITHUB_BASIC').encode('utf-8')).decode('utf-8')
  23. else:
  24. auth = 'token ' + os.environ.get('API_TOKEN')
  25. return {
  26. 'Accept': 'application/vnd.github.v3+json',
  27. 'Authorization': auth
  28. }
  29. @log_errors("github_release")
  30. def github_release(version):
  31. zip = f"Fira_Code_v{version}.zip"
  32. data = '{"tag_name":"' + version + '","name":"' + version + '"}'
  33. headers = github_headers()
  34. resp = urllib.request.urlopen(urllib.request.Request('https://api.github.com/repos/tonsky/FiraCode/releases', data=data.encode('utf-8'), headers=headers)).read()
  35. upload_url = re.match('https://.*/assets', json.loads(resp.decode('utf-8'))['upload_url']).group(0)
  36. print('github_release: Uploading', zip, 'to', upload_url)
  37. headers['Content-Type'] = 'application/zip'
  38. headers['Content-Length'] = os.path.getsize(zip)
  39. with open(zip, 'rb') as data:
  40. urllib.request.urlopen(urllib.request.Request(upload_url + '?name=' + zip, data=data, headers=headers))
  41. @log_errors("npm_publish")
  42. def npm_publish(version):
  43. print("npm_publish: Skip")
  44. @log_errors("update_homebrew")
  45. def update_homebrew(version):
  46. print("update_homebrew: Skip") # Update https://github.com/Homebrew/homebrew-cask-fonts
  47. @log_errors("update_scoop")
  48. def update_scoop(version):
  49. print("update_scoop: Skip") # Update https://github.com/matthewjberger/scoop-nerd-fonts/blob/master/bucket/FiraCode.json
  50. @log_errors("update_google_fonts")
  51. def update_google_fonts(version):
  52. print("update_google_fonts: Skip")
  53. if __name__ == '__main__':
  54. os.chdir(common.root)
  55. version = common.version()
  56. package(version)
  57. github_release(version)
  58. npm_publish(version)
  59. update_homebrew(version)
  60. update_scoop(version)
  61. update_google_fonts(version)
  62. sys.exit(0)