forked from roadsideseb/virtual-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
186 lines (156 loc) · 5.64 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import logging
import subprocess
from setuptools import setup
from setuptools.command.install import install
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("virtual-node")
class node_install(install):
env_dir = os.environ['VIRTUAL_ENV']
default_version = '0.8.11'
verbose = False
def run(self):
versions = self.distribution.get_version().rsplit('-', 1)
if len(versions) == 2:
self.version = versions[1]
else:
self.version = self.default_version
self.install_node(self.env_dir)
self.run_npm(self.env_dir)
self.run_bower(self.env_dir)
def get_node_src_url(self, version, postfix=''):
node_name = 'node-v%s%s' % (version, postfix)
tar_name = '%s.tar.gz' % (node_name)
if version > "0.5.0":
node_url = 'http://nodejs.org/dist/v%s/%s' % (version, tar_name)
else:
node_url = 'http://nodejs.org/dist/%s' % (tar_name)
return node_url
def run_cmd(self, cmd, cwd=None, extra_env=None):
"""
Execute cmd line in sub-shell
"""
all_output = []
cmd_parts = []
for part in cmd:
if len(part) > 45:
part = part[:20] + "..." + part[-20:]
if ' ' in part or '\n' in part or '"' in part or "'" in part:
part = '"%s"' % part.replace('"', '\\"')
cmd_parts.append(part)
cmd_desc = ' '.join(cmd_parts)
logger.debug(" ** Running command %s" % cmd_desc)
# output
stdout = subprocess.PIPE
# env
if extra_env:
env = os.environ.copy()
if extra_env:
env.update(extra_env)
else:
env = None
# execute
try:
proc = subprocess.Popen(
[' '.join(cmd)], stderr=subprocess.STDOUT, stdin=None, stdout=stdout,
cwd=cwd, env=env, shell=True)
except Exception:
e = sys.exc_info()[1]
logger.error("Error %s while executing command %s" % (e, cmd_desc))
raise
stdout = proc.stdout
while stdout:
line = stdout.readline()
if not line:
break
line = line.rstrip()
all_output.append(line)
logger.info(line)
proc.wait()
# error handler
if proc.returncode:
for s in all_output:
logger.critical(s)
raise OSError("Command %s failed with error code %s"
% (cmd_desc, proc.returncode))
return proc.returncode, all_output
def run_npm(self, env_dir):
if os.path.exists("%s%s" % (env_dir, "package.json")):
self.run_cmd(['npm', 'install'], env_dir)
else:
logger.warning("Could not find 'package.json', ignoring NPM dependencies.")
def run_bower(self, env_dir):
bower_bin = "%s/bin/bower" % env_dir
if not os.path.exists(bower_bin):
logger.warning("Could not find 'bower' executable, ignoring it")
return
if os.path.exists("%s%s" % (env_dir, "components.json")):
self.run_cmd(['bower', 'install'], env_dir)
else:
logger.warning("Could not find 'components.json', ignoring bower dependencies.")
def install_node(self, env_dir, version=None):
"""
Download source code for node.js, unpack it
and install it in virtual environment.
"""
logger.info(
' * Install node.js (%s' % self.version,
extra={'continued': True}
)
node_name = 'node-v%s' % (self.version)
node_url = self.get_node_src_url(self.version)
src_dir = os.path.join(env_dir, 'src')
node_src_dir = os.path.join(src_dir, node_name)
env_dir = os.path.abspath(env_dir)
if not os.path.exists(node_src_dir):
os.makedirs(node_src_dir)
cmd = [
'curl', '--silent', '-L', node_url, '|',
'tar', 'xzf', '-', '-C', src_dir,
]
try:
self.run_cmd(cmd, env_dir)
logger.info(') ', extra=dict(continued=True))
except OSError:
postfix = '-RC1'
logger.info('%s) ' % postfix, extra=dict(continued=True))
new_node_url = self.get_node_src_url(self.version, postfix)
cmd[cmd.index(node_url)] = new_node_url
self.run_cmd(cmd, env_dir)
logger.info('.', extra=dict(continued=True))
conf_cmd = [
'./configure',
'--prefix=%s' % (env_dir),
]
self.run_cmd(conf_cmd, node_src_dir)
logger.info('.', extra=dict(continued=True))
self.run_cmd(['make'], node_src_dir)
logger.info('.', extra=dict(continued=True))
self.run_cmd(['make install'], node_src_dir)
logger.info(' done.')
setup(
name='virtual-node',
version='0.0.2-0.8.11',
description='Install node.js into your virtualenv',
author='Sebastian Vetter',
author_email='[email protected]',
url='http://github.com/elbaschid/virtual-node',
long_description=open('README.rst', 'r').read(),
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Console',
'Environment :: Web Environment',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: POSIX',
'Programming Language :: JavaScript',
'Topic :: Software Development :: Libraries',
],
license='BSD',
cmdclass={
'install': node_install,
}
)