Commits

Anonymous committed 0d9bf57

Initial commit

Comments (0)

Files changed (5)

xenv/__init__.py

Empty file added.

xenv/handlers/__init__.py

+

xenv/handlers/create.py

+'''
+Creating a New Xenv
+===================
+
+The Xenv has a set of directories that mimics a typical *nix filesystem hierarchy.
+
+For a basic overview, see http://en.wikipedia.org/wiki/Filesystem_Hierarchy_Standard
+'''
+from xenv.models import XenvFS
+
+
+class CreateHandler(object):
+    '''
+    A command line handler that will create a new xenv from
+    scratch. This involves making a directory and installing a
+    virtualenv.
+    '''
+    
+    def __call__(self, args):
+        xenv = XenvFS(root=args.root)
+        xenv.create_venv()
+        return xenv
+        
+        
+        
+'''
+This defines the main 'xe' entry point to the xenv. Below, the parser
+definition describes the usage and UI for interacting with an xenv.
+'''
+import os
+from subprocess import call
+
+from argparse import ArgumentParser
+from xenv.models import Xenv
+
+
+def run_cmd(args):
+    xenv = Xenv(root=args.root)
+    xenv.run(args.cmd.pop())
+
+
+def create_xenv_cmd(args):
+    xenv = Xenv(root=args.root)
+    xenv.create()
+
+
+class CLI(object):
+    description = '''Utilize a specific executable environment (xenv) for running applications'''
+
+    def __init__(self):
+        self.parser = ArgumentParser(description=self.description)
+        self.parser.add_argument('--root', '-r', default=os.environ.get('XE_ROOT'),
+                                 help='The xenv root. This will override using the XE_ROOT env var.')
+        
+        self.subs = self.parser.add_subparsers(help='sub command help')
+
+        # Create a new xenv
+        create = self.subs.add_parser('create', help='Create a new xenv')
+        create.add_argument('root', help='The root of the xenv')
+        create.set_defaults(func=create_xenv_cmd)
+
+        # run a command via the xenv
+        run = self.subs.add_parser('run', prefix_chars='!',
+                                   help='Run a command from the xenv')
+        run.add_argument('cmd', type=str, nargs='*', action='append',
+                         help='The command and flags to run via the xenv')
+        run.set_defaults(func=run_cmd)
+
+    def run(self, args=None):
+        if not args:
+            args = self.parser.parse_known_args()
+        else:
+            args = self.parser.parse_args(args)
+        return args
+    
+
+def xe():
+    cli = CLI()
+    args, extras = cli.run()
+    if args.func:
+        args.func(args)
+        
+import os
+from paver.easy import path
+from subprocess import Popen, call
+
+
+class XenvFS(object):
+    '''The xenv filesystem object provides the specification for the
+    xenv filesystem. This will allow finding directories, establishing
+    the PATH when calling a command along with locations for specific
+    files.'''
+    def __init__(self, root=None):
+        self.root = path(root or '.')
+
+    def create_base_env(self):
+        mk_bin = ['ln', '-s', '/usr/bin',
+                  self.root / 'bin']
+        call(mk_bin)
+
+        (self.root / 'local').mkdir()
+        call(['ln', '-s', '/usr/local/bin',
+              self.root.abspath() / 'local' / 'bin'])
+    
+    def create_venv(self):
+        venv = self.root.abspath() / 'usr'
+        cmd = 'virtualenv --no-site-packages %s' % venv
+        call(cmd.split())
+
+    def path(self):
+        '''Return the path hiearchy'''
+        return ':'.join([
+                (self.root / 'usr' / 'bin').abspath(),
+                (self.root / 'usr' / 'local' / 'bin').abspath(),
+                (self.root / 'bin').abspath(),
+                ])
+
+
+class Xenv(object):
+    def __init__(self, root=None):
+        self.xenv_fs = XenvFS(root)
+
+    def run(self, cmd):
+        env = {
+            'PATH': self.xenv_fs.path()
+        }
+        call(cmd, env=env)
+
+    def create(self):
+        self.xenv_fs.root.mkdir()
+        self.xenv_fs.create_base_env()
+        self.xenv_fs.create_venv()
+
+    def activate(self):
+        self.run('python %s' % self.root / 'usr' / 'bin' / 'activate_this.py')
+
+