Skip to content

Commit ec69755

Browse files
committed
unix: fix shebangs in all installed files
Before, we only looked at files under install/bin. Now, we look at all files under install/. This caused the script to inspect a lot of new files. Many of those new files had different shebangs. And some of the files weren't UTF-8 encoded. So we had to adjust the script accordingly. Logs say that only the `install/lib/pythonX.Y/config-X.Y-arch/python-config.py` file is impacted by this change. That file is the impetus for making this change. Rust validation of shebangs has been added to ensure we don't regress on shebang rewriting. I verified this tripped on builds produced before this commit. Closes #145.
1 parent a7fc6cf commit ec69755

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

cpython-unix/build-cpython.sh

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -694,26 +694,29 @@ import sys
694694
695695
ROOT = sys.argv[1]
696696
697-
for f in sorted(os.listdir(ROOT)):
698-
full = os.path.join(ROOT, f)
699-
697+
def fix_shebang(full):
700698
if os.path.islink(full) or not os.path.isfile(full):
701-
continue
699+
return
702700
703701
with open(full, "rb") as fh:
704-
initial = fh.read(64)
702+
initial = fh.read(256)
705703
706704
if not initial.startswith(b"#!"):
707-
continue
705+
return
708706
709-
# Make sure it is a Python script and not something else.
710-
with open(full, "r", encoding="utf-8") as fh:
711-
initial = fh.readline()
712-
initial = initial.strip()
707+
if b"\n" not in initial:
708+
raise Exception("could not find end of shebang line; consider increasing read count")
713709
714-
if "python" not in initial:
710+
initial = initial.splitlines()[0].decode("utf-8", "replace")
711+
712+
# Some shebangs are allowed.
713+
if "bin/env" in initial or "bin/sh" in initial or "bin/bash" in initial:
715714
print("ignoring %s due to non-python shebang (%s)" % (full, initial))
716-
continue
715+
return
716+
717+
# Make sure it is a Python script and not something else.
718+
if "/python" not in initial:
719+
raise Exception("unexpected shebang (%s) in %s" % (initial, full))
717720
718721
print("rewriting Python shebang (%s) in %s" % (initial, full))
719722
@@ -731,9 +734,16 @@ for f in sorted(os.listdir(ROOT)):
731734
732735
with open(full, "wb") as fh:
733736
fh.write(b"".join(lines))
737+
738+
739+
for root, dirs, files in os.walk(ROOT):
740+
dirs[:] = sorted(dirs)
741+
742+
for f in sorted(files):
743+
fix_shebang(os.path.join(root, f))
734744
EOF
735745

736-
${BUILD_PYTHON} ${ROOT}/fix_shebangs.py ${ROOT}/out/python/install/bin
746+
${BUILD_PYTHON} ${ROOT}/fix_shebangs.py ${ROOT}/out/python/install
737747

738748
# Also copy object files so they can be linked in a custom manner by
739749
# downstream consumers.

src/validation.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,13 @@ fn validate_distribution(
15901590
}
15911591
}
15921592

1593+
// Verify shebangs don't reference build environment.
1594+
if data.starts_with(b"#!/install") || data.starts_with(b"#!/build") {
1595+
context
1596+
.errors
1597+
.push(format!("{} has #!/install shebang", path.display()));
1598+
}
1599+
15931600
if path == PathBuf::from("python/PYTHON.json") {
15941601
context
15951602
.errors

0 commit comments

Comments
 (0)