-
Notifications
You must be signed in to change notification settings - Fork 15
CFE-4074: Added a new input-type: file #330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -243,7 +243,86 @@ def _perform_directory_step(args, source, destination, prefix): | |
| write_json(defjson, merged) | ||
|
|
||
|
|
||
| def _perform_input_step(args, name, destination, prefix): | ||
| def _path_if_already_shipped(path, build_modules, destination): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So this function both tests if the file is already shipped and computes the if not? It feels a bit messy, but I don't know how to make it better. |
||
| """If `path` is already inside a local module's directory that has its own | ||
| "directory" build step shipping it to masterfiles, return the on-host path | ||
| that step will produce. That step already copies the whole directory | ||
| during this same build, so the caller doesn't need to (and shouldn't) | ||
| copy the file itself - just point at where it will end up. | ||
| """ | ||
| abs_path = os.path.abspath(path) | ||
| for module in build_modules: | ||
| module_name = module.get("name", "") | ||
| if not (module_name.startswith("./") and module_name.endswith("/")): | ||
| continue | ||
| module_root = os.path.abspath(module_name) | ||
|
|
||
| if os.path.commonpath([abs_path, module_root]) != module_root: | ||
| # Only a module that contains the file can be the one shipping it. | ||
| # Without this, we'd match some other module's directory step and | ||
| # return a non-None "already shipped" path, suppressing the copy the | ||
| # file actually needs. | ||
| continue | ||
|
SimonThalvorsen marked this conversation as resolved.
|
||
| for step in module.get("steps", []): | ||
| operation, args = split_build_step(step) | ||
| if operation != "directory" or len(args) != 2: | ||
| continue | ||
| src, dst = args | ||
| if src not in (".", "./"): | ||
| continue | ||
| rel = os.path.relpath(abs_path, module_root) | ||
| dst = "" if dst in (".", "./") else dst | ||
| dest = os.path.join(destination, dst, rel) | ||
| return "$(sys.inputdir)/" + os.path.relpath(dest, destination) | ||
| return None | ||
|
|
||
|
|
||
| def _localize_file_inputs(name, input_data, destination, build_modules): | ||
| """Copy files referenced by "file" type input responses into the built | ||
| masterfiles, so they're actually part of what gets deployed instead of | ||
| only existing in the project directory. Rewrites the responses in place | ||
| to the resulting on-host path. | ||
|
|
||
| If a response is already shipped by another module's own "directory" | ||
| build step (e.g. the project author set one up manually), that step's | ||
| destination is used instead of making a redundant copy. | ||
| """ | ||
| if not isinstance(input_data, list): | ||
| return | ||
|
|
||
| module_dir_name = name[2:] if name.startswith("./") else name | ||
| module_dir_name = os.path.basename(module_dir_name.rstrip("/")) | ||
|
|
||
| def _localize(path): | ||
| if not path or not os.path.isfile(path): | ||
| return path | ||
|
|
||
| already_shipped = _path_if_already_shipped(path, build_modules, destination) | ||
| if already_shipped is not None: | ||
| return already_shipped | ||
|
|
||
| dest = os.path.join( | ||
| destination, | ||
| "services", | ||
| "cfbs", | ||
| "modules", | ||
| module_dir_name, | ||
| os.path.basename(path), | ||
| ) | ||
| cp(path, dest) | ||
| return "$(sys.inputdir)/" + os.path.relpath(dest, destination) | ||
|
|
||
| for element in input_data: | ||
| if not isinstance(element, dict) or element.get("type") != "file": | ||
| continue | ||
| response = element.get("response") | ||
| if isinstance(response, list): | ||
| element["response"] = [_localize(path) for path in response] | ||
| else: | ||
| element["response"] = _localize(response) | ||
|
|
||
|
|
||
| def _perform_input_step(args, name, destination, prefix, build_modules): | ||
| src, dst = args | ||
| if dst in [".", "./"]: | ||
| dst = "" | ||
|
|
@@ -264,6 +343,7 @@ def _perform_input_step(args, name, destination, prefix): | |
| ) | ||
| return | ||
| extras, original = read_json(src), read_json(dst) | ||
| _localize_file_inputs(name, extras, destination, build_modules) | ||
| extras = generate_augment(name, extras) | ||
| log.debug("Generated augment: %s", pretty(extras)) | ||
| if not extras: | ||
|
|
@@ -424,7 +504,7 @@ def perform_build(config: CFBSConfig, diffs_filename=None) -> int: | |
| elif operation == "directory": | ||
| _perform_directory_step(args, source, destination, prefix) | ||
| elif operation == "input": | ||
| _perform_input_step(args, name, destination, prefix) | ||
| _perform_input_step(args, name, destination, prefix, config["build"]) | ||
| elif operation == "policy_files": | ||
| _perform_policy_files_step(args, destination, prefix) | ||
| elif operation == "bundles": | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.