Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/tools/install.py
4128 views
1
#!/usr/bin/env python3
2
# Copyright 2020 The Emscripten Authors. All rights reserved.
3
# Emscripten is available under two separate licenses, the MIT license and the
4
# University of Illinois/NCSA Open Source License. Both these licenses can be
5
# found in the LICENSE file.
6
7
"""Install the parts of emscripten needed for end users. This works like
8
a traditional `make dist` target but is written in python so it can be portable
9
and run on non-unix platforms (basically windows).
10
"""
11
12
import argparse
13
import fnmatch
14
import logging
15
import os
16
import shutil
17
import subprocess
18
import sys
19
20
EXCLUDES = [os.path.normpath(x) for x in '''
21
test/third_party
22
tools/maint
23
tools/install.py
24
site
25
node_modules
26
Makefile
27
.git
28
cache
29
cache.lock
30
out
31
bootstrap.py
32
'''.split()]
33
34
EXCLUDE_PATTERNS = '''
35
*.pyc
36
.*
37
__pycache__
38
'''.split()
39
40
logger = logging.getLogger('install')
41
42
43
def add_revision_file(target):
44
# text=True would be better than encoding here, but it's only supported in 3.7+
45
git_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD'], encoding='utf-8').strip()
46
with open(os.path.join(target, 'emscripten-revision.txt'), 'w') as f:
47
f.write(git_hash + '\n')
48
49
50
def copy_emscripten(target):
51
script_dir = os.path.dirname(os.path.abspath(__file__))
52
emscripten_root = os.path.dirname(script_dir)
53
os.chdir(emscripten_root)
54
for root, dirs, files in os.walk('.'):
55
# Handle the case where the target directory is underneath emscripten_root
56
if os.path.abspath(root) == os.path.abspath(target):
57
dirs.clear()
58
continue
59
60
remove_dirs = []
61
for d in dirs:
62
if d in EXCLUDE_PATTERNS:
63
remove_dirs.append(d)
64
continue
65
fulldir = os.path.normpath(os.path.join(root, d))
66
if fulldir in EXCLUDES:
67
remove_dirs.append(d)
68
continue
69
os.makedirs(os.path.join(target, fulldir))
70
71
for d in remove_dirs:
72
# Prevent recursion in excluded dirs
73
logger.debug('skipping dir: ' + os.path.join(root, d))
74
dirs.remove(d)
75
76
for f in files:
77
if any(fnmatch.fnmatch(f, pat) for pat in EXCLUDE_PATTERNS):
78
logger.debug('skipping file: ' + os.path.join(root, f))
79
continue
80
full = os.path.normpath(os.path.join(root, f))
81
if full in EXCLUDES:
82
logger.debug('skipping file: ' + os.path.join(root, f))
83
continue
84
logger.debug('installing file: ' + os.path.join(root, f))
85
shutil.copy2(full, os.path.join(target, root, f), follow_symlinks=False)
86
87
88
def npm_install(target):
89
subprocess.check_call([shutil.which('npm'), 'ci', '--omit=dev'], cwd=target)
90
91
92
def main():
93
parser = argparse.ArgumentParser(description=__doc__)
94
parser.add_argument('-v', '--verbose', action='store_true', help='verbose',
95
default=int(os.environ.get('EMCC_DEBUG', '0')))
96
parser.add_argument('target', help='target directory')
97
args = parser.parse_args()
98
target = os.path.abspath(args.target)
99
if os.path.exists(target):
100
print('target directory already exists: %s' % target)
101
return 1
102
logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
103
os.makedirs(target)
104
copy_emscripten(target)
105
npm_install(target)
106
if os.path.isdir('.git'):
107
# Add revision flag only if the source directory is a Git repository
108
# and not a source archive
109
add_revision_file(target)
110
return 0
111
112
113
if __name__ == '__main__':
114
sys.exit(main())
115
116