make_release 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import argparse
  4. import subprocess
  5. import requests
  6. def execute(*args, **kwargs):
  7. try:
  8. return subprocess.check_output(*args, **kwargs)
  9. except subprocess.CalledProcessError as e:
  10. print(e.output.decode('utf-8'))
  11. exit(1)
  12. def check_setup_py_clean():
  13. git_status = execute(['git', 'status'])
  14. for line in git_status.decode('utf-8').split('\n'):
  15. if 'setup.py' in line:
  16. print('ERR: setup.py file seems to be already modified')
  17. exit(1)
  18. def extract_version_from_setup_py_content():
  19. with open('setup.py', 'r') as setup_file:
  20. setup_file_content = setup_file.read()
  21. for line in setup_file_content.split('\n'):
  22. if line.strip().startswith('version='):
  23. version_str = line.replace('\'', '')\
  24. .replace('version=', '')\
  25. .replace(',', '')
  26. major_version_str = version_str.split('.')[0].strip()
  27. minor_version_str = version_str.split('.')[1]
  28. if minor_version_str is None:
  29. print('ERR: No minor version found !')
  30. exit(1)
  31. try:
  32. int(minor_version_str)
  33. except ValueError:
  34. print(
  35. 'ERR: Error when read minor version, value found: "{}"'
  36. .format(minor_version_str)
  37. )
  38. return version_str.strip()
  39. print('ERR: Version not found in setup.py')
  40. exit(1)
  41. def get_last_pypi_version():
  42. hapic_pypi_version_response = requests.get(
  43. 'https://pypi.python.org/pypi/hapic/json',
  44. )
  45. hapic_pypi_versions_data = hapic_pypi_version_response.json()
  46. hapic_pypi_versions = hapic_pypi_versions_data['releases'].keys()
  47. last_version = sorted(hapic_pypi_versions)[-1]
  48. return last_version
  49. def get_new_hapic_version():
  50. version_str = extract_version_from_setup_py_content()
  51. major_version_str = version_str.split('.')[0].strip()
  52. minor_version_str = version_str.split('.')[1]
  53. next_version = '{}.{}'.format(major_version_str, int(minor_version_str)+1)
  54. last_pypi_version = get_last_pypi_version()
  55. last_version_major = last_pypi_version.split('.')[0]
  56. last_version_minor = last_pypi_version.split('.')[1]
  57. last_version_more_one = '{}.{}'.format(
  58. last_version_major,
  59. int(last_version_minor)+1,
  60. )
  61. if next_version != last_version_more_one:
  62. if input(
  63. 'WARN: This script was preparing to create version {} but '
  64. 'last version on pypi is {}. Consider to use version {} '
  65. '?(y/N)'.format(
  66. next_version,
  67. last_pypi_version,
  68. last_version_more_one,
  69. )
  70. ) in ['y', 'Y']:
  71. return last_version_more_one
  72. else:
  73. exit(1)
  74. return last_version_more_one
  75. def rewrite_setup_py_file(new_version: str):
  76. with open('setup.py', 'r') as setup_file:
  77. setup_file_content = setup_file.read()
  78. new_content_lines = []
  79. for line in setup_file_content.split('\n'):
  80. if line.strip().startswith('version='):
  81. new_content_lines.append(' version=\'{}\','.format(
  82. new_version
  83. ))
  84. else:
  85. new_content_lines.append(line)
  86. with open('setup.py', 'w') as setup_file:
  87. setup_file.write('\n'.join(new_content_lines))
  88. def commit_setup_py():
  89. execute(['git', 'reset'])
  90. execute(['git', 'add', 'setup.py'])
  91. version = extract_version_from_setup_py_content()
  92. execute(
  93. ['git', 'commit', '-m', 'v{}'.format(version)]
  94. )
  95. def check_branch(must_be_branch):
  96. current_branch = execute(
  97. ['git', 'rev-parse', '--abbrev-ref', 'HEAD']
  98. ).strip().decode('utf-8')
  99. if current_branch != must_be_branch:
  100. print('ERR: Your are actually on branch {} and must be {}'.format(
  101. current_branch,
  102. must_be_branch,
  103. ))
  104. exit(1)
  105. def push_on_github(remote_name):
  106. execute(
  107. ['git', 'push', remote_name, 'master']
  108. )
  109. def push_on_pypi():
  110. execute(
  111. ['python', 'setup.py', 'sdist', 'upload']
  112. )
  113. def main():
  114. parser = argparse.ArgumentParser()
  115. parser.add_argument(
  116. '--disable-version-increment',
  117. '-d',
  118. action='store_true',
  119. dest='disable_version_increment',
  120. )
  121. args = parser.parse_args()
  122. if not args.disable_version_increment:
  123. must_be_branch = 'master'
  124. remote_name = 'origin'
  125. check_setup_py_clean()
  126. check_branch(must_be_branch)
  127. new_version_will_be = get_new_hapic_version()
  128. actual_version = extract_version_from_setup_py_content()
  129. if new_version_will_be != actual_version:
  130. rewrite_setup_py_file(new_version_will_be)
  131. commit_setup_py()
  132. push_on_github(remote_name)
  133. push_on_pypi()
  134. actual_version = extract_version_from_setup_py_content()
  135. print('Deployed version: {}'.format(actual_version))
  136. if __name__ == '__main__':
  137. main()