|
2 | 2 | from distutils import log
|
3 | 3 | from distutils.errors import DistutilsOptionError
|
4 | 4 | import distutils
|
| 5 | +import io |
5 | 6 | import os
|
| 7 | +from ..unicode_utils import detect_encoding |
6 | 8 |
|
| 9 | +from setuptools.extern import six |
7 | 10 | from setuptools.extern.six.moves import configparser
|
8 | 11 |
|
9 | 12 | from setuptools import Command
|
@@ -40,7 +43,17 @@ def edit_config(filename, settings, dry_run=False):
|
40 | 43 | """
|
41 | 44 | log.debug("Reading configuration from %s", filename)
|
42 | 45 | opts = configparser.RawConfigParser()
|
43 |
| - opts.read([filename]) |
| 46 | + try: |
| 47 | + with io.open(filename, 'rb') as fp: |
| 48 | + encoding = detect_encoding(fp) |
| 49 | + log.debug("Reading %s [%s]" % (filename, encoding or 'locale')) |
| 50 | + reader = io.TextIOWrapper(fp, encoding=encoding) |
| 51 | + (opts.read_file if six.PY3 else opts.readfp)(reader) |
| 52 | + except IOError: |
| 53 | + encoding = None |
| 54 | + except LookupError: |
| 55 | + encoding = None |
| 56 | + opts.read([filename]) |
44 | 57 | for section, options in settings.items():
|
45 | 58 | if options is None:
|
46 | 59 | log.info("Deleting section [%s] from %s", section, filename)
|
@@ -70,6 +83,8 @@ def edit_config(filename, settings, dry_run=False):
|
70 | 83 | log.info("Writing %s", filename)
|
71 | 84 | if not dry_run:
|
72 | 85 | with open(filename, 'w') as f:
|
| 86 | + if encoding: |
| 87 | + f.write("# coding: %s\n" % encoding) |
73 | 88 | opts.write(f)
|
74 | 89 |
|
75 | 90 |
|
|
0 commit comments