Skip to content

Commit 55ddcf5

Browse files
authored
Merge pull request #149 from unity-sds/output-to-file
feat: writing output content to a file if ENV is provided
2 parents 854f8e8 + b582adc commit 55ddcf5

File tree

12 files changed

+99
-18
lines changed

12 files changed

+99
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [3.8.0] - 2023-05-04
9+
### Added
10+
- [#149](https://github.com/unity-sds/unity-data-services/pull/149) feat: writing output content to a file if ENV is provided
11+
812
## [3.7.1] - 2023-05-04
913
### Changed
1014
- [#148](https://github.com/unity-sds/unity-data-services/pull/148) fix: use cas structure to generate metadata for stac

cumulus_lambda_functions/docker_entrypoint/__main__.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,32 @@
44

55
from cumulus_lambda_functions.stage_in_out.catalog_granules_factory import CatalogGranulesFactory
66
from cumulus_lambda_functions.stage_in_out.download_granules_factory import DownloadGranulesFactory
7-
from cumulus_lambda_functions.stage_in_out.download_granules_s3 import DownloadGranulesS3
87
from cumulus_lambda_functions.stage_in_out.search_granules_factory import SearchGranulesFactory
8+
from cumulus_lambda_functions.stage_in_out.stage_in_out_utils import StageInOutUtils
99
from cumulus_lambda_functions.stage_in_out.upoad_granules_factory import UploadGranulesFactory
1010

1111

1212
def choose_process():
1313
if argv[1].strip().upper() == 'SEARCH':
1414
logging.info('starting SEARCH script')
15-
return SearchGranulesFactory().get_class(os.getenv('GRANULES_SEARCH_DOMAIN', 'MISSING_GRANULES_SEARCH_DOMAIN')).search()
15+
result_str = SearchGranulesFactory().get_class(os.getenv('GRANULES_SEARCH_DOMAIN', 'MISSING_GRANULES_SEARCH_DOMAIN')).search()
16+
StageInOutUtils.write_output_to_file(result_str)
17+
return result_str
1618
if argv[1].strip().upper() == 'DOWNLOAD':
1719
logging.info('starting DOWNLOAD script')
18-
return DownloadGranulesFactory().get_class(os.getenv('GRANULES_DOWNLOAD_TYPE', 'MISSING_GRANULES_DOWNLOAD_TYPE')).download()
20+
result_str = DownloadGranulesFactory().get_class(os.getenv('GRANULES_DOWNLOAD_TYPE', 'MISSING_GRANULES_DOWNLOAD_TYPE')).download()
21+
StageInOutUtils.write_output_to_file(result_str)
22+
return result_str
1923
if argv[1].strip().upper() == 'UPLOAD':
2024
logging.info('starting UPLOAD script')
21-
return UploadGranulesFactory().get_class(os.getenv('GRANULES_UPLOAD_TYPE', 'MISSING_GRANULES_UPLOAD_TYPE')).upload()
25+
result_str = UploadGranulesFactory().get_class(os.getenv('GRANULES_UPLOAD_TYPE', 'MISSING_GRANULES_UPLOAD_TYPE')).upload()
26+
StageInOutUtils.write_output_to_file(result_str)
27+
return result_str
2228
if argv[1].strip().upper() == 'CATALOG':
2329
logging.info('starting CATALOG script')
24-
return CatalogGranulesFactory().get_class(os.getenv('GRANULES_CATALOG_TYPE', 'MISSING_GRANULES_CATALOG_TYPE')).catalog()
30+
result_str = CatalogGranulesFactory().get_class(os.getenv('GRANULES_CATALOG_TYPE', 'MISSING_GRANULES_CATALOG_TYPE')).catalog()
31+
StageInOutUtils.write_output_to_file(result_str)
32+
return result_str
2533
raise ValueError(f'invalid argument: {argv}')
2634

2735

cumulus_lambda_functions/stage_in_out/catalog_granules_unity.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import os
66

7+
78
LOGGER = logging.getLogger(__name__)
89

910

cumulus_lambda_functions/stage_in_out/download_granules_daac.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import logging
77
import os
88

9+
910
LOGGER = logging.getLogger(__name__)
1011

1112

cumulus_lambda_functions/stage_in_out/search_granules_cmr.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@ def search(self, **kwargs) -> str:
100100
if len(temp_results) < page_size:
101101
break
102102
results = self.__get_correct_result_count(results)
103-
return json.dumps(StacUtils.reduce_stac_list_to_data_links(results)) if self.__filter_results else json.dumps(results)
103+
results = StacUtils.reduce_stac_list_to_data_links(results) if self.__filter_results else results
104+
return json.dumps(results)

cumulus_lambda_functions/stage_in_out/search_granules_unity.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from cumulus_lambda_functions.cumulus_dapa_client.dapa_client import DapaClient
66
from cumulus_lambda_functions.cumulus_stac.stac_utils import StacUtils
7-
from cumulus_lambda_functions.lib.utils.file_utils import FileUtils
87
from cumulus_lambda_functions.stage_in_out.search_granules_abstract import SearchGranulesAbstract
98

109
LOGGER = logging.getLogger(__name__)
@@ -51,4 +50,5 @@ def search(self, **kwargs) -> str:
5150
self.__set_props_from_env()
5251
dapa_client = DapaClient().with_verify_ssl(self.__verify_ssl)
5352
granules_result = dapa_client.get_all_granules(self.__collection_id, self.__limit, self.__date_from, self.__date_to)
54-
return json.dumps(StacUtils.reduce_stac_list_to_data_links(granules_result)) if self.__filter_results else json.dumps(granules_result)
53+
granules_result = StacUtils.reduce_stac_list_to_data_links(granules_result) if self.__filter_results else granules_result
54+
return json.dumps(granules_result)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import json
2+
import logging
3+
import os
4+
from typing import Union
5+
6+
from cumulus_lambda_functions.lib.utils.file_utils import FileUtils
7+
8+
LOGGER = logging.getLogger(__name__)
9+
10+
11+
class StageInOutUtils:
12+
OUTPUT_FILE = 'OUTPUT_FILE'
13+
14+
@staticmethod
15+
def write_output_to_file(output_json: Union[dict, str, list]):
16+
if StageInOutUtils.OUTPUT_FILE not in os.environ:
17+
LOGGER.debug(f'Not writing output to file due to missing {StageInOutUtils.OUTPUT_FILE} in ENV')
18+
return
19+
output_filepath = os.environ.get(StageInOutUtils.OUTPUT_FILE)
20+
FileUtils.mk_dir_p(os.path.dirname(output_filepath))
21+
output_str = json.dumps(output_json) if not isinstance(output_json, str) else output_json
22+
with open(output_filepath, 'w') as ff:
23+
ff.write(output_str)
24+
return

cumulus_lambda_functions/stage_in_out/upload_granules_s3.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22

33
from cumulus_lambda_functions.stage_in_out.search_collections_factory import SearchCollectionsFactory
4-
from cumulus_lambda_functions.stage_in_out.search_granules_factory import SearchGranulesFactory
54
from cumulus_lambda_functions.stage_in_out.upload_granules_abstract import UploadGranulesAbstract
65
import logging
76
import os

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
setup(
2020
name="cumulus_lambda_functions",
21-
version="3.7.1",
21+
version="3.8.0",
2222
packages=find_packages(),
2323
install_requires=install_requires,
2424
tests_require=['mock', 'nose', 'sphinx', 'sphinx_rtd_theme', 'coverage', 'pystac', 'python-dotenv', 'jsonschema'],

tests/cumulus_lambda_functions/stage_in_out/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)