SHARE
TWEET

Untitled

a guest Aug 18th, 2019 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from fbs import path, SETTINGS
  2. from fbs._state import LOADED_PROFILES
  3. from fbs.resources import _copy
  4. from fbs_runtime._fbs import filter_public_settings
  5. from fbs_runtime._source import default_path
  6. from fbs_runtime.platform import is_mac
  7. from os import rename
  8. from os.path import join, dirname
  9. from pathlib import PurePath
  10. from PyInstaller.__main__ import run
  11. from tempfile import TemporaryDirectory
  12.  
  13. import fbs_runtime._frozen
  14.  
  15. def run_pyinstaller(extra_args=None, debug=False):
  16.     if extra_args is None:
  17.         extra_args = []
  18.     app_name = SETTINGS['app_name']
  19.     # Would like log level WARN when not debugging. This works fine for
  20.     # PyInstaller 3.3. However, for 3.4, it gives confusing warnings
  21.     # "hidden import not found". So use ERROR instead.
  22.     log_level = 'DEBUG' if debug else 'ERROR'
  23.     args = [
  24.         '--name', app_name,
  25.         '--noupx',
  26.         '--log-level', log_level,
  27.         '--noconfirm'
  28.     ]
  29.     for hidden_import in SETTINGS['hidden_imports']:
  30.         args.extend(['--hidden-import', hidden_import])
  31.     args.extend(extra_args)
  32.     args.extend([
  33.         '--distpath', path('target'),
  34.         '--specpath', path('target/PyInstaller'),
  35.         '--workpath', path('target/PyInstaller')
  36.     ])
  37.     args.extend(['--additional-hooks-dir', join(dirname(__file__), 'hooks')])
  38.     if debug:
  39.         args.extend(['--debug', 'all'])
  40.         if is_mac():
  41.             # Force generation of an .app bundle. Otherwise, PyInstaller skips
  42.             # it when --debug is given.
  43.             args.append('-w')
  44.     with _PyInstallerRuntimehook() as hook_path:
  45.         args.extend(['--runtime-hook', hook_path])
  46.         args.append(path(SETTINGS['main_module']))
  47.         run(args)
  48.     output_dir = path('target/' + app_name + ('.app' if is_mac() else ''))
  49.     freeze_dir = path('${freeze_dir}')
  50.     # In most cases, rename(src, dst) silently "works" when src == dst. But on
  51.     # some Windows drives, it raises a FileExistsError. So check src != dst:
  52.     if PurePath(output_dir) != PurePath(freeze_dir):
  53.         rename(output_dir, freeze_dir)
  54.  
  55. class _PyInstallerRuntimehook:
  56.     def __init__(self):
  57.         self._tmp_dir = TemporaryDirectory()
  58.     def __enter__(self):
  59.         module = fbs_runtime._frozen
  60.         hook_path = join(self._tmp_dir.name, 'fbs_pyinstaller_hook.py')
  61.         with open(hook_path, 'w') as f:
  62.             # Inject public settings such as "version" into the binary, so
  63.             # they're available at run time:
  64.             f.write('\n'.join([
  65.                 'import importlib',
  66.                 'module = importlib.import_module(%r)' % module.__name__,
  67.                 'module.BUILD_SETTINGS = %r' % filter_public_settings(SETTINGS)
  68.             ]))
  69.         return hook_path
  70.     def __exit__(self, *_):
  71.         self._tmp_dir.cleanup()
  72.  
  73. def _generate_resources():
  74.     """
  75.     Copy the data files from src/main/resources to ${freeze_dir}.
  76.     Automatically filters files mentioned in the setting files_to_filter:
  77.     Placeholders such as ${app_name} are automatically replaced by the
  78.     corresponding setting in files on that list.
  79.     """
  80.     freeze_dir = path('${freeze_dir}')
  81.     if is_mac():
  82.         resources_dest_dir = join(freeze_dir, 'Contents', 'Resources')
  83.     else:
  84.         resources_dest_dir = freeze_dir
  85.     for path_fn in default_path, path:
  86.         for profile in LOADED_PROFILES:
  87.             _copy(path_fn, 'src/main/resources/' + profile, resources_dest_dir)
  88.             _copy(path_fn, 'src/freeze/' + profile, freeze_dir)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top