Skip to content

Commit 753a751

Browse files
authored
Ability to change working directory in unstable module. (#453)
1 parent 6a0a3f1 commit 753a751

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Changes are grouped as follows
1616

1717
### Added
1818

19+
* Ability for users to customize the working directory of the extractor using the `--cwd` flag in the CLI.
1920
* Added a new command-line argument, --log-level (-l) to allow users to override configured log levels for a single run.
2021

2122
## 7.7.0

cognite/extractorutils/unstable/core/runtime.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ def _create_argparser(self) -> ArgumentParser:
127127
action="store_true",
128128
help="Skip any checks during startup. Useful for debugging, not recommended for production deployments.",
129129
)
130+
argparser.add_argument(
131+
"--cwd",
132+
nargs=1,
133+
type=Path,
134+
required=False,
135+
help="Set the current working directory for the extractor.",
136+
)
130137

131138
return argparser
132139

@@ -209,6 +216,17 @@ def _try_get_application_config(
209216

210217
return application_config, current_config_revision
211218

219+
def _try_change_cwd(self, cwd: Path | None) -> None:
220+
if cwd is not None:
221+
try:
222+
os.chdir(cwd)
223+
self.logger.info(f"Changed working directory to {cwd}")
224+
except OSError as e:
225+
self.logger.critical(f"Could not change working directory to {cwd}: {e}")
226+
raise InvalidConfigError(f"Could not change working directory to {cwd}") from e
227+
228+
self.logger.info(f"Using {os.getcwd()} as working directory")
229+
212230
def _safe_get_application_config(
213231
self,
214232
args: Namespace,
@@ -314,6 +332,7 @@ def run(self) -> None:
314332
self.logger.info(f"Started runtime with PID {os.getpid()}")
315333

316334
try:
335+
self._try_change_cwd(args.cwd[0])
317336
connection_config = load_file(args.connection_config[0], ConnectionConfig)
318337
except InvalidConfigError as e:
319338
self.logger.error(str(e))

tests/test_unstable/conftest.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
)
1919
from cognite.extractorutils.unstable.core.base import Extractor, StartupTask, TaskContext
2020

21+
working_dir = os.getcwd()
22+
23+
24+
@pytest.fixture(autouse=True)
25+
def reset_environment() -> Generator[None, None, None]:
26+
yield
27+
os.chdir(working_dir)
28+
2129

2230
@pytest.fixture
2331
def set_client() -> CogniteClient:

tests/test_unstable/test_runtime.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import time
23
from argparse import Namespace
34
from collections.abc import Generator
@@ -119,3 +120,12 @@ def cancel_after_delay() -> None:
119120

120121
assert len(errors["items"]) == 1
121122
assert "No configuration found for the given integration" in errors["items"][0]["description"]
123+
124+
125+
def test_changing_cwd() -> None:
126+
runtime = Runtime(TestExtractor)
127+
original_cwd = os.getcwd()
128+
runtime._try_change_cwd(Path(__file__).parent)
129+
130+
assert os.getcwd() == str(Path(__file__).parent)
131+
assert os.getcwd() != original_cwd

0 commit comments

Comments
 (0)