-
-
Notifications
You must be signed in to change notification settings - Fork 316
/
Copy pathutils.py
78 lines (54 loc) · 2.16 KB
/
utils.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
import os
import shutil
import sys
from base64 import urlsafe_b64encode
if sys.version_info[0] >= 3:
def replace_file(src, dst):
try:
os.replace(src, dst)
# Happens when on different filesystems like /tmp or caused by layering in containers
except OSError:
shutil.copy2(src, dst)
os.remove(src)
else: # no cov
def replace_file(src, dst):
shutil.copy2(src, dst)
os.remove(src)
def get_known_python_major_versions():
return map(str, sorted((2, 3)))
def normalize_relative_path(path):
return os.path.normpath(path).strip(os.path.sep)
def normalize_relative_directory(path):
return normalize_relative_path(path) + os.path.sep
def normalize_archive_path(path):
if os.sep != '/':
return path.replace(os.sep, '/')
return path
def format_file_hash(digest):
# https://www.python.org/dev/peps/pep-0427/#signed-wheel-files
return urlsafe_b64encode(digest).decode('ascii').rstrip('=')
def get_reproducible_timestamp():
"""
Returns an `int` derived from the `SOURCE_DATE_EPOCH` environment variable; see
https://reproducible-builds.org/specs/source-date-epoch/.
The default value will always be: `1580601600`
"""
return int(os.environ.get('SOURCE_DATE_EPOCH', '1580601600'))
def normalize_file_permissions(st_mode):
"""
https://github.com/takluyver/flit/blob/6a2a8c6462e49f584941c667b70a6f48a7b3f9ab/flit_core/flit_core/common.py#L257
Normalize the permission bits in the st_mode field from stat to 644/755.
Popular VCSs only track whether a file is executable or not. The exact
permissions can vary on systems with different umasks. Normalizing
to 644 (non executable) or 755 (executable) makes builds more reproducible.
"""
# Set 644 permissions, leaving higher bits of st_mode unchanged
new_mode = (st_mode | 0o644) & ~0o133
if st_mode & 0o100: # no cov
new_mode |= 0o111 # Executable: 644 -> 755
return new_mode
def set_zip_info_mode(zip_info, mode=0o644):
"""
https://github.com/takluyver/flit/commit/3889583719888aef9f28baaa010e698cb7884904
"""
zip_info.external_attr = mode << 16