swc-windows-installer.py: Only create the python-scripts dir if it doesn't exist
[swc-setup-windows-installer.git] / swc-windows-installer.py
1 #!/usr/bin/env python
2
3 """Software Carpentry Windows Installer
4
5 Helps mimic a *nix environment on Windows with as little work as possible.
6
7 The script:
8 * Installs nano and makes it accessible from msysgit
9 * Provides standard nosetests behavior for msysgit
10
11 To use:
12
13 1. Install Python, IPython, and Nose.  An easy way to do this is with
14    the Anaconda CE Python distribution
15    http://continuum.io/anacondace.html
16 2. Install msysgit
17    http://code.google.com/p/msysgit/downloads/list?q=full+installer+official+git
18 3. Run swc_windows_installer.py
19    You should be able to simply double click the file in Windows
20
21 """
22
23 import hashlib
24 try:  # Python 3
25     from io import BytesIO as _BytesIO
26 except ImportError:  # Python 2
27     from StringIO import StringIO as _BytesIO
28 import os
29 try:  # Python 3
30     from urllib.request import urlopen as _urlopen
31 except ImportError:  # Python 2
32     from urllib2 import urlopen as _urlopen
33 import zipfile
34
35
36 def zip_install(url, sha1, install_directory):
37     """Download and install a zipped bundle of compiled software"""
38     r = _urlopen(url)
39     zip_bytes = r.read()
40     download_sha1 = hashlib.sha1(zip_bytes).hexdigest()
41     if download_sha1 != sha1:
42         raise ValueError(
43             'downloaded {!r} has the wrong SHA1 hash: {} != {}'.format(
44                 url, downloaded_sha1, sha1))
45     zip_io = _BytesIO(zip_bytes)
46     zip_file = zipfile.ZipFile(zip_io)
47     if not os.path.isdir(install_directory):
48         os.makedirs(install_directory)
49         zip_file.extractall(install_directory)
50
51
52 def install_nano(install_directory):
53     """Download and install the nano text editor"""
54     zip_install(
55         url='http://www.nano-editor.org/dist/v2.2/NT/nano-2.2.6.zip',
56         sha1='f5348208158157060de0a4df339401f36250fe5b',
57         install_directory=install_directory)
58
59
60 def create_nosetests_entry_point(python_scripts_directory):
61     """Creates a terminal-based nosetests entry point for msysgit"""
62     contents = '\n'.join([
63             '#!/usr/bin/env/ python',
64             'import sys',
65             'import nose',
66             "if __name__ == '__main__':",
67             '    sys.exit(nose.core.main())',
68             '',
69             ])
70     if not os.path.isdir(python_scripts_directory):
71         os.makedirs(python_scripts_directory)
72     with open(os.path.join(python_scripts_directory, 'nosetests'), 'w') as f:
73         f.write(contents)
74
75
76 def update_bash_profile(extra_paths=()):
77     """Create or append to a .bash_profile for Software Carpentry
78
79     Adds nano to the path, sets the default editor to nano, and adds
80     additional paths for other executables.
81     """
82     lines = [
83         '',
84         '# Add paths for Software-Carpentry-installed scripts and executables',
85         'export PATH=$PATH:{}'.format(':'.join(
86             make_posix_path(path) for path in extra_paths),),
87         '',
88         '# Make nano the default editor',
89         'export EDITOR=nano',
90         '',
91         ]
92     config_path = os.path.join(os.path.expanduser('~'), '.bash_profile')
93     with open(config_path, 'a') as f:
94         f.write('\n'.join(lines))
95
96 def make_posix_path(windows_path):
97     """Convert a Windows path to a posix path"""
98     return windows_path.replace('\\', '/').replace('C:', '/c')
99
100
101 def main():
102     swc_dir = os.path.join(os.path.expanduser('~'), '.swc')
103     bin_dir = os.path.join(swc_dir, 'bin')
104     create_nosetests_entry_point(python_scripts_directory=bin_dir)
105     nano_dir = os.path.join(swc_dir, 'lib', 'nano')
106     install_nano(install_directory=nano_dir)
107     update_bash_profile(extra_paths=(nano_dir, bin_dir))
108
109
110 if __name__ == '__main__':
111     main()