Skip to content

Commit a3e072b

Browse files
committed
Let “make patchcheck” work for out-of-dir builds (python#9860)
1 parent b603b27 commit a3e072b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

Tools/scripts/patchcheck.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import shutil
55
import os.path
66
import subprocess
7+
import sysconfig
78

89
import reindent
910
import untabify
1011

1112

13+
SRCDIR = sysconfig.get_config_var('srcdir')
14+
15+
1216
def n_files_str(count):
1317
"""Return 'N file(s)' with the proper plurality on 'file'."""
1418
return "{} file{}".format(count, "s" if count != 1 else "")
@@ -36,7 +40,7 @@ def call_fxn(*args, **kwargs):
3640
info=lambda x: n_files_str(len(x)))
3741
def changed_files():
3842
"""Get the list of changed or added files from the VCS."""
39-
if os.path.isdir('.hg'):
43+
if os.path.isdir(os.path.join(SRCDIR, '.hg')):
4044
vcs = 'hg'
4145
cmd = 'hg status --added --modified --no-status'
4246
elif os.path.isdir('.svn'):
@@ -75,7 +79,7 @@ def normalize_whitespace(file_paths):
7579
reindent.makebackup = False # No need to create backups.
7680
fixed = []
7781
for path in (x for x in file_paths if x.endswith('.py')):
78-
if reindent.check(path):
82+
if reindent.check(os.path.join(SRCDIR, path)):
7983
fixed.append(path)
8084
return fixed
8185

@@ -85,10 +89,11 @@ def normalize_c_whitespace(file_paths):
8589
"""Report if any C files """
8690
fixed = []
8791
for path in file_paths:
88-
with open(path, 'r') as f:
92+
abspath = os.path.join(SRCDIR, path)
93+
with open(abspath, 'r') as f:
8994
if '\t' not in f.read():
9095
continue
91-
untabify.process(path, 8, verbose=False)
96+
untabify.process(abspath, 8, verbose=False)
9297
fixed.append(path)
9398
return fixed
9499

@@ -99,13 +104,14 @@ def normalize_c_whitespace(file_paths):
99104
def normalize_docs_whitespace(file_paths):
100105
fixed = []
101106
for path in file_paths:
107+
abspath = os.path.join(SRCDIR, path)
102108
try:
103-
with open(path, 'rb') as f:
109+
with open(abspath, 'rb') as f:
104110
lines = f.readlines()
105111
new_lines = [ws_re.sub(br'\1', line) for line in lines]
106112
if new_lines != lines:
107-
shutil.copyfile(path, path + '.bak')
108-
with open(path, 'wb') as f:
113+
shutil.copyfile(abspath, abspath + '.bak')
114+
with open(abspath, 'wb') as f:
109115
f.writelines(new_lines)
110116
fixed.append(path)
111117
except Exception as err:

0 commit comments

Comments
 (0)