-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathminiirc_bootstrap.py
More file actions
189 lines (155 loc) · 6.11 KB
/
miniirc_bootstrap.py
File metadata and controls
189 lines (155 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python3
#
# Bootstraps pip if required and installs miniirc.
# Note that this is not required if you already have and know how to use pip,
# simply run 'pip install miniirc' instead.
#
# © 2020 by luk3yx.
#
import importlib.util, os, shutil, subprocess, sys, tempfile, urllib.request
assert sys.version_info >= (3, 4)
# A horrible workaround for a partially existing distutils.
_distutils_usercustomize = """
# miniirc_bootstrap: Make user-provided packages load first.
# This can safely be removed if distutils has been properly installed.
import os, sys
dir = os.path.expanduser('~/.local/lib/python{}.{}/site-packages'.format(
*sys.version_info[:2]))
if dir in sys.path:
sys.path.remove(dir)
sys.path.insert(0, dir)
del dir
# End of miniirc_bootstrap changes
""".lstrip()
# Debian has decided to remove distutils from Python installs by default.
# TODO: Make less assumptions about the system.
def bootstrap_distutils():
"""
Bootstrap installs distutils on Debian systems. This is horrible and should
probably be avoided if possible.
"""
if (importlib.util.find_spec('distutils.util') is not None and
sys.version_info < (3, 12)):
return
print('[This should never happen] Downloading distutils...')
if sys.platform != 'linux' or not shutil.which('apt-get'):
raise NotImplementedError('Cannot bootstrap distutils on non-Debian '
'systems!')
partial_distutils = importlib.util.find_spec('distutils')
# Get paths
python = 'python{}.{}'.format(*sys.version_info[:2])
pkg = 'python{}-distutils'.format(sys.version_info[0])
local_lib = os.path.expanduser('~/.local/lib')
if not os.path.isdir(local_lib):
os.mkdir(local_lib)
local_python = os.path.join(local_lib, python)
if not os.path.isdir(local_python):
os.mkdir(local_python)
local_python = os.path.join(local_python, 'site-packages')
if not os.path.isdir(local_python):
os.mkdir(local_python)
with tempfile.TemporaryDirectory() as tmpdir:
# Download the package.
subprocess.check_call(('apt-get', 'download', pkg), cwd=tmpdir)
files = os.listdir(tmpdir)
assert len(files) == 1, 'Error downloading .deb!'
# Extract the downloaded .deb file.
print('[This should never happen] Installing distutils...')
subprocess.check_call(('dpkg-deb', '-x', files[0], tmpdir), cwd=tmpdir)
# Move distutils out of the extracted package.
f = os.path.join(tmpdir, 'usr', 'lib', python, 'distutils')
os.rename(f, os.path.join(local_python, 'distutils'))
if partial_distutils is not None:
# Symlink extra files.
old_distutils = os.path.dirname(partial_distutils.origin)
for fn in os.listdir(old_distutils):
if not fn.endswith('.py'):
continue
dst = os.path.join(local_python, 'distutils', fn)
if os.path.exists(dst):
continue
os.symlink(os.path.join(old_distutils, fn), dst)
# A horrible tweak to make user-provided packages load before system ones.
usercustomize = os.path.join(local_python, 'usercustomize.py')
print('[This should never happen] Adding {}...'.format(usercustomize))
with open(usercustomize, 'a') as f:
# If the file already contains data write a leading newline.
if f.tell():
f.write('\n')
# Write the custom distutils data.
f.write(_distutils_usercustomize)
print('[This should never happen] distutils should be installed!')
# Recommend the user installs distutils correctly if possible.
print(('If you have root access, please install {}, remove the '
'changes made in {!r}, and delete {!r}.').format(pkg, usercustomize,
os.path.join(local_python, 'distutils')), file=sys.stderr)
# Download a webpage
def wget(url, raw=False):
try:
with urllib.request.urlopen(url) as f:
if raw:
return f.read()
else:
return f.read().decode('utf-8', 'replace')
except urllib.request.HTTPError:
return ''
def bootstrap_pip():
"""
Bootstrap installs pip. This will print messages to stdout/stderr.
This is required because some versions of Ubuntu do not have pip or
ensurepip installed with Python by default.
"""
if importlib.util.find_spec('distutils.util') is None:
bootstrap_distutils()
print('Downloading pip...')
url = 'https://bootstrap.pypa.io/{}get-pip.py'
# If this machine is using an obsolete Python, download the
# version-specific one.
major, minor = sys.version_info[:2]
pip = wget(url.format('{}.{}/'.format(major, minor)), raw=True)
# Otherwise use the generic one.
if not pip:
pip = wget(url.format(''), raw=True)
assert pip, 'Error downloading pip!'
print('Installing pip...')
fd, filename = tempfile.mkstemp()
with open(fd, 'wb') as f:
f.write(pip)
del pip
subprocess.check_call((sys.executable, '--', filename, '--user'))
os.remove(filename)
print('pip (should be) installed!')
# Install a package
def pip_install(*pkgs, upgrade=False):
"""
Installs or upgrades packages using pip. `pip` will print to stdout/stderr.
This automatically calls bootstrap_pip() if required.
"""
args = [sys.executable, '-m', 'pip', 'install']
if upgrade:
args.append('--upgrade')
args.extend(('--user', '--'))
args.extend(pkgs)
try:
subprocess.check_call(args)
except subprocess.CalledProcessError:
if importlib.util.find_spec('pip') is not None:
raise
print('pip is (somehow) not installed!')
bootstrap_pip()
subprocess.check_call(args)
# Install miniirc
def main():
# Do nothing if arguments are specified.
import argparse
argparse.ArgumentParser().parse_args()
# Get miniirc
upgrade = True
try:
import miniirc
except ImportError:
upgrade = False
pip_install('miniirc', upgrade=upgrade)
print('miniirc (should be) installed!')
if __name__ == '__main__':
main()