def test_uncompress_file(self): p = os.path.join('tests', 'fixtures', 'foobar', 'foo') prz = mkstemppath() pr2 = mkstemppath() xz.compress(p, prz) xz.uncompress(prz, pr2)
def test_get_utc_mtime(self): apath = mkstemppath() with open(apath, 'a'): pass os.utime(apath, (978307200, 978307200)) # 1 Jan 2001, 00:00:00 UTC assert_equal(datetime(2001, 1, 1, 0, 0), Archive.get_mtime_as_utcdatetime(apath)) os.unlink(apath)
def test_has_version(self): foo = os.path.join('foobar', 'foo') apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) assert_equal(0, a.has_version(foo)) assert_equal(None, a.has_version(os.path.join('foobar', 'bar')))
def test_add_100_versions(self): foo = os.path.join('foobar', 'foo') apath = mkstemppath() a = Archive(apath, rzip) for n in range(0, 100): a.add_version([foo]) TestArchive.assert_ziprz_filenames(apath, [ "versions/%d/foobar/foo"%(n) for n in range(0, 100) ])
def test_add_file(self): foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) a.add_version([bar]) TestArchive.assert_ziprz_filenames(apath, ["versions/0/foobar/foo", "versions/1/foobar/bar"])
def test_add_100_versions(self): foo = os.path.join('foobar', 'foo') apath = mkstemppath() a = Archive(apath, rzip) for n in range(0, 100): a.add_version([foo]) TestArchive.assert_ziprz_filenames( apath, ["versions/%d/foobar/foo" % (n) for n in range(0, 100)])
def test_compress_file(self): p = os.path.join('tests', 'fixtures', 'foobar', 'foo') prz = mkstemppath() assert(os.path.exists(p)) assert(not(os.path.exists(prz))) rzip.compress(p, prz) assert(os.path.exists(prz)) assert_equal(hashlib.md5(open(prz).read()).hexdigest(), "dfa5ce71cda1c59f44eef332d4cb98cd") os.unlink(prz)
def test_add_file(self): foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) a.add_version([bar]) TestArchive.assert_ziprz_filenames( apath, ["versions/0/foobar/foo", "versions/1/foobar/bar"])
def test_compress_file(self): p = os.path.join('tests', 'fixtures', 'foobar', 'foo') prz = mkstemppath() assert(os.path.exists(p)) assert(not(os.path.exists(prz))) xz.compress(p, prz) assert(os.path.exists(prz)) assert_equal(hashlib.md5(open(prz, 'rb').read()).hexdigest(), "26fcf3231c59f1e991b9846b8e732452") os.unlink(prz)
def test_add_dir(self): dir = 'foobar' apath = mkstemppath() a = Archive(apath, rzip) a.add_version([dir]) TestArchive.assert_ziprz_filenames(apath, [ "versions/0/foobar/", "versions/0/foobar/symlink", "versions/0/foobar/bar", "versions/0/foobar/foo" ])
def test_external_attr(self): foo = os.path.join('foobar', 'foo') os.chmod(foo, 0o644) apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) with rzip.TempUncompress(apath) as zippath: with ZipFile(zippath, mode='r', allowZip64=True) as myzip: info = myzip.infolist()[0] assert_equal(info.external_attr, 0o100644 << 16)
def test_add_unicode_file(self): i = "“Iñtërnâtiônàlizætiøn”" apath = mkstemppath() a = Archive(apath, rzip) a.add_version([i]) TestArchive.assert_ziprz_filenames(apath, ["versions/0/“Iñtërnâtiônàlizætiøn”"]) xdir = mkdtemp() a.extract(xdir, 0) versiondir = os.path.join(xdir, os.path.basename(apath)) assert(os.path.exists(os.path.join(versiondir, '0', "“Iñtërnâtiônàlizætiøn”"))) assert_equal(open(i, 'rb').read(), open(os.path.join(versiondir, '0', '“Iñtërnâtiônàlizætiøn”'), 'rb').read())
def test_compress_file(self): p = os.path.join('tests', 'fixtures', 'foobar', 'foo') prz = mkstemppath() assert (os.path.exists(p)) assert (not (os.path.exists(prz))) rzip.compress(p, prz) assert (os.path.exists(prz)) assert_equal( hashlib.md5(open(prz, 'rb').read()).hexdigest(), "dfa5ce71cda1c59f44eef332d4cb98cd") os.unlink(prz)
def test_compress_file(self): p = os.path.join('tests', 'fixtures', 'foobar', 'foo') prz = mkstemppath() assert (os.path.exists(p)) assert (not (os.path.exists(prz))) xz.compress(p, prz) assert (os.path.exists(prz)) assert_equal( hashlib.md5(open(prz, 'rb').read()).hexdigest(), "26fcf3231c59f1e991b9846b8e732452") os.unlink(prz)
def test_zip_versions(self): foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) a.add_version([bar]) with rzip.TempUncompress(apath) as zippath: with ZipFile(zippath, mode='r', allowZip64=True) as myzip: assert_equal([[('foobar/foo', 4289425978)], [('foobar/bar', 1226766874)]], Archive._zip_versions(myzip))
def test_timestamps(self): foo = os.path.join('foobar', 'foo') os.utime(foo, (978307200, 978307200)) apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) with rzip.TempUncompress(apath) as zippath: with ZipFile(zippath, mode='r', allowZip64=True) as myzip: info = myzip.infolist()[0] assert_equal(info.date_time, (2001, 1, 1, 0, 0, 0)) mtime = Archive.parse_extended_mtime(info) assert_equal(978307200, mtime)
def test_cli(self): a = mkstemppath() foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') sparchive.cli.main(["addversion", a, foo]) sparchive.cli.main(["addversion", a, bar]) assert(os.path.exists(a)) with rzip.TempUnrzip(a) as zippath: with ZipFile(zippath, 'r') as myzip: filenames = [] for info in myzip.infolist(): filenames.append(info.filename) assert_equal(filenames, ["versions/0/foobar/foo", "versions/1/foobar/bar"]) sparchive.cli.main(["list", a])
def test_is_entries(self): foo = os.path.join('foobar') apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) with rzip.TempUncompress(apath) as zippath: with ZipFile(zippath, mode='r', allowZip64=True) as myzip: for info in myzip.infolist(): if info.filename == "versions/0/foobar/symlink": assert_true(Archive.islink_entry(info)) elif info.filename == "versions/0/foobar/": assert_true(Archive.isdir_entry(info)) else: assert_true(not(Archive.isdir_entry(info)) and not(Archive.islink_entry(info)))
def test_is_entries(self): foo = os.path.join('foobar') apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) with rzip.TempUncompress(apath) as zippath: with ZipFile(zippath, mode='r', allowZip64=True) as myzip: for info in myzip.infolist(): if info.filename == "versions/0/foobar/symlink": assert_true(Archive.islink_entry(info)) elif info.filename == "versions/0/foobar/": assert_true(Archive.isdir_entry(info)) else: assert_true(not (Archive.isdir_entry(info)) and not (Archive.islink_entry(info)))
def add_version(self, pathlist): """Add a version to this archive.""" if not(os.path.isdir(os.path.dirname(self.archive_path))): os.makedirs(os.path.dirname(self.archive_path)) for path in pathlist: if not(os.path.exists(path)): raise Exception() with rzip.TempUnrzip(self.archive_path) as zippath: new_version = self.get_version_count(zippath) with ZipFile(zippath, mode='a', allowZip64=True) as myzip: for path in pathlist: self._add_path(path, new_version, myzip) tmprzip = mkstemppath() rzip.compress(zippath, tmprzip) # perform sanity checks here os.rename(tmprzip, self.archive_path)
def test_cli_extract(self): a = mkstemppath() foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') sparchive.cli.main(["addversion", a, foo]) sparchive.cli.main(["addversion", a, bar]) xdir = mkdtemp() olddir = os.getcwd() os.chdir(os.path.join(xdir)) sparchive.cli.main(["extract", a]) os.chdir(olddir) xdir = os.path.join(xdir, os.path.basename(a)) assert(os.path.exists(os.path.join(xdir, '0', 'foobar', 'foo'))) assert_equal(open(foo).read(), open(os.path.join(xdir, '0', 'foobar', 'foo')).read()) assert(os.path.exists(os.path.join(xdir,'1', 'foobar', 'bar'))) assert_equal(open(bar).read(), open(os.path.join(xdir, '1', 'foobar', 'bar')).read())
def test_add_unicode_file(self): i = "“Iñtërnâtiônàlizætiøn”" apath = mkstemppath() a = Archive(apath, rzip) a.add_version([i]) TestArchive.assert_ziprz_filenames( apath, ["versions/0/“Iñtërnâtiônàlizætiøn”"]) xdir = mkdtemp() a.extract(xdir, 0) versiondir = os.path.join(xdir, os.path.basename(apath)) assert (os.path.exists( os.path.join(versiondir, '0', "“Iñtërnâtiônàlizætiøn”"))) assert_equal( open(i, 'rb').read(), open(os.path.join(versiondir, '0', '“Iñtërnâtiônàlizætiøn”'), 'rb').read())
def test_cli(self): a = mkstemppath() foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') sparchive.cli.main(["addversion", a, foo]) sparchive.cli.main(["addversion", a, bar]) assert (os.path.exists(a)) with rzip.TempUncompress(a) as zippath: with ZipFile(zippath, 'r') as myzip: filenames = [] for info in myzip.infolist(): filenames.append(info.filename) assert_equal( filenames, ["versions/0/foobar/foo", "versions/1/foobar/bar"]) sparchive.cli.main(["list", a])
def add_version(self, pathlist): """Add a version to this archive.""" if not (os.path.isdir( os.path.dirname(os.path.abspath(self.archive_path)))): os.makedirs(os.path.dirname(os.path.abspath(self.archive_path))) for path in pathlist: if not (os.path.exists(path)): raise Exception() with self.compress_module.TempUncompress(self.archive_path) as zippath: new_version = self.get_version_count(zippath) with ZipFile(zippath, mode='a', allowZip64=True) as myzip: for path in pathlist: self._add_path(path, new_version, myzip) tmprzip = mkstemppath() self.compress_module.compress(zippath, tmprzip) # perform sanity checks here shutil.move(tmprzip, self.archive_path)
def test_extract(self): foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') os.utime(foo, (978307200, 978307200)) os.utime(bar, (978307200, 978307200)) os.chmod(foo, 0o644) os.chmod(bar, 0o7755) apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) a.add_version([bar]) a.add_version(['foobar']) xdir = mkdtemp() a.extract(xdir, 0) versiondir = os.path.join(xdir, os.path.basename(apath)) assert(os.path.exists(os.path.join(versiondir, '0', 'foobar', 'foo'))) assert_equal(open(foo, 'rb').read(), open(os.path.join(versiondir, '0', 'foobar', 'foo'), 'rb').read()) a.extract(xdir, 1) assert(os.path.exists(os.path.join(versiondir, '1', 'foobar', 'bar'))) assert_equal(open(bar, 'rb').read(), open(os.path.join(versiondir, '1', 'foobar', 'bar'), 'rb').read()) shutil.rmtree(xdir) # now extract all versions xdir = mkdtemp() versiondir = os.path.join(xdir, os.path.basename(apath)) a.extract(xdir) assert(os.path.exists(os.path.join(versiondir, '0', 'foobar', 'foo'))) assert_equal(open(foo, 'rb').read(), open(os.path.join(versiondir, '0', 'foobar', 'foo'), 'rb').read()) assert_equal(978307200.0, os.path.getmtime(os.path.join(versiondir, '0', 'foobar', 'foo'))) assert_equal(0o644, os.stat(os.path.join(versiondir, '0', 'foobar', 'foo')).st_mode & 0o000777) assert(os.path.exists(os.path.join(versiondir, '1', 'foobar', 'bar'))) assert_equal(978307200.0, os.path.getmtime(os.path.join(versiondir, '1', 'foobar', 'bar'))) assert_equal(open(bar, 'rb').read(), open(os.path.join(versiondir, '1', 'foobar', 'bar'), 'rb').read()) assert_equal(0o7755, os.stat(os.path.join(versiondir, '1', 'foobar', 'bar')).st_mode & 0o007777) #check symlink assert(os.path.exists(os.path.join(versiondir, '2', 'foobar', 'symlink'))) assert_equal('foo', os.readlink(os.path.join(versiondir, '2', 'foobar', 'symlink'))) shutil.rmtree(xdir)
def test_cli_extract(self): a = mkstemppath() foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') sparchive.cli.main(["addversion", a, foo]) sparchive.cli.main(["addversion", a, bar]) xdir = mkdtemp() olddir = os.getcwd() try: os.chdir(os.path.join(xdir)) sparchive.cli.main(["extract", a]) finally: os.chdir(olddir) xdir = os.path.join(xdir, os.path.basename(a)) assert (os.path.exists(os.path.join(xdir, '0', 'foobar', 'foo'))) assert_equal( open(foo, 'rb').read(), open(os.path.join(xdir, '0', 'foobar', 'foo'), 'rb').read()) assert (os.path.exists(os.path.join(xdir, '1', 'foobar', 'bar'))) assert_equal( open(bar, 'rb').read(), open(os.path.join(xdir, '1', 'foobar', 'bar'), 'rb').read())
def test_add_dir(self): dir = 'foobar' apath = mkstemppath() a = Archive(apath, rzip) a.add_version([dir]) TestArchive.assert_ziprz_filenames(apath, ["versions/0/foobar/", "versions/0/foobar/symlink", "versions/0/foobar/bar", "versions/0/foobar/foo"])
def test_mkstemppath(self): n = mkstemppath() assert_false(path.exists(n))
def test_extract(self): foo = os.path.join('foobar', 'foo') bar = os.path.join('foobar', 'bar') os.utime(foo, (978307200, 978307200)) os.utime(bar, (978307200, 978307200)) os.chmod(foo, 0o644) os.chmod(bar, 0o7755) apath = mkstemppath() a = Archive(apath, rzip) a.add_version([foo]) a.add_version([bar]) a.add_version(['foobar']) xdir = mkdtemp() a.extract(xdir, 0) versiondir = os.path.join(xdir, os.path.basename(apath)) assert (os.path.exists(os.path.join(versiondir, '0', 'foobar', 'foo'))) assert_equal( open(foo, 'rb').read(), open(os.path.join(versiondir, '0', 'foobar', 'foo'), 'rb').read()) a.extract(xdir, 1) assert (os.path.exists(os.path.join(versiondir, '1', 'foobar', 'bar'))) assert_equal( open(bar, 'rb').read(), open(os.path.join(versiondir, '1', 'foobar', 'bar'), 'rb').read()) shutil.rmtree(xdir) # now extract all versions xdir = mkdtemp() versiondir = os.path.join(xdir, os.path.basename(apath)) a.extract(xdir) assert (os.path.exists(os.path.join(versiondir, '0', 'foobar', 'foo'))) assert_equal( open(foo, 'rb').read(), open(os.path.join(versiondir, '0', 'foobar', 'foo'), 'rb').read()) assert_equal( 978307200.0, os.path.getmtime(os.path.join(versiondir, '0', 'foobar', 'foo'))) assert_equal( 0o644, os.stat(os.path.join(versiondir, '0', 'foobar', 'foo')).st_mode & 0o000777) assert (os.path.exists(os.path.join(versiondir, '1', 'foobar', 'bar'))) assert_equal( 978307200.0, os.path.getmtime(os.path.join(versiondir, '1', 'foobar', 'bar'))) assert_equal( open(bar, 'rb').read(), open(os.path.join(versiondir, '1', 'foobar', 'bar'), 'rb').read()) assert_equal( 0o7755, os.stat(os.path.join(versiondir, '1', 'foobar', 'bar')).st_mode & 0o007777) #check symlink assert (os.path.exists( os.path.join(versiondir, '2', 'foobar', 'symlink'))) assert_equal( 'foo', os.readlink(os.path.join(versiondir, '2', 'foobar', 'symlink'))) shutil.rmtree(xdir)
def test_cli_addversion(self): a = mkstemppath() os.chdir('foobar') sparchive.cli.main(['addversion', a, 'foo', 'bar']) CliTest.assert_ziprz_filenames(a, ['versions/0/foo', 'versions/0/bar'])
def __enter__(self): self.uncomp_path = mkstemppath() if (os.path.exists(self.xz_path)): uncompress(self.xz_path, self.uncomp_path) return self.uncomp_path