Skip to content

Commit

Permalink
Merge pull request #115 from olehermanse/directories
Browse files Browse the repository at this point in the history
Fixed inconsistency when handling local folders as modules
  • Loading branch information
olehermanse authored May 25, 2022
2 parents 7cd6af5 + 3f469c6 commit 978fac0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 14 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,19 @@ out/steps/001_masterfiles_5c7dc5b43088e259a94de4e5a9f17c0ce9781a0f/
The `destination` parameter below should all be relative paths to `out/masterfiles`.



### Available steps are:

Note that the build steps are run inside each modules directory (the temporary copy of it inside `out/steps`).
Scripts and source files are read from that directory.
Destination is relative to the output policy set (`out/masterfiles`).

#### `copy <source> <destination>`
- Copy a single file or a directory recursively.

#### `run <command>`
- Run a command in the module's `out/steps` directory as mentioned above.
- Run a shell command / script.
- Usually used to prepare the module directory, delete files, etc. before a copy step.
- Running scripts should be avoided if possible.

#### `delete <paths ...>`
- Delete multiple files or paths recursively.
Expand Down
13 changes: 11 additions & 2 deletions cfbs/cfbs_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,26 @@ def validate_added_module(module):
log.warning(' meta: "tags" slist => { "autorun" };')

@classmethod
def get_instance(cls, index=None):
def get_instance(cls, index=None, non_interactive=False):
if cls.instance is not None:
if index is not None:
raise RuntimeError(
"Instance of %s already exists, cannot specify index" % cls.__name__
)
else:
cls.instance = cls(index)
cls.instance = cls(index, non_interactive)
return cls.instance

@classmethod
def reload(cls):
if not cls.instance:
return
# Create a new instance with the same __init__ args as last time:
index, non_interactive = cls.instance._reload_args
cls.instance = cls(index, non_interactive)

def __init__(self, index=None, non_interactive=False):
self._reload_args = (index, non_interactive)
super().__init__(path="./cfbs.json", index_argument=index)
self.non_interactive = non_interactive

Expand Down
7 changes: 3 additions & 4 deletions cfbs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,10 @@ def init_command(index=None, non_interactive=False) -> int:

"""
The CFBSConfig instance was initally created in main(). Back then
cfbs.json did not exist, thus the instance is empty. By manually deleting
this instance, a new instance will be created loading the now existing
cfbs.json.
cfbs.json did not exist, thus the instance is empty. Ensure it is reloaded
now that the JSON exists.
"""
CFBSConfig.instance = None
CFBSConfig.reload()

if prompt_user(
"Do you wish to build on top of the default policy set, masterfiles? (Recommended)",
Expand Down
3 changes: 2 additions & 1 deletion cfbs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ def _local_module_data_json_file(module):

def _local_module_data_subdir(module):
assert module.startswith("./")
assert module.endswith(("/", "/."))
dst = os.path.join("services", "cfbs", module[2:])
return {
"description": "Local subdirectory added using cfbs command line",
"tags": ["local"],
"steps": ["directory {} {}".format(module, dst)],
"steps": ["directory ./ {}".format(dst)],
"added_by": "cfbs add",
}

Expand Down
9 changes: 8 additions & 1 deletion cfbs/internal_file_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,14 @@ def local_module_copy(module, counter, max_length):
target = "out/steps/%03d_%s_local/" % (counter, pretty_name)
module["_directory"] = target
module["_counter"] = counter
cp(name, target + name)
if name.endswith(("/", "/.")):
# If this is a local folder, the target should be a copy of the folder
# (Don't create an extra unnecessary subfolder)
cp(name, target)
else:
# If this is not a folder it is a file
# create a copy of that file in the target folder
cp(name, target + name)
print(
"%03d %s @ local (Copied)"
% (counter, pad_right(name, max_length))
Expand Down
3 changes: 1 addition & 2 deletions cfbs/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ def main() -> int:
_get_arg_parser().print_help()
return 0

config = CFBSConfig.get_instance(args.index)
config.non_interactive = args.non_interactive
config = CFBSConfig.get_instance(args.index, args.non_interactive)

if args.command == "init":
return commands.init_command(
Expand Down
4 changes: 2 additions & 2 deletions test/shell/016_add_folders.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ cfbs status

cfbs status | grep "./one/"
cfbs status | grep "./two/"
cat cfbs.json | grep "directory ./one/ services/cfbs/one/"
cat cfbs.json | grep "directory ./two/ services/cfbs/two/"
cat cfbs.json | grep "directory ./ services/cfbs/one/"
cat cfbs.json | grep "directory ./ services/cfbs/two/"

cfbs build

Expand Down

0 comments on commit 978fac0

Please sign in to comment.