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