Rely on universal newline mode on python 3 for windows (#6866)

This commit is contained in:
Adrien Ferrand
2019-04-01 09:50:08 -07:00
committed by Brad Warren
parent ea568d4dc2
commit 232e0ea50f
2 changed files with 13 additions and 16 deletions
+13 -6
View File
@@ -4,17 +4,18 @@ import glob
import logging
import os
import shutil
import sys
import time
import traceback
import six
import zope.component
from certbot.compat import misc
from certbot import constants
from certbot import errors
from certbot import interfaces
from certbot import util
from certbot.compat import misc
logger = logging.getLogger(__name__)
@@ -237,7 +238,7 @@ class Reverter(object):
try:
shutil.copy2(filename, os.path.join(
cp_dir, os.path.basename(filename) + "_" + str(idx)))
op_fd.write(filename + os.linesep)
op_fd.write('{0}\n'.format(filename))
# http://stackoverflow.com/questions/4726260/effective-use-of-python-shutil-copy2
except IOError:
op_fd.close()
@@ -312,7 +313,10 @@ class Reverter(object):
"""Run all commands in a file."""
# NOTE: csv module uses native strings. That is, bytes on Python 2 and
# unicode on Python 3
with open(filepath, 'r') as csvfile:
# It is strongly advised to set newline = '' on Python 3 with CSV,
# and it fixes problems on Windows.
kwargs = {'newline': ''} if sys.version_info[0] > 2 else {}
with open(filepath, 'r', **kwargs) as csvfile: # type: ignore # pylint: disable=star-args
csvreader = csv.reader(csvfile)
for command in reversed(list(csvreader)):
try:
@@ -381,7 +385,7 @@ class Reverter(object):
for path in files:
if path not in ex_files:
new_fd.write("{0}{1}".format(path, os.linesep))
new_fd.write("{0}\n".format(path))
except (IOError, OSError):
logger.error("Unable to register file creation(s) - %s", files)
raise errors.ReverterError(
@@ -408,11 +412,14 @@ class Reverter(object):
"""
commands_fp = os.path.join(self._get_cp_dir(temporary), "COMMANDS")
command_file = None
# It is strongly advised to set newline = '' on Python 3 with CSV,
# and it fixes problems on Windows.
kwargs = {'newline': ''} if sys.version_info[0] > 2 else {}
try:
if os.path.isfile(commands_fp):
command_file = open(commands_fp, "a")
command_file = open(commands_fp, "a", **kwargs) # type: ignore # pylint: disable=star-args
else:
command_file = open(commands_fp, "w")
command_file = open(commands_fp, "w", **kwargs) # type: ignore # pylint: disable=star-args
csvwriter = csv.writer(command_file)
csvwriter.writerow(command)