swc-windows-installer.py: Restore nose entry point
[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 try:  # Python 3
24     from io import BytesIO as _BytesIO
25 except ImportError:  # Python 2
26     from StringIO import StringIO as _BytesIO
27 import os
28 try:  # Python 3
29     from urllib.request import urlopen as _urlopen
30 except ImportError:  # Python 2
31     from urllib2 import urlopen as _urlopen
32 import zipfile
33
34
35 def install_nano(install_directory):
36     """Download and install the nano text editor"""
37     url = "http://www.nano-editor.org/dist/v2.2/NT/nano-2.2.6.zip"
38     r = _urlopen(url)
39     nano_zip_content = _BytesIO(r.read())
40     nano_zip = zipfile.ZipFile(nano_zip_content)
41     nano_files = ['nano.exe', 'cygwin1.dll', 'cygintl-8.dll',
42                   'cygiconv-2.dll', 'cyggcc_s-1.dll']
43     os.makedirs(install_directory)
44     for file_name in nano_files:
45         nano_zip.extract(file_name, install_directory)
46
47
48 def create_nosetests_entry_point(python_scripts_directory):
49     """Creates a terminal-based nosetests entry point for msysgit"""
50     contents = '\n'.join([
51             '#!/usr/bin/env/ python',
52             'import sys',
53             'import nose',
54             "if __name__ == '__main__':",
55             '    sys.exit(nose.core.main())',
56             '',
57             ])
58     with open(os.path.join(python_scripts_directory, 'nosetests'), 'w') as f:
59         f.write(contents)
60
61
62 def update_bash_profile(extra_paths=()):
63     """Create or append to a .bash_profile for Software Carpentry
64
65     Adds nano to the path, sets the default editor to nano, and adds
66     additional paths for other executables.
67     """
68     lines = [
69         '',
70         '# Add paths for Software-Carpentry-installed scripts and executables',
71         'export PATH=$PATH:{}'.format(':'.join(
72             make_posix_path(path) for path in extra_paths),),
73         '',
74         '# Make nano the default editor',
75         'export EDITOR=nano',
76         '',
77         ]
78     config_path = os.path.join(os.path.expanduser('~'), '.bash_profile')
79     with open(config_path, 'a') as f:
80         f.write('\n'.join(lines))
81
82 def make_posix_path(windows_path):
83     """Convert a Windows path to a posix path"""
84     return windows_path.replace('\\', '/').replace('C:', '/c')
85
86
87 def main():
88     swc_dir = os.path.join(os.path.expanduser('~'), '.swc')
89     bin_dir = os.path.join(swc_dir, 'bin')
90     create_nosetests_entry_point(python_scripts_directory=bin_dir)
91     nano_dir = os.path.join(swc_dir, 'lib', 'nano')
92     install_nano(installation_directory=nano_dir)
93     update_bash_profile(extra_paths=(nano_dir, bin_dir))
94
95
96 if __name__ == '__main__':
97     main()