Skip to content

Commit

Permalink
Update generated scripts to match pip installed scripts (#359)
Browse files Browse the repository at this point in the history
* Update generated scripts to match pip installed scripts

* escape some backslashed

* Compute import name outside of parse_entry_point

* Fix docstring of parse_entry_point

Co-authored-by: Thomas Kluyver <takowl@gmail.com>
  • Loading branch information
ksunden and takluyver committed Jul 14, 2020
1 parent 6983011 commit f4af9e0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions flit/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,14 @@ def _auto_user(self, python):
def install_scripts(self, script_defs, scripts_dir):
for name, ep in script_defs.items():
module, func = common.parse_entry_point(ep)
import_name = func.split('.')[0]
script_file = pathlib.Path(scripts_dir) / name
log.info('Writing script to %s', script_file)
with script_file.open('w', encoding='utf-8') as f:
f.write(common.script_template.format(
interpreter=self.python,
module=module,
import_name=import_name,
func=func
))
script_file.chmod(0o755)
Expand Down
13 changes: 9 additions & 4 deletions flit_core/flit_core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,13 @@ def check_version(version):

script_template = """\
#!{interpreter}
from {module} import {func}
# -*- coding: utf-8 -*-
import re
import sys
from {module} import {import_name}
if __name__ == '__main__':
{func}()
sys.argv[0] = re.sub(r'(-script\\.pyw|\\.exe)?$', '', sys.argv[0])
sys.exit({func}())
"""

def parse_entry_point(ep):
Expand All @@ -223,8 +227,9 @@ def parse_entry_point(ep):
raise ValueError("Invalid entry point (no ':'): %r" % ep)
mod, func = ep.split(':')

if not func.isidentifier():
raise ValueError("Invalid entry point: %r is not an identifier" % func)
for piece in func.split('.'):
if not piece.isidentifier():
raise ValueError("Invalid entry point: %r is not an identifier" % piece)
for piece in mod.split('.'):
if not piece.isidentifier():
raise ValueError("Invalid entry point: %r is not a module path" % piece)
Expand Down

0 comments on commit f4af9e0

Please sign in to comment.