Skip to content

Commit 64865df

Browse files
author
stonebig
committed
move sourcefile patching in utils
1 parent 5c58abb commit 64865df

2 files changed

Lines changed: 21 additions & 13 deletions

File tree

winpython/utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,23 @@ def patch_shebang_line(fname, pad=b' '):
306306
print("failed to patch", fname)
307307

308308

309+
# =============================================================================
310+
# Patch sourcefile (instead of forking packages)
311+
# =============================================================================
312+
def patch_sourcefile(fname, in_text, out_text, silent_mode=False):
313+
"""Replace a string in a source file"""
314+
import io
315+
if osp.isfile(fname) and not in_text == out_text:
316+
with io.open(fname, 'r') as fh:
317+
content = fh.read()
318+
new_content = content.replace(in_text, out_text)
319+
if not new_content == content:
320+
if not silent_mode:
321+
print("patching " , fname, "from", in_text, "to", out_text)
322+
with io.open(fname, 'wt') as fh:
323+
fh.write(new_content)
324+
325+
309326
# =============================================================================
310327
# Extract functions
311328
# =============================================================================

winpython/wppm.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,11 @@ def install(self, package, install_options=None):
390390
import glob
391391
for ffname in glob.glob(r'%s\Scripts\*.exe' % self.target):
392392
utils.patch_shebang_line(ffname)
393-
do_replace = self.target + (r"\Lib\site-packages\pip\_vendor" +
394-
r"\distlib\scripts.py" )
395-
print("do_replace" , do_replace)
396-
fh = open(do_replace,"r")
397-
the_thing = fh.read()
398-
fh.close()
399-
the_thing_after = the_thing.replace(
393+
utils.patch_sourcefile(
394+
self.target + (
395+
r"\Lib\site-packages\pip\_vendor\distlib\scripts.py"),
400396
" executable = get_executable()",
401-
" executable = os.path.join(os.path.basename(get_executable()))")
402-
if not the_thing_after == the_thing:
403-
print("do_replace_ok" , do_replace)
404-
fh = open(do_replace,"w")
405-
fh.write(the_thing_after)
406-
fh.close()
397+
" executable = os.path.join(os.path.basename(get_executable()))")
407398

408399
def handle_specific_packages(self, package):
409400
"""Packages requiring additional configuration"""

0 commit comments

Comments
 (0)