Skip to content

Commit 054d435

Browse files
committed
Moved and renamed functions and clarified documentation related to build step splitting
Signed-off-by: jakub-nt <175944085+jakub-nt@users.noreply.github.com>
1 parent 796c41e commit 054d435

4 files changed

Lines changed: 36 additions & 19 deletions

File tree

JSON.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ These are copies of the module directories, where it's more "safe" to do things
222222
## All available build steps
223223

224224
The build steps below manipulate the temporary files in the steps directories and write results to the output policy set, in `out/masterfiles`.
225-
Unless otherwise noted, all steps are run inside the module's folder (`out/steps/...`) with sources / file paths relative to that folder, and targets / destinations mentioned below are relative to the output policy set (`out/masterfiles`, which in the end will be deployed as `/var/cfengine/masterfiles`).
225+
Unless otherwise noted, all steps are run inside the module's folder (`out/steps/...`) with sources / file paths relative to that folder, and targets / destinations mentioned below are relative to the output policy set (`out/masterfiles`, which in the end will be deployed as `/var/cfengine/masterfiles`). In `cfbs.json`'s `"steps"`, the build step name must be separated from the rest of the build step by a regular space.
226226

227227
* `copy <source> <destination>`
228228
* Copy a single file or a directory recursively.

cfbs/build.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
import os
22
import logging as log
3+
from typing import List, Tuple
34
from cfbs.utils import (
45
canonify,
56
cp,
67
deduplicate_def_json,
78
find,
8-
is_valid_arg_count,
99
merge_json,
1010
mkdir,
1111
pad_right,
1212
read_json,
1313
rm,
1414
sh,
15-
split_command,
1615
strip_left,
1716
touch,
1817
user_error,
@@ -73,8 +72,31 @@ def _generate_augment(module_name, input_data):
7372
return augment
7473

7574

75+
def split_build_step(command) -> Tuple[str, List[str]]:
76+
terms = command.split(" ")
77+
operation, args = terms[0], terms[1:]
78+
return operation, args
79+
80+
81+
def step_has_valid_arg_count(args, expected):
82+
actual = len(args)
83+
84+
if type(expected) is int:
85+
if actual != expected:
86+
return False
87+
88+
else:
89+
# Only other option is a string of 1+, 2+ or similar:
90+
assert type(expected) is str and expected.endswith("+")
91+
expected = int(expected[0:-1])
92+
if actual < expected:
93+
return False
94+
95+
return True
96+
97+
7698
def _perform_build_step(module, step, max_length):
77-
operation, args = split_command(step)
99+
operation, args = split_build_step(step)
78100
source = module["_directory"]
79101
counter = module["_counter"]
80102
destination = "out/masterfiles"
@@ -245,7 +267,7 @@ def perform_build_steps(config) -> int:
245267
# mini-validation
246268
for module in config.get("build", []):
247269
for step in module["steps"]:
248-
operation, args = split_command(step)
270+
operation, args = split_build_step(step)
249271

250272
if step.split() != [operation] + args:
251273
user_error(
@@ -258,7 +280,7 @@ def perform_build_steps(config) -> int:
258280

259281
expected = AVAILABLE_BUILD_STEPS[operation]
260282
actual = len(args)
261-
if not is_valid_arg_count(args, expected):
283+
if not step_has_valid_arg_count(args, expected):
262284
if type(expected) is int:
263285
user_error(
264286
"The `%s` build step expects %d arguments, %d were given"

cfbs/utils.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import copy
66
import subprocess
77
import hashlib
8-
from typing import List, Tuple
98
import urllib
109
import urllib.request # needed on some platforms
1110
from collections import OrderedDict
@@ -86,12 +85,6 @@ def pad_right(s, n):
8685
return s.ljust(n)
8786

8887

89-
def split_command(command) -> Tuple[str, List[str]]:
90-
terms = command.split(" ")
91-
operation, args = terms[0], terms[1:]
92-
return operation, args
93-
94-
9588
def is_valid_arg_count(args, expected):
9689
actual = len(args)
9790

@@ -173,7 +166,7 @@ def read_json(path) -> OrderedDict:
173166
except NotADirectoryError:
174167
return None
175168
except json.decoder.JSONDecodeError as ex:
176-
print("Error reading json file {} : {}".format(path, ex))
169+
print("Error reading json file '{}': {}".format(path, ex))
177170
sys.exit(1)
178171

179172

cfbs/validate.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import argparse
2-
import json
32
import sys
43
import re
54
from collections import OrderedDict
65

7-
from cfbs.utils import is_valid_arg_count, is_a_commit_hash, split_command, user_error
6+
from cfbs.utils import (
7+
is_a_commit_hash,
8+
user_error,
9+
)
810
from cfbs.pretty import TOP_LEVEL_KEYS, MODULE_KEYS
911
from cfbs.cfbs_config import CFBSConfig
10-
from cfbs.build import AVAILABLE_BUILD_STEPS
12+
from cfbs.build import AVAILABLE_BUILD_STEPS, step_has_valid_arg_count, split_build_step
1113

1214

1315
class CFBSValidationError(Exception):
@@ -266,7 +268,7 @@ def validate_steps(name, module):
266268
raise CFBSValidationError(
267269
name, '"steps" must be a list of non-empty / non-whitespace strings'
268270
)
269-
operation, args = split_command(step)
271+
operation, args = split_build_step(step)
270272
if not operation in AVAILABLE_BUILD_STEPS:
271273
x = ", ".join(AVAILABLE_BUILD_STEPS)
272274
raise CFBSValidationError(
@@ -276,7 +278,7 @@ def validate_steps(name, module):
276278
)
277279
expected = AVAILABLE_BUILD_STEPS[operation]
278280
actual = len(args)
279-
if not is_valid_arg_count(args, expected):
281+
if not step_has_valid_arg_count(args, expected):
280282
if type(expected) is int:
281283
raise CFBSValidationError(
282284
name,

0 commit comments

Comments
 (0)