File tree Expand file tree Collapse file tree 4 files changed +38
-0
lines changed
Expand file tree Collapse file tree 4 files changed +38
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 ))
Original file line number Diff line number Diff line change 1818)
1919from 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
2331def set_client () -> CogniteClient :
Original file line number Diff line number Diff line change 1+ import os
12import time
23from argparse import Namespace
34from 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
You can’t perform that action at this time.
0 commit comments