Skip to content

Commit 6d12a30

Browse files
committed
Moved update_command module update to its own function
The part of the loop in update_command responsible for actually updating modules was moved to its own function. It is done by passing a module updates class that tracks the updates generally. Signed-off-by: Victor Moene <victor.moene@northern.tech>
1 parent 4738c43 commit 6d12a30

1 file changed

Lines changed: 139 additions & 107 deletions

File tree

cfbs/commands.py

Lines changed: 139 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,113 @@ def _update_variable(input_def, input_data):
663663
return changes_made
664664

665665

666+
class ModuleUpdates:
667+
668+
def __init__(self, config):
669+
self.new_deps = []
670+
self.new_deps_added_by = dict()
671+
self.changes_made = False
672+
self.files = []
673+
self.config = config
674+
675+
676+
def update_module(old_module, new_module, module_updates, update):
677+
msg = ""
678+
commit_differs = old_module["commit"] != new_module["commit"]
679+
for key in old_module.keys():
680+
if key not in new_module or old_module[key] == new_module[key]:
681+
continue
682+
if key == "steps":
683+
# same commit => user modifications, don't revert them
684+
if commit_differs:
685+
ans = prompt_user(
686+
module_updates.config.non_interactive,
687+
"Module %s has different build steps now\n" % old_module["name"]
688+
+ "old steps: %s\n" % old_module["steps"]
689+
+ "new steps: %s\n" % new_module["steps"]
690+
+ "Do you want to use the new build steps?",
691+
choices=YES_NO_CHOICES,
692+
default="yes",
693+
)
694+
if ans.lower() in ["y", "yes"]:
695+
old_module["steps"] = new_module["steps"]
696+
module_updates.changes_made = True
697+
else:
698+
print(
699+
"Please make sure the old build steps work"
700+
+ " with the new version of the module"
701+
)
702+
elif key == "input":
703+
if commit_differs:
704+
old_module["input"] = new_module["input"]
705+
706+
input_path = os.path.join(".", old_module["name"], "input.json")
707+
input_data = read_json(input_path)
708+
if input_data == None:
709+
log.debug(
710+
"Skipping input update for module '%s': "
711+
+ "No input found in '%s'" % (old_module["name"], input_path)
712+
)
713+
else:
714+
try:
715+
module_updates.changes_made |= update_input_data(
716+
old_module, input_data
717+
)
718+
except InputDataUpdateFailed as e:
719+
log.warning(e)
720+
if prompt_user(
721+
module_updates.config.non_interactive,
722+
"Input for module '%s' has changed " % old_module["name"]
723+
+ "and may no longer be compatible. "
724+
+ "Do you want to re-enter input now?",
725+
YES_NO_CHOICES,
726+
"no",
727+
).lower() in ("no", "n"):
728+
continue
729+
input_data = copy.deepcopy(old_module["input"])
730+
module_updates.config.input_command(
731+
old_module["name"], input_data
732+
)
733+
module_updates.changes_made = True
734+
735+
if module_updates.changes_made:
736+
write_json(input_path, input_data)
737+
module_updates.files.append(input_path)
738+
else:
739+
if key == "dependencies":
740+
extra = set(new_module["dependencies"]) - set(
741+
old_module["dependencies"]
742+
)
743+
module_updates.new_deps.extend(extra)
744+
module_updates.new_deps_added_by.update(
745+
{item: old_module["name"] for item in extra}
746+
)
747+
748+
old_module[key] = new_module[key]
749+
module_updates.changes_made = True
750+
751+
for key in set(new_module.keys()) - set(old_module.keys()):
752+
old_module[key] = new_module[key]
753+
if key == "dependencies":
754+
extra = new_module["dependencies"]
755+
module_updates.new_deps.extend(extra)
756+
module_updates.new_deps_added_by.update(
757+
{item: old_module["name"] for item in extra}
758+
)
759+
760+
if new_module.get("version"):
761+
if not update.version:
762+
update.version = new_module["version"]
763+
msg += "\n - Updated module '%s' from version %s to version %s" % (
764+
update.name,
765+
old_module["version"],
766+
update.version,
767+
)
768+
else:
769+
msg += "\n - Updated module '%s'" % (update.name,)
770+
return msg
771+
772+
666773
@cfbs_command("update")
667774
@commit_after_command("Updated module%s", [PLURAL_S])
668775
def update_command(to_update):
@@ -677,163 +784,88 @@ def update_command(to_update):
677784
else [Module(m["name"]) for m in build]
678785
)
679786

680-
new_deps = []
681-
new_deps_added_by = dict()
682-
changes_made = False
683-
msg = ""
684-
files = []
685787
updated = []
788+
module_updates = ModuleUpdates(config)
789+
msg = ""
686790

687791
for update in to_update:
688-
module = config.get_module_from_build(update.name)
792+
old_module = config.get_module_from_build(update.name)
689793

690-
custom_index = module is not None and "index" in module
691-
index = Index(module["index"]) if custom_index else config.index
794+
custom_index = old_module is not None and "index" in old_module
795+
index = Index(old_module["index"]) if custom_index else config.index
692796

693-
if not module:
797+
if not old_module:
694798
index.translate_alias(update)
695-
module = config.get_module_from_build(update.name)
799+
old_module = config.get__module_from_build(update.name)
696800

697-
if not module:
698-
log.warning("Module '%s' not in build. Skipping its update." % update.name)
801+
if not old_module:
802+
log.warning(
803+
"old_Module '%s' not in build. Skipping its update." % update.name
804+
)
699805
continue
700806

701-
custom_index = module is not None and "index" in module
702-
index = Index(module["index"]) if custom_index else config.index
807+
custom_index = old_module is not None and "index" in old_module
808+
index = Index(old_module["index"]) if custom_index else config.index
703809

704-
if not module:
810+
if not old_module:
705811
index.translate_alias(update)
706-
module = config.get_module_from_build(update.name)
812+
old_module = config.get_module_from_build(update.name)
707813

708-
if not module:
814+
if not old_module:
709815
log.warning("Module '%s' not in build. Skipping its update." % update.name)
710816
continue
711817

712-
if "version" not in module:
818+
if "version" not in old_module:
713819
log.warning(
714-
"Module '%s' not updatable. Skipping its update." % module["name"]
820+
"Module '%s' not updatable. Skipping its update." % old_module["name"]
715821
)
716-
log.debug("Module '%s' has no version attribute." % module["name"])
822+
log.debug("Module '%s' has no version attribute." % old_module["name"])
717823
continue
718-
old_version = module["version"]
719824

720825
index_info = index.get_module_object(update.name)
721826
if not index_info:
722827
log.warning(
723828
"Module '%s' not present in the index, cannot update it."
724-
% module["name"]
829+
% old_module["name"]
725830
)
726831
continue
727832

728833
local_ver = [
729834
int(version_number)
730-
for version_number in re.split(r"[-\.]", module["version"])
835+
for version_number in re.split(r"[-\.]", old_module["version"])
731836
]
732837
index_ver = [
733838
int(version_number)
734839
for version_number in re.split(r"[-\.]", index_info["version"])
735840
]
736841
if local_ver == index_ver:
737-
print("Module '%s' already up to date" % module["name"])
842+
print("Module '%s' already up to date" % old_module["name"])
738843
continue
739844
elif local_ver > index_ver:
740845
log.warning(
741846
"The requested version of module '%s' is older than current version (%s < %s)."
742847
" Skipping its update."
743-
% (module["name"], index_info["version"], module["version"])
848+
% (old_module["name"], index_info["version"], old_module["version"])
744849
)
745850
continue
746851

747-
commit_differs = module["commit"] != index_info["commit"]
748-
for key in module.keys():
749-
if key not in index_info or module[key] == index_info[key]:
750-
continue
751-
if key == "steps":
752-
# same commit => user modifications, don't revert them
753-
if commit_differs:
754-
ans = prompt_user(
755-
config.non_interactive,
756-
"Module %s has different build steps now\n" % module["name"]
757-
+ "old steps: %s\n" % module["steps"]
758-
+ "new steps: %s\n" % index_info["steps"]
759-
+ "Do you want to use the new build steps?",
760-
choices=YES_NO_CHOICES,
761-
default="yes",
762-
)
763-
if ans.lower() in ["y", "yes"]:
764-
module["steps"] = index_info["steps"]
765-
changes_made = True
766-
else:
767-
print(
768-
"Please make sure the old build steps work"
769-
+ " with the new version of the module"
770-
)
771-
elif key == "input":
772-
if commit_differs:
773-
module["input"] = index_info["input"]
774-
775-
input_path = os.path.join(".", module["name"], "input.json")
776-
input_data = read_json(input_path)
777-
if input_data == None:
778-
log.debug(
779-
"Skipping input update for module '%s': " % module["name"]
780-
+ "No input found in '%s'" % input_path
781-
)
782-
else:
783-
try:
784-
changes_made |= update_input_data(module, input_data)
785-
except InputDataUpdateFailed as e:
786-
log.warning(e)
787-
if prompt_user(
788-
config.non_interactive,
789-
"Input for module '%s' has changed " % module["name"]
790-
+ "and may no longer be compatible. "
791-
+ "Do you want to re-enter input now?",
792-
YES_NO_CHOICES,
793-
"no",
794-
).lower() in ("no", "n"):
795-
continue
796-
input_data = copy.deepcopy(module["input"])
797-
config.input_command(module["name"], input_data)
798-
changes_made = True
799-
800-
if changes_made:
801-
write_json(input_path, input_data)
802-
files.append(input_path)
803-
else:
804-
if key == "dependencies":
805-
extra = set(index_info["dependencies"]) - set(
806-
module["dependencies"]
807-
)
808-
new_deps.extend(extra)
809-
new_deps_added_by.update({item: module["name"] for item in extra})
852+
new_module = index_info
810853

811-
module[key] = index_info[key]
812-
changes_made = True
854+
msg = update_module(old_module, new_module, module_updates, update)
813855

814856
# add new items
815-
for key in set(index_info.keys()) - set(module.keys()):
816-
module[key] = index_info[key]
817-
if key == "dependencies":
818-
extra = index_info["dependencies"]
819-
new_deps.extend(extra)
820-
new_deps_added_by.update({item: module["name"] for item in extra})
821857

822-
if not update.version:
823-
update.version = index_info["version"]
824858
updated.append(update)
825-
msg += "\n - Updated module '%s' from version %s to version %s" % (
826-
update.name,
827-
old_version,
828-
update.version,
829-
)
830859

831-
if new_deps:
832-
objects = [index.get_module_object(d, new_deps_added_by[d]) for d in new_deps]
860+
if module_updates.new_deps:
861+
objects = [
862+
index.get_module_object(d, module_updates.new_deps_added_by[d])
863+
for d in module_updates.new_deps
864+
]
833865
config.add_with_dependencies(objects)
834866
config.save()
835867

836-
if changes_made:
868+
if module_updates.changes_made:
837869
if len(updated) > 1:
838870
msg = "Updated %d modules\n" % len(updated) + msg
839871
else:
@@ -843,7 +875,7 @@ def update_command(to_update):
843875
else:
844876
print("Modules are already up to date")
845877

846-
return Result(0, changes_made, msg, files)
878+
return Result(0, module_updates.changes_made, msg, module_updates.files)
847879

848880

849881
@cfbs_command("validate")

0 commit comments

Comments
 (0)