Python tarfile.TarInfo() Examples
The following are 30
code examples of tarfile.TarInfo().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tarfile
, or try the search function
.
Example #1
Source File: test_tarfile.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_premature_end_of_archive(self): for size in (512, 600, 1024, 1200): with tarfile.open(tmpname, "w:") as tar: t = tarfile.TarInfo("foo") t.size = 1024 tar.addfile(t, io.BytesIO(b"a" * 1024)) with open(tmpname, "r+b") as fobj: fobj.truncate(size) with tarfile.open(tmpname) as tar: with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"): for t in tar: pass with tarfile.open(tmpname) as tar: t = tar.next() with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"): tar.extract(t, TEMPDIR) with self.assertRaisesRegex(tarfile.ReadError, "unexpected end of data"): tar.extractfile(t).read()
Example #2
Source File: test_tarfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def _test(self, name, link=None): # See GNUWriteTest. tarinfo = tarfile.TarInfo(name) if link: tarinfo.linkname = link tarinfo.type = tarfile.LNKTYPE tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) try: tar.addfile(tarinfo) finally: tar.close() tar = tarfile.open(tmpname) try: if link: l = tar.getmembers()[0].linkname self.assertTrue(link == l, "PAX longlink creation failed") else: n = tar.getmembers()[0].name self.assertTrue(name == n, "PAX longname creation failed") finally: tar.close()
Example #3
Source File: test_tarfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_100_char_name(self): # The name field in a tar header stores strings of at most 100 chars. # If a string is shorter than 100 chars it has to be padded with '\0', # which implies that a string of exactly 100 chars is stored without # a trailing '\0'. name = "0123456789" * 10 tar = tarfile.open(tmpname, self.mode) try: t = tarfile.TarInfo(name) tar.addfile(t) finally: tar.close() tar = tarfile.open(tmpname) try: self.assertTrue(tar.getnames()[0] == name, "failed to store 100 char filename") finally: tar.close()
Example #4
Source File: test_tarfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_premature_end_of_archive(self): for size in (512, 600, 1024, 1200): with tarfile.open(tmpname, "w:") as tar: t = tarfile.TarInfo("foo") t.size = 1024 tar.addfile(t, StringIO.StringIO("a" * 1024)) with open(tmpname, "r+b") as fobj: fobj.truncate(size) with tarfile.open(tmpname) as tar: with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"): for t in tar: pass with tarfile.open(tmpname) as tar: t = tar.next() with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"): tar.extract(t, TEMPDIR) with self.assertRaisesRegexp(tarfile.ReadError, "unexpected end of data"): tar.extractfile(t).read()
Example #5
Source File: sdist.py From poetry with MIT License | 6 votes |
def clean_tarinfo(cls, tar_info): """ Clean metadata from a TarInfo object to make it more reproducible. - Set uid & gid to 0 - Set uname and gname to "" - Normalise permissions to 644 or 755 - Set mtime if not None """ ti = copy(tar_info) ti.uid = 0 ti.gid = 0 ti.uname = "" ti.gname = "" ti.mode = normalize_file_permissions(ti.mode) return ti
Example #6
Source File: test_easy_install.py From pledgeservice with Apache License 2.0 | 6 votes |
def make_trivial_sdist(dist_path, setup_py): """Create a simple sdist tarball at dist_path, containing just a setup.py, the contents of which are provided by the setup_py string. """ setup_py_file = tarfile.TarInfo(name='setup.py') try: # Python 3 (StringIO gets converted to io module) MemFile = BytesIO except AttributeError: MemFile = StringIO setup_py_bytes = MemFile(setup_py.encode('utf-8')) setup_py_file.size = len(setup_py_bytes.getvalue()) dist = tarfile.open(dist_path, 'w:gz') try: dist.addfile(setup_py_file, fileobj=setup_py_bytes) finally: dist.close()
Example #7
Source File: archive.py From king-phisher with BSD 3-Clause "New" or "Revised" License | 6 votes |
def add_data(self, name, data): """ Add arbitrary data directly to the archive under the specified name. This allows data to be directly inserted into the archive without first writing it to a file or file like object. :param str name: The name of the destination file in the archive. :param data: The data to place into the archive. :type data: bytes, str """ if its.py_v2 and isinstance(data, unicode): data = data.encode(self.encoding) elif its.py_v3 and isinstance(data, str): data = data.encode(self.encoding) pseudo_file = io.BytesIO() pseudo_file.write(data) tarinfo = tarfile.TarInfo(name=name) tarinfo.mtime = self.mtime tarinfo.size = pseudo_file.tell() pseudo_file.seek(os.SEEK_SET) self._tar_h.addfile(tarinfo=tarinfo, fileobj=pseudo_file)
Example #8
Source File: conanfile.py From conan-center-index with MIT License | 6 votes |
def test(self): with tarfile.open("test.tar", "w") as f: import io bio = io.BytesIO() bio.write(b"secret text\n") tarinfo = tarfile.TarInfo("hello_world") tarinfo.size = bio.tell() import time tarinfo.mtime = time.time() bio.seek(0) f.addfile(tarinfo, bio) if not tools.cross_building(self.settings): if os.path.exists("hello_world"): raise ConanException("file extracted by tar archive should not exist yet") bin_path = os.path.join("bin", "test_package") self.run("{} {}".format(bin_path, "test.tar"), run_environment=True) if not os.path.exists("hello_world"): raise ConanException("file not extracted") extracted_text = tools.load("hello_world") if extracted_text != "secret text\n": raise ConanException("File not loaded correctly. Got \"{}\"".format(repr(extracted_text))) self.run("libtar -t test.tar", run_environment=True)
Example #9
Source File: test_tarfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_pax_extended_header(self): # The fields from the pax header have priority over the # TarInfo. pax_headers = {u"path": u"foo", u"uid": u"123"} tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1") try: t = tarfile.TarInfo() t.name = u"\xe4\xf6\xfc" # non-ASCII t.uid = 8**8 # too large t.pax_headers = pax_headers tar.addfile(t) finally: tar.close() tar = tarfile.open(tmpname, encoding="iso8859-1") try: t = tar.getmembers()[0] self.assertEqual(t.pax_headers, pax_headers) self.assertEqual(t.name, "foo") self.assertEqual(t.uid, 123) finally: tar.close()
Example #10
Source File: test_tarfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_unicode_filename_error(self): tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict") try: tarinfo = tarfile.TarInfo() tarinfo.name = "\xe4\xf6\xfc" if self.format == tarfile.PAX_FORMAT: self.assertRaises(UnicodeError, tar.addfile, tarinfo) else: tar.addfile(tarinfo) tarinfo.name = u"\xe4\xf6\xfc" self.assertRaises(UnicodeError, tar.addfile, tarinfo) tarinfo.name = "foo" tarinfo.uname = u"\xe4\xf6\xfc" self.assertRaises(UnicodeError, tar.addfile, tarinfo) finally: tar.close()
Example #11
Source File: test_tarfile.py From ironpython2 with Apache License 2.0 | 6 votes |
def _test_partial_input(self, mode): class MyStringIO(StringIO.StringIO): hit_eof = False def read(self, n): if self.hit_eof: raise AssertionError("infinite loop detected in tarfile.open()") self.hit_eof = self.pos == self.len return StringIO.StringIO.read(self, n) def seek(self, *args): self.hit_eof = False return StringIO.StringIO.seek(self, *args) data = bz2.compress(tarfile.TarInfo("foo").tobuf()) for x in range(len(data) + 1): try: tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode) except tarfile.ReadError: pass # we have no interest in ReadErrors
Example #12
Source File: docker_build.py From biweeklybudget with GNU Affero General Public License v3.0 | 6 votes |
def _tar_add_string_file(self, tarobj, fpath, content): """ Given a tarfile object, add a file to it at ``fpath``, with content ``content``. Largely based on: http://stackoverflow.com/a/40392022 :param tarobj: the tarfile to add to :type tarobj: tarfile.TarFile :param fpath: path to put the file at in the archive :type fpath: str :param content: file content :type content: str """ logger.debug('Adding %d-length string to tarfile at %s', len(content), fpath) data = content.encode('utf-8') f = BytesIO(data) info = tarfile.TarInfo(name=fpath) info.size = len(data) tarobj.addfile(tarinfo=info, fileobj=f)
Example #13
Source File: test_tarfile.py From BinderFilter with MIT License | 6 votes |
def test_ignore_zeros(self): # Test TarFile's ignore_zeros option. if self.mode.endswith(":gz"): _open = gzip.GzipFile elif self.mode.endswith(":bz2"): _open = bz2.BZ2File else: _open = open for char in ('\0', 'a'): # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') # are ignored correctly. fobj = _open(tmpname, "wb") fobj.write(char * 1024) fobj.write(tarfile.TarInfo("foo").tobuf()) fobj.close() tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) self.assertListEqual(tar.getnames(), ["foo"], "ignore_zeros=True should have skipped the %r-blocks" % char) tar.close()
Example #14
Source File: test_tarfile.py From BinderFilter with MIT License | 6 votes |
def _test(self, name, link=None): tarinfo = tarfile.TarInfo(name) if link: tarinfo.linkname = link tarinfo.type = tarfile.LNKTYPE tar = tarfile.open(tmpname, "w") tar.format = tarfile.GNU_FORMAT tar.addfile(tarinfo) v1 = self._calc_size(name, link) v2 = tar.offset self.assertTrue(v1 == v2, "GNU longname/longlink creation failed") tar.close() tar = tarfile.open(tmpname) member = tar.next() self.assertIsNotNone(member, "unable to read longname member") self.assertEqual(tarinfo.name, member.name, "unable to read longname member") self.assertEqual(tarinfo.linkname, member.linkname, "unable to read longname member")
Example #15
Source File: test_tarfile.py From BinderFilter with MIT License | 6 votes |
def test_unicode_filename_error(self): tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict") tarinfo = tarfile.TarInfo() tarinfo.name = "���" if self.format == tarfile.PAX_FORMAT: self.assertRaises(UnicodeError, tar.addfile, tarinfo) else: tar.addfile(tarinfo) tarinfo.name = u"���" self.assertRaises(UnicodeError, tar.addfile, tarinfo) tarinfo.name = "foo" tarinfo.uname = u"���" self.assertRaises(UnicodeError, tar.addfile, tarinfo)
Example #16
Source File: test_tarfile.py From BinderFilter with MIT License | 6 votes |
def test_uname_unicode(self): for name in (u"���", "���"): t = tarfile.TarInfo("foo") t.uname = name t.gname = name fobj = StringIO.StringIO() tar = tarfile.open("foo.tar", mode="w", fileobj=fobj, format=self.format, encoding="iso8859-1") tar.addfile(t) tar.close() fobj.seek(0) tar = tarfile.open("foo.tar", fileobj=fobj, encoding="iso8859-1") t = tar.getmember("foo") self.assertEqual(t.uname, "���") self.assertEqual(t.gname, "���")
Example #17
Source File: test_tarfile.py From BinderFilter with MIT License | 6 votes |
def _test_partial_input(self, mode): class MyStringIO(StringIO.StringIO): hit_eof = False def read(self, n): if self.hit_eof: raise AssertionError("infinite loop detected in tarfile.open()") self.hit_eof = self.pos == self.len return StringIO.StringIO.read(self, n) def seek(self, *args): self.hit_eof = False return StringIO.StringIO.seek(self, *args) data = bz2.compress(tarfile.TarInfo("foo").tobuf()) for x in range(len(data) + 1): try: tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode) except tarfile.ReadError: pass # we have no interest in ReadErrors
Example #18
Source File: test_easy_install.py From oss-ftp with MIT License | 6 votes |
def make_trivial_sdist(dist_path, setup_py): """Create a simple sdist tarball at dist_path, containing just a setup.py, the contents of which are provided by the setup_py string. """ setup_py_file = tarfile.TarInfo(name='setup.py') try: # Python 3 (StringIO gets converted to io module) MemFile = BytesIO except AttributeError: MemFile = StringIO setup_py_bytes = MemFile(setup_py.encode('utf-8')) setup_py_file.size = len(setup_py_bytes.getvalue()) dist = tarfile.open(dist_path, 'w:gz') try: dist.addfile(setup_py_file, fileobj=setup_py_bytes) finally: dist.close()
Example #19
Source File: test_tarfile.py From oss-ftp with MIT License | 6 votes |
def test_ignore_zeros(self): # Test TarFile's ignore_zeros option. if self.mode.endswith(":gz"): _open = gzip.GzipFile elif self.mode.endswith(":bz2"): _open = bz2.BZ2File else: _open = open for char in ('\0', 'a'): # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') # are ignored correctly. fobj = _open(tmpname, "wb") fobj.write(char * 1024) fobj.write(tarfile.TarInfo("foo").tobuf()) fobj.close() tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) self.assertListEqual(tar.getnames(), ["foo"], "ignore_zeros=True should have skipped the %r-blocks" % char) tar.close()
Example #20
Source File: test_tarfile.py From oss-ftp with MIT License | 6 votes |
def _test(self, name, link=None): # See GNUWriteTest. tarinfo = tarfile.TarInfo(name) if link: tarinfo.linkname = link tarinfo.type = tarfile.LNKTYPE tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT) tar.addfile(tarinfo) tar.close() tar = tarfile.open(tmpname) if link: l = tar.getmembers()[0].linkname self.assertTrue(link == l, "PAX longlink creation failed") else: n = tar.getmembers()[0].name self.assertTrue(name == n, "PAX longname creation failed")
Example #21
Source File: test_tarfile.py From oss-ftp with MIT License | 6 votes |
def test_pax_extended_header(self): # The fields from the pax header have priority over the # TarInfo. pax_headers = {u"path": u"foo", u"uid": u"123"} tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1") t = tarfile.TarInfo() t.name = u"���" # non-ASCII t.uid = 8**8 # too large t.pax_headers = pax_headers tar.addfile(t) tar.close() tar = tarfile.open(tmpname, encoding="iso8859-1") t = tar.getmembers()[0] self.assertEqual(t.pax_headers, pax_headers) self.assertEqual(t.name, "foo") self.assertEqual(t.uid, 123)
Example #22
Source File: test_tarfile.py From oss-ftp with MIT License | 6 votes |
def test_unicode_filename_error(self): tar = tarfile.open(tmpname, "w", format=self.format, encoding="ascii", errors="strict") tarinfo = tarfile.TarInfo() tarinfo.name = "���" if self.format == tarfile.PAX_FORMAT: self.assertRaises(UnicodeError, tar.addfile, tarinfo) else: tar.addfile(tarinfo) tarinfo.name = u"���" self.assertRaises(UnicodeError, tar.addfile, tarinfo) tarinfo.name = "foo" tarinfo.uname = u"���" self.assertRaises(UnicodeError, tar.addfile, tarinfo)
Example #23
Source File: test_tarfile.py From oss-ftp with MIT License | 6 votes |
def _test_partial_input(self, mode): class MyStringIO(StringIO.StringIO): hit_eof = False def read(self, n): if self.hit_eof: raise AssertionError("infinite loop detected in tarfile.open()") self.hit_eof = self.pos == self.len return StringIO.StringIO.read(self, n) def seek(self, *args): self.hit_eof = False return StringIO.StringIO.seek(self, *args) data = bz2.compress(tarfile.TarInfo("foo").tobuf()) for x in range(len(data) + 1): try: tarfile.open(fileobj=MyStringIO(data[:x]), mode=mode) except tarfile.ReadError: pass # we have no interest in ReadErrors
Example #24
Source File: backend.py From netjsonconfig with GNU General Public License v3.0 | 6 votes |
def _add_file(self, tar, name, contents, mode=DEFAULT_FILE_MODE): """ Adds a single file in tarfile instance. :param tar: tarfile instance :param name: string representing filename or path :param contents: string representing file contents :param mode: string representing file mode, defaults to 644 :returns: None """ byte_contents = BytesIO(contents.encode('utf8')) info = tarfile.TarInfo(name=name) info.size = len(contents) # mtime must be 0 or any checksum operation # will return a different digest even when content is the same info.mtime = 0 info.type = tarfile.REGTYPE info.mode = int(mode, 8) # permissions converted to decimal notation tar.addfile(tarinfo=info, fileobj=byte_contents)
Example #25
Source File: build.py From deepWordBug with Apache License 2.0 | 6 votes |
def mkbuildcontext(dockerfile): f = tempfile.NamedTemporaryFile() t = tarfile.open(mode='w', fileobj=f) if isinstance(dockerfile, io.StringIO): dfinfo = tarfile.TarInfo('Dockerfile') if six.PY3: raise TypeError('Please use io.BytesIO to create in-memory ' 'Dockerfiles with Python 3') else: dfinfo.size = len(dockerfile.getvalue()) dockerfile.seek(0) elif isinstance(dockerfile, io.BytesIO): dfinfo = tarfile.TarInfo('Dockerfile') dfinfo.size = len(dockerfile.getvalue()) dockerfile.seek(0) else: dfinfo = t.gettarinfo(fileobj=dockerfile, arcname='Dockerfile') t.addfile(dfinfo, dockerfile) t.close() f.seek(0) return f
Example #26
Source File: views.py From civet with Apache License 2.0 | 6 votes |
def get_job_results(request, job_id): """ Just download all the output of the job into a tarball. """ job = get_object_or_404(models.Job.objects.select_related('recipe',).prefetch_related('step_results'), pk=job_id) perms = Permissions.job_permissions(request.session, job) if not perms['can_see_results']: return HttpResponseForbidden('Not allowed to see results') response = HttpResponse(content_type='application/x-gzip') base_name = 'results_{}_{}'.format(job.pk, get_valid_filename(job.recipe.name)) response['Content-Disposition'] = 'attachment; filename="{}.tar.gz"'.format(base_name) tar = tarfile.open(fileobj=response, mode='w:gz') for result in job.step_results.all(): info = tarfile.TarInfo(name='{}/{:02}_{}'.format(base_name, result.position, get_valid_filename(result.name))) s = BytesIO(result.plain_output().replace('\u2018', "'").replace("\u2019", "'").encode("utf-8", "replace")) buf = s.getvalue() info.size = len(buf) info.mtime = time.time() tar.addfile(tarinfo=info, fileobj=s) tar.close() return response
Example #27
Source File: test_tarfile.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_ignore_zeros(self): # Test TarFile's ignore_zeros option. for char in (b'\0', b'a'): # Test if EOFHeaderError ('\0') and InvalidHeaderError ('a') # are ignored correctly. with self.open(tmpname, "w") as fobj: fobj.write(char * 1024) fobj.write(tarfile.TarInfo("foo").tobuf()) tar = tarfile.open(tmpname, mode="r", ignore_zeros=True) try: self.assertListEqual(tar.getnames(), ["foo"], "ignore_zeros=True should have skipped the %r-blocks" % char) finally: tar.close()
Example #28
Source File: test_tarfile.py From BinderFilter with MIT License | 6 votes |
def test_pax_extended_header(self): # The fields from the pax header have priority over the # TarInfo. pax_headers = {u"path": u"foo", u"uid": u"123"} tar = tarfile.open(tmpname, "w", format=tarfile.PAX_FORMAT, encoding="iso8859-1") t = tarfile.TarInfo() t.name = u"���" # non-ASCII t.uid = 8**8 # too large t.pax_headers = pax_headers tar.addfile(t) tar.close() tar = tarfile.open(tmpname, encoding="iso8859-1") t = tar.getmembers()[0] self.assertEqual(t.pax_headers, pax_headers) self.assertEqual(t.name, "foo") self.assertEqual(t.uid, 123)
Example #29
Source File: test_tarfile.py From oss-ftp with MIT License | 5 votes |
def _add_testfile(self, fileobj=None): tar = tarfile.open(self.tarname, "a", fileobj=fileobj) tar.addfile(tarfile.TarInfo("bar")) tar.close()
Example #30
Source File: test_tarfile.py From oss-ftp with MIT License | 5 votes |
def test_premature_eof(self): data = tarfile.TarInfo("foo").tobuf() self._test_error(data)