Skip to content

Commit 95fd0e5

Browse files
Don't change file encoding
1 parent 3752fa1 commit 95fd0e5

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

repo2docker/buildpacks/conda/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from ruamel.yaml import YAML
77

88
from ..base import BaseImage
9-
from ...utils import is_local_pip_requirement, open_utf8convert_read
9+
from ...utils import is_local_pip_requirement
1010

1111
# pattern for parsing conda dependency line
1212
PYTHON_REGEX = re.compile(r"python\s*=+\s*([\d\.]*)")
@@ -140,7 +140,7 @@ def environment_yaml(self):
140140
self._environment_yaml = {}
141141
return self._environment_yaml
142142

143-
with open_utf8convert_read(environment_yml) as f:
143+
with open(environment_yml) as f:
144144
env = YAML().load(f)
145145
# check if the env file is empty, if so instantiate an empty dictionary.
146146
if env is None:

repo2docker/buildpacks/python/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import os
33

44
from ..conda import CondaBuildPack
5-
from ...utils import is_local_pip_requirement, open_utf8convert_read
5+
from ...utils import is_local_pip_requirement, open_guess_encoding
66

77

88
class PythonBuildPack(CondaBuildPack):
@@ -86,7 +86,7 @@ def _should_preassemble_pip(self):
8686
requirements_txt = self.binder_path(name)
8787
if not os.path.exists(requirements_txt):
8888
continue
89-
with open_utf8convert_read(requirements_txt) as f:
89+
with open_guess_encoding(requirements_txt) as f:
9090
for line in f:
9191
if is_local_pip_requirement(line):
9292
return False

repo2docker/utils.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,23 @@ def chdir(path):
7070
finally:
7171
os.chdir(old_dir)
7272

73+
7374
@contextmanager
74-
def open_utf8convert_read(path):
75+
def open_guess_encoding(path):
76+
"""
77+
Open a file in text mode, specifying its encoding,
78+
that we guess using chardet.
79+
"""
80+
detector = chardet.universaldetector.UniversalDetector()
7581
with open(path, "rb") as f:
76-
file_to_encode = f.read()
77-
78-
encoding_detection_result = chardet.detect(file_to_encode)
79-
if not "utf-8" in encoding_detection_result:
80-
with open(path, "wb") as f:
81-
f.write(file_to_encode.decode(encoding_detection_result["encoding"]).encode("utf-8"))
82-
83-
file = open(path)
82+
for line in f.readlines():
83+
detector.feed(line)
84+
print(str(i) + str(detector.done))
85+
if detector.done:
86+
break
87+
detector.close()
88+
89+
file = open(path, encoding=detector.result["encoding"])
8490
try:
8591
yield file
8692
finally:

0 commit comments

Comments
 (0)