Skip to content

Commit 4cab6a8

Browse files
committed
Add user configurable tempory directory
1 parent fb0bd36 commit 4cab6a8

4 files changed

Lines changed: 28 additions & 28 deletions

File tree

pipelinewise/cli/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
PIPELINEWISE_HOME = os.path.abspath(
2727
os.environ.setdefault('PIPELINEWISE_HOME', PIPELINEWISE_DEFAULT_HOME)
2828
)
29+
TEMP_DIR = os.environ.get('PIPELINEWISE_TEMP_DIRECTORY')
2930
VENV_DIR = os.path.join(PIPELINEWISE_HOME, '.virtualenvs')
3031
COMMANDS = [
3132
'init',
@@ -248,7 +249,9 @@ def main():
248249

249250
profiler, profiling_dir = __init_profiler(args.profiler, logger)
250251

251-
ppw_instance = PipelineWise(args, CONFIG_DIR, VENV_DIR, profiling_dir)
252+
ppw_instance = PipelineWise(
253+
args, CONFIG_DIR, VENV_DIR, temp_dir=TEMP_DIR, profiling_dir=profiling_dir
254+
)
252255

253256
try:
254257
getattr(ppw_instance, args.command)()

pipelinewise/cli/config.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class Config:
1616
"""PipelineWise Configuration Class"""
1717

18-
def __init__(self, config_dir):
18+
def __init__(self, config_dir, temp_dir=None):
1919
"""
2020
Class Constructor
2121
@@ -24,12 +24,15 @@ def __init__(self, config_dir):
2424
self.logger = logging.getLogger(__name__)
2525
self.config_dir = config_dir
2626
self.config_path = os.path.join(self.config_dir, 'config.json')
27+
self.temp_dir = temp_dir
28+
if self.temp_dir is None:
29+
self.temp_dir = os.path.join(self.config_dir, 'tmp')
2730
self.global_config = {}
2831
self.targets = {}
2932

3033
@classmethod
3134
# pylint: disable=too-many-locals
32-
def from_yamls(cls, config_dir, yaml_dir='.', vault_secret=None):
35+
def from_yamls(cls, config_dir, yaml_dir='.', vault_secret=None, temp_dir=None):
3336
"""
3437
Class Constructor
3538
@@ -38,7 +41,7 @@ def from_yamls(cls, config_dir, yaml_dir='.', vault_secret=None):
3841
Pipelinewise can import and generate singer configurations files
3942
from human friendly easy to understand YAML files.
4043
"""
41-
config = cls(config_dir)
44+
config = cls(config_dir, temp_dir=temp_dir)
4245
targets = {}
4346
taps = {}
4447

@@ -124,12 +127,6 @@ def from_yamls(cls, config_dir, yaml_dir='.', vault_secret=None):
124127

125128
return config
126129

127-
def get_temp_dir(self):
128-
"""
129-
Returns the tap specific temp directory
130-
"""
131-
return os.path.join(self.config_dir, 'tmp')
132-
133130
def get_target_dir(self, target_id):
134131
"""
135132
Returns the absolute path of a target configuration directory
@@ -177,7 +174,7 @@ def save(self):
177174
# Save every tap JSON files
178175
for tap in target['taps']:
179176
extra_config_keys = utils.get_tap_extra_config_keys(
180-
tap, self.get_temp_dir()
177+
tap, self.temp_dir
181178
)
182179
self.save_tap_jsons(target, tap, extra_config_keys)
183180

@@ -404,7 +401,7 @@ def generate_inheritable_config(self, tap: Dict) -> Dict:
404401
# Generate tap inheritable_config dict
405402
tap_inheritable_config = utils.delete_empty_keys(
406403
{
407-
'temp_dir': self.get_temp_dir(),
404+
'temp_dir': self.temp_dir,
408405
'tap_id': tap.get('id'),
409406
'query_tag': json.dumps(
410407
{

pipelinewise/cli/pipelinewise.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class PipelineWise:
6464
STATUS_FAILED = 'FAILED'
6565
TRANSFORM_FIELD_CONNECTOR_NAME = 'transform-field'
6666

67-
def __init__(self, args, config_dir, venv_dir, profiling_dir=None):
67+
def __init__(self, args, config_dir, venv_dir, profiling_dir=None, temp_dir=None):
6868

6969
self.profiling_mode = args.profiler
7070
self.profiling_dir = profiling_dir
@@ -73,6 +73,9 @@ def __init__(self, args, config_dir, venv_dir, profiling_dir=None):
7373
self.logger = logging.getLogger(__name__)
7474
self.config_dir = config_dir
7575
self.venv_dir = venv_dir
76+
self.temp_dir = temp_dir
77+
if self.temp_dir is None:
78+
self.temp_dir = os.path.join(self.config_dir, 'tmp')
7679
self.extra_log = args.extra_log
7780
self.pipelinewise_bin = os.path.join(
7881
self.venv_dir, 'cli', 'bin', 'pipelinewise'
@@ -141,7 +144,7 @@ def create_consumable_target_config(self, target_config, tap_inheritable_config)
141144

142145
# Save the new dict as JSON into a temp file
143146
tempfile_path = utils.create_temp_file(
144-
dir=self.get_temp_dir(), prefix='target_config_', suffix='.json'
147+
dir=self.temp_dir, prefix='target_config_', suffix='.json'
145148
)[1]
146149
utils.save_json(dict_a, tempfile_path)
147150

@@ -324,12 +327,12 @@ def create_filtered_tap_properties(
324327
if create_fallback:
325328
# Save to files: filtered and fallback properties
326329
temp_properties_path = utils.create_temp_file(
327-
dir=self.get_temp_dir(), prefix='properties_', suffix='.json'
330+
dir=self.temp_dir, prefix='properties_', suffix='.json'
328331
)[1]
329332
utils.save_json(properties, temp_properties_path)
330333

331334
temp_fallback_properties_path = utils.create_temp_file(
332-
dir=self.get_temp_dir(), prefix='properties_', suffix='.json'
335+
dir=self.temp_dir, prefix='properties_', suffix='.json'
333336
)[1]
334337
utils.save_json(fallback_properties, temp_fallback_properties_path)
335338

@@ -342,7 +345,7 @@ def create_filtered_tap_properties(
342345

343346
# Fallback not required: Save only the filtered properties JSON
344347
temp_properties_path = utils.create_temp_file(
345-
dir=self.get_temp_dir(), prefix='properties_', suffix='.json'
348+
dir=self.temp_dir, prefix='properties_', suffix='.json'
346349
)[1]
347350
utils.save_json(properties, temp_properties_path)
348351

@@ -363,12 +366,6 @@ def load_config(self):
363366
else:
364367
self.config = {}
365368

366-
def get_temp_dir(self):
367-
"""
368-
Returns the tap specific temp directory
369-
"""
370-
return os.path.join(self.config_dir, 'tmp')
371-
372369
def get_tap_dir(self, target_id, tap_id):
373370
"""
374371
Get absolute path of a tap directory
@@ -1083,7 +1080,7 @@ def run_tap_fastsync(
10831080
target=target,
10841081
transform=transform,
10851082
venv_dir=self.venv_dir,
1086-
temp_dir=self.get_temp_dir(),
1083+
temp_dir=self.temp_dir,
10871084
tables=self.args.tables,
10881085
profiling_mode=self.profiling_mode,
10891086
profiling_dir=self.profiling_dir,
@@ -1565,7 +1562,9 @@ def import_project(self):
15651562
"""
15661563
# Read the YAML config files and transform/save into singer compatible
15671564
# JSON files in a common directory structure
1568-
config = Config.from_yamls(self.config_dir, self.args.dir, self.args.secret)
1565+
config = Config.from_yamls(
1566+
self.config_dir, self.args.dir, self.args.secret, self.temp_dir
1567+
)
15691568
config.save()
15701569

15711570
# Activating tap stream selections
@@ -1801,7 +1800,7 @@ def __validate_transformations(
18011800
# create a temp file with the content being the given catalog object
18021801
# we need this file to execute the validation cli command
18031802
temp_catalog_file = utils.create_temp_file(
1804-
dir=self.get_temp_dir(), prefix='properties_', suffix='.json'
1803+
dir=self.temp_dir, prefix='properties_', suffix='.json'
18051804
)[1]
18061805

18071806
utils.save_json(catalog, temp_catalog_file)

tests/units/cli/test_config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from pipelinewise.cli.errors import InvalidConfigException
88

99
PIPELINEWISE_TEST_HOME = '/tmp/.pipelinewise'
10+
PIPELINEWISE_TEST_TEMP_DIR = '/tmp/pipelinewise_tmp'
1011

1112

1213
# Todo: Inherit from unittest.TestCase
@@ -208,10 +209,10 @@ def test_from_invalid_yamls_fails(self):
208209

209210
def test_getters(self):
210211
"""Test Config getter functions"""
211-
config = Config(PIPELINEWISE_TEST_HOME)
212+
config = Config(PIPELINEWISE_TEST_HOME, PIPELINEWISE_TEST_TEMP_DIR)
212213

213214
# Target and tap directory should be g
214-
assert config.get_temp_dir() == '{}/tmp'.format(PIPELINEWISE_TEST_HOME)
215+
assert config.temp_dir == PIPELINEWISE_TEST_TEMP_DIR
215216
assert config.get_target_dir('test-target-id') == '{}/test-target-id'.format(
216217
PIPELINEWISE_TEST_HOME
217218
)

0 commit comments

Comments
 (0)