@@ -394,29 +394,31 @@ def guess_encoding(csv_file):
394394 except :
395395 return [locale .getdefaultlocale ()[1 ], "utf-8" ]
396396
397+ def replace_in_file (filepath : Path , replacements : list [tuple [str , str ]], filedest : Path = None , verbose = False ):
398+ """
399+ Replaces strings in a file
400+ Args:
401+ filepath: Path to the file to modify.
402+ replacements: A list of tuples of ('old string 'new string')
403+ filedest: optional output file, otherwise will be filepath
404+ """
405+ the_encoding = guess_encoding (filepath )[0 ]
406+ with open (filepath , "r" , encoding = the_encoding ) as f :
407+ content = f .read ()
408+ new_content = content
409+ for old_text , new_text in replacements :
410+ new_content = new_content .replace (old_text , new_text )
411+ outfile = filedest if filedest else filepath
412+ if new_content != content or str (outfile ) != str (filepath ):
413+ with open (outfile , "w" , encoding = the_encoding ) as f :
414+ f .write (new_content )
415+ if verbose :
416+ print (f"patched { filepath } into { outfile } !" )
397417
398418def patch_sourcefile (fname , in_text , out_text , silent_mode = False ):
399419 """Replace a string in a source file"""
400- import io
401-
402420 if Path (fname ).is_file () and not in_text == out_text :
403- the_encoding = guess_encoding (fname )[0 ]
404- with io .open (fname , 'r' , encoding = the_encoding ) as fh :
405- content = fh .read ()
406- new_content = content .replace (in_text , out_text )
407- if not new_content == content :
408- if not silent_mode :
409- print (
410- "patching " ,
411- fname ,
412- "from" ,
413- in_text ,
414- "to" ,
415- out_text ,
416- )
417- with io .open (fname , 'wt' , encoding = the_encoding ) as fh :
418- fh .write (new_content )
419-
421+ replace_in_file (Path (fname ), [(in_text , out_text )], verbose = True )
420422
421423def _create_temp_dir ():
422424 """Create a temporary directory and remove it at exit"""
@@ -480,8 +482,7 @@ def buildflit_wininst(
480482 copy_to = None ,
481483 verbose = False ,
482484):
483- """Build Wheel from Python package located in *root*
484- with flit"""
485+ """Build Wheel from Python package located in *root*with flit"""
485486 if python_exe is None :
486487 python_exe = sys .executable
487488 assert Path (python_exe ).is_file ()
@@ -531,16 +532,7 @@ def buildflit_wininst(
531532 dst_fname = str (Path (copy_to ) / distname )
532533 shutil .move (src_fname , dst_fname )
533534 if verbose :
534- print (
535- (
536- f"Move: { src_fname } --> { dst_fname } "
537- )
538- )
539- # remove tempo dir 'root' no more needed
540- #try:
541- # shutil.rmtree(root, onexc=onerror)
542- #except TypeError: # before 3.12
543- # shutil.rmtree(root, onerror=onerror)
535+ print (f"Move: { src_fname } --> { dst_fname } " )
544536 return dst_fname
545537
546538
0 commit comments