From a4b378214816c1a595fd134a7262a3f369563afd Mon Sep 17 00:00:00 2001 From: mwerezak <242428+mwerezak@users.noreply.github.com> Date: Thu, 19 Oct 2023 11:50:44 -0400 Subject: [PATCH 1/2] Improve handling of prepend_sys_path Don't split using colons on windows Fixes #1330 --- alembic/script/base.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/alembic/script/base.py b/alembic/script/base.py index d0f9abbd..7930fe14 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -212,9 +212,12 @@ def from_config(cls, config: Config) -> ScriptDirectory: prepend_sys_path = config.get_main_option("prepend_sys_path") if prepend_sys_path: - sys.path[:0] = list( - _split_on_space_comma_colon.split(prepend_sys_path) - ) + if os.name == 'nt': + prepend_paths = _split_on_space_comma.split(prepend_sys_path) + else: + prepend_paths = _split_on_space_comma_colon.split(prepend_sys_path) + + sys.path[:0] = (os.path.normpath(path.strip()) for path in prepend_paths) rvl = config.get_main_option("recursive_version_locations") == "true" return ScriptDirectory( From 50cad3d0a181beeaeb36f6b0b6eefc83058f6f52 Mon Sep 17 00:00:00 2001 From: mwerezak Date: Sat, 4 Nov 2023 19:24:53 -0400 Subject: [PATCH 2/2] Implement prepend_sys_path_separator --- alembic/config.py | 19 +++++++++++++++ alembic/script/base.py | 55 +++++++++++++++++------------------------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/alembic/config.py b/alembic/config.py index 55b5811a..39f28a45 100644 --- a/alembic/config.py +++ b/alembic/config.py @@ -341,6 +341,25 @@ def messaging_opts(self) -> MessagingOptions: ), ) + def get_separator_char(self, name: str) -> Optional[str]: + separator = self.get_main_option(name) + + split_on_path = { + None: None, + "space": " ", + "os": os.pathsep, + ":": ":", + ";": ";", + } + + try: + return split_on_path[separator] + except KeyError as ke: + raise ValueError( + "'%s' is not a valid value for %s; " + "expected 'space', 'os', ':', ';'" % (separator, name) + ) from ke + class MessagingOptions(TypedDict, total=False): quiet: bool diff --git a/alembic/script/base.py b/alembic/script/base.py index 7930fe14..33e875ec 100644 --- a/alembic/script/base.py +++ b/alembic/script/base.py @@ -175,49 +175,38 @@ def from_config(cls, config: Config) -> ScriptDirectory: version_locations_str = config.get_main_option("version_locations") version_locations: Optional[List[str]] if version_locations_str: - version_path_separator = config.get_main_option( + split_char = config.get_separator_char( "version_path_separator" ) - split_on_path = { - None: None, - "space": " ", - "os": os.pathsep, - ":": ":", - ";": ";", - } - - try: - split_char: Optional[str] = split_on_path[ - version_path_separator - ] - except KeyError as ke: - raise ValueError( - "'%s' is not a valid value for " - "version_path_separator; " - "expected 'space', 'os', ':', ';'" % version_path_separator - ) from ke + if split_char is None: + # legacy behaviour for backwards compatibility + version_locations = _split_on_space_comma.split( + version_locations_str + ) else: - if split_char is None: - # legacy behaviour for backwards compatibility - version_locations = _split_on_space_comma.split( - version_locations_str - ) - else: - version_locations = [ - x for x in version_locations_str.split(split_char) if x - ] + version_locations = [ + x for x in version_locations_str.split(split_char) if x + ] else: version_locations = None prepend_sys_path = config.get_main_option("prepend_sys_path") if prepend_sys_path: - if os.name == 'nt': - prepend_paths = _split_on_space_comma.split(prepend_sys_path) + split_char = config.get_separator_char( + "prepend_sys_path_separator" + ) + + if split_char is None: + # legacy behaviour for backwards compatibility + sys.path[:0] = list( + _split_on_space_comma_colon.split(prepend_sys_path) + ) else: - prepend_paths = _split_on_space_comma_colon.split(prepend_sys_path) - - sys.path[:0] = (os.path.normpath(path.strip()) for path in prepend_paths) + sys.path[:0] = ( + os.path.normpath(path.strip()) + for path in prepend_sys_path.split(split_char) + ) rvl = config.get_main_option("recursive_version_locations") == "true" return ScriptDirectory(