forked from ekalinin/nodeenv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1f519bc
Showing
3 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
*.pyc | ||
*.swp | ||
env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
Copyright (c) 2011, Eugene Kalinin. | ||
|
||
Some rights reserved. | ||
|
||
Redistribution and use in source and binary forms of the software as well | ||
as documentation, with or without modification, are permitted provided | ||
that the following conditions are met: | ||
|
||
* Redistributions of source code must retain the above copyright | ||
notice, this list of conditions and the following disclaimer. | ||
|
||
* Redistributions in binary form must reproduce the above | ||
copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided | ||
with the distribution. | ||
|
||
* The names of the contributors may not be used to endorse or | ||
promote products derived from this software without specific | ||
prior written permission. | ||
|
||
THIS SOFTWARE AND DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND | ||
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | ||
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER | ||
OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | ||
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | ||
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | ||
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | ||
SOFTWARE AND DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH | ||
DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,293 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
nve | ||
~~~ | ||
nve - Node.js virtual environment. | ||
TODO: | ||
- set exec bit for activate | ||
- install npm | ||
- no newline in logs | ||
- check g++ libssl-dev | ||
- create helloworld.js for node.js install test | ||
:copyright: (c) 2011 by Eugene Kalinin | ||
:license: BSD, see LICENSE for more details. | ||
""" | ||
|
||
nodeenv_version = '0.1' | ||
|
||
import sys | ||
import os | ||
import optparse | ||
import urllib | ||
import tarfile | ||
import logging | ||
|
||
join = os.path.join | ||
abspath = os.path.abspath | ||
|
||
# --------------------------------------------------------- | ||
# Utils | ||
|
||
def create_logger(): | ||
""" | ||
Create logger for diagnostic | ||
""" | ||
# create logger | ||
logger = logging.getLogger("simple_example") | ||
logger.setLevel(logging.DEBUG) | ||
|
||
# create console handler and set level to debug | ||
ch = logging.StreamHandler() | ||
ch.setLevel(logging.DEBUG) | ||
|
||
# create formatter | ||
formatter = logging.Formatter(fmt="%(asctime)s :: %(message)s", | ||
datefmt="%Y-%m-%d %H:%M:%S") | ||
|
||
# add formatter to ch | ||
ch.setFormatter(formatter) | ||
|
||
# add ch to logger | ||
logger.addHandler(ch) | ||
return logger | ||
logger = create_logger() | ||
|
||
|
||
def parse_args(): | ||
""" | ||
Parses command line arguments | ||
""" | ||
parser = optparse.OptionParser( | ||
version=nodeenv_version , | ||
usage="%prog [OPTIONS] DEST_DIR") | ||
|
||
parser.add_option( | ||
'-n', '--node', dest='node', | ||
metavar='NODE_VER', default="0.2.6", | ||
help='The node.js version to use, e.g., --version=0.4.3 will use the node-v0.4.3 ' | ||
'to create the new environment. The default is 0.2.6') | ||
|
||
parser.add_option( | ||
'--prompt', | ||
dest='prompt', | ||
help='Provides an alternative prompt prefix for this environment') | ||
|
||
parser.add_option( '--without-ssl', | ||
action='store_true', default=False, | ||
dest='without_ssl', | ||
help='Build node.js without SSL support') | ||
|
||
options, args = parser.parse_args() | ||
|
||
if not args: | ||
print('You must provide a DEST_DIR') | ||
parser.print_help() | ||
sys.exit(2) | ||
|
||
if len(args) > 1: | ||
print('There must be only one argument: DEST_DIR (you gave %s)' % ( | ||
' '.join(args))) | ||
parser.print_help() | ||
sys.exit(2) | ||
|
||
return options, args | ||
|
||
|
||
def mkdir(path): | ||
""" | ||
Create directory | ||
""" | ||
if not os.path.exists(path): | ||
logger.info('Creating %s ...', path) | ||
os.makedirs(path) | ||
logger.info(' ... done') | ||
else: | ||
logger.info('Directory %s already exists', path) | ||
|
||
|
||
def writefile(dest, content, overwrite=True): | ||
if not os.path.exists(dest): | ||
logger.info('Writing %s', dest) | ||
f = open(dest, 'wb') | ||
f.write(content.encode('utf-8')) | ||
f.close() | ||
return | ||
else: | ||
f = open(dest, 'rb') | ||
c = f.read() | ||
f.close() | ||
if c != content: | ||
if not overwrite: | ||
logger.notify('File %s exists with different content; not overwriting', dest) | ||
return | ||
logger.notify('Overwriting %s with new content', dest) | ||
f = open(dest, 'wb') | ||
f.write(content.encode('utf-8')) | ||
f.close() | ||
else: | ||
logger.info('Content %s already in place', dest) | ||
|
||
# --------------------------------------------------------- | ||
# Create env functions | ||
|
||
def path_locations(env_dir, version): | ||
""" | ||
Returns the path locations for the environment | ||
(where libraries are, where scripts go, etc) | ||
""" | ||
dirs = {} | ||
dirs["src"] = abspath(join(env_dir, 'src')) | ||
return dirs | ||
|
||
|
||
def install_node(env_dir, src_dir, opt): | ||
node_name = 'node-v%s'%(opt.node) | ||
tar_name = '%s.tar.gz'%(node_name) | ||
node_url = 'http://nodejs.org/dist/%s'%(tar_name) | ||
node_tar = join(src_dir, tar_name) | ||
|
||
if not os.path.exists(node_tar): | ||
logger.info('Retrieve URL: %s ...'%(node_url)) | ||
urllib.urlretrieve(node_url, node_tar) | ||
logger.info(' ... done.') | ||
else: | ||
logger.info('Source for %s exists: %s'%(node_name, node_tar)) | ||
|
||
logger.info('Unpack file: %s'%(node_tar)) | ||
tar = tarfile.open(node_tar) | ||
tar.extractall(src_dir) | ||
tar.close() | ||
logger.info(' ... done.') | ||
|
||
src_dir = join(src_dir, node_name) | ||
env_dir = abspath(env_dir) | ||
old_chdir = os.getcwd() | ||
conf_cmd = './configure --prefix=%s'%(env_dir) | ||
if opt.without_ssl: | ||
conf_cmd += ' --without-ssl' | ||
try: | ||
logger.info('Compile: %s ...'%(src_dir)) | ||
os.chdir(src_dir) | ||
os.system(conf_cmd) | ||
os.system('make') | ||
os.system('make install') | ||
logger.info(' ... done.') | ||
finally: | ||
if os.getcwd() != old_chdir: | ||
os.chdir(old_chdir) | ||
|
||
|
||
def install_npm(env_dir, src_dir): | ||
pass | ||
|
||
|
||
def install_activate(env_dir, prompt=None): | ||
files = {'activate': ACTIVATE_SH} | ||
bin_dir = join(env_dir, 'bin') | ||
for name, content in files.items(): | ||
content = content.replace('__VIRTUAL_PROMPT__', prompt or '') | ||
content = content.replace('__VIRTUAL_ENV__', os.path.abspath(env_dir)) | ||
content = content.replace('__BIN_NAME__', os.path.basename(bin_dir)) | ||
writefile(os.path.join(bin_dir, name), content) | ||
|
||
|
||
def create_environment(env_dir, opt): | ||
""" | ||
Creates a new environment in ``env_dir``. | ||
""" | ||
dirs = path_locations(env_dir, opt.node) | ||
for dir_name, dir_path in dirs.items(): | ||
mkdir(dir_path) | ||
|
||
install_node(env_dir, dirs["src"], opt) | ||
#install_npm(env_dir, dirs["src"]) | ||
install_activate(env_dir, opt.prompt) | ||
|
||
|
||
def main(): | ||
opt, args = parse_args() | ||
env_dir = args[0] | ||
create_environment(env_dir, opt) | ||
|
||
|
||
# --------------------------------------------------------- | ||
# Shell scripts content | ||
|
||
ACTIVATE_SH = """ | ||
# This file must be used with "source bin/activate" *from bash* | ||
# you cannot run it directly | ||
deactivate () { | ||
# reset old environment variables | ||
if [ -n "$_OLD_VIRTUAL_PATH" ] ; then | ||
PATH="$_OLD_VIRTUAL_PATH" | ||
export PATH | ||
unset _OLD_VIRTUAL_PATH | ||
fi | ||
if [ -n "$_OLD_VIRTUAL_PYTHONHOME" ] ; then | ||
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME" | ||
export PYTHONHOME | ||
unset _OLD_VIRTUAL_PYTHONHOME | ||
fi | ||
# This should detect bash and zsh, which have a hash command that must | ||
# be called to get it to forget past commands. Without forgetting | ||
# past commands the $PATH changes we made may not be respected | ||
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then | ||
hash -r | ||
fi | ||
if [ -n "$_OLD_VIRTUAL_PS1" ] ; then | ||
PS1="$_OLD_VIRTUAL_PS1" | ||
export PS1 | ||
unset _OLD_VIRTUAL_PS1 | ||
fi | ||
unset VIRTUAL_ENV | ||
if [ ! "$1" = "nondestructive" ] ; then | ||
# Self destruct! | ||
unset -f deactivate | ||
fi | ||
} | ||
# unset irrelavent variables | ||
deactivate nondestructive | ||
VIRTUAL_ENV="__VIRTUAL_ENV__" | ||
export VIRTUAL_ENV | ||
_OLD_VIRTUAL_PATH="$PATH" | ||
PATH="$VIRTUAL_ENV/__BIN_NAME__:$PATH" | ||
export PATH | ||
if [ -z "$VIRTUAL_ENV_DISABLE_PROMPT" ] ; then | ||
_OLD_VIRTUAL_PS1="$PS1" | ||
if [ "x__VIRTUAL_PROMPT__" != x ] ; then | ||
PS1="__VIRTUAL_PROMPT__$PS1" | ||
else | ||
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then | ||
# special case for Aspen magic directories | ||
# see http://www.zetadev.com/software/aspen/ | ||
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1" | ||
else | ||
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1" | ||
fi | ||
fi | ||
export PS1 | ||
fi | ||
# This should detect bash and zsh, which have a hash command that must | ||
# be called to get it to forget past commands. Without forgetting | ||
# past commands the $PATH changes we made may not be respected | ||
if [ -n "$BASH" -o -n "$ZSH_VERSION" ] ; then | ||
hash -r | ||
fi | ||
""" | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|