def testBSDUTF(self): fileobj = ClosesSaveIOBytes() afw = arfile.ArFileWriter(fileobj) ai = arfile.ArInfo(arfile.AR_FORMAT_BSD, u'\u2603', 4, 1234, 1001, 1001, 0100644) afw.addfile(ai, io.BytesIO(u'\U0001f4a9'.encode('utf-8'))) afw.close() self.assertMultiLineEqual(AR_TEST_BSD_UTF, fileobj.getvalue())
def testBSD1(self): fileobj = ClosesSaveIOBytes() afw = arfile.ArFileWriter(fileobj) ai = arfile.ArInfo(arfile.AR_FORMAT_BSD, 'filename1', 6, 1234, 1001, 1001, 0100644) afw.addfile(ai, io.BytesIO('abc123')) afw.close() self.assertMultiLineEqual(AR_TEST_BSD1, fileobj.getvalue())
def testSimple1(self): fileobj = ClosesSaveIOBytes() afw = arfile.ArFileWriter(fileobj) ai = arfile.ArInfo(arfile.AR_FORMAT_SIMPLE, 'filename1', 6, 123, 1000, 1000, 0100640) afw.addfile(ai, io.BytesIO('abc123')) afw.close() self.assertMultiLineEqual(AR_TEST_SIMPLE1, fileobj.getvalue())
def assertWorking(self, *initems): tf = tempfile.NamedTemporaryFile(mode='wb') afw = arfile.ArFileWriter(tf) files = [] for ai, data in initems: files.append(ai.name) assert ai.size == len(data) afw.addfile(ai, io.BytesIO(data)) afw.flush() output = subprocess.check_output(['ar', 't', tf.name]) self.assertMultiLineEqual('\n'.join(files), output.decode('utf-8').strip()) tf.close()
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 testBSD2(self): fileobj = ClosesSaveIOBytes() afw = arfile.ArFileWriter(fileobj) afw.addfile( arfile.ArInfo.fromdefault( 'file1', 8, arformat=arfile.AR_FORMAT_BSD), io.BytesIO('contents')) afw.addfile( arfile.ArInfo.fromdefault( 'fileabc', 3, arformat=arfile.AR_FORMAT_BSD), io.BytesIO('123')) afw.addfile( arfile.ArInfo.fromdefault( 'dir1/file1', 6, arformat=arfile.AR_FORMAT_BSD), io.BytesIO('123abc')) afw.close() self.assertMultiLineEqual(AR_TEST_BSD2, fileobj.getvalue())
def create_cmd(filename, dirs, progress, read_ahead, verbose, dont_use_defaults): afw = arfile.ArFileWriter(filename) try: for path in dirs: for dirpath, child_dirs, filenames in os.walk(path): # In-place sort the child_dirs so we walk in lexicographical order child_dirs.sort() filenames.sort() for fn in filenames: fp = os.path.join(dirpath, fn) if verbose: print(fp, file=sys.stderr) progress.inc() with open(fp, 'rb') as f: if dont_use_defaults: afw.addfile( arfile.ArInfo.frompath(fp[len(path) + 1:], cwd=path), f) continue # If a file is small, it is cheaper to just read the file rather # than doing a stat data = f.read(read_ahead) if len(data) < read_ahead: afw.addfile( arfile.ArInfo.fromdefault( fp[len(path) + 1:], len(data)), io.BytesIO(data)) else: size = os.stat(fp).st_size f.seek(0) afw.addfile( arfile.ArInfo.fromdefault( fp[len(path) + 1:], size), f) finally: afw.close()