Commits

Takayuki Shimizukawa  committed 4da88e1

fix: make.bat is always saved with 'LF' instead of 'CRLF' for both Win/Unix.

  • Participants
  • Parent commits 1e780c3

Comments (0)

Files changed (3)

File sphinx/quickstart.py

 
 import sys, os, time, re
 from os import path
-from codecs import open
 
 TERM_ENCODING = getattr(sys.stdin, 'encoding', None)
 
 from sphinx.util.console import purple, bold, red, turquoise, \
      nocolor, color_terminal
 from sphinx.util import texescape
+from sphinx.util.pycompat import open
 
 # function to get input from terminal -- overridden by the test suite
 try:
 
 if sys.version_info >= (3, 0):
     # prevents that the file is checked for being written in Python 2.x syntax
-    QUICKSTART_CONF = '#!/usr/bin/env python3\n'
+    QUICKSTART_CONF = u'#!/usr/bin/env python3\n'
 else:
-    QUICKSTART_CONF = ''
+    QUICKSTART_CONF = u''
 
-QUICKSTART_CONF += '''\
+QUICKSTART_CONF += u'''\
 # -*- coding: utf-8 -*-
 #
 # %(project)s documentation build configuration file, created by
 #texinfo_show_urls = 'footnote'
 '''
 
-EPUB_CONFIG = '''
+EPUB_CONFIG = u'''
 
 # -- Options for Epub output ---------------------------------------------------
 
 #epub_show_urls = 'inline'
 '''
 
-INTERSPHINX_CONFIG = '''
+INTERSPHINX_CONFIG = u'''
 
 # Example configuration for intersphinx: refer to the Python standard library.
 intersphinx_mapping = {'http://docs.python.org/': None}
 '''
 
-MASTER_FILE = '''\
+MASTER_FILE = u'''\
 .. %(project)s documentation master file, created by
    sphinx-quickstart on %(now)s.
    You can adapt this file completely to your liking, but it should at least
 
 '''
 
-MAKEFILE = '''\
+MAKEFILE = u'''\
 # Makefile for Sphinx documentation
 #
 
 \t@echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml."
 '''
 
-BATCHFILE = '''\
+BATCHFILE = u'''\
 @ECHO OFF
 
 REM Command file for Sphinx documentation
     mkdir_p(path.join(srcdir, d['dot'] + 'templates'))
     mkdir_p(path.join(srcdir, d['dot'] + 'static'))
 
-    def write_file(fpath, mode, content):
+    def write_file(fpath, content, newline=None):
         if overwrite or not path.isfile(fpath):
             print 'Creating file %s.' % fpath
-            f = open(fpath, mode, encoding='utf-8')
+            f = open(fpath, 'wt', encoding='utf-8', newline=newline)
             try:
                 f.write(content)
             finally:
     if d.get('ext_intersphinx'):
         conf_text += INTERSPHINX_CONFIG
 
-    write_file(path.join(srcdir, 'conf.py'), 'w', conf_text)
+    write_file(path.join(srcdir, 'conf.py'), conf_text)
 
     masterfile = path.join(srcdir, d['master'] + d['suffix'])
-    write_file(masterfile, 'w', MASTER_FILE % d)
+    write_file(masterfile, MASTER_FILE % d)
 
     if d['makefile']:
         d['rsrcdir'] = d['sep'] and 'source' or '.'
         d['rbuilddir'] = d['sep'] and 'build' or d['dot'] + 'build'
         # use binary mode, to avoid writing \r\n on Windows
-        write_file(path.join(d['path'], 'Makefile'), 'wb', MAKEFILE % d)
+        write_file(path.join(d['path'], 'Makefile'), MAKEFILE % d, u'\n')
 
     if d['batchfile']:
         d['rsrcdir'] = d['sep'] and 'source' or '.'
         d['rbuilddir'] = d['sep'] and 'build' or d['dot'] + 'build'
-        write_file(path.join(d['path'], 'make.bat'), 'w', BATCHFILE % d)
+        write_file(path.join(d['path'], 'make.bat'), BATCHFILE % d, u'\r\n')
 
     if silent:
         return

File sphinx/util/pycompat.py

     relpath = os.path.relpath
     del os
 
+    import io
+    open = io.open
+
 else:
     # Python < 2.6
     from itertools import izip, repeat, chain
         return join(*rel_list)
     del curdir
 
+    from types import MethodType
+    def open(filename, mode='r', *args, **kw):
+        newline = kw.pop('newline', None)
+        mode = mode.replace('t', '')
+        f = codecs.open(filename, mode, *args, **kw)
+        if newline is not None:
+            f._write = f.write
+            def write(self, text):
+                text = text.replace(u'\r\n', u'\n').replace(u'\n', newline)
+                self._write(text)
+            f.write = MethodType(write, f)
+        return f
+
 
 # ------------------------------------------------------------------------------
 # Missing builtins and codecs in Python < 2.5

File tests/test_quickstart.py

     assert (tempdir / 'source' / '.static').isdir()
     assert (tempdir / 'source' / '.templates').isdir()
     assert (tempdir / 'source' / 'contents.txt').isfile()
+
+
+@with_tempdir
+def test_generated_files_eol(tempdir):
+    answers = {
+        'Root path': tempdir,
+        'Project name': 'Sphinx Test',
+        'Author name': 'Georg Brandl',
+        'Project version': '0.1',
+    }
+    qs.term_input = mock_raw_input(answers)
+    d = {}
+    qs.ask_user(d)
+    qs.generate(d)
+
+    def assert_eol(filename, eol):
+        content = filename.bytes().decode('unicode-escape')
+        assert all([l[-len(eol):]==eol for l in content.splitlines(True)])
+
+    assert_eol(tempdir / 'make.bat', '\r\n')
+    assert_eol(tempdir / 'Makefile', '\n')