- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathpatchcheck.py
executable file
·214 lines (178 loc) · 7.05 KB
/
patchcheck.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
#!/usr/bin/env python3
"""Check proposed changes for common issues."""
importsys
importos.path
importsubprocess
importsysconfig
defget_python_source_dir():
src_dir=sysconfig.get_config_var('abs_srcdir')
ifnotsrc_dir:
src_dir=sysconfig.get_config_var('srcdir')
returnos.path.abspath(src_dir)
SRCDIR=get_python_source_dir()
defn_files_str(count):
"""Return 'N file(s)' with the proper plurality on 'file'."""
s="s"ifcount!=1else""
returnf"{count} file{s}"
defstatus(message, modal=False, info=None):
"""Decorator to output status info to stdout."""
defdecorated_fxn(fxn):
defcall_fxn(*args, **kwargs):
sys.stdout.write(message+' ... ')
sys.stdout.flush()
result=fxn(*args, **kwargs)
ifnotmodalandnotinfo:
print("done")
elifinfo:
print(info(result))
else:
print("yes"ifresultelse"NO")
returnresult
returncall_fxn
returndecorated_fxn
defget_git_branch():
"""Get the symbolic name for the current git branch"""
cmd="git rev-parse --abbrev-ref HEAD".split()
try:
returnsubprocess.check_output(cmd,
stderr=subprocess.DEVNULL,
cwd=SRCDIR,
encoding='UTF-8')
exceptsubprocess.CalledProcessError:
returnNone
defget_git_upstream_remote():
"""Get the remote name to use for upstream branches
Uses "upstream" if it exists, "origin" otherwise
"""
cmd="git remote get-url upstream".split()
try:
subprocess.check_output(cmd,
stderr=subprocess.DEVNULL,
cwd=SRCDIR,
encoding='UTF-8')
exceptsubprocess.CalledProcessError:
return"origin"
return"upstream"
defget_git_remote_default_branch(remote_name):
"""Get the name of the default branch for the given remote
It is typically called 'main', but may differ
"""
cmd=f"git remote show {remote_name}".split()
env=os.environ.copy()
env['LANG'] ='C'
try:
remote_info=subprocess.check_output(cmd,
stderr=subprocess.DEVNULL,
cwd=SRCDIR,
encoding='UTF-8',
env=env)
exceptsubprocess.CalledProcessError:
returnNone
forlineinremote_info.splitlines():
if"HEAD branch:"inline:
base_branch=line.split(":")[1].strip()
returnbase_branch
returnNone
@status("Getting base branch for PR",
info=lambdax: xifxisnotNoneelse"not a PR branch")
defget_base_branch():
ifnotos.path.exists(os.path.join(SRCDIR, '.git')):
# Not a git checkout, so there's no base branch
returnNone
upstream_remote=get_git_upstream_remote()
version=sys.version_info
ifversion.releaselevel=='alpha':
base_branch=get_git_remote_default_branch(upstream_remote)
else:
base_branch="{0.major}.{0.minor}".format(version)
this_branch=get_git_branch()
ifthis_branchisNoneorthis_branch==base_branch:
# Not on a git PR branch, so there's no base branch
returnNone
returnupstream_remote+"/"+base_branch
@status("Getting the list of files that have been added/changed",
info=lambdax: n_files_str(len(x)))
defchanged_files(base_branch=None):
"""Get the list of changed or added files from git."""
ifos.path.exists(os.path.join(SRCDIR, '.git')):
# We just use an existence check here as:
# directory = normal git checkout/clone
# file = git worktree directory
ifbase_branch:
cmd='git diff --name-status '+base_branch
else:
cmd='git status --porcelain'
filenames= []
withsubprocess.Popen(cmd.split(),
stdout=subprocess.PIPE,
cwd=SRCDIR) asst:
git_file_status, _=st.communicate()
ifst.returncode!=0:
sys.exit(f'error running {cmd}')
forlineingit_file_status.splitlines():
line=line.decode().rstrip()
status_text, filename=line.split(maxsplit=1)
status=set(status_text)
# modified, added or unmerged files
ifnotstatus.intersection('MAU'):
continue
if' -> 'infilename:
# file is renamed
filename=filename.split(' -> ', 2)[1].strip()
filenames.append(filename)
else:
sys.exit('need a git checkout to get modified files')
returnlist(map(os.path.normpath, filenames))
@status("Docs modified", modal=True)
defdocs_modified(file_paths):
"""Report if any file in the Doc directory has been changed."""
returnbool(file_paths)
@status("Misc/ACKS updated", modal=True)
defcredit_given(file_paths):
"""Check if Misc/ACKS has been changed."""
returnos.path.join('Misc', 'ACKS') infile_paths
@status("Misc/NEWS.d updated with `blurb`", modal=True)
defreported_news(file_paths):
"""Check if Misc/NEWS.d has been changed."""
returnany(p.startswith(os.path.join('Misc', 'NEWS.d', 'next'))
forpinfile_paths)
@status("configure regenerated", modal=True, info=str)
defregenerated_configure(file_paths):
"""Check if configure has been regenerated."""
if'configure.ac'infile_paths:
return"yes"if'configure'infile_pathselse"no"
else:
return"not needed"
@status("pyconfig.h.in regenerated", modal=True, info=str)
defregenerated_pyconfig_h_in(file_paths):
"""Check if pyconfig.h.in has been regenerated."""
if'configure.ac'infile_paths:
return"yes"if'pyconfig.h.in'infile_pathselse"no"
else:
return"not needed"
defmain():
base_branch=get_base_branch()
file_paths=changed_files(base_branch)
has_doc_files=any(fnforfninfile_pathsiffn.startswith('Doc') and
fn.endswith(('.rst', '.inc')))
misc_files= {pforpinfile_pathsifp.startswith('Misc')}
# Docs updated.
docs_modified(has_doc_files)
# Misc/ACKS changed.
credit_given(misc_files)
# Misc/NEWS changed.
reported_news(misc_files)
# Regenerated configure, if necessary.
regenerated_configure(file_paths)
# Regenerated pyconfig.h.in, if necessary.
regenerated_pyconfig_h_in(file_paths)
# Test suite run and passed.
has_c_files=any(fnforfninfile_pathsiffn.endswith(('.c', '.h')))
has_python_files=any(fnforfninfile_pathsiffn.endswith('.py'))
print()
ifhas_c_files:
print("Did you run the test suite and check for refleaks?")
elifhas_python_files:
print("Did you run the test suite?")
if__name__=='__main__':
main()