commands.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 command in commands:
  19. name = command['name']
  20. shutil.copy(exe_src, str(target / (name+'.exe')))
  21. specified_preamble = command.get('extra_preamble', None)
  22. if isinstance(specified_preamble, text_types):
  23. # Filename
  24. extra_preamble = io.open(specified_preamble, encoding='utf-8')
  25. elif specified_preamble is None:
  26. extra_preamble = io.StringIO() # Empty
  27. else:
  28. # Passed a StringIO or similar object
  29. extra_preamble = specified_preamble
  30. module, func = command['entry_point'].split(':')
  31. with (target / (name+'-script.py')).open('w') as f:
  32. f.write(SCRIPT_TEMPLATE.format(
  33. module=module, func=func,
  34. extra_preamble=extra_preamble.read().rstrip(),
  35. ))