Skip to content

Commit 397fceb

Browse files
authored
Merge pull request #103 from dlt-hub/rfix/fixes-module-import
fixes module import
2 parents 9360075 + d249b74 commit 397fceb

9 files changed

+36
-14
lines changed

dlt/cli/_dlt.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def deploy_command_wrapper(pipeline_script_path: str, deployment_method: str, sc
5555
click.echo("Currently you must run the pipeline at least once")
5656
except InvalidGitRepositoryError:
5757
click.secho(
58-
"The path %s is invalid, outside of any git repository or git repository is not initialized. Initialize the repository and add it to Github by following the guide %s" % (pipeline_script_path, fmt.bold("https://docs.github.com/en/get-started/quickstart/create-a-repo")),
58+
"No git repository found for pipeline script %s. Add your local code to Github as described here: %s" % (fmt.bold(pipeline_script_path), fmt.bold("https://docs.github.com/en/get-started/importing-your-projects-to-github/importing-source-code-to-github/adding-locally-hosted-code-to-github")),
5959
err=True,
6060
fg="red"
6161
)

dlt/cli/deploy_command.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def deploy_command(pipeline_script_path: str, deployment_method: str, schedule:
4545
if "github.com" not in origin:
4646
raise CliCommandException("deploy", f"Your current repository origin is not set to github but to {origin}.\nYou must change it to be able to run the pipelines with github actions: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories")
4747
except ValueError:
48-
raise CliCommandException("deploy", "Your current repository has no origin set. Please set it up to be able to run the pipelines with github actions: https://docs.github.com/en/get-started/getting-started-with-git/managing-remote-repositories")
48+
raise CliCommandException("deploy", "Your current repository has no origin set. Please set it up to be able to run the pipelines with github actions: https://docs.github.com/en/get-started/importing-your-projects-to-github/importing-source-code-to-github/adding-locally-hosted-code-to-github")
4949

5050
# convert to path relative to repo
5151
repo_pipeline_script_path = repo_storage.from_wd_to_relative_path(pipeline_script_path)

dlt/cli/init_command.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ def init_command(pipeline_name: str, destination_name: str, use_generic_template
183183
)
184184

185185
# inspect the script
186-
script_module = inspect_pipeline_script(clone_storage.make_full_path(init_script_name))
186+
script_module = inspect_pipeline_script(clone_storage.storage_path, clone_storage.to_relative_path(init_script_name))
187187

188188
if len(_SOURCES) == 0:
189189
raise CliCommandException("init", f"The pipeline script {init_script_name} is not creating or importing any sources or resources")

dlt/cli/utils.py

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ast
22
import os
3+
from pathlib import Path
34
import sys
45
import tempfile
56
from importlib import import_module
@@ -32,11 +33,19 @@ def clone_command_repo(command: str, branch: str) -> FileStorage:
3233
def load_command_module(template_dir: str) -> ModuleType:
3334
# import the settings from the clone
3435
template_dir, template_module_name = os.path.split(template_dir.rstrip("/"))
35-
sys.path.append(template_dir)
36+
module_path, package = os.path.split(template_dir)
37+
module, _ = os.path.splitext(template_module_name)
38+
module = ".".join(Path(module).parts)
39+
40+
sys_path: str = None
41+
if module_path not in sys.path:
42+
sys_path = module_path
43+
# path must be first so we always load our module of
44+
sys.path.insert(0, sys_path)
3645
try:
37-
return import_module(template_module_name)
46+
return import_module(f"{package}.{module}")
3847
finally:
39-
sys.path.remove(template_dir)
48+
sys.path.remove(sys_path)
4049

4150

4251
def parse_init_script(command: str, script_source: str, init_script_name: str) -> PipelineScriptVisitor:

dlt/reflection/script_inspector.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import os
22
import sys
3+
from pathlib import Path
34
from types import ModuleType
45
from typing import Any, Tuple
56
from unittest.mock import patch
67
from importlib import import_module
78

9+
from dlt.common import logger
810
from dlt.common.exceptions import DltException
911
from dlt.common.typing import DictStrAny
1012

@@ -16,14 +18,18 @@ def patch__init__(self: Any, *args: Any, **kwargs: Any) -> None:
1618
raise PipelineIsRunning(self, args, kwargs)
1719

1820

19-
def inspect_pipeline_script(script_path: str) -> ModuleType:
21+
def inspect_pipeline_script(module_path:str, script_relative_path: str) -> ModuleType:
22+
if os.path.isabs(script_relative_path):
23+
raise ValueError(script_relative_path, f"Not relative path to {module_path}")
24+
script_path = os.path.join(module_path, script_relative_path)
2025
if not os.path.isfile(script_path):
2126
raise FileNotFoundError(script_path)
2227

2328
# get module import data
24-
path, module = os.path.split(script_path)
25-
_, package = os.path.split(path)
26-
module, _ = os.path.splitext(module)
29+
path, package = os.path.split(module_path)
30+
# _, package = os.path.split(path)
31+
module, _ = os.path.splitext(script_relative_path)
32+
module = ".".join(Path(module).parts)
2733

2834
# add path to module search
2935
sys_path: str = None
@@ -35,6 +41,7 @@ def inspect_pipeline_script(script_path: str) -> ModuleType:
3541

3642
# patch entry points to pipeline, sources and resources to prevent pipeline from running
3743
with patch.object(Pipeline, '__init__', patch__init__), patch.object(DltSource, '__init__', patch__init__), patch.object(PipeIterator, '__init__', patch__init__):
44+
print(f"Importing pipeline script from path {path} and module: {package}.{module}")
3845
return import_module(f"{package}.{module}")
3946
finally:
4047
# remove script module path

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "python-dlt"
3-
version = "0.2.0a5"
3+
version = "0.2.0a6"
44
description = "DLT is an open-source python-native scalable data loading framework that does not require any devops efforts to run."
55
authors = ["dltHub Inc. <[email protected]>"]
66
maintainers = [ "Marcin Rudolf <[email protected]>", "Adrian Brudaru <[email protected]>",]

tests/cli/test_cli.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def test_init_command_template() -> None:
3535
assert_init_files("debug_pipeline", "bigquery")
3636

3737

38-
def test_init_command_chess() -> None:
38+
def test_init_command_chess_verified_pipeline() -> None:
3939
_SOURCES.clear()
4040

4141
with set_working_dir(TEST_STORAGE_ROOT):
42-
init_command.init_command("chess_pipeline", "postgres", False)
42+
init_command.init_command("chess", "postgres", False)
4343
assert_init_files("chess_pipeline", "postgres")
4444

4545

tests/common/test_git.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from dlt.common.storages import FileStorage
66
from dlt.common.git import clone_repo, ensure_remote_head, git_custom_key_command
77

8-
from tests.utils import test_storage
8+
from tests.utils import test_storage, skipifwindows
99
from tests.common.utils import load_secret, modify_and_commit_file, restore_secret_storage_path
1010

1111

@@ -62,6 +62,7 @@ def test_clone_with_deploy_key_access_denied(test_storage: FileStorage) -> None:
6262
clone_repo(PRIVATE_REPO, repo_path, with_git_command=git_command)
6363

6464

65+
@skipifwindows
6566
def test_clone_with_deploy_key(test_storage: FileStorage) -> None:
6667
secret = load_secret("deploy_key")
6768
repo_path = test_storage.make_full_path("private_repo_access")
@@ -70,6 +71,7 @@ def test_clone_with_deploy_key(test_storage: FileStorage) -> None:
7071
ensure_remote_head(repo_path, with_git_command=git_command)
7172

7273

74+
@skipifwindows
7375
def test_repo_status_update(test_storage: FileStorage) -> None:
7476
secret = load_secret("deploy_key")
7577
repo_path = test_storage.make_full_path("private_repo_access")

tests/utils.py

+4
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ def assert_no_dict_key_starts_with(d: StrAny, key_prefix: str) -> None:
124124
skipifnotwindows = pytest.mark.skipif(
125125
platform.system() != "Windows", reason="runs only on windows"
126126
)
127+
128+
skipifwindows = pytest.mark.skipif(
129+
platform.system() == "Windows", reason="does not runs on windows"
130+
)

0 commit comments

Comments
 (0)