forked from dzhu/rstfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathset-tgz-mtimes.py
36 lines (27 loc) · 872 Bytes
/
set-tgz-mtimes.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
#!/usr/bin/env python3
# As a hack, necessary since setuptools doesn't support SOURCE_DATE_EPOCH, set all mtimes in the
# given tar.gz files to a consistent value.
import gzip
import io
import os
import shutil
import sys
import tarfile
def edit_file(fn, epoch):
buf = io.BytesIO()
with open(fn, "rb") as in_file:
shutil.copyfileobj(in_file, buf)
buf.seek(0)
with tarfile.open(mode="r", fileobj=buf) as in_tar, gzip.GzipFile(
fn, "wb", mtime=epoch
) as gz, tarfile.open(mode="w", fileobj=gz) as out_tar:
for m in in_tar.getmembers():
m.mtime = epoch
m.pax_headers["mtime"] = str(epoch)
out_tar.addfile(m, in_tar.extractfile(m))
def main():
epoch = int(os.environ["SOURCE_DATE_EPOCH"])
for fn in sys.argv[1:]:
edit_file(fn, epoch)
if __name__ == "__main__":
main()