]> kaliko git repositories - mpd-sima.git/blob - vinstall.py
Remove bad heuristic to infer artist aliases
[mpd-sima.git] / vinstall.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2013 Vinay Sajip. New BSD License.
3 # Copyright (C) 2014, 2020 kaliko <kaliko@azylum.org>
4 #
5
6 REQ_VER = (3,4)
7 import sys
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)
10     sys.exit(1)
11
12 import os
13 import os.path
14 import venv
15
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
21
22 class ExtendedEnvBuilder(venv.EnvBuilder):
23     """
24     This builder installs setuptools and pip so that you can pip or
25     easy_install other packages into the created environment.
26     """
27
28     def __init__(self, *args, **kwargs):
29         self.verbose = kwargs.pop('verbose', False)
30         super().__init__(*args, **kwargs)
31
32     def post_setup(self, context):
33         """
34         Set up any packages which need to be pre-installed into the
35         environment being created.
36
37         :param context: The information for the environment creation request
38                         being processed.
39         """
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)
43
44     def reader(self, stream, context):
45         """
46         Read lines from a subprocess' output stream and write progress
47         information to sys.stderr.
48         """
49         while True:
50             s = stream.readline()
51             if not s:
52                 break
53             if not self.verbose:
54                 sys.stderr.write('.')
55             else:
56                 sys.stderr.write(s.decode('utf-8'))
57             sys.stderr.flush()
58         stream.close()
59
60     def install_script(self, context, name, url=None, setup=None):
61         if url:
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)
68         if url:
69             args = [context.env_exe, fn]
70         else:
71             args = [context.env_exe, setup, 'install']
72             binpath = os.path.dirname(setup)
73         if self.verbose:
74             term = '\n'
75         else:
76             term = ''
77         sys.stderr.write('Installing %s ...%s' % (name, term))
78         sys.stderr.flush()
79         # Install in the env
80         p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
81         t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
82         t1.start()
83         t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
84         t2.start()
85         p.wait()
86         t1.join()
87         t2.join()
88         sys.stderr.write('done.\n')
89         if url:
90             # Clean up - no longer needed
91             os.unlink(distpath)
92
93
94 def main(args=None):
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)
98     builder.create(vdir)
99     # clean up
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))
103     # Write wrapper
104     with open(os.path.join(root, 'vmpd-sima'),'w') as fd:
105         fd.write('#!/bin/sh\n')
106         fd.write('. "{}/venv/bin/activate"\n'.format(root))
107         fd.write('"{}/venv/bin/mpd-sima" "$@"'.format(root))
108     os.chmod(os.path.join(root, 'vmpd-sima'), 0o744)
109
110
111 if __name__ == '__main__':
112     rc = 1
113     try:
114         main()
115         rc = 0
116     except ImportError as e:
117         print('Error: %s' % e)
118     sys.exit(rc)