2 # Copyright (C) 2013 Vinay Sajip. New BSD License.
3 # Copyright (C) 2014, 2021 kaliko <kaliko@azylum.org>
8 if sys.version_info < REQ_VER:
9 print('Need at least python {0}.{1} to run this script'.format(*REQ_VER), file=sys.stderr)
16 from subprocess import Popen, PIPE
17 from threading import Thread
18 from urllib.parse import urlparse
19 from urllib.request import urlretrieve
20 from shutil import rmtree
22 class ExtendedEnvBuilder(venv.EnvBuilder):
24 This builder installs setuptools and pip so that you can pip or
25 easy_install other packages into the created environment.
28 def __init__(self, *args, **kwargs):
29 self.verbose = kwargs.pop('verbose', False)
30 super().__init__(*args, **kwargs)
32 def post_setup(self, context):
34 Set up any packages which need to be pre-installed into the
35 environment being created.
37 :param context: The information for the environment creation request
40 os.environ['VIRTUAL_ENV'] = context.env_dir
41 setup = os.path.abspath(os.path.join(context.env_dir, '../setup.py'))
42 self.install_script(context, 'sima', setup=setup)
44 def reader(self, stream, context):
46 Read lines from a subprocess' output stream and write progress
47 information to sys.stderr.
56 sys.stderr.write(s.decode('utf-8'))
60 def install_script(self, context, name, url=None, setup=None):
62 binpath = context.bin_path
63 _, _, path, _, _, _ = urlparse(url)
64 fn = os.path.split(path)[-1]
65 distpath = os.path.join(binpath, fn)
66 # Download script into the env's binaries folder
67 urlretrieve(url, distpath)
69 args = [context.env_exe, fn]
71 args = [context.env_exe, setup, 'install']
72 binpath = os.path.dirname(setup)
77 sys.stderr.write('Installing %s ...%s' % (name, term))
80 p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
81 t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
83 t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
88 sys.stderr.write('done.\n')
90 # Clean up - no longer needed
95 root = os.path.dirname(os.path.abspath(__file__))
96 vdir = os.path.join(root, 'venv')
97 builder = ExtendedEnvBuilder(clear=True, verbose=False, with_pip=True)
100 for residu in ['MPD_sima.egg-info', 'dist', 'build']:
101 if os.path.exists(os.path.join(root, residu)):
102 rmtree(os.path.join(root, residu))
104 with open(os.path.join(root, 'vmpd-sima'),'w') as fd:
105 fd.write('#!/bin/sh\n')
106 fd.write(f'. "{root}/venv/bin/activate"\n')
107 fd.write(f'"{root}/venv/bin/mpd-sima" "$@"')
108 os.chmod(os.path.join(root, 'vmpd-sima'), 0o744)
111 if __name__ == '__main__':
116 except ImportError as e: