Esempio n. 1
0
 def add_to_loaded_collection(self, name):
     """Add a module `name` to the currently loaded collection"""
     collection_name = pymod.environ.get(pymod.names.loaded_collection)
     if collection_name is None:  # pragma: no cover
         tty.die("There is no collection currently loaded")
     data = OrderedDict(self.data.pop(collection_name))
     module = pymod.modulepath.get(name)
     if module is None:
         raise pymod.error.ModuleNotFoundError(name)
     if not module.is_loaded:
         pymod.mc.load_impl(module)
     for (mp, modules) in data.items():
         if mp != module.modulepath:
             continue
         for other in modules:
             if other["fullname"] == module.fullname:  # pragma: no cover
                 tty.warn("{0} is already in collection {1}".format(
                     name, collection_name))
                 return
     ar = pymod.mc.archive_module(module)
     ar["refcount"] = 0
     data.setdefault(module.modulepath, []).append(ar)
     data = list(data.items())
     self.data.update({collection_name: data})
     self.write(self.data, self.filename)
     return None
Esempio n. 2
0
 def save(self, name, modules):
     collection = OrderedDict()
     for module in modules:
         ar = pymod.mc.archive_module(module)
         ar["refcount"] = 0
         collection.setdefault(module.modulepath, []).append(ar)
     collection = list(collection.items())
     self.data.update({name: collection})
     self.write(self.data, self.filename)
     return None
Esempio n. 3
0
def upgrade_None_to_1_0(new, old, depth=[0]):

    depth[0] += 1
    if depth[0] > 1:
        raise ValueError("Recursion!")

    version_string = ".".join(str(_) for _ in new.version)
    tty.info(
        "Converting Modulecmd.py collections version 0.0 to "
        "version {0}".format(version_string)
    )
    new_collections = {}
    for (name, old_collection) in old.items():
        new_collection = OrderedDict()
        mp = pymod.modulepath.Modulepath([])
        for (path, m_descs) in old_collection:
            if new_collection is None:
                break
            if not os.path.isdir(path):
                tty.warn(
                    "Collection {0} contains directory {1} which "
                    "does not exist!  This collection will be skipped".format(
                        name, path
                    )
                )
                new_collection = None
                break
            avail = mp.append_path(path)
            if avail is None:
                tty.warn(
                    "Collection {0} contains directory {1} which "
                    "does not have any available modules!  "
                    "This collection will be skipped".format(name, path)
                )
                new_collection = None
                break
            for (fullname, filename, opts) in m_descs:
                m = mp.get(filename)
                if m is None:
                    tty.warn(
                        "Collection {0} requests module {1} which "
                        "can not be found! This collection will be skipped".format(
                            name, fullname
                        )
                    )
                    new_collection = None
                    break
                m.opts = opts
                m.acquired_as = m.fullname
                ar = pymod.mc.archive_module(m)
                new_collection.setdefault(m.modulepath, []).append(ar)

        if new_collection is None:
            tty.warn(
                "Skipping collection {0} because of previous " "errors".format(name)
            )
            continue

        new_collections[name] = list(new_collection.items())

    bak = new.filename + ".bak"
    with open(bak, "w") as fh:
        json.dump(old, fh, indent=2)

    new.write(list(new_collections.items()), new.filename)
    return new_collections