[packagekit] [PATCH 4/5] conary: fix refresh-cache

Jesse Zhang zh.jesse at gmail.com
Sat Jan 29 03:59:44 PST 2011


Stop trying to parse the XML file in __init__, or else refresh-cache has
no way to continue.

Also need to put _fetch_XML in XMLRepo. Data about a single repo should
be managed in XMLRepo instead of XMLCache.
---
 backends/conary/XMLCache.py |   93 +++++++++++++++++++++----------------------
 1 files changed, 45 insertions(+), 48 deletions(-)

diff --git a/backends/conary/XMLCache.py b/backends/conary/XMLCache.py
index f0c45a3..9175bc9 100644
--- a/backends/conary/XMLCache.py
+++ b/backends/conary/XMLCache.py
@@ -41,12 +41,28 @@ def mapGroup(categorieList):
     return where
 #}}}
 class XMLRepo:
-    xml_path = ""
-    repository = ""
-    def __init__(self, repo, path, pk):
+
+    # Let's only get XML data from things that we support.
+    # XXX We really should replace this with the Conary
+    #     RESTful API real soon now.
+    server = "http://packages.foresightlinux.org/cache/"
+    pregenerated_XML_labels = (
+        'conary.rpath.com at rpl:2-qa',
+        'foresight.rpath.org at fl:2',
+        'foresight.rpath.org at fl:2-qa',
+        'foresight.rpath.org at fl:2-devel',
+        'foresight.rpath.org at fl:2-kernel',
+        'foresight.rpath.org at fl:2-qa-kernel',
+        'foresight.rpath.org at fl:2-devel-kernel',
+    )
+
+    def __init__(self, label, path, pk):
         self.pk = pk
-        self.xml_path = path
-        self._setRepo(repo)
+        self.label = label
+        self.xml_file = "%s/%s.xml" % (path, label)
+
+        # Build up cache on first run
+        self.refresh_cache()
 
     def resolve(self, search_trove):
         """ resolve its a search with name """
@@ -71,21 +87,34 @@ class XMLRepo:
             return self._getAllPackages()
         return []
 
-    def _setRepo(self,repo):  
-        self.repo = repo
-        doc = self._open()
-        self.label = str( doc.get("label") )
+    def _fetchXML(self):
+        log.info("Updating XMLCache for label %s" % self.label)
+        if self.label in self.pregenerated_XML_labels:
+            wwwfile = "%s/%s.xml" % (self.server, self.label)
+            try:
+                wget = url.urlopen(wwwfile)
+                openfile = open(self.xml_file, 'w')
+                openfile.write(wget.read())
+                openfile.close()
+            except:
+                self.pk.error(ERROR_NO_NETWORK,"Failed to fetch %s." % wwwfile)
+        else:
+            generateXML.init(self.label, self.xml_file, self.conarypk)
+
+    def refresh_cache(self, force=False):
+        if force or not os.path.exists(self.xml_file):
+            self._fetchXML()
 
     def _open(self):
         try:
             return self._repo
         except AttributeError:
             try:
-                r = self.xml_path +self.repo
-                self._repo =   cElementTree.parse(r).getroot()
+                self._repo = cElementTree.parse(self.xml_file).getroot()
                 return self._repo
-            except:
-                self.pk.error(ERROR_REPO_CONFIGURATION_ERROR," The file %s not parsed submit a issue at http://issues.foresightlinux.org" % self.repo )
+            except SyntaxError as e:
+                self.pk.error(ERROR_REPO_CONFIGURATION_ERROR, "Failed to parse %s: %s. A cache refresh should fix this." %
+                        (self.xml_file, str(e)))
        
 
     def _generatePackage(self, package_node ): 
@@ -179,20 +208,6 @@ class XMLRepo:
 
 class XMLCache:
 
-    # Let's only get XML data from things that we support.
-    # XXX We really should replace this with the Conary
-    #     RESTful API real soon now.
-    pregenerated_XML_labels = (
-        'conary.rpath.com at rpl:2-qa',
-        'foresight.rpath.org at fl:2',
-        'foresight.rpath.org at fl:2-qa',
-        'foresight.rpath.org at fl:2-devel',
-        'foresight.rpath.org at fl:2-kernel',
-        'foresight.rpath.org at fl:2-qa-kernel',
-        'foresight.rpath.org at fl:2-devel-kernel',
-    )
-
-    server = "http://packages.foresightlinux.org/cache/"
     repos = []
     dbPath = '/var/cache/conary/'
     jobPath = dbPath + 'jobs'
@@ -211,9 +226,7 @@ class XMLCache:
             os.makedirs(self.xml_path )
 
         for label in self.labels:
-           if not os.path.exists( self.xml_path + label + ".xml"  ):
-                self._fetchXML(label)
-           self.repos.append(XMLRepo( label + ".xml", self.xml_path, self.pk ))
+            self.repos.append(XMLRepo(label, self.xml_path, self.pk))
 
     def _getJobCachePath(self, applyList):
         applyStr = '\0'.join(['%s=%s[%s]--%s[%s]%s' % (x[0], x[1][0], x[1][1], x[2][0], x[2][1], x[3]) for x in applyList])
@@ -252,8 +265,8 @@ class XMLCache:
         pass
 
     def refresh(self):
-        for label in self.labels:
-            self._fetchXML(label)
+        for repo in self.repos:
+            repo.refresh_cache(force=True)
 
     def resolve(self, name ):
         for repo in self.repos:
@@ -295,22 +308,6 @@ class XMLCache:
         #log.debug([i["name"] for i in results ] )
         return results
 
-    def _fetchXML(self, label):
-        log.info("Updating XMLCache for label %s" % label)
-        filename = label + '.xml'
-        filepath = self.xml_path + filename
-        if label in self.pregenerated_XML_labels:
-            wwwfile = self.server + filename
-            try:
-                wget = url.urlopen( wwwfile )
-                openfile = open(filepath,'w')
-                openfile.writelines(wget.readlines())
-                openfile.close()
-            except:
-                self.pk.error(ERROR_NO_NETWORK,"%s can not open" % wwwfile)
-        else:
-            generateXML.init(label,filepath,self.conarypk)
-
     def getGroup(self,categorieList):
         return getGroup(categorieList)
                 
-- 
1.7.3.4




More information about the PackageKit mailing list