-
Notifications
You must be signed in to change notification settings - Fork 575
/
__init__.py
273 lines (202 loc) · 6.8 KB
/
__init__.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# coding=utf-8
#
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Most of this work is copyright (C) 2013-2019 David R. MacIver
# (david@drmaciver.com), but it contains contributions by others. See
# CONTRIBUTING.rst for a full list of people who may hold copyright, and
# consult the git log if you need to determine who owns an individual
# contribution.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
#
# END HEADER
from __future__ import absolute_import, division, print_function
import os
import shlex
import subprocess
def current_branch():
return (
subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
.decode("ascii")
.strip()
)
def tags():
result = [
t.decode("ascii") for t in subprocess.check_output(["git", "tag"]).split(b"\n")
]
assert len(set(result)) == len(result)
return set(result)
ROOT = (
subprocess.check_output(
["git", "-C", os.path.dirname(__file__), "rev-parse", "--show-toplevel"]
)
.decode("ascii")
.strip()
)
REPO_TESTS = os.path.join(ROOT, "whole-repo-tests")
PYUP_FILE = os.path.join(ROOT, ".pyup.yml")
def hash_for_name(name):
return subprocess.check_output(["git", "rev-parse", name]).decode("ascii").strip()
def is_ancestor(a, b):
check = subprocess.call(["git", "merge-base", "--is-ancestor", a, b])
assert 0 <= check <= 1
return check == 0
def merge_base(a, b):
return subprocess.check_output(["git", "merge-base", a, b]).strip()
def point_of_divergence():
return merge_base("HEAD", "origin/master")
def has_changes(files):
return (
subprocess.call(
[
"git",
"diff",
"--no-patch",
"--exit-code",
point_of_divergence(),
"HEAD",
"--",
*files,
]
)
!= 0
)
def has_uncommitted_changes(filename):
return subprocess.call(["git", "diff", "--exit-code", filename]) != 0
def last_committer():
out, _ = subprocess.Popen(
["git", "log", "-1", "--pretty=format:%an"],
stdout=subprocess.PIPE,
universal_newlines=True,
).communicate()
return out
def git(*args):
subprocess.check_call(("git",) + args)
def configure_git():
git("config", "user.name", "Travis CI on behalf of David R. MacIver")
git("config", "user.email", "david@drmaciver.com")
git("config", "core.sshCommand", "ssh -i deploy_key")
git("remote", "add", "ssh-origin", "git@github.com:HypothesisWorks/hypothesis.git")
def create_tag(tagname):
assert tagname not in tags()
git("tag", tagname)
def push_tag(tagname):
assert_can_release()
subprocess.check_call(
[
"ssh-agent",
"sh",
"-c",
"ssh-add %s && " % (shlex.quote(DEPLOY_KEY),)
+ "git push ssh-origin HEAD:master &&"
+ "git push ssh-origin %s" % (shlex.quote(tagname),),
]
)
def assert_can_release():
assert not IS_PULL_REQUEST, "Cannot release from pull requests"
assert has_travis_secrets(), "Cannot release without travis secure vars"
def has_travis_secrets():
return os.environ.get("TRAVIS_SECURE_ENV_VARS", None) == "true"
def build_jobs():
"""Query the Travis API to find out what the state of the other build jobs
is.
Note: This usage of Travis has been somewhat reverse engineered due
to a certain dearth of documentation as to what values what takes
when.
"""
import requests
build_id = os.environ["TRAVIS_BUILD_ID"]
url = "https://api.travis-ci.org/builds/%s" % (build_id,)
data = requests.get(
url, headers={"Accept": "application/vnd.travis-ci.2+json"}
).json()
matrix = data["jobs"]
jobs = {}
for m in matrix:
name = m["config"]["env"].replace("TASK=", "")
status = m["state"]
jobs.setdefault(status, []).append(name)
return jobs
def modified_files():
files = set()
for command in [
[
"git",
"diff",
"--name-only",
"--diff-filter=d",
point_of_divergence(),
"HEAD",
],
["git", "diff", "--name-only"],
]:
diff_output = subprocess.check_output(command).decode("ascii")
for l in diff_output.split("\n"):
filepath = l.strip()
if filepath:
assert os.path.exists(filepath), filepath
files.add(filepath)
return files
def all_files():
return [
f
for f in subprocess.check_output(["git", "ls-files"])
.decode("ascii")
.splitlines()
if os.path.exists(f)
]
def changed_files_from_master():
"""Returns a list of files which have changed between a branch and
master."""
files = set()
command = ["git", "diff", "--name-only", "HEAD", "master"]
diff_output = subprocess.check_output(command).decode("ascii")
for line in diff_output.splitlines():
filepath = line.strip()
if filepath:
files.add(filepath)
return files
SECRETS_BASE = os.path.join(ROOT, "secrets")
SECRETS_TAR = SECRETS_BASE + ".tar"
ENCRYPTED_SECRETS = SECRETS_TAR + ".enc"
SECRETS = os.path.join(ROOT, "secrets")
DEPLOY_KEY = os.path.join(SECRETS, "deploy_key")
PYPIRC = os.path.join(SECRETS, ".pypirc")
RUBYGEMS_API_KEY = os.path.join(SECRETS, "api_key.yaml")
CARGO_API_KEY = os.path.join(SECRETS, "cargo-credentials")
SECRET_FILES = [DEPLOY_KEY, PYPIRC, RUBYGEMS_API_KEY, CARGO_API_KEY]
def decrypt_secrets():
subprocess.check_call(
[
"openssl",
"aes-256-cbc",
"-K",
os.environ["encrypted_b8618e5d043b_key"],
"-iv",
os.environ["encrypted_b8618e5d043b_iv"],
"-in",
ENCRYPTED_SECRETS,
"-out",
SECRETS_TAR,
"-d",
]
)
subprocess.check_call(["tar", "-xvf", SECRETS_TAR], cwd=ROOT)
missing_files = [os.path.basename(f) for f in SECRET_FILES if not os.path.exists(f)]
assert not missing_files, missing_files
os.chmod(DEPLOY_KEY, int("0600", 8))
IS_TRAVIS_PULL_REQUEST = os.environ.get("TRAVIS_EVENT_TYPE") == "pull_request"
IS_CIRCLE_PULL_REQUEST = (
os.environ.get("CIRCLE_BRANCH") == "master"
and os.environ.get("CI_PULL_REQUESTS", "") != ""
)
IS_PULL_REQUEST = IS_TRAVIS_PULL_REQUEST or IS_CIRCLE_PULL_REQUEST
def all_projects():
import hypothesistooling.projects.conjecturerust as cr
import hypothesistooling.projects.hypothesispython as hp
import hypothesistooling.projects.hypothesisruby as hr
return [cr, hp, hr]