13
13
from __future__ import print_function
14
14
15
15
import os
16
- # import os.path as osp
17
16
from pathlib import Path
18
17
import subprocess
19
18
import re
33
32
def get_python_executable (path = None ):
34
33
"""return the python executable"""
35
34
my_path = sys .executable if path == None else path # default = current one
36
- # my_path = my_path if osp.isdir(my_path) else osp.dirname(my_path)
37
35
my_path = my_path if Path (my_path ).is_dir () else str (Path (my_path ).parent )
38
- #exec_py = os.path.join(my_path, 'python.exe')
39
36
exec_py = str (Path (my_path ) / 'python.exe' )
40
- # exec_pypy = os.path.join(my_path, 'pypy3.exe') # PyPy !
41
37
exec_pypy = str (Path (my_path ) / 'pypy3.exe' ) # PyPy !
42
38
# PyPy >=7.3.6 3.8 aligns to python.exe and Lib\site-packages
43
- #python_executable = exec_pypy if osp.isfile(exec_pypy) else exec_py
44
- #python_executable = exec_py if osp.isfile(exec_py) else exec_pypy
45
39
python_executable = exec_py if Path (exec_py ).is_file () else exec_pypy
46
40
return python_executable
47
41
48
42
def get_site_packages_path (path = None ):
49
43
"""return the python site-packages"""
50
44
my_path = sys .executable if path == None else path # default = current one
51
- # my_path = my_path if osp.isdir(my_path) else osp.dirname(my_path)
52
45
my_path = my_path if Path (my_path ).is_dir () else str (Path (my_path ).parent )
53
- # site_py = os.path.join(my_path, 'Lib', 'site-packages')
54
46
site_py = str (Path (my_path ) / 'Lib' / 'site-packages' )
55
- # site_pypy = os.path.join(my_path, 'site-packages') # PyPy !!
56
47
site_pypy = str (Path (my_path ) / 'site-packages' ) # PyPy !!
57
- # site_packages_path = site_pypy if osp.isfile(site_pypy) else site_py
58
- site_packages_path = site_pypy if Path (site_pypy ).is_file () else site_py
48
+ site_packages_path = site_pypy if Path (site_pypy ).is_dir () else site_py
59
49
return site_packages_path
60
50
61
51
def onerror (function , path , excinfo ):
@@ -79,9 +69,7 @@ def is_program_installed(basename):
79
69
"""Return program absolute path if installed in PATH
80
70
Otherwise, return None"""
81
71
for path in os .environ ["PATH" ].split (os .pathsep ):
82
- # abspath = osp.join(path, basename)
83
72
abspath = str (Path (path ) / basename )
84
- # if osp.isfile(abspath):
85
73
if Path (abspath ).is_file ():
86
74
return abspath
87
75
@@ -225,15 +213,13 @@ def get_winpython_start_menu_folder(current=True):
225
213
folder = get_special_folder_path (
226
214
"CSIDL_PROGRAMS"
227
215
)
228
- # return osp.join(folder, 'WinPython')
229
216
return str (Path (folder ) / 'WinPython' )
230
217
231
218
232
219
233
220
def create_winpython_start_menu_folder (current = True ):
234
221
"""Create WinPython Start menu folder -- remove it if it already exists"""
235
222
path = get_winpython_start_menu_folder (current = current )
236
- # if osp.isdir(path):
237
223
if Path (path ).is_dir ():
238
224
try :
239
225
shutil .rmtree (path , onerror = onerror )
@@ -298,9 +284,7 @@ def print_box(text):
298
284
def is_python_distribution (path ):
299
285
"""Return True if path is a Python distribution"""
300
286
# XXX: This test could be improved but it seems to be sufficient
301
- # has_exec = osp.isfile(get_python_executable(path))
302
287
has_exec = Path (get_python_executable (path )).is_file ()
303
- # has_site = osp.isdir(get_site_packages_path(path))
304
288
has_site = Path (get_site_packages_path (path )).is_dir ()
305
289
return has_exec and has_site
306
290
@@ -550,7 +534,6 @@ def patch_sourcefile(
550
534
"""Replace a string in a source file"""
551
535
import io
552
536
553
- # if osp.isfile(fname) and not in_text == out_text:
554
537
if Path (fname ).is_file () and not in_text == out_text :
555
538
the_encoding = guess_encoding (fname )[0 ]
556
539
with io .open (fname , 'r' , encoding = the_encoding ) as fh :
@@ -582,9 +565,7 @@ def patch_sourcelines(
582
565
):
583
566
"""Replace the middle of lines between in_line_start and endline """
584
567
import io
585
- # import os.path as osp
586
568
587
- # if osp.isfile(fname):
588
569
if Path (fname ).is_file ():
589
570
the_encoding = guess_encoding (fname )[0 ]
590
571
with io .open (fname , 'r' , encoding = the_encoding ) as fh :
@@ -655,18 +636,15 @@ def extract_exe(fname, targetdir=None, verbose=False):
655
636
assert is_program_installed (extract ), (
656
637
"Required program '%s' was not found" % extract
657
638
)
658
- #bname = osp.basename(fname)
659
639
bname = Path (fname ).name
660
640
args = ['x' , '-o%s' % targetdir , '-aos' , bname ]
661
641
if verbose :
662
642
retcode = subprocess .call (
663
- # [extract] + args, cwd=osp.dirname(fname)
664
643
[extract ] + args , cwd = str (Path (fname ).parent )
665
644
)
666
645
else :
667
646
p = subprocess .Popen (
668
647
[extract ] + args ,
669
- #cwd=osp.dirname(fname),
670
648
cwd = str (Path (fname ).parent ),
671
649
stdout = subprocess .PIPE ,
672
650
)
@@ -692,7 +670,6 @@ def extract_archive(fname, targetdir=None, verbose=False):
692
670
os .mkdir (targetdir )
693
671
except :
694
672
pass
695
- #if osp.splitext(fname)[1] in ('.zip', '.exe'):
696
673
if Path (fname ).suffix in ('.zip' , '.exe' ):
697
674
obj = zipfile .ZipFile (fname , mode = "r" )
698
675
elif fname .endswith ('.tar.gz' ):
@@ -725,9 +702,7 @@ def extract_archive(fname, targetdir=None, verbose=False):
725
702
def get_source_package_infos (fname ):
726
703
"""Return a tuple (name, version) of the Python source package"""
727
704
if fname [- 4 :] == '.whl' :
728
- #return osp.basename(fname).split("-")[:2]
729
705
return Path (fname ).name .split ("-" )[:2 ]
730
- # match = re.match(SOURCE_PATTERN, osp.basename(fname))
731
706
match = re .match (SOURCE_PATTERN , Path (fname ).name )
732
707
if match is not None :
733
708
return match .groups ()[:2 ]
@@ -746,7 +721,6 @@ def build_wininst(
746
721
Return wininst installer full path."""
747
722
if python_exe is None :
748
723
python_exe = sys .executable
749
- #assert osp.isfile(python_exe)
750
724
assert Path (python_exe ).is_file ()
751
725
cmd = [python_exe , 'setup.py' , 'build' ]
752
726
if architecture is not None :
@@ -768,9 +742,7 @@ def build_wininst(
768
742
p .communicate ()
769
743
p .stdout .close ()
770
744
p .stderr .close ()
771
- # distdir = osp.join(root, 'dist')
772
745
distdir = str (Path (root ) / 'dist' )
773
- # if not osp.isdir(distdir):
774
746
if not Path (distdir ).is_dir ():
775
747
raise RuntimeError (
776
748
"Build failed: see package README file for further"
@@ -800,12 +772,10 @@ def build_wininst(
800
772
"Build failed: not a pure Python package? %s"
801
773
% distdir
802
774
)
803
- # src_fname = osp.join(distdir, distname)
804
775
src_fname = str (Path (distdir ) / distname )
805
776
if copy_to is None :
806
777
return src_fname
807
778
else :
808
- # dst_fname = osp.join(copy_to, distname)
809
779
dst_fname = str (Path (copy_to ) / distname )
810
780
shutil .move (src_fname , dst_fname )
811
781
if verbose :
@@ -828,14 +798,11 @@ def direct_pip_install(
828
798
install_options = None ,
829
799
):
830
800
"""Direct install via pip !"""
831
- # copy_to = osp.dirname(fname)
832
801
copy_to = str (Path (fname ).parent )
833
802
834
803
if python_exe is None :
835
804
python_exe = sys .executable
836
- # assert osp.isfile(python_exe)
837
805
assert Path (python_exe ).is_file ()
838
- # myroot = os.path.dirname(python_exe)
839
806
myroot = str (Path (python_exe ).parent )
840
807
841
808
cmd = [python_exe , '-m' , 'pip' , 'install' ]
@@ -923,17 +890,11 @@ def do_script(
923
890
# print dname+':', '\n', get_python_infos(dname)
924
891
925
892
tmpdir = r'D:\Tests\winpython_tests'
926
- # if not osp.isdir(tmpdir):
927
893
if not Path (tmpdir ).is_dir ():
928
894
os .mkdir (tmpdir )
929
895
print (
930
896
(
931
897
extract_archive (
932
- #osp.join(
933
- # r'D:\WinP\bd37',
934
- # 'packages.win-amd64',
935
- # 'python-3.7.3.amd64.zip',
936
- #),
937
898
str (Path (r'D:\WinP\bd37' ) / 'packages.win-amd64' /
938
899
'python-3.7.3.amd64.zip' ),
939
900
tmpdir ,
0 commit comments