deploy-release.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env python3
  2. import argparse
  3. import json
  4. import os
  5. import re
  6. import shlex
  7. import stat
  8. import subprocess
  9. import sys
  10. from pathlib import Path
  11. from packaging import version as pv
  12. sys.path.append(str(Path(__file__).parent.parent))
  13. from tools.list_distros import generate_help
  14. from tools.reformat_readme import reformat_readme
  15. def pre_check():
  16. """
  17. Check source code status before releasing.
  18. """
  19. assert os.path.isfile('./neofetch'), './neofetch doesn\'t exist, you are running this script in the wrong directory'
  20. assert os.stat('./neofetch').st_mode & stat.S_IEXEC, 'neofetch is not executable'
  21. assert os.path.islink('./hyfetch/scripts/neowofetch'), 'neowofetch is not a symbolic link'
  22. # subprocess.check_call(shlex.split('git diff-index --quiet HEAD --')) # 'Please commit all changes before release'
  23. subprocess.check_call(shlex.split('shellcheck neofetch'))
  24. def edit_versions(version: str):
  25. """
  26. Edit version numbers in hyfetch/constants.py, package.json, and README.md
  27. Also edits version number of neofetch, but the neofetch version number is separate.
  28. :param version: Version to release
  29. """
  30. # 1. package.json
  31. path = Path('package.json')
  32. content = json.loads(path.read_text())
  33. cur = pv.parse(content['version'])
  34. assert cur < pv.parse(version), 'Version did not increase'
  35. content['version'] = version
  36. path.write_text(json.dumps(content, ensure_ascii=False, indent=2))
  37. # 2. hyfetch/constants.py
  38. path = Path('hyfetch/constants.py')
  39. content = [f"VERSION = '{version}'" if l.startswith('VERSION = ') else l for l in path.read_text().split('\n')]
  40. path.write_text('\n'.join(content))
  41. # 3. README.md
  42. path = Path('README.md')
  43. content = path.read_text()
  44. changelog_i = content.index('<!-- CHANGELOG STARTS HERE --->')
  45. version_i = content.index('###', changelog_i)
  46. version_end = content.index('\n', version_i)
  47. content = content[:version_i] + f'### {version}' + content[version_end:]
  48. path.write_text(content)
  49. # 4. neofetch script
  50. path = Path('neofetch')
  51. lines = path.read_text().split('\n')
  52. version_i = next(i for i, l in enumerate(lines) if l.startswith('version='))
  53. nf = pv.parse(lines[version_i].replace('version=', ''))
  54. new = pv.parse(version)
  55. nf = f'{nf.major + new.major - cur.major}.{nf.minor + new.minor - cur.minor}.{nf.micro + new.micro - cur.micro}'
  56. lines[version_i] = f"version={nf}"
  57. path.write_text('\n'.join(lines))
  58. def finalize_neofetch():
  59. """
  60. Finalize current version
  61. """
  62. # 1. Update distro list
  63. path = Path('neofetch')
  64. content = path.read_text()
  65. content = re.compile(r'(?<=# Flag: --ascii_distro\n#\n).*?(?=ascii_distro=)', re.DOTALL)\
  66. .sub(generate_help(100, '# ') + '\n', content)
  67. content = re.compile(r"""(?<=Which Distro's ascii art to print\n\n).*?{distro}_small to use them\.""", re.DOTALL)\
  68. .sub(generate_help(100, ' ' * 32), content)
  69. path.write_text(content)
  70. # 2. Regenerate man page
  71. Path('neofetch.1').write_text(subprocess.check_output(['help2man', './neofetch']).decode())
  72. # 3. Reformat readme links
  73. reformat_readme()
  74. if __name__ == '__main__':
  75. parser = argparse.ArgumentParser(description='HyFetch Release Utility')
  76. parser.add_argument('version', help='Version to release')
  77. args = parser.parse_args()
  78. pre_check()
  79. edit_versions(args.version)
  80. finalize_neofetch()