2 # Copyright (C) 2013 Vinay Sajip. New BSD License.
3 # Copyright (C) 2014 Kaliko Jack
5 from __future__ import print_function
9 if sys.version_info < REQ_VER:
10 print('Need at least python {0}.{1} to run this script'.format(*REQ_VER), file=sys.stderr)
17 from subprocess import Popen, PIPE
18 from threading import Thread
19 from urllib.parse import urlparse
20 from urllib.request import urlretrieve
21 from shutil import rmtree
23 class ExtendedEnvBuilder(venv.EnvBuilder):
25 This builder installs setuptools and pip so that you can pip or
26 easy_install other packages into the created environment.
29 def __init__(self, *args, **kwargs):
30 self.verbose = kwargs.pop('verbose', False)
31 super().__init__(*args, **kwargs)
33 def post_setup(self, context):
35 Set up any packages which need to be pre-installed into the
36 environment being created.
38 :param context: The information for the environment creation request
41 os.environ['VIRTUAL_ENV'] = context.env_dir
42 self.install_setuptools(context)
43 self.install_pip(context)
44 setup = os.path.abspath(os.path.join(context.env_dir, '../setup.py'))
45 self.install_script(context, 'sima', setup=setup)
47 def reader(self, stream, context):
49 Read lines from a subprocess' output stream and write progress
50 information to sys.stderr.
59 sys.stderr.write(s.decode('utf-8'))
63 def install_script(self, context, name, url=None, setup=None):
65 binpath = context.bin_path
66 _, _, path, _, _, _ = urlparse(url)
67 fn = os.path.split(path)[-1]
68 distpath = os.path.join(binpath, fn)
69 # Download script into the env's binaries folder
70 urlretrieve(url, distpath)
72 args = [context.env_exe, fn]
74 args = [context.env_exe, setup, 'install']
75 binpath = os.path.dirname(setup)
80 sys.stderr.write('Installing %s ...%s' % (name, term))
83 p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
84 t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
86 t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
91 sys.stderr.write('done.\n')
93 # Clean up - no longer needed
96 def install_setuptools(self, context):
98 Install setuptools in the environment.
100 :param context: The information for the environment creation request
103 url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py'
104 self.install_script(context, 'setuptools', url)
105 # clear up the setuptools archive which gets downloaded
106 pred = lambda o: o.startswith('setuptools-') and o.endswith('.tar.gz')
107 files = filter(pred, os.listdir(context.bin_path))
109 f = os.path.join(context.bin_path, f)
112 def install_pip(self, context):
114 Install pip in the environment.
116 :param context: The information for the environment creation request
119 url = 'https://bootstrap.pypa.io/get-pip.py'
120 self.install_script(context, 'pip', url)
121 # pip installs to "local/bin" on Linux, but it needs to be accessible
122 # from "bin" since the "activate" script prepends "bin" to $PATH
123 pip_path = os.path.join(context.env_dir, 'local', 'bin', 'pip')
124 if sys.platform != 'win32' and os.path.exists(pip_path):
125 self.symlink_or_copy(pip_path, os.path.join(context.bin_path, 'pip'))
129 root = os.path.dirname(os.path.abspath(__file__))
130 vdir = os.path.join(root, 'venv')
131 builder = ExtendedEnvBuilder(clear=True, verbose=False)
134 for residu in ['MPD_sima.egg-info', 'dist', 'build']:
135 if os.path.exists(os.path.join(root, residu)):
136 rmtree(os.path.join(root, residu))
138 with open(os.path.join(root, 'vmpd-sima'),'w') as fd:
139 fd.write('#!/bin/sh\n')
140 fd.write('. "{}/venv/bin/activate"\n'.format(root))
141 fd.write('"{}/venv/bin/mpd-sima" "$@"'.format(root))
142 os.chmod(os.path.join(root, 'vmpd-sima'), 0o744)
144 if __name__ == '__main__':
149 except ImportError as e:
150 print('Error: %s' % e)