commands.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import io
  2. import shutil
  3. import win_cli_launchers
  4. from .util import text_types
  5. SCRIPT_TEMPLATE = u"""#!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. # Allowing .dll files in Python directory to be found
  12. os.environ['PATH'] += ';' + os.path.dirname(sys.executable)
  13. {extra_preamble}
  14. if __name__ == '__main__':
  15. from {module} import {func}
  16. {func}()
  17. """
  18. def prepare_bin_directory(target, commands, bitness=32):
  19. exe_src = win_cli_launchers.find_exe('x64' if bitness == 64 else 'x86')
  20. for name, command in commands.items():
  21. shutil.copy(exe_src, str(target / (name+'.exe')))
  22. specified_preamble = command.get('extra_preamble', None)
  23. if isinstance(specified_preamble, text_types):
  24. # Filename
  25. extra_preamble = io.open(specified_preamble, encoding='utf-8')
  26. elif specified_preamble is None:
  27. extra_preamble = io.StringIO() # Empty
  28. else:
  29. # Passed a StringIO or similar object
  30. extra_preamble = specified_preamble
  31. module, func = command['entry_point'].split(':')
  32. with (target / (name+'-script.py')).open('w') as f:
  33. f.write(SCRIPT_TEMPLATE.format(
  34. module=module, func=func,
  35. extra_preamble=extra_preamble.read().rstrip(),
  36. ))