PageRenderTime 72ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/release.py

https://gitlab.com/LocutusOfPenguin/python-chess
Python | 178 lines | 145 code | 31 blank | 2 comment | 17 complexity | 33cd8561ceb7268f273f93300cb7cf87 MD5 | raw file
  1. #!/usr/bin/python3
  2. # Helper script to create and publish a new python-chess release.
  3. import os
  4. import chess
  5. import sys
  6. import zipfile
  7. import textwrap
  8. import configparser
  9. import requests
  10. import bs4
  11. def system(command):
  12. print(command)
  13. exit_code = os.system(command)
  14. if exit_code != 0:
  15. sys.exit(exit_code)
  16. def check_git():
  17. print("--- CHECK GIT ----------------------------------------------------")
  18. system("git diff --exit-code")
  19. system("git diff --cached --exit-code")
  20. def test():
  21. print("--- TEST ---------------------------------------------------------")
  22. system("python2 test.py")
  23. system("python3 test.py")
  24. def doctest():
  25. print("--- DOCTEST ------------------------------------------------------")
  26. system("python2 -m doctest README.rst")
  27. system("python3 -m doctest README.rst")
  28. def check_changelog():
  29. print("--- CHECK CHANGELOG ----------------------------------------------")
  30. with open("CHANGELOG.rst", "r") as changelog_file:
  31. changelog = changelog_file.read()
  32. if "Upcoming in the next release" in changelog:
  33. print("Found: Upcoming in the next release")
  34. sys.exit(1)
  35. tagname = "v{0}".format(chess.__version__)
  36. if tagname not in changelog:
  37. print("Not found: {0}".format(tagname))
  38. sys.exit(1)
  39. def check_docs():
  40. print("--- CHECK DOCS ---------------------------------------------------")
  41. system("python3 setup.py --long-description | rst2html --strict --no-raw > /dev/null")
  42. def tag_and_push():
  43. print("--- TAG AND PUSH -------------------------------------------------")
  44. tagname = "v{0}".format(chess.__version__)
  45. release_filename = "release-{0}.txt".format(tagname)
  46. if not os.path.exists(release_filename):
  47. print(">>> Creating {0} ...".format(release_filename))
  48. first_section = False
  49. prev_line = None
  50. with open(release_filename, "w") as release_txt, open("CHANGELOG.rst", "r") as changelog_file:
  51. headline = "python-chess {0}".format(tagname)
  52. release_txt.write(headline + os.linesep)
  53. for line in changelog_file:
  54. if not first_section:
  55. if line.startswith("-------"):
  56. first_section = True
  57. else:
  58. if line.startswith("-------"):
  59. break
  60. else:
  61. if not prev_line.startswith("------"):
  62. release_txt.write(prev_line)
  63. prev_line = line
  64. with open(release_filename, "r") as release_txt:
  65. release = release_txt.read().strip() + os.linesep
  66. print(release)
  67. with open(release_filename, "w") as release_txt:
  68. release_txt.write(release)
  69. guessed_tagname = input(">>> Sure? Confirm tagname: ")
  70. if guessed_tagname != tagname:
  71. print("Actual tagname is: {0}".format(tagname))
  72. sys.exit(1)
  73. system("git tag {0} -s -F {1}".format(tagname, release_filename))
  74. system("git push origin master {0}".format(tagname))
  75. return tagname
  76. def update_rtd():
  77. print("--- UPDATE RTD ---------------------------------------------------")
  78. system("curl -X POST http://readthedocs.org/build/python-chess")
  79. def pypi():
  80. print("--- PYPI ---------------------------------------------------------")
  81. system("python3 setup.py sdist upload")
  82. def pythonhosted(tagname):
  83. print("--- PYTHONHOSTED -------------------------------------------------")
  84. print("Creating pythonhosted.zip ...")
  85. with zipfile.ZipFile("pythonhosted.zip", "w") as zip_file:
  86. zip_file.writestr("index.html", textwrap.dedent("""\
  87. <html>
  88. <head>
  89. <meta http-equiv="refresh" content="0;url=http://python-chess.readthedocs.io/en/{0}/">
  90. <script>
  91. window.location.href = 'http://python-chess.readthedocs.io/en/{0}/';
  92. </script>
  93. </head>
  94. </html>""".format(tagname)))
  95. print("Getting credentials ...")
  96. config = configparser.ConfigParser()
  97. config.read(os.path.expanduser("~/.pypirc"))
  98. username = config.get("pypi", "username")
  99. password = config.get("pypi", "password")
  100. auth = requests.auth.HTTPBasicAuth(username, password)
  101. print("Username: {0}".format(username))
  102. print("Getting CSRF token ...")
  103. session = requests.Session()
  104. res = session.get("https://pypi.python.org/pypi?:action=pkg_edit&name=python-chess", auth=auth)
  105. if res.status_code != 200:
  106. print(res.text)
  107. print(res)
  108. sys.exit(1)
  109. soup = bs4.BeautifulSoup(res.text, "html.parser")
  110. csrf = soup.find("input", {"name": "CSRFToken"})["value"]
  111. print("CSRF: {0}".format(csrf))
  112. print("Uploading ...")
  113. with open("pythonhosted.zip", "rb") as zip_file:
  114. res = session.post("https://pypi.python.org/pypi", auth=auth, data={
  115. "CSRFToken": csrf,
  116. ":action": "doc_upload",
  117. "name": "python-chess",
  118. }, files={
  119. "content": zip_file,
  120. })
  121. if res.status_code != 200 or tagname not in res.text:
  122. print(res.text)
  123. print(res)
  124. sys.exit(1)
  125. print("Done.")
  126. def github_release(tagname):
  127. print("--- GITHUB RELEASE -----------------------------------------------")
  128. print("https://github.com/niklasf/python-chess/releases/new?tag={0}".format(tagname))
  129. if __name__ == "__main__":
  130. test()
  131. doctest()
  132. check_docs()
  133. check_changelog()
  134. check_git()
  135. tagname = tag_and_push()
  136. update_rtd()
  137. pypi()
  138. pythonhosted(tagname)
  139. github_release(tagname)