From 6a9bca3f64f094187d4392538d767a31882395f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20=C3=96zkural?= Date: Tue, 21 Jun 2005 15:01:14 +0000 Subject: [PATCH] * metadata ve specfile icin testlere basla * specfile ve metadata write'larini yazmaya basla * bunun icin xmlfile'a yazma destegi ekle * metadata'yi ayir --- pisi/files.py | 2 -- pisi/metadata.py | 23 ++++++++++++++++ pisi/specfile.py | 22 +--------------- pisi/xmlfile.py | 54 +++++++++++++++++++++++++++++++++----- tests/metadatatests.py | 17 ++++++++++++ tests/run.py | 9 ++++--- tests/sandbox/metadata.xml | 35 ++++++++++++++++++++++++ tests/specfiletests.py | 9 +++++-- 8 files changed, 136 insertions(+), 35 deletions(-) create mode 100644 pisi/metadata.py create mode 100644 tests/metadatatests.py create mode 100644 tests/sandbox/metadata.xml diff --git a/pisi/files.py b/pisi/files.py index a3fed5e7..5dcb3e27 100644 --- a/pisi/files.py +++ b/pisi/files.py @@ -57,5 +57,3 @@ class Files(XmlFile): for x in self.list: document.appendChild(x.elt(self.dom)) self.writexml(filename) - - diff --git a/pisi/metadata.py b/pisi/metadata.py new file mode 100644 index 00000000..ef9f8806 --- /dev/null +++ b/pisi/metadata.py @@ -0,0 +1,23 @@ + +from xmlfile import * +import specfile +from specfile import SpecFile + +class MetaData(SpecFile): + """This is a superset of the source spec definition""" + + def read(self, filename): + super(MetaData, self).read(filename) + distribution = self.getNodeText("Source/Distribution") + distributionRelease = self.getNodeText("Source/DistributionRelease") + architecture = self.getNodeText("Source/Architecture") + installedSize = int(self.getNodeText("Source/InstalledSize")) + + def write(self, filename): + ui.info("METADATA WRITE NOT IMPLEMENTED\n") + pass + + def verify(): + if len(packages) != 1: + return False + return True diff --git a/pisi/specfile.py b/pisi/specfile.py index f4341573..fac59e8e 100644 --- a/pisi/specfile.py +++ b/pisi/specfile.py @@ -95,25 +95,5 @@ class SpecFile(XmlFile): def write(self, filename): """Write PSPEC file""" self.newDOM() - ##TODO: write the good stuff here + self.addNodeText("Source/Name", source.name) self.writexml(filename) - -class MetaData(SpecFile): - """This is a superset of the source spec definition""" - - def read(self, filename): - super(MetaData, self).read(filename) - distribution = self.getNodeText("Source/Distribution") - distributionRelease = self.getNodeText("Source/DistributionRelease") - architecture = self.getNodeText("Source/Architecture") - installSize = int(self.getNodeText("Source/InstallSize")) - - def write(self, filename): - ui.info("METADATA WRITE NOT IMPLEMENTED\n") - pass - - def verify(): - if len(packages) != 1: - return False - return True - diff --git a/pisi/xmlfile.py b/pisi/xmlfile.py index 47e25767..e4068d03 100644 --- a/pisi/xmlfile.py +++ b/pisi/xmlfile.py @@ -9,13 +9,14 @@ class XmlError(Exception): # static functions def getNodeAttribute(node, attrname): + """get named attribute from DOM node""" for i in range(node.attributes.length): attr = node.attributes.item(i) if attr.name == attrname: return attr.childNodes[0].data def getNodeText(node): - # get the first child + """get the first child and expect it to be text!""" try: child = node.childNodes[0] except IndexError: @@ -28,6 +29,7 @@ def getNodeText(node): raise XmlError("getNodeText: Expected text node, got something else!") def getChildText(node_s, tagpath): + """get the text of a child at the end of a tag path""" node = getNode(node_s, tagpath) if not node: return None @@ -84,22 +86,29 @@ def getAllNodes(node, tagPath): return nodeList -def appendTagPath(node, tagpath, child): - """append child at the end of a tag chain""" +def appendTagPath(dom, node, tagpath, child): + """append child at the end of a tag chain starting from node""" for tag in tagpath[0:len(tagpath)-1]: - node = node.appendChild(createElement()) + node = node.appendChild(dom.createElement(tag)) node.appendChild(child) -def putNode(node, tagpath, newnode): +def addNode(dom, node, tagpath, newnode): tags = tagpath.split('/') + assert len(tags)>0 # iterative code to search for the path # get DOM for top node nodeList = node.getElementsByTagName(tags[0]) - if len(nodeList) == 0: - appendTagPath(node, tagpath, newnode) + print 'tags', tagpath + print '****', nodeList + + if len(nodeList) == 0: + print 'SIFIR' + appendTagPath(dom, node, tagpath, newnode) + return + node = nodeList[0] # discard other matches tags.pop(0) while len(tags)>0: @@ -129,6 +138,7 @@ class XmlFile(object): self.dom = impl.createDocument(None, self.rootTag, None) def unlink(self): + """deallocate DOM structure""" self.dom.unlink() def readxml(self, fileName): @@ -142,11 +152,27 @@ class XmlFile(object): if self.dom.documentElement.tagName != self.rootTag: raise XmlError("Root tagname not " + self.rootTag + " as expected") + def newNode(self, tag): + return self.dom.createElement(tag) + + def newTextNode(self, text): + return self.dom.createTextNode(text) + + def newAttribute(self, name): + return self.dom.createAttribute(name) + + # read helpers + def getNode(self, tagPath): """returns the *first* matching node for given tag path.""" self.verifyRootTag() return getNode(self.dom.documentElement, tagPath) + def getNodeText(self, tagPath): + """returns the text of *first* matching node for given tag path.""" + self.verifyRootTag() + return getNodeText(getNode(self.dom.documentElement, tagPath)) + def getAllNodes(self, tagPath): """returns all nodes matching a given tag path.""" self.verifyRootTag() @@ -179,3 +205,17 @@ class XmlFile(object): return None return getNodeText(node) + # write helpers + + def addNode(self, tagPath, newnode): + self.verifyRootTag() + return addNode(self.dom, self.dom.documentElement, tagPath, newnode) + + def addText(self, node, text): + node.appendChild(self.newTextNode(text)) + + def addNodeText(self, tagPath, text): + node = self.addNode(tagPath, self.newTextNode(text)) + return node + + diff --git a/tests/metadatatests.py b/tests/metadatatests.py new file mode 100644 index 00000000..ecd24ece --- /dev/null +++ b/tests/metadatatests.py @@ -0,0 +1,17 @@ + +import unittest +import os + +from pisi import metadata +from pisi import util +from pisi import context + +class MetaDataTestCase(unittest.TestCase): + def setUp(self): + pass + + def testReadWrite(self): + md = metadata.MetaData() + md.read('tests/sandbox/metadata.xml') + +suite = unittest.makeSuite(MetaDataTestCase) diff --git a/tests/run.py b/tests/run.py index 2ec516bc..c4fa4f45 100755 --- a/tests/run.py +++ b/tests/run.py @@ -9,6 +9,7 @@ runTestSuite = lambda(x): unittest.TextTestRunner(verbosity=2).run(x) def run_all(): import specfiletests + import metadatatests import contexttests import fetchertests import archivetests @@ -18,12 +19,14 @@ def run_all(): alltests = unittest.TestSuite(( specfiletests.suite, + specfiletests.suite, + metadatatests.suite, contexttests.suite, fetchertests.suite, archivetests.suite, - installdbtests.suite, - packagedbtests.suite, - actionsapitests.suite + installdbtests.suite, + packagedbtests.suite, + actionsapitests.suite )) runTestSuite(alltests) diff --git a/tests/sandbox/metadata.xml b/tests/sandbox/metadata.xml new file mode 100644 index 00000000..aae94bfc --- /dev/null +++ b/tests/sandbox/metadata.xml @@ -0,0 +1,35 @@ + + + + + + sandbox + http://cekirdek.uludag.org.tr/~meren/ + + Meral + meren@uludag.org.tr + + As-Is + + http://cekirdek.uludag.org.tr/~meren/sandbox.zip + + + + 06/15/2005 + 0.1 + 1 + + + Pardus + 0.1 + + + + sandbox + Bla + temprorary:zip + x86 + 40500 + + + diff --git a/tests/specfiletests.py b/tests/specfiletests.py index 2f12022a..9783e863 100644 --- a/tests/specfiletests.py +++ b/tests/specfiletests.py @@ -1,9 +1,10 @@ import unittest +import os from pisi import specfile -class SpecReadTestCase(unittest.TestCase): +class SpecFileTestCase(unittest.TestCase): def setUp(self): self.spec = specfile.SpecFile() self.spec.read("samples/popt/popt.pspec") @@ -27,4 +28,8 @@ class SpecReadTestCase(unittest.TestCase): def testLenPackages(self): self.assertEqual(len(self.spec.packages), 1) -suite = unittest.makeSuite(SpecReadTestCase) + def testCopy(self): + self.spec.read("samples/popt/popt.pspec") + self.spec.write(os.path.join(ctx.tmp_dir(), 'popt-copy.pspec')) + +suite = unittest.makeSuite(SpecFileTestCase)