[csw-devel] SF.net SVN: gar:[18161] csw/mgar/gar/v2/lib/python
wahwah at users.sourceforge.net
wahwah at users.sourceforge.net
Tue May 29 21:29:12 CEST 2012
Revision: 18161
http://gar.svn.sourceforge.net/gar/?rev=18161&view=rev
Author: wahwah
Date: 2012-05-29 19:29:11 +0000 (Tue, 29 May 2012)
Log Message:
-----------
generate-catalog-file: Initial commit
Allows to skip bldcat and generate from the database directly (with some help
of local caches).
Added Paths:
-----------
csw/mgar/gar/v2/lib/python/generate_catalog_file.py
csw/mgar/gar/v2/lib/python/generate_catalog_file_test.py
Added: csw/mgar/gar/v2/lib/python/generate_catalog_file.py
===================================================================
--- csw/mgar/gar/v2/lib/python/generate_catalog_file.py (rev 0)
+++ csw/mgar/gar/v2/lib/python/generate_catalog_file.py 2012-05-29 19:29:11 UTC (rev 18161)
@@ -0,0 +1,105 @@
+#!/usr/bin/env python
+
+"""Generates a catalog file from the REST interface.
+
+Does not require a database connection.
+
+PKG_DATA_1 = {
+ "basename": "389_admin-1.1.29,REV=2012.05.02-SunOS5.10-sparc-CSW.pkg.gz",
+ "catalogname": "389_admin",
+ "file_basename": "389_admin-1.1.29,REV=2012.05.02-SunOS5.10-sparc-CSW.pkg.gz",
+ "md5_sum": "fdb7912713da36afcbbe52266c15cb3f",
+ "mtime": "2012-05-02 12:06:38",
+ "rev": "2012.05.02",
+ "size": 395802,
+ "version": "1.1.29,REV=2012.05.02",
+ "version_string": "1.1.29,REV=2012.05.02"
+}
+
+"""
+
+CATALOG_FN = "catalog"
+DESC_FN = "descriptions"
+
+import rest
+import os
+import optparse
+
+
+class Error(Exception):
+ """A general error."""
+
+class CatalogFileGenerator(object):
+
+ def __init__(self, catrel, arch, osrel,
+ pkgcache=None, rest_client=None):
+ self.catrel = catrel
+ self.arch = arch
+ self.osrel = osrel
+ self.pkgcache = pkgcache or rest.CachedPkgstats("pkgstats")
+ self.rest_client = rest_client or rest.RestClient()
+ self._catalog = None
+
+ @property
+ def catalog(self):
+ if not self._catalog:
+ self._catalog = self.rest_client.GetCatalog(self.catrel, self.arch, self.osrel)
+ return self._catalog
+
+ def ComposeCatalogLine(self, pkg_data):
+ deps_data = self.pkgcache.GetDeps(pkg_data["md5_sum"])
+ pkg_stats = self.pkgcache.GetPkgstats(pkg_data["md5_sum"])
+ i_deps = pkg_stats["i_depends"]
+ if i_deps:
+ i_deps = "|".join(i_deps)
+ else:
+ i_deps = "none"
+ items = [
+ pkg_data["catalogname"],
+ pkg_data["version_string"],
+ deps_data["pkgname"],
+ pkg_data["basename"],
+ pkg_data["md5_sum"],
+ unicode(pkg_data["size"]),
+ "|".join(x[0] for x in deps_data["deps"]),
+ "none",
+ i_deps]
+ return " ".join(items)
+
+
+ def GenerateCatalog(self, out_dir):
+ out_file = os.path.join(out_dir, CATALOG_FN)
+ if os.path.exists(out_file):
+ raise Error("File %s already exists." % out_file)
+ lines = []
+ for pkg_data in self.catalog:
+ lines.append(self.ComposeCatalogLine(pkg_data))
+ with open(out_file, "w") as fd:
+ fd.write("\n".join(lines))
+
+ def GenerateDescriptions(self, out_dir):
+ out_file = os.path.join(out_dir, DESC_FN)
+ if os.path.exists(out_file):
+ raise Error("File %s already exists." % out_file)
+ lines = []
+ for pkg_data in self.catalog:
+ pkg_stats = self.pkgcache.GetPkgstats(pkg_data["md5_sum"])
+ lines.append(pkg_stats["pkginfo"]["NAME"])
+ with open(out_file, "w") as fd:
+ fd.write("\n".join(lines))
+
+
+def main():
+ parser = optparse.OptionParser()
+ parser.add_option("--out-dir", dest="out_dir")
+ parser.add_option("--catalog-release", dest="catrel")
+ parser.add_option("--arch", dest="arch")
+ parser.add_option("--os-release", dest="osrel")
+ options, args = parser.parse_args()
+ cfg = CatalogFileGenerator(options.catrel, options.arch, options.osrel)
+ cfg.GenerateCatalog(options.out_dir)
+ cfg.GenerateDescriptions(options.out_dir)
+
+
+if __name__ == '__main__':
+ main()
Property changes on: csw/mgar/gar/v2/lib/python/generate_catalog_file.py
___________________________________________________________________
Added: svn:executable
+ *
Added: csw/mgar/gar/v2/lib/python/generate_catalog_file_test.py
===================================================================
--- csw/mgar/gar/v2/lib/python/generate_catalog_file_test.py (rev 0)
+++ csw/mgar/gar/v2/lib/python/generate_catalog_file_test.py 2012-05-29 19:29:11 UTC (rev 18161)
@@ -0,0 +1,60 @@
+#!/usr/bin/env python
+
+import unittest
+import mox
+import generate_catalog_file
+import rest
+
+PKG_DATA_1 = {
+ "basename": "389_admin-1.1.29,REV=2012.05.02-SunOS5.10-sparc-CSW.pkg.gz",
+ "catalogname": "389_admin",
+ "file_basename": "389_admin-1.1.29,REV=2012.05.02-SunOS5.10-sparc-CSW.pkg.gz",
+ "md5_sum": "fdb7912713da36afcbbe52266c15cb3f",
+ "mtime": "2012-05-02 12:06:38",
+ "rev": "2012.05.02",
+ "size": 395802,
+ "version": "1.1.29,REV=2012.05.02",
+ "version_string": "1.1.29,REV=2012.05.02"
+}
+
+class CatalogFileGeneratorUnitTest(mox.MoxTestBase):
+
+ def testComposeCatalogLineBasic(self):
+ mock_pkgcache = self.mox.CreateMock(rest.CachedPkgstats)
+ mock_rest = self.mox.CreateMock(rest.RestClient)
+ # Catalog format:
+ # http://wiki.opencsw.org/catalog-format
+ # common version package file md5 size dependencies category i-dependencies
+ # For example:
+ # bind 9.4.2,REV=2008.07.09_rev=p1 CSWbind
+ # bind-9.4.2,REV=2008.07.09_rev=p1-SunOS5.8-sparc-CSW.pkg.gz
+ # f68df57fcf54bfd37304b79d6f7eeacc 2954112 CSWcommon|CSWosslrt net none
+ cfg = generate_catalog_file.CatalogFileGenerator("dublin", "sparc", "SunOS5.10", mock_pkgcache, mock_rest)
+ mock_pkgcache.GetDeps('fdb7912713da36afcbbe52266c15cb3f').AndReturn(
+ {
+ "pkgname": "CSW389-admin-mock",
+ "deps": [
+ ["CSWfoo", ""],
+ ["CSWbar", ""],
+ ]
+ }
+ )
+ mock_pkgcache.GetPkgstats('fdb7912713da36afcbbe52266c15cb3f').AndReturn(
+ # {"i_depends": ["CSWincompatible", "CSWzorg"]}
+ {"i_depends": []}
+ )
+ self.mox.ReplayAll()
+ self.assertEquals(
+ "389_admin "
+ "1.1.29,REV=2012.05.02 "
+ "CSW389-admin-mock "
+ "389_admin-1.1.29,REV=2012.05.02-SunOS5.10-sparc-CSW.pkg.gz "
+ "fdb7912713da36afcbbe52266c15cb3f "
+ "395802 "
+ "CSWfoo|CSWbar "
+ "none none",
+ cfg.ComposeCatalogLine(PKG_DATA_1))
+
+
+if __name__ == '__main__':
+ unittest.main()
Property changes on: csw/mgar/gar/v2/lib/python/generate_catalog_file_test.py
___________________________________________________________________
Added: svn:executable
+ *
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
More information about the devel
mailing list