@@ -413,12 +413,14 @@ def replace_in_file(filepath: Path, replacements: list[tuple[str, str]], filedes
413
413
with open (outfile , "w" , encoding = the_encoding ) as f :
414
414
f .write (new_content )
415
415
if verbose :
416
- print (f"patched { filepath } into { outfile } !" )
416
+ print (f"patched from { Path ( filepath ). name } into { outfile } !" )
417
417
418
418
def patch_sourcefile (fname , in_text , out_text , silent_mode = False ):
419
419
"""Replace a string in a source file"""
420
+ if not silent_mode :
421
+ print (f"patching { fname } from { in_text } to { out_text } " )
420
422
if Path (fname ).is_file () and not in_text == out_text :
421
- replace_in_file (Path (fname ), [(in_text , out_text )], verbose = True )
423
+ replace_in_file (Path (fname ), [(in_text , out_text )])
422
424
423
425
def _create_temp_dir ():
424
426
"""Create a temporary directory and remove it at exit"""
@@ -429,26 +431,18 @@ def _create_temp_dir():
429
431
)
430
432
return tmpdir
431
433
432
-
433
434
def extract_archive (fname , targetdir = None , verbose = False ):
434
435
"""Extract .zip, .exe (considered to be a zip archive) or .tar.gz archive
435
436
to a temporary directory (if targetdir is None).
436
437
Return the temporary directory path"""
437
- if targetdir is None :
438
- targetdir = _create_temp_dir ()
439
- else :
440
- try :
441
- Path (targetdir ).mkdir (parents = True , exist_ok = True )
442
- except :
443
- pass
438
+ targetdir = targetdir or create_temp_dir ()
439
+ Path (targetdir ).mkdir (parents = True , exist_ok = True )
444
440
if Path (fname ).suffix in ('.zip' , '.exe' ):
445
441
obj = zipfile .ZipFile (fname , mode = "r" )
446
442
elif fname .endswith ('.tar.gz' ):
447
443
obj = tarfile .open (fname , mode = 'r:gz' )
448
444
else :
449
- raise RuntimeError (
450
- f"Unsupported archive filename { fname } "
451
- )
445
+ raise RuntimeError (f"Unsupported archive filename { fname } " )
452
446
obj .extractall (path = targetdir )
453
447
return targetdir
454
448
@@ -468,13 +462,11 @@ def extract_archive(fname, targetdir=None, verbose=False):
468
462
469
463
470
464
def get_source_package_infos (fname ):
471
- """Return a tuple (name, version) of the Python source package"""
472
- if fname [ - 4 :] == '.whl' :
465
+ """Return a tuple (name, version) of the Python source package. """
466
+ if fname . endswith ( '.whl' ) :
473
467
return Path (fname ).name .split ("-" )[:2 ]
474
468
match = re .match (SOURCE_PATTERN , Path (fname ).name )
475
- if match is not None :
476
- return match .groups ()[:2 ]
477
-
469
+ return match .groups ()[:2 ] if match else None
478
470
479
471
def buildflit_wininst (
480
472
root ,
@@ -522,9 +514,8 @@ def buildflit_wininst(
522
514
if match is not None :
523
515
break
524
516
else :
525
- raise RuntimeError (
526
- f"Build failed: not a pure Python package? { distdir } "
527
- )
517
+ raise RuntimeError (f"Build failed: not a pure Python package? { distdir } " )
518
+
528
519
src_fname = str (Path (distdir ) / distname )
529
520
if copy_to is None :
530
521
return src_fname
@@ -583,16 +574,9 @@ def direct_pip_install(
583
574
return src_fname
584
575
585
576
586
- def do_script (
587
- this_script ,
588
- python_exe = None ,
589
- copy_to = None ,
590
- verbose = False ,
591
- install_options = None ,
592
- ):
593
- """Execute a script (get-pip typically)"""
594
- if python_exe is None :
595
- python_exe = sys .executable
577
+ def do_script (this_script , python_exe = None , copy_to = None , verbose = False , install_options = None ):
578
+ """Execute a script (get-pip typically)."""
579
+ python_exe = python_exe or sys .executable
596
580
myroot = os .path .dirname (python_exe )
597
581
598
582
# cmd = [python_exe, myroot + r'\Scripts\pip-script.py', 'install']
@@ -618,49 +602,36 @@ def do_script(
618
602
p .stdout .close ()
619
603
p .stderr .close ()
620
604
if verbose :
621
- print ("Executed " , cmd )
605
+ print ("Executed " , cmd )
622
606
return 'ok'
623
607
624
608
def columns_width (list_of_lists ):
625
- """return the maximum string length of each column of a list of list"""
626
- if not isinstance (list_of_lists , list ):
627
- return [0 ]
628
-
629
- # Transpose the list of lists using zip
630
- transposed_lists = list (zip (* list_of_lists ))
631
- # Calculate the maximum width for each column
632
- column_widths = [max (len (str (item )) for item in sublist ) for sublist in transposed_lists ]
633
- return column_widths
609
+ """Return the maximum string length of each column of a list of lists."""
610
+ if not isinstance (list_of_lists , list ):
611
+ return [0 ]
612
+ return [max (len (str (item )) for item in sublist ) for sublist in zip (* list_of_lists )]
634
613
635
614
def formatted_list (list_of_list , full = False , max_width = 70 ):
636
- """format a list_of_list to fix length columns"""
637
- columns_size = columns_width (list_of_list )
638
- nb_columns = len (columns_size )
639
-
640
- # normalize each columns to columns_size[col] width, in the limit of max_width
641
-
642
- zz = [
643
- list (
644
- line [col ].ljust (columns_size [col ])[:max_width ] for col in range (nb_columns )
645
- )
646
- for line in list_of_list
647
- ]
648
- return zz
615
+ """Format a list_of_list to fixed length columns."""
616
+ columns_size = columns_width (list_of_list )
617
+ columns = range (len (columns_size ))
618
+ return [list (line [col ].ljust (columns_size [col ])[:max_width ] for col in columns ) for line in list_of_list ]
649
619
650
620
def normalize (this ):
651
- """apply https://peps.python.org/pep-0503/#normalized-names """
621
+ """Apply PEP 503 normalization to the string. """
652
622
return re .sub (r"[-_.]+" , "-" , this ).lower ()
653
623
654
624
def get_package_metadata (database , name ):
655
- """Extract infos (description, url) from the local database"""
656
- DATA_PATH = str (Path (sys .modules ['winpython' ].__file__ ).parent / 'data' )
625
+ """Extract infos (description, url) from the local database. """
626
+ DATA_PATH = str (Path (sys .modules ['winpython' ].__file__ ).parent / 'data' )
657
627
db = cp .ConfigParser ()
658
628
filepath = Path (database ) if Path (database ).is_absolute () else Path (DATA_PATH ) / database
659
- db .read_file (open (str (filepath ), encoding = guess_encoding (filepath )[0 ]))
660
- my_metadata = dict (
661
- description = "" ,
662
- url = "https://pypi.org/project/" + name ,
663
- )
629
+ db .read_file (open (str (filepath ), encoding = guess_encoding (filepath )[0 ]))
630
+
631
+ my_metadata = {
632
+ "description" : "" ,
633
+ "url" : f"https://pypi.org/project/{ name } " ,
634
+ }
664
635
for key in my_metadata :
665
636
# wheel replace '-' per '_' in key
666
637
for name2 in (name , normalize (name )):
@@ -672,23 +643,11 @@ def get_package_metadata(database, name):
672
643
673
644
return my_metadata
674
645
675
-
676
646
if __name__ == '__main__' :
677
-
678
647
print_box ("Test" )
679
648
dname = sys .prefix
680
649
print ((dname + ':' , '\n ' , get_python_infos (dname )))
681
- # dname = r'E:\winpython\sandbox\python-2.7.3'
682
- # print dname+':', '\n', get_python_infos(dname)
683
650
684
651
tmpdir = r'D:\Tests\winpython_tests'
685
- Path (tmpdir ).mkdir (parents = True , exist_ok = True )
686
- print (
687
- (
688
- extract_archive (
689
- str (Path (r'D:\WinP\bd37' ) / 'packages.win-amd64' /
690
- 'python-3.7.3.amd64.zip' ),
691
- tmpdir ,
692
- )
693
- )
694
- )
652
+ Path (tmpdir ).mkdir (parents = True , exist_ok = True )
653
+ print (extract_archive (str (Path (r'D:\WinP\bd37' ) / 'packages.win-amd64' / 'python-3.7.3.amd64.zip' ), tmpdir ))
0 commit comments