Skip to content

Commit 1f519bc

Browse files
committed
init version
0 parents  commit 1f519bc

File tree

3 files changed

+328
-0
lines changed

3 files changed

+328
-0
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.pyc
2+
*.swp
3+
env

LICENSE

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Copyright (c) 2011, Eugene Kalinin.
2+
3+
Some rights reserved.
4+
5+
Redistribution and use in source and binary forms of the software as well
6+
as documentation, with or without modification, are permitted provided
7+
that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright
10+
notice, this list of conditions and the following disclaimer.
11+
12+
* Redistributions in binary form must reproduce the above
13+
copyright notice, this list of conditions and the following
14+
disclaimer in the documentation and/or other materials provided
15+
with the distribution.
16+
17+
* The names of the contributors may not be used to endorse or
18+
promote products derived from this software without specific
19+
prior written permission.
20+
21+
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND
22+
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
23+
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
25+
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26+
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27+
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28+
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29+
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31+
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
32+
DAMAGE.

nve.py

+293
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
"""
5+
nve
6+
~~~
7+
nve - Node.js virtual environment.
8+
9+
TODO:
10+
- set exec bit for activate
11+
- install npm
12+
- no newline in logs
13+
- check g++ libssl-dev
14+
- create helloworld.js for node.js install test
15+
16+
:copyright: (c) 2011 by Eugene Kalinin
17+
:license: BSD, see LICENSE for more details.
18+
"""
19+
20+
nodeenv_version = '0.1'
21+
22+
import sys
23+
import os
24+
import optparse
25+
import urllib
26+
import tarfile
27+
import logging
28+
29+
join = os.path.join
30+
abspath = os.path.abspath
31+
32+
# ---------------------------------------------------------
33+
# Utils
34+
35+
def create_logger():
36+
"""
37+
Create logger for diagnostic
38+
"""
39+
# create logger
40+
logger = logging.getLogger("simple_example")
41+
logger.setLevel(logging.DEBUG)
42+
43+
# create console handler and set level to debug
44+
ch = logging.StreamHandler()
45+
ch.setLevel(logging.DEBUG)
46+
47+
# create formatter
48+
formatter = logging.Formatter(fmt="%(asctime)s :: %(message)s",
49+
datefmt="%Y-%m-%d %H:%M:%S")
50+
51+
# add formatter to ch
52+
ch.setFormatter(formatter)
53+
54+
# add ch to logger
55+
logger.addHandler(ch)
56+
return logger
57+
logger = create_logger()
58+
59+
60+
def parse_args():
61+
"""
62+
Parses command line arguments
63+
"""
64+
parser = optparse.OptionParser(
65+
version=nodeenv_version ,
66+
usage="%prog [OPTIONS] DEST_DIR")
67+
68+
parser.add_option(
69+
'-n', '--node', dest='node',
70+
metavar='NODE_VER', default="0.2.6",
71+
help='The node.js version to use, e.g., --version=0.4.3 will use the node-v0.4.3 '
72+
'to create the new environment. The default is 0.2.6')
73+
74+
parser.add_option(
75+
'--prompt',
76+
dest='prompt',
77+
help='Provides an alternative prompt prefix for this environment')
78+
79+
parser.add_option( '--without-ssl',
80+
action='store_true', default=False,
81+
dest='without_ssl',
82+
help='Build node.js without SSL support')
83+
84+
options, args = parser.parse_args()
85+
86+
if not args:
87+
print('You must provide a DEST_DIR')
88+
parser.print_help()
89+
sys.exit(2)
90+
91+
if len(args) > 1:
92+
print('There must be only one argument: DEST_DIR (you gave %s)' % (
93+
' '.join(args)))
94+
parser.print_help()
95+
sys.exit(2)
96+
97+
return options, args
98+
99+
100+
def mkdir(path):
101+
"""
102+
Create directory
103+
"""
104+
if not os.path.exists(path):
105+
logger.info('Creating %s ...', path)
106+
os.makedirs(path)
107+
logger.info(' ... done')
108+
else:
109+
logger.info('Directory %s already exists', path)
110+
111+
112+
def writefile(dest, content, overwrite=True):
113+
if not os.path.exists(dest):
114+
logger.info('Writing %s', dest)
115+
f = open(dest, 'wb')
116+
f.write(content.encode('utf-8'))
117+
f.close()
118+
return
119+
else:
120+
f = open(dest, 'rb')
121+
c = f.read()
122+
f.close()
123+
if c != content:
124+
if not overwrite:
125+
logger.notify('File %s exists with different content; not overwriting', dest)
126+
return
127+
logger.notify('Overwriting %s with new content', dest)
128+
f = open(dest, 'wb')
129+
f.write(content.encode('utf-8'))
130+
f.close()
131+
else:
132+
logger.info('Content %s already in place', dest)
133+
134+
# ---------------------------------------------------------
135+
# Create env functions
136+
137+
def path_locations(env_dir, version):
138+
"""
139+
Returns the path locations for the environment
140+
(where libraries are, where scripts go, etc)
141+
"""
142+
dirs = {}
143+
dirs["src"] = abspath(join(env_dir, 'src'))
144+
return dirs
145+
146+
147+
def install_node(env_dir, src_dir, opt):
148+
node_name = 'node-v%s'%(opt.node)
149+
tar_name = '%s.tar.gz'%(node_name)
150+
node_url = 'http://nodejs.org/dist/%s'%(tar_name)
151+
node_tar = join(src_dir, tar_name)
152+
153+
if not os.path.exists(node_tar):
154+
logger.info('Retrieve URL: %s ...'%(node_url))
155+
urllib.urlretrieve(node_url, node_tar)
156+
logger.info(' ... done.')
157+
else:
158+
logger.info('Source for %s exists: %s'%(node_name, node_tar))
159+
160+
logger.info('Unpack file: %s'%(node_tar))
161+
tar = tarfile.open(node_tar)
162+
tar.extractall(src_dir)
163+
tar.close()
164+
logger.info(' ... done.')
165+
166+
src_dir = join(src_dir, node_name)
167+
env_dir = abspath(env_dir)
168+
old_chdir = os.getcwd()
169+
conf_cmd = './configure --prefix=%s'%(env_dir)
170+
if opt.without_ssl:
171+
conf_cmd += ' --without-ssl'
172+
try:
173+
logger.info('Compile: %s ...'%(src_dir))
174+
os.chdir(src_dir)
175+
os.system(conf_cmd)
176+
os.system('make')
177+
os.system('make install')
178+
logger.info(' ... done.')
179+
finally:
180+
if os.getcwd() != old_chdir:
181+
os.chdir(old_chdir)
182+
183+
184+
def install_npm(env_dir, src_dir):
185+
pass
186+
187+
188+
def install_activate(env_dir, prompt=None):
189+
files = {'activate': ACTIVATE_SH}
190+
bin_dir = join(env_dir, 'bin')
191+
for name, content in files.items():
192+
content = content.replace('__VIRTUAL_PROMPT__', prompt or '')
193+
content = content.replace('__VIRTUAL_ENV__', os.path.abspath(env_dir))
194+
content = content.replace('__BIN_NAME__', os.path.basename(bin_dir))
195+
writefile(os.path.join(bin_dir, name), content)
196+
197+
198+
def create_environment(env_dir, opt):
199+
"""
200+
Creates a new environment in ``env_dir``.
201+
"""
202+
dirs = path_locations(env_dir, opt.node)
203+
for dir_name, dir_path in dirs.items():
204+
mkdir(dir_path)
205+
206+
install_node(env_dir, dirs["src"], opt)
207+
#install_npm(env_dir, dirs["src"])
208+
install_activate(env_dir, opt.prompt)
209+
210+
211+
def main():
212+
opt, args = parse_args()
213+
env_dir = args[0]
214+
create_environment(env_dir, opt)
215+
216+
217+
# ---------------------------------------------------------
218+
# Shell scripts content
219+
220+
ACTIVATE_SH = """
221+
# This file must be used with "source bin/activate" *from bash*
222+
# you cannot run it directly
223+
224+
deactivate () {
225+
# reset old environment variables
226+
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then
227+
PATH="$_OLD_VIRTUAL_PATH"
228+
export PATH
229+
unset _OLD_VIRTUAL_PATH
230+
fi
231+
if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then
232+
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
233+
export PYTHONHOME
234+
unset _OLD_VIRTUAL_PYTHONHOME
235+
fi
236+
237+
# This should detect bash and zsh, which have a hash command that must
238+
# be called to get it to forget past commands. Without forgetting
239+
# past commands the $PATH changes we made may not be respected
240+
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
241+
hash -r
242+
fi
243+
244+
if [ -n "$_OLD_VIRTUAL_PS1" ] ; then
245+
PS1="$_OLD_VIRTUAL_PS1"
246+
export PS1
247+
unset _OLD_VIRTUAL_PS1
248+
fi
249+
250+
unset VIRTUAL_ENV
251+
if [ ! "$1" = "nondestructive" ] ; then
252+
# Self destruct!
253+
unset -f deactivate
254+
fi
255+
}
256+
257+
# unset irrelavent variables
258+
deactivate nondestructive
259+
260+
VIRTUAL_ENV="__VIRTUAL_ENV__"
261+
export VIRTUAL_ENV
262+
263+
_OLD_VIRTUAL_PATH="$PATH"
264+
PATH="$VIRTUAL_ENV/__BIN_NAME__:$PATH"
265+
export PATH
266+
267+
if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then
268+
_OLD_VIRTUAL_PS1="$PS1"
269+
if [ "x__VIRTUAL_PROMPT__" != x ] ; then
270+
PS1="__VIRTUAL_PROMPT__$PS1"
271+
else
272+
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
273+
# special case for Aspen magic directories
274+
# see http://www.zetadev.com/software/aspen/
275+
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
276+
else
277+
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
278+
fi
279+
fi
280+
export PS1
281+
fi
282+
283+
# This should detect bash and zsh, which have a hash command that must
284+
# be called to get it to forget past commands. Without forgetting
285+
# past commands the $PATH changes we made may not be respected
286+
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then
287+
hash -r
288+
fi
289+
"""
290+
291+
if __name__ == '__main__':
292+
main()
293+

0 commit comments

Comments
 (0)