Skip to content

Commit 3308fc4

Browse files
[gn build] Use shutil.which to find git in write_vcsrevision.py (#142570)
Relates to #54337 This is just a comment referencing distutils but even so, we can ditch the custom which and use the one Python 3.3 added. Which has the .bat bug fixed: https://docs.python.org/3.3/library/shutil.html#shutil.which I tested this on Windows: ``` C:\Users\tcwg>touch foo.bat C:\Users\tcwg>python Python 3.11.9 (tags/v3.11.9:de54cf5, Apr 2 2024, 12:24:25) [MSC v.1938 64 bit (ARM64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import shutil >>> shutil.which("foo") '.\\foo.BAT' ``` I just ran the script manually and got reasonable results, I haven't done a GN build.
1 parent c4a0125 commit 3308fc4

File tree

1 file changed

+4
-13
lines changed

1 file changed

+4
-13
lines changed

llvm/utils/gn/build/write_vcsrevision.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,13 @@
66
import os
77
import subprocess
88
import sys
9+
import shutil
910

1011

1112
THIS_DIR = os.path.abspath(os.path.dirname(__file__))
1213
LLVM_DIR = os.path.dirname(os.path.dirname(os.path.dirname(THIS_DIR)))
1314

1415

15-
def which(program):
16-
# distutils.spawn.which() doesn't find .bat files,
17-
# https://bugs.python.org/issue2200
18-
for path in os.environ["PATH"].split(os.pathsep):
19-
candidate = os.path.join(path, program)
20-
if os.path.isfile(candidate) and os.access(candidate, os.X_OK):
21-
return candidate
22-
return None
23-
24-
2516
def main():
2617
parser = argparse.ArgumentParser(description=__doc__)
2718
parser.add_argument(
@@ -46,11 +37,11 @@ def main():
4637

4738
vcsrevision_contents = ""
4839
if args.write_git_rev:
49-
git, use_shell = which("git"), False
40+
git, use_shell = shutil.which("git"), False
5041
if not git:
51-
git = which("git.exe")
42+
git = shutil.which("git.exe")
5243
if not git:
53-
git, use_shell = which("git.bat"), True
44+
git, use_shell = shutil.which("git.bat"), True
5445
git_dir = (
5546
subprocess.check_output(
5647
[git, "rev-parse", "--git-dir"], cwd=LLVM_DIR, shell=use_shell

0 commit comments

Comments
 (0)