-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from carte-data/DE-211-delete-removed-datasets
DE-211: publisher for removing deleted datasets
- Loading branch information
Showing
7 changed files
with
124 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import os | ||
import glob | ||
from typing import Iterator, List, Set | ||
from databuilder.publisher.base_publisher import Publisher | ||
from pyhocon.config_tree import ConfigTree | ||
|
||
|
||
class RemoveDeletedPublisher(Publisher): | ||
def init(self, conf: ConfigTree) -> None: | ||
self.conf = conf | ||
self.manifests_path = self.conf.get_string("manifests_path") | ||
self.tables_path = self.conf.get_string("tables_output_path") | ||
|
||
if self.tables_path is None: | ||
raise ValueError("Output path is needed for publisher") | ||
|
||
def _get_datasets_to_delete( | ||
self, datasets: Set[str], file_paths: List[str] | ||
) -> Iterator[str]: | ||
file_ids = [ | ||
path[(len(self.tables_path) + 1) : -(len(".md"))] for path in file_paths | ||
] | ||
|
||
for file_path, file_id in zip(file_paths, file_ids): | ||
if file_id not in datasets: | ||
yield file_path | ||
|
||
def publish_impl(self) -> None: | ||
print("Publishing") | ||
print(f"Tables path: {self.tables_path}") | ||
with open(self.manifests_path) as f: | ||
lines = f.readlines() | ||
datasets = set([line.strip() for line in lines]) | ||
|
||
file_paths = glob.glob(self.tables_path + "/*/*/*.md", recursive=True) | ||
|
||
for file_path in self._get_datasets_to_delete(datasets, file_paths): | ||
os.remove(file_path) | ||
print(f"Removed {file_path}") | ||
|
||
def get_scope(self) -> str: | ||
return "publisher.carte" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from carte_cli.publisher.remove_deleted_publisher import RemoveDeletedPublisher | ||
|
||
|
||
def test_get_datasets_to_delete(): | ||
publisher = RemoveDeletedPublisher() | ||
tables_path = "test-tables-path" | ||
|
||
publisher.tables_path = tables_path | ||
|
||
test_datasets = set( | ||
[ | ||
f"db1/dataset1", | ||
f"db1/dataset3", | ||
f"db1/dataset4", | ||
f"db1/dataset5", | ||
f"db2/dataset1", | ||
] | ||
) | ||
|
||
test_file_paths = [ | ||
f"{tables_path}/db1/dataset1.md", | ||
f"{tables_path}/db2/dataset1.md", | ||
f"{tables_path}/db2/dataset2.md", | ||
f"{tables_path}/db1/dataset3.md", | ||
f"{tables_path}/db1/dataset4.md", | ||
f"{tables_path}/db3/dataset1.md", | ||
] | ||
|
||
to_delete = [ | ||
dataset | ||
for dataset in publisher._get_datasets_to_delete(test_datasets, test_file_paths) | ||
] | ||
|
||
assert len(to_delete) == 2 | ||
assert "test-tables-path/db3/dataset1.md" in to_delete | ||
assert "test-tables-path/db2/dataset2.md" in to_delete |