Return-Path: X-Original-To: apmail-incubator-rat-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-rat-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 63D26685E for ; Sun, 10 Jul 2011 18:48:49 +0000 (UTC) Received: (qmail 79640 invoked by uid 500); 10 Jul 2011 18:48:49 -0000 Delivered-To: apmail-incubator-rat-commits-archive@incubator.apache.org Received: (qmail 79604 invoked by uid 500); 10 Jul 2011 18:48:49 -0000 Mailing-List: contact rat-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: rat-dev@incubator.apache.org Delivered-To: mailing list rat-commits@incubator.apache.org Received: (qmail 79597 invoked by uid 99); 10 Jul 2011 18:48:49 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 10 Jul 2011 18:48:49 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 10 Jul 2011 18:48:47 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 9AA762388906; Sun, 10 Jul 2011 18:48:27 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1144932 - in /incubator/rat/eye/trunk: org/apache/rat/eye/engine.py test/bogo-justajar-0.1.jar test/test_engine.py Date: Sun, 10 Jul 2011 18:48:27 -0000 To: rat-commits@incubator.apache.org From: rdonkin@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110710184827.9AA762388906@eris.apache.org> Author: rdonkin Date: Sun Jul 10 18:48:27 2011 New Revision: 1144932 URL: http://svn.apache.org/viewvc?rev=1144932&view=rev Log: Add methods to extract metainf from Jars. Added: incubator/rat/eye/trunk/test/bogo-justajar-0.1.jar (with props) Modified: incubator/rat/eye/trunk/org/apache/rat/eye/engine.py incubator/rat/eye/trunk/test/test_engine.py Modified: incubator/rat/eye/trunk/org/apache/rat/eye/engine.py URL: http://svn.apache.org/viewvc/incubator/rat/eye/trunk/org/apache/rat/eye/engine.py?rev=1144932&r1=1144931&r2=1144932&view=diff ============================================================================== --- incubator/rat/eye/trunk/org/apache/rat/eye/engine.py (original) +++ incubator/rat/eye/trunk/org/apache/rat/eye/engine.py Sun Jul 10 18:48:27 2011 @@ -24,6 +24,7 @@ Drives model creation. import os import logging import os.path +import zipfile # # Utilities @@ -87,6 +88,53 @@ def extension(filename): # Prune any trailing or leading dots return result.strip('.') +def contentFromZip(resource, names): + """ + Reads contents indexed by name from a zip archive. + + return: dictionary indexing lines read from a document by its name + rtype: dict + """ + if (hasattr(resource, "open")): + # Assume it's a ZipFile + return dict((name, resource.open(name).readlines()) for name in names) + else: + zip = zipfile.ZipFile(resource, "r") + try: + return readContentFromZip(zip) + finally: + zip.close() + +def filterContentFromZip(resource, selectionFunction=None): + """ + Reads names from zip archive, with optional filtering. + + return: dictionary indexing lines read from a document by its name + rtype: dict + """ + if (hasattr(resource, "open")): + # Assume it's a ZipFile + return contentFromZip(resource, filter(selectionFunction, resource.namelist())) + else: + zip = zipfile.ZipFile(resource, "r") + try: + return filterContentFromZip(zip, selectionFunction) + finally: + zip.close() + +def readContentsFromDirectoryInZip(resource, directory): + """ + Reads names from zip archive, with optional filtering. + + return: dictionary indexing lines read from a document by its name + rtype: dict + """ + return filterContentFromZip(resource, + lambda name: + os.path.dirname(name) == directory and not + name == directory and not + name == directory + "/") + # # Builders # @@ -184,22 +232,48 @@ class Artifact(): return "artifact:name=" + self.name def __eq__(self, other): - try: return (self.name == getattr(other, "name") and self.path == getattr(other, "path")) except AttributeError: return False + + def getFilePath(self): + """ + Full file path. + """ + return os.path.join(self.path, self.name); + +class Zip(Artifact): + """ + An artifact compressed using the zip algorithm. + """ + + def __init__(self, path, name): + Artifact.__init__(self, path, name) + -class Jar(Artifact): + def __repr__(self): + return "zip:name=" + self.name + + def contentsOfDirectory(self, directory): + return readContentsFromDirectoryInZip(self.getFilePath(), directory) + +class Jar(Zip): """ An artifact compressed using the zip algorithm and laid out according to the Jar format. """ def __init__(self, path, name): - Artifact.__init__(self, path, name) + Zip.__init__(self, path, name) def __repr__(self): return "jar:name=" + self.name + + def metainf(self): + """ + Reads the contents of the META-INF directory. + """ + return self.contentsOfDirectory("META-INF") \ No newline at end of file Added: incubator/rat/eye/trunk/test/bogo-justajar-0.1.jar URL: http://svn.apache.org/viewvc/incubator/rat/eye/trunk/test/bogo-justajar-0.1.jar?rev=1144932&view=auto ============================================================================== Binary file - no diff available. Propchange: incubator/rat/eye/trunk/test/bogo-justajar-0.1.jar ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Modified: incubator/rat/eye/trunk/test/test_engine.py URL: http://svn.apache.org/viewvc/incubator/rat/eye/trunk/test/test_engine.py?rev=1144932&r1=1144931&r2=1144932&view=diff ============================================================================== --- incubator/rat/eye/trunk/test/test_engine.py (original) +++ incubator/rat/eye/trunk/test/test_engine.py Sun Jul 10 18:48:27 2011 @@ -18,8 +18,10 @@ # import unittest +import os.path from org.apache.rat.eye import engine +from zipfile import ZipFile class TestScanner(unittest.TestCase): @@ -110,5 +112,53 @@ class TestScanner(unittest.TestCase): self.assertEqual(a.path, "another") + def sampleJar(self): + sampleJarName = "test/bogo-justajar-0.1.jar" + if not (os.path.exists(sampleJarName)): + self.fail("Cannot find required resource: " + sampleJarName) + try: + return ZipFile(sampleJarName, "r") + except IOError: + self.fail("Test requires sample jar " + sampleJarName) + + def testSampleJarIsReadable(self): + self.sampleJar().testzip() + + def sampleManifest(self): + return ['Manifest-Version: 1.0\r\n', 'Archiver-Version: Plexus Archiver\r\n', + 'Created-By: Apache Maven\r\n', 'Built-By: rob\r\n', + 'Build-Jdk: 1.6.0_22\r\n', 'Specification-Title: bogo-justajar\r\n', + 'Specification-Version: 0.1\r\n', + 'Specification-Vendor: The Apache Software Foundation\r\n', + 'Implementation-Title: bogo-justajar\r\n', + 'Implementation-Version: 0.1\r\n', + 'Implementation-Vendor-Id: org.apache.rat.eye.test\r\n', + 'Implementation-Vendor: The Apache Software Foundation\r\n', '\r\n'] + + + def testReadContentFromJar(self): + manifestName = "META-INF/MANIFEST.MF" + self.assertEquals(engine.contentFromZip(self.sampleJar(), [manifestName]), + {manifestName: self.sampleManifest()}) + + def testfilterContentFromZip(self): + manifestName = "META-INF/MANIFEST.MF" + self.assertEquals(engine.filterContentFromZip(self.sampleJar(), lambda name: (name == manifestName)), + {manifestName: self.sampleManifest()}) + + def testReadContentsFromDirectoryInZip(self): + contents = engine.readContentsFromDirectoryInZip(self.sampleJar(), "META-INF") + self.assertEquals(sorted(contents.keys()), + sorted(["META-INF/MANIFEST.MF", "META-INF/LICENSE", + "META-INF/NOTICE", "META-INF/DEPENDENCIES"])) + self.assertEquals(contents["META-INF/MANIFEST.MF"], self.sampleManifest()) + + def testJarMetainf(self): + contents = engine.Jar("test", "bogo-justajar-0.1.jar").metainf() + self.assertEquals(sorted(contents.keys()), + sorted(["META-INF/MANIFEST.MF", "META-INF/LICENSE", + "META-INF/NOTICE", "META-INF/DEPENDENCIES"])) + self.assertEquals(contents["META-INF/MANIFEST.MF"], self.sampleManifest()) + if __name__ == '__main__': unittest.main() \ No newline at end of file