f27aac1bf72632bc5d7cd58ea9e2b0b0298d2c26
[swc-setup-windows-installer.git] / setup / bin / swc-nano-installer.py
1 #!/usr/bin/env python
2
3 """Software Carpentry Nano Installer for Windows
4
5 Installs nano and makes it the default editor in msysgit
6
7 To use:
8
9 1. Install Python
10 2. Install msysgit
11    http://code.google.com/p/msysgit/downloads/list?q=full+installer+official+git
12 3. Run swc_nano_installer.py
13    You should be able to simply double click the file in Windows
14
15 This is a stripped down version of swc_windows_installer.py
16 originally written by Ethan White and W. Trevor Price.
17
18 """
19
20 try:  # Python 3
21     from io import BytesIO as _BytesIO
22 except ImportError:  # Python 2
23     from StringIO import StringIO as _BytesIO
24 import os
25 try:  # Python 3
26     from urllib.request import urlopen as _urlopen
27 except ImportError:  # Python 2
28     from urllib2 import urlopen as _urlopen
29 import zipfile
30
31
32 def install_nano(install_dir):
33     """Download and install the nano text editor"""
34     url = "http://www.nano-editor.org/dist/v2.2/NT/nano-2.2.6.zip"
35     r = _urlopen(url)
36     nano_zip_content = _BytesIO(r.read())
37     nano_zip = zipfile.ZipFile(nano_zip_content)
38     nano_files = ['nano.exe', 'cygwin1.dll', 'cygintl-8.dll',
39                   'cygiconv-2.dll', 'cyggcc_s-1.dll']
40     for file_name in nano_files:
41         nano_zip.extract(file_name, install_dir)
42
43 def make_bash_profile(home_dir, nano_dir):
44     """Creates a .bash_profile file for nano setup
45
46     Adds nano to the path and sets the default editor to nano
47
48     """
49
50     nano_path = make_posix_path(nano_dir)
51     contents = '\n'.join([
52         'export PATH=$PATH:%s' % nano_path,
53         'export EDITOR=nano',
54             '',
55             ])
56     with open(os.path.join(home_dir, '.bash_profile'), 'w') as f:
57         f.write(contents)
58
59 def make_posix_path(windows_path):
60     """Convert a Windows path to a posix path"""
61     return windows_path.replace('\\', '/').replace('C:', '/c')
62
63 def main():
64     home_dir = os.path.expanduser("~")
65     nano_dir = os.path.join(home_dir, '.nano')
66     #home_dir = "/home/ethan/swc-nano-test"
67     if not os.path.exists(nano_dir):
68         os.makedirs(nano_dir)
69     install_nano(nano_dir)
70     make_bash_profile(home_dir, nano_dir)
71
72 if __name__ == '__main__':
73     main()