2020
2121import sys
2222import os
23- import os .path as osp
23+ # import os.path as osp
2424from pathlib import Path
2525import shutil
2626import traceback
3636def get_module_path (modname ):
3737 """Return module *modname* base path"""
3838 module = sys .modules .get (modname , __import__ (modname ))
39- return osp .abspath (osp .dirname (module .__file__ ))
39+ # return osp.abspath(osp.dirname(module.__file__))
40+ return str (Path (module .__file__ ).parent .resolve ())
4041
4142
4243# ==============================================================================
@@ -67,12 +68,15 @@ def prepend_module_to_path(module_path):
6768 1) In your application to import local frozen copies of internal libraries
6869 2) In your py2exe distributed package to add a text file containing the returned string
6970 """
70- if not osp .isdir (module_path ):
71+ #if not osp.isdir(module_path):
72+ if not Path (module_path ).is_dir ():
7173 # Assuming py2exe distribution
7274 return
73- sys .path .insert (0 , osp .abspath (module_path ))
75+ #sys.path.insert(0, osp.abspath(module_path))
76+ sys .path .insert (0 , str (Path (module_path ).resolve ()))
7477 changeset = get_changeset (module_path )
75- name = osp .basename (module_path )
78+ # name = osp.basename(module_path)
79+ name = Path (module_path ).name
7680 prefix = "Prepending module to sys.path"
7781 message = prefix + (
7882 "%s [revision %s]" % (name , changeset )
@@ -95,7 +99,8 @@ def prepend_module_to_path(module_path):
9599
96100def prepend_modules_to_path (module_base_path ):
97101 """Prepend to sys.path all modules located in *module_base_path*"""
98- if not osp .isdir (module_base_path ):
102+ # if not osp.isdir(module_base_path):
103+ if not Path (module_base_path ).is_dir ():
99104 # Assuming py2exe distribution
100105 return
101106 fnames = [
@@ -106,7 +111,8 @@ def prepend_modules_to_path(module_base_path):
106111 messages = [
107112 prepend_module_to_path (dirname )
108113 for dirname in fnames
109- if osp .isdir (dirname )
114+ # if osp.isdir(dirname)
115+ if Path (dirname ).is_dir ()
110116 ]
111117 return os .linesep .join (messages )
112118
@@ -118,10 +124,12 @@ def _remove_later(fname):
118124 """Try to remove file later (at exit)"""
119125
120126 def try_to_remove (fname ):
121- if osp .exists (fname ):
127+ # if osp.exists(fname):
128+ if Path (fname ).exists ():
122129 os .remove (fname )
123130
124- atexit .register (try_to_remove , osp .abspath (fname ))
131+ # atexit.register(try_to_remove, osp.abspath(fname))
132+ atexit .register (try_to_remove , str (Path (fname ).resolve ()))
125133
126134
127135def to_include_files (data_files ):
@@ -142,7 +150,7 @@ def to_include_files(data_files):
142150 #dest_fname = osp.join(
143151 # dest_dir, osp.basename(source_fname))
144152 dest_fname = str (Path (dest_dir ) /
145- osp . basename (source_fname ))
153+ Path (source_fname ). name )
146154 include_files .append ((source_fname , dest_fname ))
147155 return include_files
148156
@@ -276,7 +284,8 @@ def setup(
276284 else version
277285 )
278286 self .description = description
279- assert osp .isfile (script )
287+ # assert osp.isfile(script)
288+ assert Path (script ).is_file ()
280289 self .script = script
281290 self .target_name = target_name
282291 self .target_dir = target_dir
@@ -339,7 +348,8 @@ def add_pyqt4(self):
339348
340349 import PyQt4
341350
342- pyqt_path = osp .dirname (PyQt4 .__file__ )
351+ # pyqt_path = osp.dirname(PyQt4.__file__)
352+ pyqt_path = str (Path (PyQt4 .__file__ ).parent )
343353
344354 # Configuring PyQt4
345355 conf = os .linesep .join (
@@ -351,7 +361,8 @@ def add_pyqt4(self):
351361 if self .msvc :
352362 vc90man = "Microsoft.VC90.CRT.manifest"
353363 pyqt_tmp = 'pyqt_tmp'
354- if osp .isdir (pyqt_tmp ):
364+ # if osp.isdir(pyqt_tmp):
365+ if Path (pyqt_tmp ).is_dir ():
355366 shutil .rmtree (pyqt_tmp )
356367 os .mkdir (pyqt_tmp )
357368 # vc90man_pyqt = osp.join(pyqt_tmp, vc90man)
@@ -373,12 +384,14 @@ def add_pyqt4(self):
373384 # osp.join(dirpath, f)
374385 str (Path (dirpath ) / f )
375386 for f in filenames
376- if osp .splitext (f )[1 ] in ('.dll' , '.py' )
387+ # if osp.splitext(f)[1] in ('.dll', '.py')
388+ if Path (f ).suffix in ('.dll' , '.py' )
377389 ]
378390 if self .msvc and [
379391 f
380392 for f in filelist
381- if osp .splitext (f )[1 ] == '.dll'
393+ # if osp.splitext(f)[1] == '.dll'
394+ if Path (f ).suffix == '.dll'
382395 ]:
383396 # Where there is a DLL build with Microsoft Visual C++ 2008,
384397 # there must be a manifest file as well...
@@ -397,9 +410,10 @@ def add_pyqt4(self):
397410 # Including french translation
398411 # fr_trans = osp.join(
399412 # pyqt_path, "translations", "qt_fr.qm"
413+ # )
400414 fr_trans = str (Path (pyqt_path ) / "translations" / "qt_fr.qm" )
401- )
402- if osp .exists (fr_trans ):
415+ # if osp.exists(fr_trans):
416+ if Path ( fr_trans ) .exists ():
403417 self .data_files .append (
404418 ('translations' , (fr_trans ,))
405419 )
@@ -429,7 +443,8 @@ def add_pyside(self):
429443
430444 import PySide
431445
432- pyside_path = osp .dirname (PySide .__file__ )
446+ # pyside_path = osp.dirname(PySide.__file__)
447+ pyside_path = str (Path (PySide .__file__ ).parent )
433448
434449 # Configuring PySide
435450 conf = os .linesep .join (
@@ -460,12 +475,14 @@ def add_pyside(self):
460475 # osp.join(dirpath, f)
461476 str (Path (dirpath ) / f )
462477 for f in filenames
463- if osp .splitext (f )[1 ] in ('.dll' , '.py' )
478+ # if osp.splitext(f)[1] in ('.dll', '.py')
479+ if Path (f ).suffix in ('.dll' , '.py' )
464480 ]
465481 if self .msvc and [
466482 f
467483 for f in filelist
468- if osp .splitext (f )[1 ] == '.dll'
484+ # if osp.splitext(f)[1] == '.dll'
485+ if Path (f ).suffix == '.dll'
469486 ]:
470487 # Where there is a DLL build with Microsoft Visual C++ 2008,
471488 # there must be a manifest file as well...
@@ -485,7 +502,8 @@ def add_pyside(self):
485502 # osp.join(pyside_path, fname)
486503 str (Path (pyside_path ) / fname )
487504 for fname in os .listdir (pyside_path )
488- if osp .splitext (fname )[1 ] == '.dll'
505+ #if osp.splitext(fname)[1] == '.dll'
506+ if Path (fname ).suffix == '.dll'
489507 ]
490508 self .data_files .append (('' , dlls ))
491509
@@ -495,7 +513,8 @@ def add_pyside(self):
495513 # fr_trans = osp.join(
496514 # pyside_path, "translations", "qt_fr.qm")
497515 fr_trans = str (Path (pyside_path ) / "translations" / "qt_fr.qm" )
498- if osp .exists (fr_trans ):
516+ #if osp.exists(fr_trans):
517+ if Path (fr_trans ).exists ():
499518 self .data_files .append (
500519 ('translations' , (fr_trans ,))
501520 )
@@ -613,12 +632,15 @@ def add_modules(self, *module_names):
613632 import sphinx .ext
614633
615634 for fname in os .listdir (
616- osp .dirname (sphinx .ext .__file__ )
635+ #osp.dirname(sphinx.ext.__file__)
636+ str (Path (sphinx .ext .__file__ ).parent )
617637 ):
618- if osp .splitext (fname )[1 ] == '.py' :
638+ #if osp.splitext(fname)[1] == '.py':
639+ if Path (fname ).suffix == '.py' :
619640 modname = (
620641 'sphinx.ext.%s'
621- % osp .splitext (fname )[0 ]
642+ # % osp.splitext(fname)[0]
643+ % Path (fname ).stem
622644 )
623645 self .includes .append (modname )
624646 elif module_name == 'pygments' :
@@ -699,18 +721,21 @@ def add_module_data_dir(
699721 *extensions*: list of file extensions, e.g. ('.png', '.svg')
700722 """
701723 module_dir = get_module_path (module_name )
702- nstrip = len (module_dir ) + len (osp .sep )
724+ # nstrip = len(module_dir) + len(osp.sep)
725+ nstrip = len (module_dir ) + len (os .sep )
703726 # data_dir = osp.join(module_dir, data_dir_name)
704727 data_dir = str (Path (module_dir ) / data_dir_name )
705- if not osp .isdir (data_dir ):
728+ # if not osp.isdir(data_dir):
729+ if not Path (data_dir ).is_dir ():
706730 raise IOError (
707731 "Directory not found: %s" % data_dir
708732 )
709733 for dirpath , _dirnames , filenames in os .walk (
710734 data_dir
711735 ):
712736 dirname = dirpath [nstrip :]
713- if osp .basename (dirpath ) in exclude_dirs :
737+ #if osp.basename(dirpath) in exclude_dirs:
738+ if Path (dirpath ).name in exclude_dirs :
714739 continue
715740 if not copy_to_root :
716741 # dirname = osp.join(module_name, dirname)
@@ -719,7 +744,8 @@ def add_module_data_dir(
719744 # osp.join(dirpath, f)
720745 str (Path (dirpath ) / f )
721746 for f in filenames
722- if osp .splitext (f )[1 ].lower () in extensions
747+ # if osp.splitext(f)[1].lower() in extensions
748+ if Path (f ).suffix .lower () in extensions
723749 ]
724750 self .data_files .append ((dirname , pathlist ))
725751 if verbose :
@@ -767,23 +793,30 @@ def add_module_data_files(
767793 #)
768794 translation_file = str (Path (module_dir ) / "locale" / "fr" /
769795 "LC_MESSAGES" / f"{ module_name } .mo" )
770- if osp .isfile (translation_file ):
796+ # if osp.isfile(translation_file):
797+ if Path (translation_file ).is_file ():
771798 self .data_files .append (
772799 (
773- osp .join (
774- module_name ,
775- "locale" ,
776- "fr" ,
777- "LC_MESSAGES" ,
778- ),
800+ #osp.join(
801+ # module_name,
802+ # "locale",
803+ # "fr",
804+ # "LC_MESSAGES",
805+ #),
806+ str (Path (
807+ module_name ) /
808+ "locale" /
809+ "fr" /
810+ "LC_MESSAGES" ),
779811 (translation_file ,),
780812 )
781813 )
782814 print (
783815 "Adding module '%s' translation file: %s"
784816 % (
785817 module_name ,
786- osp .basename (translation_file ),
818+ #osp.basename(translation_file),
819+ Path (translation_file ).name ,
787820 )
788821 )
789822
@@ -821,7 +854,8 @@ def build(
821854 def __cleanup (self ):
822855 """Remove old build and dist directories"""
823856 remove_dir ("build" )
824- if osp .isdir ("dist" ):
857+ # if osp.isdir("dist"):
858+ if Path ("dist" ).is_dir ():
825859 remove_dir ("dist" )
826860 remove_dir (self .target_dir )
827861
@@ -877,7 +911,8 @@ def build_py2exe(
877911 icon_resources = [(0 , self .icon )],
878912 bitmap_resources = [],
879913 other_resources = [],
880- dest_base = osp .splitext (self .target_name )[0 ],
914+ # dest_base=osp.splitext(self.target_name)[0],
915+ dest_base = Path (self .target_name ).stem ,
881916 version = self .version ,
882917 company_name = company_name ,
883918 copyright = copyright ,
0 commit comments