commands.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import io
  2. import shutil
  3. import win_cli_launchers
  4. from .util import text_types
  5. SCRIPT_TEMPLATE = """#!python
  6. import sys, os
  7. installdir = os.path.dirname(os.path.dirname(__file__))
  8. pkgdir = os.path.join(installdir, 'pkgs')
  9. sys.path.insert(0, pkgdir)
  10. os.environ['PYTHONPATH'] = pkgdir + os.pathsep + os.environ.get('PYTHONPATH', '')
  11. {extra_preamble}
  12. if __name__ == '__main__':
  13. from {module} import {func}
  14. {func}()
  15. """
  16. def prepare_bin_directory(target, commands, bitness=32):
  17. exe_src = win_cli_launchers.find_exe('x64' if bitness == 64 else 'x86')
  18. for name, command in commands.items():
  19. shutil.copy(exe_src, str(target / (name+'.exe')))
  20. specified_preamble = command.get('extra_preamble', None)
  21. if isinstance(specified_preamble, text_types):
  22. # Filename
  23. extra_preamble = io.open(specified_preamble, encoding='utf-8')
  24. elif specified_preamble is None:
  25. extra_preamble = io.StringIO() # Empty
  26. else:
  27. # Passed a StringIO or similar object
  28. extra_preamble = specified_preamble
  29. module, func = command['entry_point'].split(':')
  30. with (target / (name+'-script.py')).open('w') as f:
  31. f.write(SCRIPT_TEMPLATE.format(
  32. module=module, func=func,
  33. extra_preamble=extra_preamble.read().rstrip(),
  34. ))