forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
135 lines (98 loc) · 3.68 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# Copyright 2020 The Emscripten Authors. All rights reserved.
# Emscripten is available under two separate licenses, the MIT license and the
# University of Illinois/NCSA Open Source License. Both these licenses can be
# found in the LICENSE file.
import os
import shutil
import sys
from functools import wraps
from pathlib import Path
from . import diagnostics
__rootpath__ = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
WINDOWS = sys.platform.startswith('win')
MACOS = sys.platform == 'darwin'
LINUX = sys.platform.startswith('linux')
def exit_with_error(msg, *args):
diagnostics.error(msg, *args)
def path_from_root(*pathelems):
return str(Path(__rootpath__, *pathelems))
def normalize_path(path):
"""Normalize path separators to UNIX-style forward slashes.
This can be useful when converting paths to URLs or JS strings,
or when trying to generate consistent output file contents
across all platforms. In most cases UNIX-style separators work
fine on windows.
"""
return path.replace('\\', '/').replace('//', '/')
def safe_ensure_dirs(dirname):
os.makedirs(dirname, exist_ok=True)
# TODO(sbc): Replace with str.removeprefix once we update to python3.9
def removeprefix(string, prefix):
if string.startswith(prefix):
return string[len(prefix):]
return string
def convert_line_endings_in_file(filename, to_eol):
if to_eol == os.linesep:
return # No conversion needed
text = read_file(filename)
write_file(filename, text, line_endings=to_eol)
def read_file(file_path):
"""Read from a file opened in text mode"""
with open(file_path, encoding='utf-8') as fh:
return fh.read()
def read_binary(file_path):
"""Read from a file opened in binary mode"""
with open(file_path, 'rb') as fh:
return fh.read()
def write_file(file_path, text, line_endings=None):
"""Write to a file opened in text mode"""
if line_endings and line_endings != os.linesep:
text = text.replace('\n', line_endings)
write_binary(file_path, text.encode('utf-8'))
else:
with open(file_path, 'w', encoding='utf-8') as fh:
fh.write(text)
def write_binary(file_path, contents):
"""Write to a file opened in binary mode"""
with open(file_path, 'wb') as fh:
fh.write(contents)
def delete_file(filename):
"""Delete a file (if it exists)."""
if os.path.lexists(filename):
os.remove(filename)
def delete_dir(dirname):
"""Delete a directory (if it exists)."""
if not os.path.exists(dirname):
return
shutil.rmtree(dirname)
def delete_contents(dirname, exclude=None):
"""Delete the contents of a directory without removing
the directory itself."""
if not os.path.exists(dirname):
return
for entry in os.listdir(dirname):
if exclude and entry in exclude:
continue
entry = os.path.join(dirname, entry)
if os.path.isdir(entry):
delete_dir(entry)
else:
delete_file(entry)
# TODO(sbc): Replace with functools.cache, once we update to python 3.7
def memoize(func):
results = {}
@wraps(func)
def helper(*args, **kwargs):
assert not kwargs
key = (func.__name__, args)
if key not in results:
results[key] = func(*args)
return results[key]
return helper
# TODO: Move this back to shared.py once importing that file becoming side effect free (i.e. it no longer requires a config).
def set_version_globals():
global EMSCRIPTEN_VERSION, EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY
filename = path_from_root('emscripten-version.txt')
EMSCRIPTEN_VERSION = read_file(filename).strip().strip('"')
parts = [int(x) for x in EMSCRIPTEN_VERSION.split('-')[0].split('.')]
EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY = parts