def testExtractIllegal(self): try: with open("HelloWorld.tgz", "rb") as stream: unpack.unpack_archive("ftp://foo/file.tgz", stream, self.tmpdir, extract="Hello`World`") assert False except SafeException as ex: assert "Illegal" in str(ex)
def testBadExt(self): try: with open('HelloWorld.tgz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.foo', stream, self.tmpdir) assert False except SafeException as ex: assert 'Unknown extension' in str(ex)
def unpack_to_tmp(url, archive_file, mime_type): """Creates a temporary directory and unpacks the archive to it in "unpacked". Permissions are correct for importing into the cache. Returns the tmpdir.""" if not mime_type: mime_type = unpack.type_from_url(url) assert mime_type, "Can't guess MIME type from {url}".format(url = url) tmpdir = tempfile.mkdtemp('-0template') try: # Must be readable to helper process running as 'zeroinst'... old_umask = os.umask(0o022) try: unpack_dir = os.path.join(tmpdir, 'unpacked') os.mkdir(unpack_dir) with open(archive_file, 'rb') as stream: unpack.unpack_archive(url, stream, unpack_dir, type = mime_type, start_offset = 0) manifest.fixup_permissions(unpack_dir) finally: os.umask(old_umask) except: support.ro_rmtree(tmpdir) raise return tmpdir
def testNonAscii(self): mydir = os.path.join(self.tmpdir, "unicode") os.mkdir(mydir) with open("unicode.tar.gz", "rb") as stream: unpack.unpack_archive("ftp://foo/file.tgz", stream, self.tmpdir) self.assertEqual( [ s.decode("utf-8") for s in [ b"D /unicode", b"D /unicode/test-unic\xcc\xa7\xc3\xb8\xc3\xb0e\xcc\x88", b"F c1c727274876ed5915c75a907131b8462cfdd5ba278140067dc80a2bcba033d6 1377477018 14 file", ] ], list(manifest.generate_manifest(self.tmpdir, alg="sha256")), ) self.assertEqual( [ s.decode("utf-8") for s in [ b"D /unicode", b"D /unicode/test-unic\xcc\xa7\xc3\xb8\xc3\xb0e\xcc\x88", b"F 5f1ff6172591102593950d1ae6c4a78709b1c44c 1377477018 14 file", ] ], list(manifest.generate_manifest(self.tmpdir, alg="sha1new")), )
def testExtractNonAscii(self): with open('unicode.tar.gz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir, extract=b'unicode'.decode('ascii')) self.assert_manifest('sha1=af2d132f5f15532bbf041b59414d08c8bc1a616e')
def testExtractZip(self): with open('HelloWorld.zip', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.zip', stream, self.tmpdir, extract='HelloWorld') self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testBadExt(self): try: with open("HelloWorld.tgz", "rb") as stream: unpack.unpack_archive("ftp://foo/file.foo", stream, self.tmpdir) assert False except SafeException as ex: assert "Unknown extension" in str(ex)
def testBadExt(self): try: unpack.unpack_archive('ftp://foo/file.foo', file('HelloWorld.tgz'), self.tmpdir) assert False except SafeException as ex: assert 'Unknown extension' in str(ex)
def testExtractIllegal(self): try: with open('HelloWorld.tgz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir, extract = 'Hello`World`') assert False except SafeException as ex: assert 'Illegal' in str(ex)
def testExtractIllegal(self): try: unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract='Hello`World`') assert False except SafeException as ex: assert 'Illegal' in str(ex)
def fetch(url, base, filename, dest, extract=None, type=None, local_file=None): mode = 'w+' if local_file is None else 'r' with open(local_file or os.path.join(base, filename), mode) as data: if local_file is None: log.info("downloading %s -> %s" % (url, os.path.join(base, filename))) with contextlib.closing(urllib2.urlopen(url)) as stream: while True: chunk = stream.read(1024) if not chunk: break data.write(chunk) data.seek(0) os.makedirs(os.path.join(base, dest)) unpack.unpack_archive(url, data = data, destdir = os.path.join(base, dest), extract=extract, type=type)
def testExtractFails(self): stderr = os.dup(2) try: null = os.open("/dev/null", os.O_WRONLY) os.close(2) os.dup2(null, 2) try: unpack.unpack_archive("ftp://foo/file.tgz", file("HelloWorld.tgz"), self.tmpdir, extract="HelloWorld2") assert False except SafeException, ex: if "Failed to extract" not in str(ex) and "Unable to find" not in str(ex): # GNU tar # Python tar raise ex finally: os.dup2(stderr, 2)
def testExtractFails(self): stderr = os.dup(2) try: null = os.open(os.devnull, os.O_WRONLY) os.close(2) os.dup2(null, 2) try: unpack.unpack_archive('ftp://foo/file.tgz', open('HelloWorld.tgz'), self.tmpdir, extract = 'HelloWorld2') assert False except SafeException as ex: if ('Failed to extract' not in str(ex) and # GNU tar 'Unable to find' not in str(ex)): # Python tar raise ex finally: os.dup2(stderr, 2)
def testExtractFails(self): stderr = os.dup(2) try: null = os.open('/dev/null', os.O_WRONLY) os.close(2) os.dup2(null, 2) try: unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract='HelloWorld2') assert False except SafeException as ex: if ('Failed to extract' not in str(ex) and # GNU tar 'Unable to find' not in str(ex)): # Python tar raise ex finally: os.dup2(stderr, 2)
def handle(config, options, args): """@type args: [str]""" if len(args) == 1: extract = None elif len(args) == 2: extract = args[1] else: raise UsageError() source = args[0] alg = manifest.algorithms.get(options.algorithm or 'sha1new', None) if alg is None: raise SafeException(_('Unknown algorithm "%s"') % alg) show_manifest = bool(options.manifest) show_digest = bool(options.digest) or not show_manifest def do_manifest(d): if extract is not None: d = os.path.join(d, extract) digest = alg.new_digest() for line in alg.generate_manifest(d): if show_manifest: print(line) digest.update((line + '\n').encode('utf-8')) if show_digest: print(alg.getID(digest)) if os.path.isdir(source): if extract is not None: raise SafeException("Can't use extract with a directory") do_manifest(source) else: data = None tmpdir = tempfile.mkdtemp() try: data = open(args[0], 'rb') unpack.unpack_archive(source, data, tmpdir, extract) do_manifest(tmpdir) finally: support.ro_rmtree(tmpdir) if data: data.close()
def testNonAscii(self): mydir = os.path.join(self.tmpdir, 'unicode') os.mkdir(mydir) with open('unicode.tar.gz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir) self.assertEqual([ s.decode('utf-8') for s in [ b'D /unicode', b'D /unicode/test-unic\xcc\xa7\xc3\xb8\xc3\xb0e\xcc\x88', b'F c1c727274876ed5915c75a907131b8462cfdd5ba278140067dc80a2bcba033d6 1377477018 14 file' ] ], list(manifest.generate_manifest(self.tmpdir, alg='sha256'))) self.assertEqual([ s.decode('utf-8') for s in [ b'D /unicode', b'D /unicode/test-unic\xcc\xa7\xc3\xb8\xc3\xb0e\xcc\x88', b'F 5f1ff6172591102593950d1ae6c4a78709b1c44c 1377477018 14 file' ] ], list(manifest.generate_manifest(self.tmpdir, alg='sha1new')))
def testExtractNonAscii(self): with open('unicode.tar.gz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir, extract= b'unicode'.decode('ascii')) self.assert_manifest('sha1=af2d132f5f15532bbf041b59414d08c8bc1a616e')
def testTbz(self): unpack.unpack_archive("ftp://foo/file.tar.bz2", file("HelloWorld.tar.bz2"), self.tmpdir) self.assert_manifest("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a")
def testDmg(self): with open('HelloWorld.dmg', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.dmg', stream, self.tmpdir) self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testExtract(self): unpack.unpack_archive('ftp://foo/file.tgz', file('HelloWorld.tgz'), self.tmpdir, extract='HelloWorld') self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testDmg(self): with open("HelloWorld.dmg", "rb") as stream: unpack.unpack_archive("ftp://foo/file.dmg", stream, self.tmpdir) self.assert_manifest("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a")
def testLzma(self): with open("HelloWorld.tar.lzma", "rb") as stream: unpack.unpack_archive("ftp://foo/file.tar.lzma", stream, self.tmpdir) self.assert_manifest("sha1new=290eb133e146635fe37713fd58174324a16d595f")
def testDeb(self): with open("dummy_1-1_all.deb", "rb") as stream: unpack.unpack_archive("ftp://foo/file.deb", stream, self.tmpdir) self.assert_manifest("sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea")
def testExtract(self): with open('HelloWorld.tgz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir, extract = 'HelloWorld') self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testTar(self): with open('HelloWorld.tar', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tar', stream, self.tmpdir) self.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
def testDmg(self): unpack.unpack_archive('ftp://foo/file.dmg', file('HelloWorld.dmg'), self.tmpdir) self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testTar(self): unpack.unpack_archive('ftp://foo/file.tar', file('HelloWorld.tar'), self.tmpdir) self.assert_manifest( 'sha1new=290eb133e146635fe37713fd58174324a16d595f')
def local_archive_changed(chooser): model.clear() path = chooser.get_filename() widgets.get_widget('subdirectory_frame').set_sensitive(False) self.destroy_tmp() if not path: return if mime_type.get_active() == 0: type = None else: type = mime_type.get_active_text() archive_url = widgets.get_widget('archive_url') url = archive_url.get_text() if not url: url = 'http://SITE/' + os.path.basename(path) archive_url.set_text(url) start_offset = 0 if not type: if url.endswith('.package'): type = 'Autopackage' else: type = unpack.type_from_url(url) if type == 'Autopackage': # Autopackage isn't a real type. Examine the .package file # and find out what it really is. start_offset, type = autopackage_get_details(path) self.tmpdir = tempfile.mkdtemp('-0publish-gui') try: # Must be readable to helper process running as 'zeroinst'... old_umask = os.umask(0022) try: unpack_dir = os.path.join(self.tmpdir, 'unpacked') os.mkdir(unpack_dir) dialog.window.set_cursor(watch) gtk.gdk.flush() try: unpack.unpack_archive(url, file(path), unpack_dir, type = type, start_offset = start_offset) manifest.fixup_permissions(unpack_dir) finally: dialog.window.set_cursor(None) finally: os.umask(old_umask) except: chooser.unselect_filename(path) self.destroy_tmp() raise iter = model.append(None, ['Everything']) items = os.listdir(unpack_dir) for f in items: model.append(iter, [f]) tree.expand_all() # Choose a sensible default iter = model.get_iter_root() if len(items) == 1 and \ os.path.isdir(os.path.join(unpack_dir, items[0])) and \ items[0] not in ('usr', 'opt', 'bin', 'etc', 'sbin', 'doc', 'var'): iter = model.iter_children(iter) selection.select_iter(iter) self.mime_type = type self.start_offset = start_offset widgets.get_widget('subdirectory_frame').set_sensitive(True)
def testDeb(self): unpack.unpack_archive('ftp://foo/file.deb', open('dummy_1-1_all.deb'), self.tmpdir) self.assert_manifest('sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
def testRPM(self): with open("dummy-1-1.noarch.rpm", "rb") as stream: unpack.unpack_archive("ftp://foo/file.rpm", stream, self.tmpdir) self.assert_manifest("sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d")
def testLzma(self): unpack.unpack_archive('ftp://foo/file.tar.lzma', open('HelloWorld.tar.lzma'), self.tmpdir) self.assert_manifest('sha1new=290eb133e146635fe37713fd58174324a16d595f')
def testGem(self): with open("hello-0.1.gem", "rb") as stream: unpack.unpack_archive("ftp://foo/file.gem", stream, self.tmpdir) self.assert_manifest("sha1new=fbd4827be7a18f9821790bdfd83132ee60d54647")
def testNonAsciiTgz(self): with open('unicode.tar.gz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir) self.assert_manifest('sha1new=e42ffed02179169ef2fa14a46b0d9aea96a60c10')
def testExtractIllegal(self): try: unpack.unpack_archive("ftp://foo/file.tgz", file("HelloWorld.tgz"), self.tmpdir, extract="Hello`World`") assert False except SafeException, ex: assert "Illegal" in str(ex)
def testNonAsciiTgz(self): with open('unicode.tar.gz', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tgz', stream, self.tmpdir) self.assert_manifest( 'sha1new=e42ffed02179169ef2fa14a46b0d9aea96a60c10')
def testExtractZip(self): with open("HelloWorld.zip", "rb") as stream: unpack.unpack_archive("ftp://foo/file.zip", stream, self.tmpdir, extract="HelloWorld") self.assert_manifest("sha1=3ce644dc725f1d21cfcf02562c76f375944b266a")
def testZip(self): unpack.unpack_archive('ftp://foo/file.zip', file('HelloWorld.zip'), self.tmpdir) self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testRPM(self): unpack.unpack_archive('ftp://foo/file.rpm', open('dummy-1-1.noarch.rpm'), self.tmpdir) self.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
def testRPM(self): with open('dummy-1-1.noarch.rpm', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.rpm', stream, self.tmpdir) self.assert_manifest('sha1=7be9228c8fe2a1434d4d448c4cf130e3c8a4f53d')
def testGem(self): unpack.unpack_archive('ftp://foo/file.gem', open('hello-0.1.gem'), self.tmpdir) self.assert_manifest('sha1new=fbd4827be7a18f9821790bdfd83132ee60d54647')
def testDeb(self): with open('dummy_1-1_all.deb', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.deb', stream, self.tmpdir) self.assert_manifest( 'sha1new=2c725156ec3832b7980a3de2270b3d8d85d4e3ea')
def testExtractZip(self): unpack.unpack_archive('ftp://foo/file.zip', open('HelloWorld.zip'), self.tmpdir, extract = 'HelloWorld') self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testGem(self): with open('hello-0.1.gem', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.gem', stream, self.tmpdir) self.assert_manifest( 'sha1new=fbd4827be7a18f9821790bdfd83132ee60d54647')
def testTbz(self): unpack.unpack_archive('ftp://foo/file.tar.bz2', open('HelloWorld.tar.bz2'), self.tmpdir) self.assert_manifest('sha1=3ce644dc725f1d21cfcf02562c76f375944b266a')
def testLzma(self): with open('HelloWorld.tar.lzma', 'rb') as stream: unpack.unpack_archive('ftp://foo/file.tar.lzma', stream, self.tmpdir) self.assert_manifest( 'sha1new=290eb133e146635fe37713fd58174324a16d595f')