This repository has been archived by the owner on May 19, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
swc-windows-installer.py
executable file
·327 lines (279 loc) · 11.1 KB
/
swc-windows-installer.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python
"""Software Carpentry Windows Installer
Helps mimic a *nix environment on Windows with as little work as possible.
The script:
* Installs GNU Make and makes it accessible from msysGit
* Installs nano and makes it accessible from msysGit
* Installs SQLite and makes it accessible from msysGit
* Creates a ~/nano.rc with links to syntax highlighting configs
* Provides standard nosetests behavior for msysGit
* Adds R's bin directory to the path (if we can find it)
To use:
1. Install Python, IPython, and Nose. An easy way to do this is with
the Anaconda Python distribution
http://continuum.io/downloads
2. Install msysGit
https://github.com/msysgit/msysgit/releases
3. Install R (if your workshop uses R)
http://cran.r-project.org/bin/windows/base/rw-FAQ.html#Installation-and-Usage
4. Run swc-windows-installer.py.
You should be able to simply double click the file in Windows
"""
import glob
import hashlib
try: # Python 3
from io import BytesIO as _BytesIO
except ImportError: # Python 2
from StringIO import StringIO as _BytesIO
import logging
import os
import re
import sys
import tarfile
try: # Python 3
from urllib.request import urlopen as _urlopen
except ImportError: # Python 2
from urllib2 import urlopen as _urlopen
import zipfile
__version__ = '0.3'
LOG = logging.getLogger('swc-windows-installer')
LOG.addHandler(logging.StreamHandler())
LOG.setLevel(logging.INFO)
if sys.version_info >= (3, 0): # Python 3
_MakeDirsError = OSError
open3 = open
else:
_MakeDirsError = os.error
def open3(file, mode='r', newline=None):
if newline:
if newline != '\n':
raise NotImplementedError(newline)
f = open(file, mode + 'b')
else:
f = open(file, mode)
return f
def download(url, sha1):
"""Download a file and verify its hash"""
LOG.debug('download {}'.format(url))
r = _urlopen(url)
byte_content = r.read()
download_sha1 = hashlib.sha1(byte_content).hexdigest()
if download_sha1 != sha1:
raise ValueError(
'downloaded {!r} has the wrong SHA-1 hash: {} != {}'.format(
url, download_sha1, sha1))
LOG.debug('SHA-1 for {} matches the expected {}'.format(url, sha1))
return byte_content
def splitall(path):
"""Split a path into a list of components
>>> splitall('nano-2.2.6/doc/Makefile.am')
['nano-2.2.6', 'doc', 'Makefile.am']
"""
parts = []
while True:
head, tail = os.path.split(path)
if tail:
parts.insert(0, tail)
elif head:
parts.insert(0, head)
break
else:
break
path = head
return parts
def transform(tarinfo, strip_components=0):
"""Transform TarInfo objects for extraction"""
path_components = splitall(tarinfo.name)
try:
tarinfo.name = os.path.join(*path_components[strip_components:])
except TypeError:
if len(path_components) <= strip_components:
return None
raise
return tarinfo
def tar_install(url, sha1, install_directory, compression='*',
strip_components=0):
"""Download and install a tar bundle"""
if not os.path.isdir(install_directory):
tar_bytes = download(url=url, sha1=sha1)
tar_io = _BytesIO(tar_bytes)
filename = os.path.basename(url)
mode = 'r:{}'.format(compression)
tar_file = tarfile.open(filename, mode, tar_io)
LOG.info('installing {} into {}'.format(url, install_directory))
os.makedirs(install_directory)
members = [
transform(tarinfo=tarinfo, strip_components=strip_components)
for tarinfo in tar_file]
tar_file.extractall(
path=install_directory,
members=[m for m in members if m is not None])
else:
LOG.info('existing installation at {}'.format(install_directory))
def zip_install(url, sha1, install_directory, path=None):
"""Download and install a zipped bundle"""
if path is None:
path = install_directory
if not os.path.exists(path):
zip_bytes = download(url=url, sha1=sha1)
zip_io = _BytesIO(zip_bytes)
zip_file = zipfile.ZipFile(zip_io)
LOG.info('installing {} into {}'.format(url, install_directory))
try:
os.makedirs(install_directory)
except _MakeDirsError:
pass
zip_file.extractall(install_directory)
else:
LOG.info('existing installation at {}'.format(install_directory))
def install_make(install_directory):
"""Download and install GNU Make"""
zip_install(
url='http://downloads.sourceforge.net/project/gnuwin32/make/3.81/make-3.81-bin.zip',
sha1='7c1e23a93e6cb78975f36efd22d598241b1f0e8b',
install_directory=install_directory,
path=os.path.join(install_directory, 'bin', 'make.exe'))
zip_install(
url='http://downloads.sourceforge.net/project/gnuwin32/make/3.81/make-3.81-dep.zip',
sha1='ee90e45c1bacc24a0c3852a95fc6dcfbcabe802b',
install_directory=install_directory,
path=os.path.join(install_directory, 'bin', 'libiconv2.dll'))
def install_nano(install_directory):
"""Download and install the nano text editor"""
zip_install(
url='http://www.nano-editor.org/dist/v2.2/NT/nano-2.2.6.zip',
sha1='f5348208158157060de0a4df339401f36250fe5b',
install_directory=install_directory)
def install_nanorc(install_directory):
"""Download and install nano syntax highlighting"""
tar_install(
url='http://www.nano-editor.org/dist/v2.2/nano-2.2.6.tar.gz',
sha1='f2a628394f8dda1b9f28c7e7b89ccb9a6dbd302a',
install_directory=install_directory,
strip_components=1)
home = os.path.expanduser('~')
nanorc = os.path.join(home, 'nano.rc')
if not os.path.isfile(nanorc):
syntax_dir = os.path.join(install_directory, 'doc', 'syntax')
LOG.info('include nanorc from {} in {}'.format(syntax_dir, nanorc))
with open3(nanorc, 'w', newline='\n') as f:
for filename in os.listdir(syntax_dir):
if filename.endswith('.nanorc'):
path = os.path.join(syntax_dir, filename)
rel_path = os.path.relpath(path, home)
include_path = make_posix_path(os.path.join('~', rel_path))
f.write('include {}\n'.format(include_path))
def install_sqlite(install_directory):
"""Download and install the SQLite shell"""
zip_install(
url='https://sqlite.org/2015/sqlite-shell-win32-x86-3090200.zip',
sha1='25d78bbba37d2a0d9b9f86ed897e454ccc94d7b2',
install_directory=install_directory)
def create_nosetests_entry_point(python_scripts_directory):
"""Creates a terminal-based nosetests entry point for msysGit"""
contents = '\n'.join([
'#!/usr/bin/env python',
'import sys',
'import nose',
"if __name__ == '__main__':",
' sys.exit(nose.core.main())',
'',
])
if not os.path.isdir(python_scripts_directory):
os.makedirs(python_scripts_directory)
path = os.path.join(python_scripts_directory, 'nosetests')
LOG.info('create nosetests entrypoint {}'.format(path))
with open(path, 'w') as f:
f.write(contents)
def get_r_bin_directory():
"""Locate the R bin directory (if R is installed)
"""
version_re = re.compile('^R-(\d+)[.](\d+)[.](\d+)$')
paths = {}
for pf in [
os.environ.get('ProgramW6432', r'c:\Program Files'),
os.environ.get('ProgramFiles', r'c:\Program Files'),
os.environ.get('ProgramFiles(x86)', r'c:\Program Files(x86)'),
]:
bin_glob = os.path.join(pf, 'R', 'R-[0-9]*.[0-9]*.[0-9]*', 'bin')
for path in glob.glob(bin_glob):
version_dir = os.path.basename(os.path.dirname(path))
version_match = version_re.match(version_dir)
if version_match and version_match.groups() not in paths:
paths[version_match.groups()] = path
if not paths:
LOG.info('no R installation found under {}'.format(pf))
return
LOG.debug('detected R installs:\n* {}'.format('\n* '.join([
v for k,v in sorted(paths.items())])))
version = sorted(paths.keys())[-1]
LOG.info('using R v{} bin directory at {}'.format(
'.'.join(version), paths[version]))
return paths[version]
def update_bash_profile(extra_paths=()):
"""Create or append to a .bash_profile for Software Carpentry
Adds nano to the path, sets the default editor to nano, and adds
additional paths for other executables.
"""
lines = [
'',
'# Add paths for Software-Carpentry-installed scripts and executables',
'export PATH=\"$PATH:{}\"'.format(':'.join(
make_posix_path(path) for path in extra_paths),),
'',
'# Make nano the default editor',
'export EDITOR=nano',
'',
]
config_path = os.path.join(os.path.expanduser('~'), '.bash_profile')
LOG.info('update bash profile at {}'.format(config_path))
LOG.debug('extra paths:\n* {}'.format('\n* '.join(extra_paths)))
with open(config_path, 'a') as f:
f.write('\n'.join(lines))
def make_posix_path(windows_path):
"""Convert a Windows path to a posix path"""
for regex, sub in [
(re.compile(r'\\'), '/'),
(re.compile('^[Cc]:'), '/c'),
]:
windows_path = regex.sub(sub, windows_path)
return windows_path
def main():
swc_dir = os.path.join(os.path.expanduser('~'), '.swc')
bin_dir = os.path.join(swc_dir, 'bin')
make_dir = os.path.join(swc_dir, 'opt', 'make')
make_bin = os.path.join(make_dir, 'bin')
nano_dir = os.path.join(swc_dir, 'opt', 'nano')
nanorc_dir = os.path.join(swc_dir, 'share', 'nanorc')
sqlite_dir = os.path.join(swc_dir, 'opt', 'sqlite')
create_nosetests_entry_point(python_scripts_directory=bin_dir)
install_make(install_directory=make_dir)
install_nano(install_directory=nano_dir)
install_nanorc(install_directory=nanorc_dir)
install_sqlite(install_directory=sqlite_dir)
paths = [make_bin, nano_dir, sqlite_dir, bin_dir]
r_dir = get_r_bin_directory()
if r_dir:
paths.append(r_dir)
update_bash_profile(extra_paths=paths)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument(
'-v', '--verbose',
choices=['critical', 'error', 'warning', 'info', 'debug'],
help='Verbosity (defaults to {!r})'.format(
logging.getLevelName(LOG.level).lower()))
parser.add_argument(
'--version', action='version',
version='%(prog)s {}'.format(__version__))
args = parser.parse_args()
if args.verbose:
level = getattr(logging, args.verbose.upper())
LOG.setLevel(level)
LOG.info('Preparing your Software Carpentry awesomeness!')
LOG.info('installer version {}'.format(__version__))
main()
LOG.info('Installation complete.')