2 # Copyright (C) 2013 Vinay Sajip. New BSD License.
3 # Copyright (C) 2014, 2020 kaliko <kaliko@azylum.org>
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 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
96 root = os.path.dirname(os.path.abspath(__file__))
97 vdir = os.path.join(root, 'venv')
98 builder = ExtendedEnvBuilder(clear=True, verbose=False, with_pip=True)
101 for residu in ['MPD_sima.egg-info', 'dist', 'build']:
102 if os.path.exists(os.path.join(root, residu)):
103 rmtree(os.path.join(root, residu))
105 with open(os.path.join(root, 'vmpd-sima'),'w') as fd:
106 fd.write('#!/bin/sh\n')
107 fd.write('. "{}/venv/bin/activate"\n'.format(root))
108 fd.write('"{}/venv/bin/mpd-sima" "$@"'.format(root))
109 os.chmod(os.path.join(root, 'vmpd-sima'), 0o744)
112 if __name__ == '__main__':
117 except ImportError as e:
118 print('Error: %s' % e)