-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.py
More file actions
85 lines (76 loc) · 3.17 KB
/
setup.py
File metadata and controls
85 lines (76 loc) · 3.17 KB
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
import subprocess
import os
import json
import sys
from pathlib import Path
venvName = sys.argv[1]
absDir = os.path.dirname(os.path.abspath(__file__))
if Path(venvName).is_absolute():
venvPath = venvName
else:
venvPath = f'{absDir}/{venvName}'
if os.path.isdir(venvPath):
print(f'{venvName} already exists.')
sys.exit()
# Check if venv module is available
try:
import venv
except ImportError:
if os.name == 'nt':
print('WARNING: Python venv module is not available.', file=sys.stderr)
print('Please install Python from https://www.python.org/ (not the Microsoft Store version).', file=sys.stderr)
else:
print('WARNING: python3-venv is not installed.', file=sys.stderr)
print('On Debian/Ubuntu, install it with: sudo apt install python3-venv', file=sys.stderr)
sys.exit(1)
# Check if ensurepip (used by venv to bootstrap pip) is available
pip_available = True
try:
import ensurepip
except ImportError:
pip_available = False
print('WARNING: ensurepip is not available. pip may not be installed in the virtual environment.', file=sys.stderr)
if os.name == 'nt':
print('Please install Python from https://www.python.org/ and ensure pip is included.', file=sys.stderr)
else:
print('On Debian/Ubuntu, install it with: sudo apt install python3-pip python3-venv', file=sys.stderr)
# Determine python command for subprocess calls
python_cmd = 'python' if os.name == 'nt' else 'python3'
if os.name == 'nt':
if len(sys.argv) == 3:
version = sys.argv[2]
result = subprocess.run(['py', f'-{version}', '-m', 'venv', venvPath])
else:
result = subprocess.run([python_cmd, '-m', 'venv', venvPath])
if result.returncode != 0:
print('ERROR: Failed to create virtual environment.', file=sys.stderr)
sys.exit(1)
# Try to upgrade pip, but don't fail if pip isn't available
pip_result = subprocess.run([f'{venvPath}/Scripts/python.exe', '-m', 'pip', 'install', '--upgrade', 'pip'])
if pip_result.returncode != 0:
print('WARNING: Failed to upgrade pip. pip may not be available in the virtual environment.', file=sys.stderr)
path = {
'NODE_PYENV_PYTHON': 'Scripts/python.exe',
'NODE_PYENV_PIP': 'Scripts/pip.exe',
'NODE_PYENV_EXEC': 'Scripts/'
}
else:
# Create venv; if ensurepip is missing, use --without-pip to avoid failure
if pip_available:
result = subprocess.run([python_cmd, '-m', 'venv', venvPath])
else:
result = subprocess.run([python_cmd, '-m', 'venv', '--without-pip', venvPath])
if result.returncode != 0:
print('ERROR: Failed to create virtual environment.', file=sys.stderr)
sys.exit(1)
# Try to upgrade pip, but don't fail if pip isn't available
pip_result = subprocess.run([f'{venvPath}/bin/python', '-m', 'pip', 'install', '--upgrade', 'pip'])
if pip_result.returncode != 0:
print('WARNING: Failed to upgrade pip in virtual environment.', file=sys.stderr)
path = {
'NODE_PYENV_PYTHON': 'bin/python',
'NODE_PYENV_PIP': 'bin/pip',
'NODE_PYENV_EXEC': 'bin/'
}
with open(f'{venvPath}/path.json', 'w') as f:
json.dump(path, f, indent=4)