Skip to content

Commit 122b3fb

Browse files
adds incremental override and resource folder to state.json (#358)
* adds incremental override and resource folder to state.json * handles cases when state doesn't have incremental override * uses .get(i) in comparison * tidies
1 parent cecf405 commit 122b3fb

File tree

7 files changed

+127
-8
lines changed

7 files changed

+127
-8
lines changed

digital_land/cli.py

+51-4
Original file line numberDiff line numberDiff line change
@@ -642,15 +642,36 @@ def config_load_cmd(ctx, config_path):
642642
default="pipeline",
643643
help="directory containing the pipeline",
644644
)
645+
@click.option(
646+
"--resource-dir",
647+
type=click.Path(),
648+
default="collection/resource",
649+
help="directory containing resources",
650+
)
651+
@click.option("--incremental-override", type=click.BOOL, default=False)
645652
@click.option(
646653
"--output-path",
647654
"-o",
648655
type=click.Path(),
649656
default="state.json",
650657
help="path of the output state file",
651658
)
652-
def save_state_cmd(specification_dir, collection_dir, pipeline_dir, output_path):
653-
save_state(specification_dir, collection_dir, pipeline_dir, output_path)
659+
def save_state_cmd(
660+
specification_dir,
661+
collection_dir,
662+
pipeline_dir,
663+
resource_dir,
664+
incremental_override,
665+
output_path,
666+
):
667+
save_state(
668+
specification_dir,
669+
collection_dir,
670+
pipeline_dir,
671+
resource_dir,
672+
incremental_override,
673+
output_path,
674+
)
654675

655676

656677
@cli.command(
@@ -675,17 +696,43 @@ def save_state_cmd(specification_dir, collection_dir, pipeline_dir, output_path)
675696
default="pipeline",
676697
help="directory containing the pipeline",
677698
)
699+
@click.option(
700+
"--resource-dir",
701+
type=click.Path(),
702+
default="collection/resource",
703+
help="directory containing resources",
704+
)
705+
@click.option("--incremental-override", type=click.BOOL, default=False)
678706
@click.option(
679707
"--state-path",
680708
type=click.Path(),
681709
default="state.json",
682710
help="path of the output state file",
683711
)
684-
def check_state_cmd(specification_dir, collection_dir, pipeline_dir, state_path):
712+
def check_state_cmd(
713+
specification_dir,
714+
collection_dir,
715+
pipeline_dir,
716+
resource_dir,
717+
incremental_override,
718+
state_path,
719+
):
685720
# If the state isn't the same, use a non-zero return code so scripts can
686721
# detect this, and print a message. If it is the same, exit silenty wirh a
687722
# 0 retun code.
688-
diffs = compare_state(specification_dir, collection_dir, pipeline_dir, state_path)
723+
724+
if incremental_override:
725+
print("State comparison skipped as incremental override enabled")
726+
sys.exit(1)
727+
728+
diffs = compare_state(
729+
specification_dir,
730+
collection_dir,
731+
pipeline_dir,
732+
resource_dir,
733+
incremental_override,
734+
state_path,
735+
)
689736
if diffs:
690737
print(f"State differs from {state_path} - {', '.join(diffs)}")
691738
sys.exit(1)

digital_land/commands.py

+25-3
Original file line numberDiff line numberDiff line change
@@ -1405,29 +1405,51 @@ def organisation_check(**kwargs):
14051405
package.check(lpa_path, output_path)
14061406

14071407

1408-
def save_state(specification_dir, collection_dir, pipeline_dir, output_path):
1408+
def save_state(
1409+
specification_dir,
1410+
collection_dir,
1411+
pipeline_dir,
1412+
resource_dir,
1413+
incremental_override,
1414+
output_path,
1415+
):
14091416
state = State.build(
14101417
specification_dir=specification_dir,
14111418
collection_dir=collection_dir,
14121419
pipeline_dir=pipeline_dir,
1420+
resource_dir=resource_dir,
1421+
incremental_override=incremental_override,
14131422
)
14141423
state.save(
14151424
output_path=output_path,
14161425
)
14171426

14181427

1419-
def compare_state(specification_dir, collection_dir, pipeline_dir, state_path):
1428+
def compare_state(
1429+
specification_dir,
1430+
collection_dir,
1431+
pipeline_dir,
1432+
resource_dir,
1433+
incremental_override,
1434+
state_path,
1435+
):
14201436
"""Compares the current state against the one in state_path.
14211437
Returns a list of different elements, or None if they are the same."""
14221438
current = State.build(
14231439
specification_dir=specification_dir,
14241440
collection_dir=collection_dir,
14251441
pipeline_dir=pipeline_dir,
1442+
resource_dir=resource_dir,
1443+
incremental_override=incremental_override,
14261444
)
1445+
# in here current incremental override must be false
14271446

14281447
compare = State.load(state_path)
1448+
# we don't want to include whether the previous state was an incremental override in comparison
1449+
current.pop("incremental_override", None)
1450+
compare.pop("incremental_override", None)
14291451

14301452
if current == compare:
14311453
return None
14321454

1433-
return [i for i in current.keys() if current[i] != compare[i]]
1455+
return [i for i in current.keys() if current[i] != compare.get(i, "")]

digital_land/state.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@ def __init__(self, data):
1212
for k, v in data.items():
1313
self.__setitem__(k, v)
1414

15-
def build(specification_dir, collection_dir, pipeline_dir):
15+
def build(
16+
specification_dir,
17+
collection_dir,
18+
pipeline_dir,
19+
resource_dir,
20+
incremental_override,
21+
):
1622
"""Build a state object from the current configuration and code"""
1723
return State(
1824
{
@@ -21,7 +27,9 @@ def build(specification_dir, collection_dir, pipeline_dir):
2127
"collection": State.get_dir_hash(
2228
collection_dir, ["log/", "log.csv", "pipeline.mk", "resource/"]
2329
),
30+
"resource": State.get_dir_hash(resource_dir),
2431
"pipeline": State.get_dir_hash(pipeline_dir),
32+
"incremental_override": incremental_override,
2533
}
2634
)
2735

Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
resource here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this resource is different

tests/e2e/test_state.py

+36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
specification_hash = "ebe620f5228d01170b1857bad3e738aa432f5fd6"
88
collection_hash = "ed4c5979268ad880f7edbdc2047cfcfa6b9ee3b4"
99
pipeline_hash = "4a5a778d678db812e4f3d498a5aaa6f39af38d10"
10+
resource_hash = "063e908c6695671063dee27c534bf3471aa3f5d5"
1011

1112

1213
def get_code_hash():
@@ -26,6 +27,8 @@ def test_state(tmp_path):
2627
specification_dir=os.path.join(test_data_dir, "specification"),
2728
collection_dir=os.path.join(test_data_dir, "collection"),
2829
pipeline_dir=os.path.join(test_data_dir, "pipeline"),
30+
resource_dir=os.path.join(test_data_dir, "resource"),
31+
incremental_override=True,
2932
output_path=state_path,
3033
)
3134

@@ -36,18 +39,24 @@ def test_state(tmp_path):
3639
"code",
3740
"specification",
3841
"collection",
42+
"resource",
3943
"pipeline",
44+
"incremental_override",
4045
]
4146
assert state_data["code"] == get_code_hash()
4247
assert state_data["specification"] == specification_hash
4348
assert state_data["collection"] == collection_hash
4449
assert state_data["pipeline"] == pipeline_hash
50+
assert state_data["resource"] == resource_hash
51+
assert state_data["incremental_override"]
4552

4653
assert (
4754
compare_state(
4855
specification_dir=os.path.join(test_data_dir, "specification"),
4956
collection_dir=os.path.join(test_data_dir, "collection"),
5057
pipeline_dir=os.path.join(test_data_dir, "pipeline"),
58+
resource_dir=os.path.join(test_data_dir, "resource"),
59+
incremental_override=True,
5160
state_path=state_path,
5261
)
5362
is None
@@ -58,6 +67,8 @@ def test_state(tmp_path):
5867
specification_dir=os.path.join(test_data_dir, "specification"),
5968
collection_dir=os.path.join(test_data_dir, "collection_exclude"),
6069
pipeline_dir=os.path.join(test_data_dir, "pipeline"),
70+
resource_dir=os.path.join(test_data_dir, "resource"),
71+
incremental_override=True,
6172
state_path=state_path,
6273
)
6374
is None
@@ -67,5 +78,30 @@ def test_state(tmp_path):
6778
specification_dir=os.path.join(test_data_dir, "specification"),
6879
collection_dir=os.path.join(test_data_dir, "collection_blank"),
6980
pipeline_dir=os.path.join(test_data_dir, "pipeline"),
81+
resource_dir=os.path.join(test_data_dir, "resource"),
82+
incremental_override=True,
7083
state_path=state_path,
7184
) == ["collection"]
85+
86+
assert compare_state(
87+
specification_dir=os.path.join(test_data_dir, "specification"),
88+
collection_dir=os.path.join(test_data_dir, "collection"),
89+
pipeline_dir=os.path.join(test_data_dir, "pipeline"),
90+
resource_dir=os.path.join(test_data_dir, "resource_diff"),
91+
incremental_override=True,
92+
state_path=state_path,
93+
) == ["resource"]
94+
95+
# we shouldn't include the incremental override value in state comparison
96+
# so test it isn't flagged if different
97+
assert (
98+
compare_state(
99+
specification_dir=os.path.join(test_data_dir, "specification"),
100+
collection_dir=os.path.join(test_data_dir, "collection"),
101+
pipeline_dir=os.path.join(test_data_dir, "pipeline"),
102+
resource_dir=os.path.join(test_data_dir, "resource"),
103+
incremental_override=False,
104+
state_path=state_path,
105+
)
106+
is None
107+
)

tests/integration/test_state.py

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ def test_state_build_persist(tmp_path):
6565
os.path.join(test_dir, "specification"),
6666
os.path.join(test_dir, "collection"),
6767
os.path.join(test_dir, "pipeline"),
68+
os.path.join(test_dir, "resource"),
69+
True,
6870
)
6971
state_1.save(tmp_json)
7072

@@ -77,6 +79,8 @@ def test_state_build_persist(tmp_path):
7779
os.path.join(test_dir, "specification"),
7880
os.path.join(test_dir, "collection_blank"),
7981
os.path.join(test_dir, "pipeline"),
82+
os.path.join(test_dir, "resource"),
83+
True,
8084
)
8185

8286
# Check that's different from the first one

0 commit comments

Comments
 (0)