Don't change file encoding
MODIFY
repo2docker/buildpacks/conda/__init__.py
repo2docker/buildpacks/conda/__init__.py
@@ -6,7 +6,7 @@ from collections import Mapping
from ruamel.yaml import YAML
from ..base import BaseImage
-from ...utils import is_local_pip_requirement, open_utf8convert_read
+from ...utils import is_local_pip_requirement
# pattern for parsing conda dependency line
PYTHON_REGEX = re.compile(r"python\s*=+\s*([\d\.]*)")
@@ -140,7 +140,7 @@ class CondaBuildPack(BaseImage):
self._environment_yaml = {}
return self._environment_yaml
- with open_utf8convert_read(environment_yml) as f:
+ with open(environment_yml) as f:
env = YAML().load(f)
# check if the env file is empty, if so instantiate an empty dictionary.
if env is None:

MODIFY
repo2docker/buildpacks/python/__init__.py
repo2docker/buildpacks/python/__init__.py
import os
from ..conda import CondaBuildPack
-from ...utils import is_local_pip_requirement, open_utf8convert_read
+from ...utils import is_local_pip_requirement, open_guess_encoding
class PythonBuildPack(CondaBuildPack):
@@ -86,7 +86,7 @@ class PythonBuildPack(CondaBuildPack):
requirements_txt = self.binder_path(name)
if not os.path.exists(requirements_txt):
continue
- with open_utf8convert_read(requirements_txt) as f:
+ with open_guess_encoding(requirements_txt) as f:
for line in f:
if is_local_pip_requirement(line):
return False

MODIFY
repo2docker/utils.py
repo2docker/utils.py
@@ -70,17 +70,23 @@ def chdir(path):
finally:
os.chdir(old_dir)
+
@contextmanager
-def open_utf8convert_read(path):
+def open_guess_encoding(path):
+ """
+ Open a file in text mode, specifying its encoding,
+ that we guess using chardet.
+ """
+ detector = chardet.universaldetector.UniversalDetector()
with open(path, "rb") as f:
- file_to_encode = f.read()
-
- encoding_detection_result = chardet.detect(file_to_encode)
- if not "utf-8" in encoding_detection_result:
- with open(path, "wb") as f:
- f.write(file_to_encode.decode(encoding_detection_result["encoding"]).encode("utf-8"))
-
- file = open(path)
+ for line in f.readlines():
+ detector.feed(line)
+ print(str(i) + str(detector.done))
+ if detector.done:
+ break
+ detector.close()
+
+ file = open(path, encoding=detector.result["encoding"])
try:
yield file
finally:

Don't change file encoding