Exemple #1
0
 def translate_alias(self, module: Module):
     if module.name in self:
         data = self[module.name]
         if "alias" in data:
             print("%s is an alias for %s" % (module.name, data["alias"]))
             module.name = data["alias"]
     else:
         if os.path.exists(module.name):
             module.name = local_module_name(module.name)
Exemple #2
0
def test_init():
    """Test initializing with and without version"""

    module = Module("[email protected]")
    assert module.name == "groups"
    assert module.version == "0.1.2"

    module = Module("groups")
    assert module.name == "groups"
    assert module.version == None
Exemple #3
0
    def get_module_object(self, module, added_by=None):
        if isinstance(module, str):
            module = Module(module)
        name = module.name
        version = module.version
        module = module.to_dict()

        if name.startswith("./"):
            object = _generate_local_module_object(name)
        else:
            object = self[name]
            if version:
                versions = get_json(_VERSION_INDEX)
                new_values = versions[name][version]
                specifics = {
                    k: v
                    for (k, v) in new_values.items()
                    if k in Module.attributes()
                }
                object.update(specifics)
                object["version"] = version
        module.update(object)
        if added_by:
            module["added_by"] = added_by
        module.move_to_end("steps")
        return module
Exemple #4
0
def test_mutual_exclusion():
    """Test that mutual exclusion constraint works"""

    module = Module("groups")

    try:
        module.repo = "my_repo"
        success = True
    except ValueError as e:
        print(e)
        success = False
    assert success

    try:
        module.path = "my_path"
        success = False
    except ValueError as e:
        print(e)
        success = True
    assert success
Exemple #5
0
def test_type_constraints():
    """Make sure type constraints are only enforced for Module specific attributes"""

    module = Module("groups")

    try:
        module.description = "Manage local groups"
        success = True
    except ValueError as e:
        print(e)
        success = False
    assert success

    try:
        module.description = 123
        success = False
    except ValueError as e:
        print(e)
        success = True

    try:
        module.steps = ["alice", "bob", "charlie"]
        success = True
    except ValueError as e:
        print(e)
        success = False
    assert success

    try:
        module.steps = "alice"
        success = False
    except ValueError as e:
        print(e)
        success = True
    assert success

    try:
        module.steps = ["alice", "bob", 123]
        success = False
    except ValueError as e:
        print(e)
        success = True
    assert success

    try:
        module.x = 123
        success = True
    except ValueError as e:
        print(e)
        success = False
    assert success
Exemple #6
0
def test_attribute_defaults():
    """Make sure only Module specific attributes default to None"""

    module = Module("groups")

    try:
        assert module.commit is None
        success = True
    except AttributeError as e:
        print(e)
        success = False
    assert success

    try:
        print(module.x)
        success = False
    except AttributeError as e:
        print(e)
        success = True
    assert success
Exemple #7
0
    def _add_modules(
        self,
        to_add: list,
        added_by="cfbs add",
        checksum=None,
    ) -> int:
        index = self.index

        modules = [Module(m) for m in to_add]
        index.translate_aliases(modules)
        index.check_existence(modules)

        # Convert added_by to dictionary:
        added_by = self._convert_added_by(added_by, modules)

        # If some modules were added as deps previously, mark them as user requested:
        self._update_added_by(modules, added_by)

        # Filter modules which are already added:
        to_add = self._filter_modules_to_add(modules)
        if not to_add:
            return  # Everything already added

        # Convert names to objects:
        modules_to_add = [
            index.get_module_object(m, added_by[m.name]) for m in to_add
        ]
        modules_already_added = self["build"]

        assert not any(m for m in modules_to_add if "name" not in m)
        assert not any(m for m in modules_already_added if "name" not in m)

        # Find all unmet dependencies:
        dependencies = self._find_dependencies(modules_to_add,
                                               modules_already_added)

        if dependencies:
            self._add_without_dependencies(dependencies)

        self._add_without_dependencies(modules_to_add)
Exemple #8
0
def update_command(to_update):
    config = CFBSConfig.get_instance()
    index = config.index

    if to_update:
        to_update = [Module(m) for m in to_update]
        index.translate_aliases(to_update)
        skip = (m for m in to_update
                if all(n["name"] != m.name for n in config["build"]))
        for m in skip:
            log.warning("Module '%s' not in build. Skipping its update.",
                        m.name)
            to_update.remove(m)
    else:
        # Update all modules in build if no modules are specified
        to_update = [Module(m["name"]) for m in config["build"]]

    new_deps = []
    new_deps_added_by = dict()
    changes_made = False
    msg = ""
    updated = []

    for update in to_update:
        module = config.get_module_from_build(update.name)
        assert module is not None  # Checked above when logging skipped modules

        if "version" not in module:
            print("Module '%s' not updatable" % module["name"])
            continue
        old_version = module["version"]

        if "index" in module:
            # TODO: Support custom index
            log.warning(
                "Module '%s' is not from the default index. " +
                "Updating from custom index is currently not supported. " +
                "Skipping its update.",
                module["name"],
            )
            continue

        index_info = index.get_module_object(update)
        if not index_info:
            log.warning(
                "Module '%s' not present in the index, cannot update it",
                module["name"])
            continue

        if (module["version"] != index_info["version"]
                and module["commit"] == index_info["commit"]):
            log.warning(
                "Version and commit mismatch detected." +
                " The module %s has the same commit but different version" +
                " locally (%s) and in the index (%s)." +
                " Skipping its update.",
                module["name"],
                module["version"],
                index_info["version"],
            )
            continue

        local_ver = [
            int(version_number)
            for version_number in re.split("[-\.]", module["version"])
        ]
        index_ver = [
            int(version_number)
            for version_number in re.split("[-\.]", index_info["version"])
        ]
        if local_ver == index_ver:
            continue
        elif local_ver > index_ver:
            log.warning(
                "The requested version of module '%s' is older than current version (%s < %s)."
                " Skipping its update.",
                module["name"],
                index_info["version"],
                module["version"],
            )
            continue

        commit_differs = module["commit"] != index_info["commit"]
        for key in module.keys():
            if key not in index_info or module[key] == index_info[key]:
                continue
            if key == "steps":
                # same commit => user modifications, don't revert them
                if commit_differs:
                    ans = prompt_user(
                        "Module %s has different build steps now\n" %
                        module["name"] + "old steps: %s\n" % module["steps"] +
                        "new steps: %s\n" % index_info["steps"] +
                        "Do you want to use the new build steps?",
                        choices=YES_NO_CHOICES,
                        default="yes",
                    )
                    if ans.lower() in ["y", "yes"]:
                        module["steps"] = index_info["steps"]
                    else:
                        print("Please make sure the old build steps work" +
                              " with the new version of the module")
            else:
                if key == "dependencies":
                    extra = set(index_info["dependencies"]) - set(
                        module["dependencies"])
                    new_deps.extend(extra)
                    new_deps_added_by.update(
                        {item: module["name"]
                         for item in extra})

                module[key] = index_info[key]
                changes_made = True

        # add new items
        for key in set(index_info.keys()) - set(module.keys()):
            module[key] = index_info[key]
            if key == "dependencies":
                extra = index_info["dependencies"]
                new_deps.extend(extra)
                new_deps_added_by.update(
                    {item: module["name"]
                     for item in extra})

        if not update.version:
            update.version = index_info["version"]
        updated.append(update)
        msg += "\n - Updated module '%s' from version %s to version %s" % (
            update.name,
            old_version,
            update.version,
        )

    if new_deps:
        objects = [
            index.get_module_object(d, new_deps_added_by[d]) for d in new_deps
        ]
        config.add_with_dependencies(objects)
    config.save()

    if changes_made:
        if len(updated) > 1:
            msg = "Updated %d modules\n" % len(updated) + msg
        else:
            assert updated
            msg = msg[4:]  # Remove the '\n - ' part of the message
        print("%s\n" % msg)
    else:
        print("Modules are already up to date")

    return Result(0, changes_made, msg)