File tree Expand file tree Collapse file tree 3 files changed +19
-13
lines changed Expand file tree Collapse file tree 3 files changed +19
-13
lines changed Original file line number Diff line number Diff line change 66from ruamel .yaml import YAML
77
88from ..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
1212PYTHON_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 :
Original file line number Diff line number Diff line change 22import os
33
44from ..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
88class 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
Original file line number Diff line number Diff 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 :
You can’t perform that action at this time.
0 commit comments