2 # Copyright (C) 2013 Vinay Sajip. New BSD License.
3 # Copyright (C) 2014 Kaliko Jack
7 if sys.version_info < REQ_VER:
8 print('Need at least python {0}.{1} to run this script'.format(*REQ_VER), file=sys.stderr)
15 from subprocess import Popen, PIPE
16 from threading import Thread
17 from urllib.parse import urlparse
18 from urllib.request import urlretrieve
19 from shutil import rmtree
21 class ExtendedEnvBuilder(venv.EnvBuilder):
23 This builder installs setuptools and pip so that you can pip or
24 easy_install other packages into the created environment.
27 def __init__(self, *args, **kwargs):
28 self.verbose = kwargs.pop('verbose', False)
29 super().__init__(*args, **kwargs)
31 def post_setup(self, context):
33 Set up any packages which need to be pre-installed into the
34 environment being created.
36 :param context: The information for the environment creation request
39 os.environ['VIRTUAL_ENV'] = context.env_dir
40 self.install_setuptools(context)
41 self.install_pip(context)
42 setup = os.path.abspath(os.path.join(context.env_dir, '../setup.py'))
43 self.install_script(context, 'sima', setup=setup)
45 def reader(self, stream, context):
47 Read lines from a subprocess' output stream and write progress
48 information to sys.stderr.
57 sys.stderr.write(s.decode('utf-8'))
61 def install_script(self, context, name, url=None, setup=None):
63 binpath = context.bin_path
64 _, _, path, _, _, _ = urlparse(url)
65 fn = os.path.split(path)[-1]
66 distpath = os.path.join(binpath, fn)
67 # Download script into the env's binaries folder
68 urlretrieve(url, distpath)
70 args = [context.env_exe, fn]
72 args = [context.env_exe, setup, 'install']
73 binpath = os.path.dirname(setup)
78 sys.stderr.write('Installing %s ...%s' % (name, term))
81 p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
82 t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
84 t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
89 sys.stderr.write('done.\n')
91 # Clean up - no longer needed
94 def install_setuptools(self, context):
96 Install setuptools in the environment.
98 :param context: The information for the environment creation request
101 url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py'
102 self.install_script(context, 'setuptools', url)
103 # clear up the setuptools archive which gets downloaded
104 pred = lambda o: o.startswith('setuptools-') and o.endswith('.tar.gz')
105 files = filter(pred, os.listdir(context.bin_path))
107 f = os.path.join(context.bin_path, f)
110 def install_pip(self, context):
112 Install pip in the environment.
114 :param context: The information for the environment creation request
117 url = 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py'
118 self.install_script(context, 'pip', url)
119 # pip installs to "local/bin" on Linux, but it needs to be accessible
120 # from "bin" since the "activate" script prepends "bin" to $PATH
121 pip_path = os.path.join(context.env_dir, 'local', 'bin', 'pip')
122 if sys.platform != 'win32' and os.path.exists(pip_path):
123 self.symlink_or_copy(pip_path, os.path.join(context.bin_path, 'pip'))
127 root = os.path.dirname(os.path.abspath(__file__))
128 vdir = os.path.join(root, 'venv')
129 builder = ExtendedEnvBuilder(clear=True, verbose=False)
132 for residu in ['MPD_sima.egg-info', 'dist', 'build']:
133 if os.path.exists(os.path.join(root, residu)):
134 rmtree(os.path.join(root, residu))
136 with open(os.path.join(root, 'vmpd-sima'),'w') as fd:
137 fd.write('#!/bin/sh\n')
138 fd.write('. "{}/venv/bin/activate"\n'.format(root))
139 fd.write('"{}/venv/bin/mpd-sima" "$@"'.format(root))
140 os.chmod(os.path.join(root, 'vmpd-sima'), 0o744)
142 if __name__ == '__main__':
147 except ImportError as e:
148 print('Error: %s' % e)