def testRpmMagic(self): packages = [ ('rpm-with-bzip-5.0.29-1.i386.rpm', ('rpm-with-bzip', None, '5.0.29', '1', 'i386', False, 'RPM with a bzip payload', 'junk', 'GPL')), ('rpm-with-bzip-5.0.29-1.src.rpm', ('rpm-with-bzip', None, '5.0.29', '1', 'i386', True, 'RPM with a bzip payload', 'junk', 'GPL')), ('fileless-0.1-1.noarch.rpm', ('fileless', None, '0.1', '1', 'noarch', False, 'Foo', 'Foo', 'GPL')), ('fileless-0.1-1.src.rpm', ('fileless', None, '0.1', '1', 'noarch', True, 'Foo', 'Foo', 'GPL')), ('tags-1.2-3.noarch.rpm', ('tags', 4, '1.2', '3', 'noarch', False, 'Some Summary', 'Some Description', 'Some License')), ('tags-1.2-3.src.rpm', ('tags', 4, '1.2', '3', 'noarch', True, 'Some Summary', 'Some Description', 'Some License')), ] for (pkg, data) in packages: pn, pe, pv, pr, pa, isSource, summary, description, license = data fpath = os.path.join(resources.get_archive(), pkg) m = magic.magic(fpath) self.assertEqual(m.contents['name'], pn) self.assertEqual(m.contents['version'], pv) self.assertEqual(m.contents['release'], pr) self.assertEqual(m.contents['arch'], pa) self.assertEqual(m.contents['epoch'], pe) self.assertEqual(m.contents['isSource'], isSource) self.assertEqual(m.contents['summary'], summary) self.assertEqual(m.contents['description'], description) self.assertEqual(m.contents['license'], license)
def testJavaMagic(self): def _p(fpath): return os.path.join(resources.get_archive(), fpath) # Copy the jar and the war with names that don't suggest the type with # the extension ojar = os.path.join(self.workDir, "aJarFile") util.copyfile(_p("servlet-api.jar"), ojar) oear = os.path.join(self.workDir, "aEarFile") util.copyfile(_p("stockgrant.ear"), oear) # the EAR file convenienty has a WAR file in it, so let's use it warPath = os.path.join(self.workDir, "warfile") z = zipfile.ZipFile(oear) file(warPath, "a").write(z.read("war-ic.war")) tests = [ (_p("broken.jar"), magic.jar, None), (_p("servlet-api.jar"), magic.jar, None), (_p("stockgrant.ear"), magic.EAR, ("stockgrant3", "Application description")), (_p("ownerships.zip"), magic.ZIP, None), (ojar, magic.jar, None), (oear, magic.EAR, ("stockgrant3", "Application description")), (warPath, magic.WAR, ("WebApp1", None)), ] for fpath, expClass, metadata in tests: m = magic.magic(fpath) self.assertEqual(m.__class__, expClass) if not metadata: continue displayName, description = metadata self.assertEqual(m.contents["displayName"], displayName) if description is not None: self.assertEqual(m.contents["description"], description)
def testRpmMagic(self): packages = [ ( "rpm-with-bzip-5.0.29-1.i386.rpm", ("rpm-with-bzip", None, "5.0.29", "1", "i386", False, "RPM with a bzip payload", "junk", "GPL"), ), ( "rpm-with-bzip-5.0.29-1.src.rpm", ("rpm-with-bzip", None, "5.0.29", "1", "i386", True, "RPM with a bzip payload", "junk", "GPL"), ), ("fileless-0.1-1.noarch.rpm", ("fileless", None, "0.1", "1", "noarch", False, "Foo", "Foo", "GPL")), ("fileless-0.1-1.src.rpm", ("fileless", None, "0.1", "1", "noarch", True, "Foo", "Foo", "GPL")), ( "tags-1.2-3.noarch.rpm", ("tags", 4, "1.2", "3", "noarch", False, "Some Summary", "Some Description", "Some License"), ), ( "tags-1.2-3.src.rpm", ("tags", 4, "1.2", "3", "noarch", True, "Some Summary", "Some Description", "Some License"), ), ] for (pkg, data) in packages: pn, pe, pv, pr, pa, isSource, summary, description, license = data fpath = os.path.join(resources.get_archive(), pkg) m = magic.magic(fpath) self.assertEqual(m.contents["name"], pn) self.assertEqual(m.contents["version"], pv) self.assertEqual(m.contents["release"], pr) self.assertEqual(m.contents["arch"], pa) self.assertEqual(m.contents["epoch"], pe) self.assertEqual(m.contents["isSource"], isSource) self.assertEqual(m.contents["summary"], summary) self.assertEqual(m.contents["description"], description) self.assertEqual(m.contents["license"], license)
def testCarArchive(self): # for now, just make sure we don't treat them as zip m = magic.magic(os.path.join(resources.get_archive(), 'Transmitter.car')) if sys.version_info[:2] == (2, 6): self.assertFalse(m is None) else: self.assertEqual(m, None)
def testBzipFile(self): archivePath = os.path.join(self.workDir, 'archive.bz') b = bz2.BZ2File(archivePath, 'w') b.write('testing some random file') b.close() m = magic.magic(archivePath) self.assertEquals(m.name, 'bzip') self.assertEquals(sorted(m.contents.keys()), ['compression'])
def testBzipFile(self): archivePath = os.path.join(self.workDir, "archive.bz") b = bz2.BZ2File(archivePath, "w") b.write("testing some random file") b.close() m = magic.magic(archivePath) self.assertEquals(m.name, "bzip") self.assertEquals(sorted(m.contents.keys()), ["compression"])
def testBadBzipContents(self): archivePath = os.path.join(self.workDir, 'busted.tar.gz') f = open(archivePath, 'w') # this is the magic signature of a bzip file f.write('BZh') # and some garbage to fill out the file f.write('some random data') f.close() m = magic.magic(archivePath)
def testBadGzipContents(self): archivePath = os.path.join(self.workDir, "busted.tar.gz") f = open(archivePath, "w") # this is the magic signature of a gzip file f.write("\x1f\x8b") # and some garbage to fill out the file f.write("some random data") f.close() m = magic.magic(archivePath)
def testBzipContentsBasedir(self): archivePath = os.path.join('nested_dir', 'busted.tar.gz') fullPath = os.path.join(self.workDir, archivePath) util.mkdirChain(os.path.dirname(fullPath)) b = bz2.BZ2File(fullPath, 'w') b.write('some random data') b.close() m = magic.magic(archivePath, basedir = self.workDir) self.assertEquals(m.name, 'bzip')
def testBzipContentsBasedir(self): archivePath = os.path.join("nested_dir", "busted.tar.gz") fullPath = os.path.join(self.workDir, archivePath) util.mkdirChain(os.path.dirname(fullPath)) b = bz2.BZ2File(fullPath, "w") b.write("some random data") b.close() m = magic.magic(archivePath, basedir=self.workDir) self.assertEquals(m.name, "bzip")
def testTarArchives(self): fooFile = open(os.path.join(self.workDir, "foo"), "w") fooFile.write("test file") archivePath = os.path.join(self.workDir, "archive.tar") t = tarfile.TarFile(archivePath, mode="w") t.add(fooFile.name) t.close() m = magic.magic(archivePath) self.assertEquals(m.name, "tar") self.assertEquals(m.contents.keys(), ["GNU"])
def replaceBuildPath(self, path): #Now remove references to builddir, but # we don't want to mess with binaries # XXX probbaly need a better check for binary status m = magic.magic(path, basedir='/') extension = path.split('.')[-1] if m and m.name != 'ltwrapper': return if extension in ('pyo', 'pyc'): # add as needed return util.execute(("sed -i -e 's|%%(builddir)s|%%(testdir)s/%%(name)s-%%(version)s|g' '%s'" % path) % self.macros, verbose=False)
def testTarArchives(self): fooFile = open(os.path.join(self.workDir, 'foo'), 'w') fooFile.write('test file') archivePath = os.path.join(self.workDir, 'archive.tar') t = tarfile.TarFile(archivePath, mode = 'w') t.add(fooFile.name) t.close() m = magic.magic(archivePath) self.assertEquals(m.name, 'tar') self.assertEquals(m.contents.keys(), ['GNU'])
def testGzipFile(self): archivePath = os.path.join(self.workDir, 'archive.gz') g = gzip.GzipFile(archivePath, 'w') g.write('testing some random file that must be as least 262 bytes' \ 'long to test that the tar magic test in conary.lib.magic' \ 'will not mistakenly identify these file contents as a tar' \ 'archive. Originally there was a typo in the magic logic,' \ 'and anything that was longer than 262 bytes would trigger') g.close() m = magic.magic(archivePath) self.assertEquals(m.name, 'gzip') self.assertEquals(sorted(m.contents.keys()), ['compression', 'name'])
def doFile(self, path): if hasattr(self.recipe, '_getCapsulePathsForFile'): if self.recipe._getCapsulePathsForFile(path): return fullPath = util.joinPaths(self.recipe.macros.destdir, path) m = magic.magic(fullPath) if not (m and m.name == 'ZIP'): self.error("%s exists but isn't a valid Python .egg", path) else: self.error('Python .egg %s exists; use' ' --single-version-externally-managed argument' ' to setup.py or use r.PythonSetup()', path)
def testBzip2CompressedTarArchives(self): fooFile = open(os.path.join(self.workDir, "foo"), "w") fooFile.write("test file") archivePath = os.path.join(self.workDir, "archive.tar.bz2") b = bz2.BZ2File(archivePath, "w") t = tarfile.TarFile(mode="w", fileobj=b) t.add(fooFile.name) t.close() b.close() m = magic.magic(archivePath) self.assertEquals(m.name, "tar_bz2") self.assertEquals(sorted(m.contents.keys()), ["GNU", "compression"])
def testBzip2CompressedTarArchives(self): fooFile = open(os.path.join(self.workDir, 'foo'), 'w') fooFile.write('test file') archivePath = os.path.join(self.workDir, 'archive.tar.bz2') b = bz2.BZ2File(archivePath, 'w') t = tarfile.TarFile(mode = 'w', fileobj = b) t.add(fooFile.name) t.close() b.close() m = magic.magic(archivePath) self.assertEquals(m.name, 'tar_bz2') self.assertEquals(sorted(m.contents.keys()), ['GNU', 'compression'])
def replaceBuildPath(self, path): #Now remove references to builddir, but # we don't want to mess with binaries # XXX probbaly need a better check for binary status m = magic.magic(path, basedir='/') extension = path.split('.')[-1] if m and m.name != 'ltwrapper': return if extension in ('pyo', 'pyc'): # add as needed return util.execute(( "sed -i -e 's|%%(builddir)s|%%(testdir)s/%%(name)s-%%(version)s|g' '%s'" % path) % self.macros, verbose=False)
def testGzipFile(self): archivePath = os.path.join(self.workDir, "archive.gz") g = gzip.GzipFile(archivePath, "w") g.write( "testing some random file that must be as least 262 bytes" "long to test that the tar magic test in conary.lib.magic" "will not mistakenly identify these file contents as a tar" "archive. Originally there was a typo in the magic logic," "and anything that was longer than 262 bytes would trigger" ) g.close() m = magic.magic(archivePath) self.assertEquals(m.name, "gzip") self.assertEquals(sorted(m.contents.keys()), ["compression", "name"])
def _getConfigInfo(fileName): # RMK-973: it is now quite important not to mark a file as config # unless we can be quite confident that it can be handled as such fileMagic = magic.magic(fileName) if fileMagic: if isinstance(fileMagic, (magic.script, magic.ltwrapper)): return not _hasNullNearBeginning(fileName) else: # all other magic-recognized files are binary return False else: if checkin.nonCfgRe.match(fileName): return False elif checkin.cfgRe.match(fileName): return not _hasNullNearBeginning(fileName) return False
def doFile(self, path): if hasattr(self.recipe, '_getCapsulePathsForFile'): if self.recipe._getCapsulePathsForFile(path): return dir = self.recipe.macros.destdir fullPath = util.joinPaths(dir, path) m = magic.magic(fullPath) if not (m and m.name == 'ZIP'): # if it's not a zip, we can't unpack it, PythonEggs will raise # an error on this path return tmpPath = tempfile.mkdtemp(dir = self.recipe.macros.builddir) util.execute("unzip -q -o -d '%s' '%s'" % (tmpPath, fullPath)) self._addActionPathBuildRequires(['unzip']) os.unlink(fullPath) shutil.move(tmpPath, fullPath)
def testGzipCompressedTarArchives(self): fooFile = open(os.path.join(self.workDir, "foo"), "w") fooFile.write("test file") archivePath = os.path.join(self.workDir, "archive.tar.gz") g = gzip.GzipFile(archivePath, "w") t = tarfile.TarFile(mode="w", fileobj=g) t.add(fooFile.name) t.close() g.close() m = magic.magic(archivePath) self.assertEquals(m.name, "tar_gz") self.assertEquals(sorted(m.contents.keys()), ["GNU", "compression", "name"]) # tarfile in python 2.4 creates tar archives without the complete GNU magic # POSIX = "ustar\0" # GNU = "ustar \0" isGnu = sys.version_info[:2] in ((2, 6), (2, 7)) self.assertEquals(m.contents["GNU"], isGnu)
def testGzipCompressedTarArchives(self): fooFile = open(os.path.join(self.workDir, 'foo'), 'w') fooFile.write('test file') archivePath = os.path.join(self.workDir, 'archive.tar.gz') g = gzip.GzipFile(archivePath, 'w') t = tarfile.TarFile(mode = 'w', fileobj = g) t.add(fooFile.name) t.close() g.close() m = magic.magic(archivePath) self.assertEquals(m.name, 'tar_gz') self.assertEquals(sorted(m.contents.keys()), ['GNU', 'compression', 'name']) # tarfile in python 2.4 creates tar archives without the complete GNU magic # POSIX = "ustar\0" # GNU = "ustar \0" isGnu = (sys.version_info[:2] == (2, 6)) self.assertEquals(m.contents['GNU'], isGnu)
def testJavaMagic(self): def _p(fpath): return os.path.join(resources.get_archive(), fpath) # Copy the jar and the war with names that don't suggest the type with # the extension ojar = os.path.join(self.workDir, "aJarFile") util.copyfile(_p('servlet-api.jar'), ojar) oear = os.path.join(self.workDir, "aEarFile") util.copyfile(_p('stockgrant.ear'), oear) # the EAR file convenienty has a WAR file in it, so let's use it warPath = os.path.join(self.workDir, "warfile") z = zipfile.ZipFile(oear) file(warPath, "a").write(z.read("war-ic.war")) tests = [ (_p('broken.jar'), magic.jar, None), (_p('servlet-api.jar'), magic.jar, None), (_p('stockgrant.ear'), magic.EAR, ('stockgrant3', 'Application description')), (_p('ownerships.zip'), magic.ZIP, None), (ojar, magic.jar, None), (oear, magic.EAR, ('stockgrant3', 'Application description')), (warPath, magic.WAR, ('WebApp1', None)), ] for fpath, expClass, metadata in tests: m = magic.magic(fpath) self.assertEqual(m.__class__, expClass) if not metadata: continue displayName, description = metadata self.assertEqual(m.contents['displayName'], displayName) if description is not None: self.assertEqual(m.contents['description'], description)
def testGzipCompressedTarArchives3(self): fpath = os.path.join(resources.get_archive(), 'logrotate-3.7.1.tar.gz') m = magic.magic(fpath) self.assertEquals(m.name, 'tar_gz') self.assertEquals(sorted(m.contents.keys()), ['GNU', 'compression']) self.assertEquals(m.contents['GNU'], True)
def testDeb(self): m = magic.magic(os.path.join(resources.get_archive(), 'bash.deb')) self.assertTrue(isinstance(m, magic.deb))
def testShortGzipContents(self): # example short file from perl-PerlIO-gzip m = magic.magic(os.path.join(resources.get_archive(), 'ok50.gz.short'))
def testXzFile(self): m = magic.magic(os.path.join(resources.get_archive(), 'foo.tar.xz')) self.assertEquals(m.name, 'xz')
def testLzoFile(self): m = magic.magic(resources.get_archive('foo.tar.lzo')) self.assertEquals(m.name, 'lzo')
def _m(fpath): return magic.magic(os.path.join(resources.get_archive(), fpath))
def testGzipCompressedTarArchives3(self): fpath = os.path.join(resources.get_archive(), "logrotate-3.7.1.tar.gz") m = magic.magic(fpath) self.assertEquals(m.name, "tar_gz") self.assertEquals(sorted(m.contents.keys()), ["GNU", "compression"]) self.assertEquals(m.contents["GNU"], True)