def testBSD2(self): fileobj = io.BytesIO(AR_TEST_BSD2) afri = iter(arfile.ArFileReader(fileobj)) ai, af = afri.next() self.assertIs(arfile.AR_FORMAT_BSD, ai.format) self.assertEqual('file1', ai.name) self.assertEqual(8, ai.size) self.assertEqual(1447140471, ai.mtime) self.assertEqual(1000, ai.uid) self.assertEqual(1000, ai.gid) self.assertEqual('0100640', oct(ai.mode)) self.assertEqual('contents', af.read(ai.size)) ai, af = afri.next() self.assertIs(arfile.AR_FORMAT_BSD, ai.format) self.assertEqual('fileabc', ai.name) self.assertEqual(3, ai.size) self.assertEqual(1447140471, ai.mtime) self.assertEqual(1000, ai.uid) self.assertEqual(1000, ai.gid) self.assertEqual('0100640', oct(ai.mode)) self.assertEqual('123', af.read(ai.size)) ai, af = afri.next() self.assertIs(arfile.AR_FORMAT_BSD, ai.format) self.assertEqual('dir1/file1', ai.name) self.assertEqual(6, ai.size) self.assertEqual(1447140471, ai.mtime) self.assertEqual(1000, ai.uid) self.assertEqual(1000, ai.gid) self.assertEqual('0100640', oct(ai.mode)) self.assertEqual('123abc', af.read(ai.size))
def testBSDUTF(self): fileobj = io.BytesIO(AR_TEST_BSD_UTF) afri = iter(arfile.ArFileReader(fileobj)) ai, af = afri.next() self.assertIs(arfile.AR_FORMAT_BSD, ai.format) self.assertEqual(u'\u2603', ai.name) self.assertEqual(4, ai.size) self.assertEqual(1234, ai.mtime) self.assertEqual(1001, ai.uid) self.assertEqual(1001, ai.gid) self.assertEqual('0100644', oct(ai.mode)) self.assertEqual(u'\U0001f4a9', af.read(ai.size).decode('utf-8'))
def testBSD1(self): fileobj = io.BytesIO(AR_TEST_BSD1) afri = iter(arfile.ArFileReader(fileobj)) ai, af = afri.next() self.assertIs(arfile.AR_FORMAT_BSD, ai.format) self.assertEqual('filename1', ai.name) self.assertEqual(6, ai.size) self.assertEqual(1234, ai.mtime) self.assertEqual(1001, ai.uid) self.assertEqual(1001, ai.gid) self.assertEqual('0100644', oct(ai.mode)) self.assertEqual('abc123', af.read(ai.size))
def assertWorking(self, *initems): outfile = ClosesSaveIOBytes() afw = arfile.ArFileWriter(outfile) for ai, data in initems: assert ai.size == len(data) afw.addfile(ai, io.BytesIO(data)) afw.close() infile = io.BytesIO(outfile.getvalue()) afr = arfile.ArFileReader(infile) outitems = [] for ai, fd in afr: data = fd.read(ai.size) outitems.append((ai, data)) self.assertSequenceEqual(initems, outitems)
def extract_cmd(filename, progress, verbose, dont_use_defaults, blocksize=1024 * 64): afr = arfile.ArFileReader(filename, fullparse=dont_use_defaults) for ai, ifd in afr: assert not ai.name.startswith('/') if verbose: print(ai.name, file=sys.stderr) try: os.makedirs(os.path.dirname(ai.name)) except OSError: pass with open(ai.name, 'wb') as ofd: written = 0 while written < ai.size: readsize = min(blocksize, ai.size - written) ofd.write(ifd.read(readsize)) written += readsize progress.inc()
def list_cmd(filename, progress): afr = arfile.ArFileReader(filename, fullparse=False) for ai, _ in afr: print(ai.name) progress.inc()