File tree Expand file tree Collapse file tree 3 files changed +34
-2
lines changed Expand file tree Collapse file tree 3 files changed +34
-2
lines changed 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
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 (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 33import os
44import re
55import subprocess
6+ import chardet
67
78from shutil import copystat , copy2
89
@@ -70,6 +71,27 @@ def chdir(path):
7071 os .chdir (old_dir )
7172
7273
74+ @contextmanager
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 ()
81+ with open (path , "rb" ) as f :
82+ for line in f .readlines ():
83+ detector .feed (line )
84+ if detector .done :
85+ break
86+ detector .close ()
87+
88+ file = open (path , encoding = detector .result ["encoding" ])
89+ try :
90+ yield file
91+ finally :
92+ file .close ()
93+
94+
7395def validate_and_generate_port_mapping (port_mappings ):
7496 """
7597 Validate a list of port mappings and return a dictionary of port mappings.
Original file line number Diff line number Diff line change 66from repo2docker import utils
77import pytest
88import subprocess
9+ import tempfile
910
1011
1112def test_capture_cmd_no_capture_success ():
@@ -112,6 +113,15 @@ def test_normalize_doi():
112113 assert utils .normalize_doi ("http://dx.doi.org/10.1234/jshd123" ) == "10.1234/jshd123"
113114
114115
116+ def test_open_guess_encoding ():
117+ data = "Rică nu știa să zică râu, rățușcă, rămurică."
118+ with tempfile .NamedTemporaryFile (mode = "wb" ) as test_file :
119+ test_file .write (str .encode (data , "utf-16" ))
120+ test_file .seek (0 )
121+ with utils .open_guess_encoding (test_file .name ) as fd :
122+ assert fd .read () == data
123+
124+
115125@pytest .mark .parametrize (
116126 "req, is_local" ,
117127 [
You can’t perform that action at this time.
0 commit comments