diff options
Diffstat (limited to 'alembic/script/write_hooks.py')
-rw-r--r-- | alembic/script/write_hooks.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/alembic/script/write_hooks.py b/alembic/script/write_hooks.py index d6d1d38..27e9a48 100644 --- a/alembic/script/write_hooks.py +++ b/alembic/script/write_hooks.py @@ -1,3 +1,4 @@ +import shlex import subprocess import sys @@ -5,6 +6,8 @@ from .. import util from ..util import compat +REVISION_SCRIPT_TOKEN = "REVISION_SCRIPT_FILENAME" + _registry = {} @@ -85,6 +88,24 @@ def _run_hooks(path, hook_config): ) +def _parse_options(options_str, path): + """Parse options from a string into a list. + + Also substitutes the revision script token with the actual filename of + the revision script. + + If the revision script token doesn't occur in the options string, it is + automatically prepended. + """ + if REVISION_SCRIPT_TOKEN not in options_str: + options_str = REVISION_SCRIPT_TOKEN + " " + options_str + options_list = shlex.split(options_str) + options_list = [ + option.replace(REVISION_SCRIPT_TOKEN, path) for option in options_list + ] + return options_list + + @register("console_scripts") def console_scripts(path, options): import pkg_resources @@ -101,14 +122,15 @@ def console_scripts(path, options): ) iter_ = pkg_resources.iter_entry_points("console_scripts", entrypoint_name) impl = next(iter_) - options = options.get("options", "") + options_str = options.get("options", "") + options_list = _parse_options(options_str, path) + subprocess.run( [ sys.executable, "-c", "import %s; %s()" % (impl.module_name, ".".join((impl.module_name,) + impl.attrs)), - path, ] - + options.split() + + options_list ) |