File: pymailgui-products/unzipped/fixfrozenpaths.py

"""
================================================================================
fixfrozenpaths.py:
  setup path/cwd context for all frozen scripts (part of the PyMailGUI system)

For frozen apps/exes, fix module+resource visibility.  This configures
sys.path and the CWD as needed for the freeze tool used, to grant importers
access to these items.  This module is imported by both launcher and main
scripts frozen in the PyMailGUI package; it updates the global sys.path and
CWD in-place.  See PyEdit's version of this module for more background details.
================================================================================
"""

import sys, os
RunningOnMac     = sys.platform.startswith('darwin')
RunningOnWindows = sys.platform.startswith('win')
RunningOnLinux   = sys.platform.startswith('linux')

DEBUG = False
if DEBUG:
    print('file', __file__)
    print('argv', sys.argv[0])        
    print('exec', sys.executable)     
    print('path', sys.path)           
    print('cwd ', os.getcwd())        

#===============================================================================

# Set global import-path context
    
if hasattr(sys, 'frozen') and sys.frozen == 'macosx_app':
    #
    # 1) Mac py2app app-bundle folder distribution
    # Frozen importer's bootloader is in the app's Content/MacOS dir.
    # Add '.' for importing config module in app's Content/Resources dir.
    # dirname(__file__) and cwd work for icons, UserGuide.html, scripts,
    # and imports: code is source files, run by the app's bundled Python.
    # Ok to use cwd here: py2app cds to the data dir by default anyhow. 
    #
    # HERE: PyMailGUI uses an entire MailConfigs folder (not one file), 
    # and imports use the manual copy of the PyMailGui-PP4E package in 
    # Resources (which is CWD) instead of the copy auto added by py2app 
    # in Resources/lib/python3.5.  The latter is deleted by build scripts 
    # because it's large (63M), and not required: all its stdlib needs are 
    # "baked in" to the python zip in /lib, and the path extension here 
    # makes the manual copy in Resources visible.
    #
    sys.path.append(os.getcwd())

elif hasattr(sys, 'frozen') and (RunningOnWindows or RunningOnLinux):
    #
    # 2) Windows and Linux PyInstaller single-file executable distribution
    # Use exe's path (not temp _MEI*) for config module and all data items.
    # The config module cannot be in PyInstaller's auto-unzip Temp dir.
    # DROPPED os.chdir(): this made empty __file__ dir map to install dir,
    # but precluded any relative paths in cmdline args to frozen exes; see
    # fetchMyInstallDir() below for the later explicit-install-dir scheme.
    #
    exepath = sys.argv[0]
    exedir  = os.path.dirname(os.path.abspath(exepath))
    sys.path.append(exedir)         # for configs .py import
    ##os.chdir(exedir)              # for extras => now call fetchMyInstallDir!
    
else:
    #
    # 3) Portable Source code distributions - no tweaking required
    # The src dir is on sys.path: no action required for imports.
    # Data will be located by dirname(__file__) and/or cwd.
    pass

#===============================================================================

def fetchMyInstallDir(__file__):     # not global __file__
    """
    --------------------------------------------------------
    call this to fetch folder where extra items reside;
    use to access installed icons, help, readme, scripts;
    replaces former os.chdir() which precluded rel paths;
    the return value is always an absolute pathname;
    
    pass importer's __file__ to __file__ arg: for frozen
    Mac apps, this module's dir(__file__) is in a zipfile,
    and differs from the importer's dir(__file__); they're
    the same for source code, and unused for Win/Lin exes;
    --------------------------------------------------------
    """
    
    if hasattr(sys, 'frozen') and (RunningOnWindows or RunningOnLinux):
        #
        # PyInstaller executable: from sys.argv[0] = exe's dir;
        # __file__ dir is empty and cwd may be any user folder
        # if the importer is run from a command line elsewhere;
        #
        exepath = sys.argv[0]
        exedir  = os.path.dirname(os.path.abspath(exepath))
        return exedir

    else:
        #
        # Mac app bundle or source-code: from __file__ as usual;
        # cwd is anywhere: return importing file's install folder;
        # this mod's __file__ is *not* ok to use here for frozen
        # Mac apps: it's in a zipfile, not importer's Resources/;
        #
        srcpath = __file__
        srcdir  = os.path.dirname(os.path.abspath(srcpath))
        return srcdir



[Home page] Books Code Blog Python Author Train Find ©M.Lutz