Cleanup license-printing code and make output format better
authorIlpo Ruotsalainen <ilpo.ruotsalainen@movial.com>
Thu, 2 Jul 2009 13:40:51 +0000 (16:40 +0300)
committerDaniel Bainton <daniel.bainton@movial.com>
Fri, 3 Jul 2009 07:49:38 +0000 (10:49 +0300)
matrix/components.py
matrix/matrix.py

index 5a8a95e..55bdcdd 100644 (file)
@@ -43,8 +43,6 @@ class Component(object):
 
                self.rank = rank
 
-               self.license = self.get_license()
-
        def __str__(self):
                return str(self.source)
 
@@ -75,18 +73,11 @@ class Component(object):
        def get_depends(self):
                return self.depends
 
-       def get_license(self):
-               path = os.path.join(self.meta.path, 'info')
-               if not os.path.exists(path):
-                       return 'None'
-
-               f = open(path, 'r')
-               for line in f:
-                       if (line.find('license') != -1) and (line.count('"') == 2):
-                               index_quote1 = line.find('"')
-                               index_quote2 = line.find('"', index_quote1 + 1)
-                               return line[index_quote1 + 1 : index_quote2].strip()
-               return 'None'
+       def get_licenses(self):
+               l = set()
+               for p in self.packages.values():
+                       l.add(p.license)
+               return l
 
 class PlatformProvidedComponent(Component):
        """A Component that is provided by the platform.
@@ -264,6 +255,7 @@ class Package(object):
                self.depends = None
                self.conflicts = None
                self.architectures = None
+               self.license = 'Unknown'
 
 def to_seq(value):
        if value is None:
index dc13243..f54be4d 100644 (file)
@@ -363,16 +363,28 @@ def dist_sources(c, location):
        c.source.archive(name, path)
 
 def print_licenses(targets):
-       file = sys.stdout
-       
        meta(targets)
-       
-       for name in targets:
-               c = find_component(name)
-               print >> file, c.name + ' --> ' + c.license
-               
-               for dep in c.get_depends():
-                       print >> file, '\t' + dep.name + ' --> ' + dep.license
 
-               print >> file, '\n'
+       try:
+               selected = set([components.by_name[i] for i in targets])
+       except KeyError, e:
+               raise Error("Component " + i + " not found")
+
+       components.fill_in_depends(selected)
+
+       by_license = {}
+
+       for c in selected:
+               for l in c.get_licenses():
+                       if not by_license.has_key(l):
+                               by_license[l] = []
+
+                       by_license[l].append(c.name)
+
+       for l in by_license.keys():
+               print l
+               
+               for c in by_license[l]:
+                       print '  %s' % c
 
+               print