You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@creadur.apache.org by rd...@apache.org on 2011/07/10 20:48:27 UTC
svn commit: r1144932 - in /incubator/rat/eye/trunk:
org/apache/rat/eye/engine.py test/bogo-justajar-0.1.jar test/test_engine.py
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