From: W. Trevor King Date: Thu, 13 Mar 2014 15:19:25 +0000 (-0700) Subject: swc-windows-installer.py: Force UNIX-style \n line endings for ~/nano.rc X-Git-Tag: v0.1~15 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=21a58ad021e97a01d207db1f939a6575ab9819e5;p=swc-setup-windows-installer.git swc-windows-installer.py: Force UNIX-style \n line endings for ~/nano.rc On Thu, Mar 13, 2014 at 03:37:12AM -0700, Ethan White wrote [1]: > We now have a line ending issue resulting in: > > Error reading /cygdrive/c/Users/... > > This is fixed by running `dos2unix` on the resulting nano.rc. This is also mentioned in the nano FAQ [2]: > Note that the nano.rc file must remain Unix formated in order for > nano to unerstand it. Since Python 3.0 and PEP 3116, Python's open() has taken a 'newline' argument that allows you to override the default line separator for a particular file [3,4]. However, Python 2.x does not support that argument [5]. This commit adds a local 'open3' which uses a terrible hack of a wrapper on Python 2 to fake that support. It opens the file in binary mode, and takes advantage of Python 2's lack of distinction between str and bytes to treat the \n-terminated strings as the raw binary output. That's going to crash and burn if you have any non-ASCII characters in the output content, but we explicitly set all the components in our relative include_path, so that shouldn't be a problem. Another side effect of binary-mode is that we are going to get ASCII-encoded output, not output encoded in the user's preferred encoding (locale.getpreferredencoding [3,6]). That's unfortunate, because nano does the right thing and uses your locale to decode input (at least for UTF-8 locales [7], presumably including your nano.rc file). [1]: https://github.com/swcarpentry/bc/pull/357#issuecomment-37518899 [2]: http://www.nano-editor.org/dist/v2.2/faq.html#3.9.1 [3]: http://docs.python.org/3/library/functions.html#open [4]: http://legacy.python.org/dev/peps/pep-3116/ [5]: http://docs.python.org/2/library/functions.html#open [6]: http://docs.python.org/2/library/stdtypes.html#file.encoding [7]: http://www.nano-editor.org/dist/v2.2/faq.html#5.3 --- diff --git a/swc-windows-installer.py b/swc-windows-installer.py index 3fee893..b305138 100755 --- a/swc-windows-installer.py +++ b/swc-windows-installer.py @@ -28,6 +28,7 @@ except ImportError: # Python 2 from StringIO import StringIO as _BytesIO import os import re +import sys import tarfile try: # Python 3 from urllib.request import urlopen as _urlopen @@ -36,6 +37,19 @@ except ImportError: # Python 2 import zipfile +if sys.version_info >= (3, 0): # Python 3 + open3 = open +else: + 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 it's hash""" r = _urlopen(url) @@ -127,7 +141,7 @@ def install_nanorc(install_directory): nanorc = os.path.join(home, 'nano.rc') if not os.path.isfile(nanorc): syntax_dir = os.path.join(install_directory, 'doc', 'syntax') - with open(nanorc, 'w') as f: + 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)