-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.py
75 lines (71 loc) · 3.13 KB
/
setup.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
import shutil
import os.path
import subprocess
from setuptools import setup
from setuptools.command.build import build as default_build
class Build(default_build):
''' Overrides the default install command to ensure NodeJS dependencies are installed '''
description = 'Install Webshooter and dependencies'
def run(self):
install_node()
default_build.run(self)
def install_node():
def get_download_url(NODE_VERSION='v22.11.0'):
node_links = {
'linux-x86_64': f'https://nodejs.org/dist/{NODE_VERSION}/node-{NODE_VERSION}-linux-x64.tar.xz',
'windows-amd64': f'https://nodejs.org/dist/{NODE_VERSION}/node-{NODE_VERSION}-win-x64.zip',
'darwin-x86_64': f'https://nodejs.org/dist/{NODE_VERSION}/node-{NODE_VERSION}-darwin-x64.tar.gz',
'darwin-arm64': f'https://nodejs.org/dist/{NODE_VERSION}/node-{NODE_VERSION}-darwin-arm64.tar.gz'
}
target = (platform.system() + '-' + platform.machine()).lower()
if target not in node_links:
raise RuntimeError(f'Unrecognized platform {target}. Install node and npm, then re-run this installer.')
return node_links[target]
npm_env = dict(os.environ)
if not shutil.which('npm', path=npm_env['PATH']):
from urllib.request import urlopen
from zipfile import ZipFile
import platform, tarfile
print('npm not found. Installing.')
url = get_download_url()
filename = os.path.basename(url)
bin_dir = None
extract_dir = None
node_dir = os.path.join('src', 'webshooter', 'screen', 'nodejs')
if os.path.exists(node_dir):
raise FileExistsError(f'Did not expect to find directory: {node_dir}')
print('Downloading from', url)
with urlopen(url) as response:
with open(filename, 'wb') as dest:
shutil.copyfileobj(response, dest)
print(f'Downloaded to {filename}. Extracting.')
if filename.endswith('.tar.xz') or filename.endswith('.tar.gz'):
with tarfile.open(filename) as tar:
tar.extractall()
extract_dir = tar.getnames()[0]
bin_dir = os.path.join(node_dir, 'bin')
elif filename.endswith('.zip'):
with ZipFile(filename, 'r') as zipf:
zipf.extractall()
extract_dir = zipf.namelist()[0]
bin_dir = node_dir
shutil.move(extract_dir, node_dir)
sep = ';' if platform.system().lower() == 'windows' else ':'
npm_env['PATH'] = os.path.abspath(bin_dir) + sep + os.environ['PATH']
cwd = os.getcwd()
os.chdir(os.path.join('src', 'webshooter', 'screen'))
print('Running `npm install`')
# see https://docs.python.org/3/library/subprocess.html#subprocess.Popen
npm_path = shutil.which('npm', path=npm_env['PATH'])
try:
result = subprocess.run([npm_path, 'install'], capture_output=True, check=True, env=npm_env)
print(result.stdout.decode())
except subprocess.CalledProcessError as e:
print('Error `npm install`:', e.stderr)
raise e
os.chdir(cwd)
setup(
cmdclass={
'build': Build
}
)