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