Skip to content

Commit c11be2d

Browse files
committed
remove osp from utils.py
1 parent 01e3b4c commit c11be2d

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

winpython/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@
2828
OTHER DEALINGS IN THE SOFTWARE.
2929
"""
3030

31-
__version__ = '5.2.20221113'
31+
__version__ = '5.2.20221120'
3232
__license__ = __doc__
3333
__project_url__ = 'http://winpython.github.io/'

winpython/utils.py

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from __future__ import print_function
1414

1515
import os
16-
import os.path as osp
16+
# import os.path as osp
1717
from pathlib import Path
1818
import subprocess
1919
import re
@@ -33,21 +33,29 @@
3333
def get_python_executable(path = None):
3434
"""return the python executable"""
3535
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-
exec_py = os.path.join(my_path, 'python.exe')
38-
exec_pypy = os.path.join(my_path, 'pypy3.exe') # PyPy !
36+
# my_path = my_path if osp.isdir(my_path) else osp.dirname(my_path)
37+
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+
exec_py = str(Path(my_path) / 'python.exe')
40+
# exec_pypy = os.path.join(my_path, 'pypy3.exe') # PyPy !
41+
exec_pypy = str(Path(my_path) / 'pypy3.exe') # PyPy !
3942
# PyPy >=7.3.6 3.8 aligns to python.exe and Lib\site-packages
4043
#python_executable = exec_pypy if osp.isfile(exec_pypy) else exec_py
41-
python_executable = exec_py if osp.isfile(exec_py) else exec_pypy
44+
#python_executable = exec_py if osp.isfile(exec_py) else exec_pypy
45+
python_executable = exec_py if Path(exec_py).is_file() else exec_pypy
4246
return python_executable
4347

4448
def get_site_packages_path(path = None):
4549
"""return the python site-packages"""
4650
my_path = sys.executable if path == None else path # default = current one
47-
my_path = my_path if osp.isdir(my_path) else osp.dirname(my_path)
48-
site_py = os.path.join(my_path, 'Lib', 'site-packages')
49-
site_pypy = os.path.join(my_path, 'site-packages') # PyPy !!
50-
site_packages_path = site_pypy if osp.isfile(site_pypy) else site_py
51+
# my_path = my_path if osp.isdir(my_path) else osp.dirname(my_path)
52+
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+
site_py = str(Path(my_path) / 'Lib' / 'site-packages')
55+
# site_pypy = os.path.join(my_path, 'site-packages') # PyPy !!
56+
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
5159
return site_packages_path
5260

5361
def onerror(function, path, excinfo):
@@ -73,7 +81,8 @@ def is_program_installed(basename):
7381
for path in os.environ["PATH"].split(os.pathsep):
7482
# abspath = osp.join(path, basename)
7583
abspath = str(Path(path) / basename)
76-
if osp.isfile(abspath):
84+
# if osp.isfile(abspath):
85+
if Path(abspath).is_file():
7786
return abspath
7887

7988

@@ -224,7 +233,8 @@ def get_winpython_start_menu_folder(current=True):
224233
def create_winpython_start_menu_folder(current=True):
225234
"""Create WinPython Start menu folder -- remove it if it already exists"""
226235
path = get_winpython_start_menu_folder(current=current)
227-
if osp.isdir(path):
236+
# if osp.isdir(path):
237+
if Path(path).is_dir():
228238
try:
229239
shutil.rmtree(path, onerror=onerror)
230240
except WindowsError:
@@ -288,8 +298,10 @@ def print_box(text):
288298
def is_python_distribution(path):
289299
"""Return True if path is a Python distribution"""
290300
# XXX: This test could be improved but it seems to be sufficient
291-
has_exec = osp.isfile(get_python_executable(path))
292-
has_site = osp.isdir(get_site_packages_path(path))
301+
# has_exec = osp.isfile(get_python_executable(path))
302+
has_exec = Path(get_python_executable(path)).is_file()
303+
# has_site = osp.isdir(get_site_packages_path(path))
304+
has_site = Path(get_site_packages_path(path)).is_dir()
293305
return has_exec and has_site
294306

295307

@@ -538,7 +550,8 @@ def patch_sourcefile(
538550
"""Replace a string in a source file"""
539551
import io
540552

541-
if osp.isfile(fname) and not in_text == out_text:
553+
# if osp.isfile(fname) and not in_text == out_text:
554+
if Path(fname).is_file() and not in_text == out_text:
542555
the_encoding = guess_encoding(fname)[0]
543556
with io.open(fname, 'r', encoding=the_encoding) as fh:
544557
content = fh.read()
@@ -569,9 +582,10 @@ def patch_sourcelines(
569582
):
570583
"""Replace the middle of lines between in_line_start and endline """
571584
import io
572-
import os.path as osp
585+
# import os.path as osp
573586

574-
if osp.isfile(fname):
587+
# if osp.isfile(fname):
588+
if Path(fname).is_file():
575589
the_encoding = guess_encoding(fname)[0]
576590
with io.open(fname, 'r', encoding=the_encoding) as fh:
577591
contents = fh.readlines()
@@ -641,16 +655,19 @@ def extract_exe(fname, targetdir=None, verbose=False):
641655
assert is_program_installed(extract), (
642656
"Required program '%s' was not found" % extract
643657
)
644-
bname = osp.basename(fname)
658+
#bname = osp.basename(fname)
659+
bname = Path(fname).name
645660
args = ['x', '-o%s' % targetdir, '-aos', bname]
646661
if verbose:
647662
retcode = subprocess.call(
648-
[extract] + args, cwd=osp.dirname(fname)
663+
# [extract] + args, cwd=osp.dirname(fname)
664+
[extract] + args, cwd=str(Path(fname).parent)
649665
)
650666
else:
651667
p = subprocess.Popen(
652668
[extract] + args,
653-
cwd=osp.dirname(fname),
669+
#cwd=osp.dirname(fname),
670+
cwd=str(Path(fname).parent),
654671
stdout=subprocess.PIPE,
655672
)
656673
p.communicate()
@@ -675,7 +692,8 @@ def extract_archive(fname, targetdir=None, verbose=False):
675692
os.mkdir(targetdir)
676693
except:
677694
pass
678-
if osp.splitext(fname)[1] in ('.zip', '.exe'):
695+
#if osp.splitext(fname)[1] in ('.zip', '.exe'):
696+
if Path(fname).suffix in ('.zip', '.exe'):
679697
obj = zipfile.ZipFile(fname, mode="r")
680698
elif fname.endswith('.tar.gz'):
681699
obj = tarfile.open(fname, mode='r:gz')
@@ -707,8 +725,10 @@ def extract_archive(fname, targetdir=None, verbose=False):
707725
def get_source_package_infos(fname):
708726
"""Return a tuple (name, version) of the Python source package"""
709727
if fname[-4:] == '.whl':
710-
return osp.basename(fname).split("-")[:2]
711-
match = re.match(SOURCE_PATTERN, osp.basename(fname))
728+
#return osp.basename(fname).split("-")[:2]
729+
return Path(fname).name.split("-")[:2]
730+
# match = re.match(SOURCE_PATTERN, osp.basename(fname))
731+
match = re.match(SOURCE_PATTERN, Path(fname).name)
712732
if match is not None:
713733
return match.groups()[:2]
714734

@@ -726,7 +746,8 @@ def build_wininst(
726746
Return wininst installer full path."""
727747
if python_exe is None:
728748
python_exe = sys.executable
729-
assert osp.isfile(python_exe)
749+
#assert osp.isfile(python_exe)
750+
assert Path(python_exe).is_file()
730751
cmd = [python_exe, 'setup.py', 'build']
731752
if architecture is not None:
732753
archstr = (
@@ -749,7 +770,8 @@ def build_wininst(
749770
p.stderr.close()
750771
# distdir = osp.join(root, 'dist')
751772
distdir = str(Path(root) / 'dist')
752-
if not osp.isdir(distdir):
773+
# if not osp.isdir(distdir):
774+
if not Path(distdir).is_dir():
753775
raise RuntimeError(
754776
"Build failed: see package README file for further"
755777
" details regarding installation requirements.\n\n"
@@ -806,12 +828,15 @@ def direct_pip_install(
806828
install_options=None,
807829
):
808830
"""Direct install via pip !"""
809-
copy_to = osp.dirname(fname)
831+
# copy_to = osp.dirname(fname)
832+
copy_to = str(Path(fname).parent)
810833

811834
if python_exe is None:
812835
python_exe = sys.executable
813-
assert osp.isfile(python_exe)
814-
myroot = os.path.dirname(python_exe)
836+
# assert osp.isfile(python_exe)
837+
assert Path(python_exe).is_file()
838+
# myroot = os.path.dirname(python_exe)
839+
myroot = str(Path(python_exe).parent)
815840

816841
cmd = [python_exe, '-m', 'pip', 'install']
817842
if install_options:
@@ -898,7 +923,8 @@ def do_script(
898923
# print dname+':', '\n', get_python_infos(dname)
899924

900925
tmpdir = r'D:\Tests\winpython_tests'
901-
if not osp.isdir(tmpdir):
926+
# if not osp.isdir(tmpdir):
927+
if not Path(tmpdir).is_dir():
902928
os.mkdir(tmpdir)
903929
print(
904930
(

0 commit comments

Comments
 (0)