Debian Codegraph @

tooling/src/hypothesistooling/__init__.py

raw
1 # This file is part of Hypothesis, which may be found at
2 # https://github.com/HypothesisWorks/hypothesis/
3 #
4 # Copyright the Hypothesis Authors.
5 # Individual contributors are listed in AUTHORS.rst and the git log.
6 #
7 # This Source Code Form is subject to the terms of the Mozilla Public License,
8 # v. 2.0. If a copy of the MPL was not distributed with this file, You can
9 # obtain one at https://mozilla.org/MPL/2.0/.
10
11 import os
12 import shlex
13 import subprocess
14 from pathlib import Path
15
16
17 def current_branch():
18 return (
19 subprocess.check_output(["git", "rev-parse", "--abbrev-ref", "HEAD"])
20 .decode("ascii")
21 .strip()
22 )
23
24
25 def tags():
26 result = [
27 t.decode("ascii") for t in subprocess.check_output(["git", "tag"]).split(b"\n")
28 ]
29 assert len(set(result)) == len(result)
30 return set(result)
31
32
33 ROOT = Path(
34 subprocess.check_output(
35 ["git", "-C", os.path.dirname(__file__), "rev-parse", "--show-toplevel"]
36 )
37 .decode("ascii")
38 .strip()
39 )
40 REPO_TESTS = ROOT / "whole_repo_tests"
41
42
43 def hash_for_name(name):
44 return subprocess.check_output(["git", "rev-parse", name]).decode("ascii").strip()
45
46
47 def is_ancestor(a, b):
48 check = subprocess.call(["git", "merge-base", "--is-ancestor", a, b])
49 assert 0 <= check <= 1
50 return check == 0
51
52
53 def merge_base(a, b):
54 return subprocess.check_output(["git", "merge-base", a, b]).strip()
55
56
57 def point_of_divergence():
58 return merge_base("HEAD", "origin/master")
59
60
61 def has_changes(files):
62 command = [
63 "git",
64 "diff",
65 "--no-patch",
66 "--exit-code",
67 point_of_divergence(),
68 "HEAD",
69 "--",
70 *files,
71 ]
72 return subprocess.call(command) != 0
73
74
75 def has_uncommitted_changes(filename):
76 return subprocess.call(["git", "diff", "--exit-code", filename]) != 0
77
78
79 def last_committer():
80 out, _ = subprocess.Popen(
81 ["git", "log", "-1", "--pretty=format:%an"],
82 stdout=subprocess.PIPE,
83 universal_newlines=True,
84 ).communicate()
85 return out
86
87
88 def git(*args):
89 subprocess.check_call(("git", *args))
90
91
92 TOOLING_COMMITTER_NAME = "CI on behalf of the Hypothesis team"
93
94
95 def configure_git():
96 git("config", "user.name", TOOLING_COMMITTER_NAME)
97 git("config", "user.email", "[email protected]")
98
99
100 def create_tag(tagname):
101 assert tagname not in tags()
102 git("tag", tagname)
103
104
105 def push_tag(tagname):
106 assert_can_release()
107 subprocess.check_call(["git", "push", "origin", shlex.quote(tagname)])
108 subprocess.check_call(["git", "push", "origin", "HEAD:master"])
109
110
111 def assert_can_release():
112 assert not IS_PULL_REQUEST, "Cannot release from pull requests"
113
114
115 def modified_files():
116 files = set()
117 for command in [
118 [
119 "git",
120 "diff",
121 "--name-only",
122 "--diff-filter=d",
123 point_of_divergence(),
124 "HEAD",
125 ],
126 ["git", "diff", "--name-only"],
127 ]:
128 diff_output = subprocess.check_output(command).decode("ascii")
129 for l in diff_output.split("\n"):
130 filepath = l.strip()
131 if filepath and os.path.exists(filepath):
132 files.add(Path(filepath))
133 return files
134
135
136 def all_files() -> list[Path]:
137 return [
138 Path(f)
139 for f in subprocess.check_output(["git", "ls-files"])
140 .decode("ascii")
141 .splitlines()
142 if os.path.exists(f)
143 ]
144
145
146 def changed_files_from_master():
147 """Returns a list of files which have changed between a branch and master."""
148 files = set()
149 command = ["git", "diff", "--name-only", "HEAD", "master"]
150 diff_output = subprocess.check_output(command).decode("ascii")
151 for line in diff_output.splitlines():
152 filepath = line.strip()
153 if filepath:
154 files.add(filepath)
155 return files
156
157
158 IS_PULL_REQUEST = os.environ.get("GITHUB_REF", "").startswith("refs/pull/")
159
160
161 def all_projects():
162 import hypothesistooling.projects.hypothesispython as hp
163
164 return [hp]