From 8815726c76dbbbcd132a68e5cf37b48f2cf275cd Mon Sep 17 00:00:00 2001
From: Jakub Kadlčík <frostyx@email.cz>
Date: Nov 25 2016 11:01:07 +0000
Subject: [PATCH 1/3] Rewrite 'list' and 'info' commands to work with new modulemd format


---

diff --git a/fm/api_client.py b/fm/api_client.py
index a2ca9c7..d89f532 100644
--- a/fm/api_client.py
+++ b/fm/api_client.py
@@ -199,13 +199,14 @@ class URLAPIClient(APIClient):
             else:
                 mmd.name = mod["name"]
                 mmd.summary = mod["summary"]
+                mmd.stream = mod["stream"]
                 mmd.version = mod["version"]
-                mmd.release = mod["release"]
 
                 for req_name, req_ver in mod["requires"].items():
                     mmd.update_requires(req_name, req_ver)
 
-                mmd.xmd["_fm_need_refetch"] = True
+                # @TODO do we need this?
+                #mmd.xmd["_fm_need_refetch"] = True
 
             if "url" in mod:
                 url = mod["url"]
diff --git a/fm/metadata_cache.py b/fm/metadata_cache.py
index 57355af..04e1481 100644
--- a/fm/metadata_cache.py
+++ b/fm/metadata_cache.py
@@ -139,8 +139,8 @@ class MetadataCache(object):
         Returns full path to cached mmd file for this module.
         """
 
-        mmd_file = os.path.join(self.cache_dir, mod.name + "-" + mod.mmd.version
-                                + "-" + mod.mmd.release + ".yaml")
+        mmd_file = os.path.join(self.cache_dir, mod.name + "-" + mod.mmd.stream
+                                + "-" + str(mod.mmd.version) + ".yaml")
         return mmd_file
 
     def store(self, mod, enabled_by_user = False):
diff --git a/fm/module.py b/fm/module.py
index 76315d5..175ccc2 100644
--- a/fm/module.py
+++ b/fm/module.py
@@ -57,9 +57,9 @@ class Module(object):
 
     def get_nvr(self):
         """
-        Returns NVR ("$name-$version-$release) of this module.
+        Returns NVR ("$name-$stream-$version) of this module.
         """
-        return self.name + "-" + self.mmd.version + "-" + self.mmd.release
+        return self.name + "-" + self.mmd.stream + "-" + self.mmd.version
 
     def get_metadata_expire(self):
         """
@@ -180,8 +180,8 @@ class Module(object):
         ret = "Name: " + self.name + "\n"
         if self.mmd:
             ret += "  Summary: " + self.mmd.summary + "\n"
-            ret += "  Version: " + self.mmd.version + "\n"
-            ret += "  Release: " + self.mmd.release + "\n"
+            ret += "  Stream: " + self.mmd.stream + "\n"
+            ret += "  Release: " + str(self.mmd.version) + "\n"
             ret += "  Description: " + self.mmd.description + "\n"
 
             if self.mmd.profiles and len(self.mmd.profiles) != 0:
@@ -211,8 +211,8 @@ class Module(object):
         if self.url != other.url:
             return False
         if self.mmd and other.mmd:
-            if self.mmd.version != other.mmd.version:
+            if self.mmd.stream != other.mmd.stream:
                 return False
-            if self.mmd.release != other.mmd.release:
+            if self.mmd.version != other.mmd.version:
                 return False
         return True
diff --git a/fm/modules.py b/fm/modules.py
index 3afad53..e3027fe 100644
--- a/fm/modules.py
+++ b/fm/modules.py
@@ -107,9 +107,9 @@ class Modules(OrderedDict):
                 del self[key]
                 self[key] = m
                 if key in mods:
-                    self[key] += sorted(mods[key], key = lambda mod: (mod.mmd.version, int(mod.mmd.release)))
+                    self[key] += sorted(mods[key], key = lambda mod: (mod.mmd.stream, int(mod.mmd.version)))
             else:
-                self[key] = sorted(mods[key], key = lambda mod: (mod.mmd.version, int(mod.mmd.release)))
+                self[key] = sorted(mods[key], key = lambda mod: (mod.mmd.stream, int(mod.mmd.version)))
 
     def enable(self, name, profiles):
         """
@@ -159,8 +159,8 @@ class Modules(OrderedDict):
             if not enabled_mod:
                 continue
 
-            available_mods = self.get_modules(name, enabled_mod.mmd.version)
-            available_mods = sorted(available_mods, key = lambda mod: int(mod.mmd.release))
+            available_mods = self.get_modules(name, enabled_mod.mmd.stream)
+            available_mods = sorted(available_mods, key = lambda mod: int(mod.mmd.version))
             latest_mod = available_mods[-1]
             if latest_mod != enabled_mod:
                 to_upgrade.add_module(latest_mod, False)
@@ -179,7 +179,7 @@ class Modules(OrderedDict):
             if not enabled_mod:
                 continue
             available_mods = self.get_modules(name)
-            available_mods = sorted(available_mods, key = lambda mod: (mod.mmd.version, int(mod.mmd.release)))
+            available_mods = sorted(available_mods, key = lambda mod: (mod.mmd.stream, int(mod.mmd.version)))
             latest_mod = available_mods[-1]
             if latest_mod != enabled_mod:
                 to_upgrade.add_module(latest_mod, False)
@@ -195,8 +195,8 @@ class Modules(OrderedDict):
             if not enabled_mod:
                 raise fm.exceptions.DependencyError("Module {} is not enabled".format(name))
 
-            available_mods = self.get_modules(name, enabled_mod.mmd.version)
-            available_mods = sorted(available_mods, key = lambda mod: int(mod.mmd.release))
+            available_mods = self.get_modules(name, enabled_mod.mmd.stream)
+            available_mods = sorted(available_mods, key = lambda mod: int(mod.mmd.version))
             latest_mod = available_mods[-1]
 
             resolver = FmModulesResolver(self)
@@ -210,9 +210,9 @@ class Modules(OrderedDict):
         if len(lst) > 2:
             try:
                 int(lst[-2][0])
-                release = str(int(lst[-1]))
-                version = lst[-2]
-                return '-'.join(lst[:-2]), version, release
+                version = str(int(lst[-1]))
+                stream = lst[-2]
+                return '-'.join(lst[:-2]), stream, version
             except:
                 pass
 
@@ -231,13 +231,13 @@ class Modules(OrderedDict):
         Rebases the module named `name` to version defined by `nvr.
         """
 
-        name, version, release = self._name_to_nvr(nvr)
+        name, stream, version = self._name_to_nvr(nvr)
         enabled_mod = self.get_enabled_module(name)
         if not enabled_mod:
             raise fm.exceptions.DependencyError("Module {} is not enabled".format(name))
 
-        available_mods = self.get_modules(name, version, release)
-        available_mods = sorted(available_mods, key = lambda mod: (mod.mmd.version, int(mod.mmd.release)))
+        available_mods = self.get_modules(name, stream, version)
+        available_mods = sorted(available_mods, key = lambda mod: (mod.mmd.stream, int(mod.mmd.version)))
         latest_mod = available_mods[-1]
 
         resolver = FmModulesResolver(self)
@@ -290,8 +290,8 @@ class Modules(OrderedDict):
         # Modules instance and remove them from cache.
         for mods in cached_mods.values():
             for mod in mods:
-                our_modules = self.get_modules(mod.name, mod.mmd.version,
-                                               mod.mmd.release)
+                our_modules = self.get_modules(mod.name, mod.mmd.stream,
+                                               mod.mmd.version)
                 if not our_modules or len(our_modules) == 0:
                     self.available_cache.remove(mod)
 
@@ -309,9 +309,9 @@ class Modules(OrderedDict):
         if cache and mod.mmd and not self.available_cache.is_valid(mod) and not self.disable_cache:
             self.available_cache.store(mod)
 
-    def get_modules(self, name, version = None, release = None):
+    def get_modules(self, name, stream = None, version = None):
         """
-        Returns all modules matching the `name` and `version` or `release`
+        Returns all modules matching the `name` and `stream` or `version`
         when defined.
         """
 
@@ -320,9 +320,9 @@ class Modules(OrderedDict):
 
         mods = []
         for mod in self[name]:
-            if version and mod.mmd.version != version:
+            if stream and mod.mmd.stream != stream:
                 continue
-            if release and mod.mmd.release != release:
+            if version and mod.mmd.version != version:
                 continue
             mods.append(mod)
 
@@ -350,12 +350,12 @@ class Modules(OrderedDict):
         """
         Returns full description of all modules matching the specified `nvr`.
         """
-        name, version, release = self._name_to_nvr(nvr)
-        mods = self.get_modules(name, version, release)
+        name, stream, version = self._name_to_nvr(nvr)
+        mods = self.get_modules(name, stream, version)
         if not mods or len(mods) == 0:
             raise fm.exceptions.DependencyError("Unknown module {}".format(nvr))
 
-        mods = sorted(mods, key = lambda mod: (mod.mmd.version, int(mod.mmd.release)))
+        mods = sorted(mods, key = lambda mod: (mod.mmd.stream, int(mod.mmd.version)))
 
         ret = ""
         did_fetch = False
@@ -378,14 +378,14 @@ class Modules(OrderedDict):
         # Get the maximum width of the text we will show in each column
         # of output.
         max_name_width = max(len(word) for word in self.keys()) + 4  # padding
-        max_vr_width = (max(len(mod.mmd.version) + 1 + 
-                            len(mod.mmd.release) for mods in self.values()
+        max_vr_width = (max(len(mod.mmd.stream) + 1 +
+                            len(str(mod.mmd.version)) for mods in self.values()
                             for mod in mods) + 4)  # padding
 
         ret = ""
         for mods in self.values():
             for mod in mods:
-                vr = mod.mmd.version + "-" + mod.mmd.release
+                vr = mod.mmd.stream + "-" + str(mod.mmd.version)
 
                 # Show output in 3 columns...
                 ret += mod.name.ljust(max_name_width)

From 1c3783bd05bac6ab48ff3204db39cf48745f33fe Mon Sep 17 00:00:00 2001
From: Jakub Kadlčík <frostyx@email.cz>
Date: Nov 28 2016 13:59:50 +0000
Subject: [PATCH 2/3] Rewrite 'enable' and 'disable' commands to work with new modulemd format


---

diff --git a/fm/fm_modules_resolver.py b/fm/fm_modules_resolver.py
index 02be6a0..d6fff97 100644
--- a/fm/fm_modules_resolver.py
+++ b/fm/fm_modules_resolver.py
@@ -90,7 +90,7 @@ class FmModulesResolver(ModulesResolver):
                 raise fm.exceptions.DependencyError("Dependency on module {} is not satisfied.".format(name))
 
             # Get the Module instance for this ModuleMetadata object.
-            mods = self.mods.get_modules(mmd.name, mmd.version, mmd.release)
+            mods = self.mods.get_modules(mmd.name, mmd.stream, mmd.version)
             if not mods:
                 raise fm.exceptions.DependencyError("There is no module named {}".format(name))
 
@@ -98,7 +98,7 @@ class FmModulesResolver(ModulesResolver):
             if not mod.mmd:
                 raise fm.exceptions.DependencyError("There is no metadata associated with module {}".format(name))
 
-            print("Enabling module", name + "-" + mod.mmd.version + "-" + mod.mmd.release)
+            print("Enabling module", name + "-" + mod.mmd.stream + "-" + str(mod.mmd.version))
 
             # Enable current module.
             if no_dnf == None:
@@ -123,7 +123,7 @@ class FmModulesResolver(ModulesResolver):
         for mmd in modules:
             name = mmd.name
             # Get the Module instance for this ModuleMetadata object.
-            mods = self.mods.get_modules(mmd.name, mmd.version, mmd.release)
+            mods = self.mods.get_modules(mmd.name, mmd.stream, mmd.version)
             if not mods:
                 raise fm.exceptions.DependencyError("There is no module named {}".format(name))
 
@@ -134,7 +134,7 @@ class FmModulesResolver(ModulesResolver):
             if not mod.is_enabled():
                 continue
 
-            print("Disabling module", name + "-" + mod.mmd.version + "-" + mod.mmd.release)
+            print("Disabling module", name + "-" + mod.mmd.stream + "-" + str(mod.mmd.version))
 
             # Now when all the depending modules are disabled, disable also
             # the original module.

From 419581ab18e59921fb8693f71814fd6f6162285e Mon Sep 17 00:00:00 2001
From: clime <clime@redhat.com>
Date: Nov 30 2016 08:24:54 +0000
Subject: [PATCH 3/3] mmd.xmd inited to {}


---

diff --git a/fm/api_client.py b/fm/api_client.py
index d89f532..dbbe024 100644
--- a/fm/api_client.py
+++ b/fm/api_client.py
@@ -205,8 +205,10 @@ class URLAPIClient(APIClient):
                 for req_name, req_ver in mod["requires"].items():
                     mmd.update_requires(req_name, req_ver)
 
-                # @TODO do we need this?
-                #mmd.xmd["_fm_need_refetch"] = True
+                if not mmd.xmd:
+                    mmd.xmd = {} # otherwise mmd.xmd == None in 1.0.2
+
+                mmd.xmd["_fm_need_refetch"] = True
 
             if "url" in mod:
                 url = mod["url"]

