本文整理汇总了Python中tarfile.TarFile.add方法的典型用法代码示例。如果您正苦于以下问题:Python TarFile.add方法的具体用法?Python TarFile.add怎么用?Python TarFile.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tarfile.TarFile
的用法示例。
在下文中一共展示了TarFile.add方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: create_archive
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def create_archive(self):
(handle, path) = mkstemp(dir=self.temp_dir)
os.close(handle)
archive = TarFile(path, mode="w")
archive.add(os.path.join(_common.RSRC, "full.mp3"), "full.mp3")
archive.close()
return path
示例2: _createScriptExtensionTarArchive
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def _createScriptExtensionTarArchive(self, sourceDirectory, scriptExtensionName):
""" Creates a TAR archive for the given script extension. """
tarFileName = scriptExtensionName + ".tar"
tarFilePath = os.path.join(self.__buildConfiguration.distDirectory, tarFileName)
tarFile = TarFile(tarFilePath, "w")
for inputDirectory in ["lib", "src"]:
baseDirectory = os.path.join(sourceDirectory, inputDirectory)
if os.path.exists(baseDirectory):
for packageDirName in os.listdir(baseDirectory):
pythonModulesToAddList = list()
packageDirectory = os.path.join(baseDirectory, packageDirName)
if os.path.exists(packageDirectory):
for walkTuple in os.walk(packageDirectory):
directoryPath = walkTuple[0]
fileNameList = walkTuple[2]
for fileName in fileNameList:
if fileName.endswith(".py") or fileName == "SCRIPTS":
filePath = os.path.join(directoryPath, fileName)
pythonModulesToAddList.append(filePath)
for pythonModule in pythonModulesToAddList:
startPosition = pythonModule.find(baseDirectory) + len(baseDirectory) + 1
archiveName = pythonModule[startPosition:]
tarFile.add(pythonModule, archiveName)
tarFile.close()
if self.verbose:
print("Created tar archive '%s'." % tarFilePath)
示例3: reader
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def reader(self):
"""Package up filesystem contents as a tarball."""
result = BytesIO()
tarball = TarFile(fileobj=result, mode="w")
for child in self.path.children():
tarball.add(child.path, arcname=child.basename(), recursive=True)
tarball.close()
result.seek(0, 0)
yield result
示例4: download
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def download(self):
"""
Ein Download wird ausgeführt
"""
self.init2() # Basisklasse einrichten
simulation = self.request.POST.get("simulation", False)
self._setup_path()
if simulation:
self.request.echo("<h1>Download Simulation!</h1><pre>")
self.request.echo("request path: %s\n" % self.request_path)
log_typ = "download simulation start"
else:
log_typ = "download start"
self.db.log(log_typ, self.context['request_path'])
artist = self.request.POST.get("artist", "")
album = self.request.POST.get("album", "")
files, _ = self._read_dir()
args = {"prefix": "PyDown_%s_" % self.request.environ["REMOTE_USER"]}
if self.request.cfg["temp"]:
args["dir"] = self.request.cfg["temp"]
temp = NamedTemporaryFile(**args)
tar = TarFile(mode="w", fileobj=temp)
if simulation:
self.request.write("-"*80)
self.request.write("\n")
for file_info in files:
filename = file_info[0]
abs_path = posixpath.join(self.request_path, filename)
arcname = posixpath.join(artist, album, filename)
if simulation:
#~ self.request.write("absolute path..: %s\n" % abs_path)
self.request.write("<strong>%s</strong>\n" % arcname)
try:
tar.add(abs_path, arcname)
except IOError, e:
self.request.write("<h1>Error</h1><h2>Can't create archive: %s</h2>" % e)
try:
tar.close()
except:
pass
try:
temp.close()
except:
pass
return
示例5: replace_or_append_file_to_layer
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def replace_or_append_file_to_layer(file_to_replace: str,
content_or_path: bytes,
img: tarfile.TarFile):
# Is content or path?
if not os.path.exists(content_or_path):
# Is a content
t = tarfile.TarInfo(file_to_replace)
t.size = len(content_or_path)
img.addfile(t, io.BytesIO(content_or_path))
else:
# Is a path
img.add(content_or_path, file_to_replace)
示例6: run
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def run(self, args, argv):
# Create a temporary tarball with our whole build context and
# dockerfile for the update
tmp = tempfile.NamedTemporaryFile(suffix="dckr.tar.gz")
tmp_tar = TarFile(fileobj=tmp, mode='w')
# Add the executable to the tarball, using the current
# configured binfmt_misc path. If we don't get a path then we
# only need the support libraries copied
ff, enabled = _check_binfmt_misc(args.executable)
if not enabled:
print("binfmt_misc not enabled, update disabled")
return 1
if ff:
tmp_tar.add(args.executable, arcname=ff)
# Add any associated libraries
libs = _get_so_libs(args.executable)
if libs:
for l in libs:
tmp_tar.add(os.path.realpath(l), arcname=l)
# Create a Docker buildfile
df = StringIO()
df.write("FROM %s\n" % args.tag)
df.write("ADD . /\n")
df.seek(0)
df_tar = TarInfo(name="Dockerfile")
df_tar.size = len(df.buf)
tmp_tar.addfile(df_tar, fileobj=df)
tmp_tar.close()
# reset the file pointers
tmp.flush()
tmp.seek(0)
# Run the build with our tarball context
dkr = Docker()
dkr.update_image(args.tag, tmp, quiet=args.quiet)
return 0
示例7: run
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def run(self, args, argv):
# Create a temporary tarball with our whole build context and
# dockerfile for the update
tmp = tempfile.NamedTemporaryFile(suffix="dckr.tar.gz")
tmp_tar = TarFile(fileobj=tmp, mode='w')
# Add the executable to the tarball
bn = os.path.basename(args.executable)
ff = "/usr/bin/%s" % bn
tmp_tar.add(args.executable, arcname=ff)
# Add any associated libraries
libs = _get_so_libs(args.executable)
if libs:
for l in libs:
tmp_tar.add(os.path.realpath(l), arcname=l)
# Create a Docker buildfile
df = StringIO()
df.write("FROM %s\n" % args.tag)
df.write("ADD . /\n")
df.seek(0)
df_tar = TarInfo(name="Dockerfile")
df_tar.size = len(df.buf)
tmp_tar.addfile(df_tar, fileobj=df)
tmp_tar.close()
# reset the file pointers
tmp.flush()
tmp.seek(0)
# Run the build with our tarball context
dkr = Docker()
dkr.update_image(args.tag, tmp, quiet=args.quiet)
return 0
示例8: reader
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def reader(self, remote_snapshots=None):
"""
Package up filesystem contents as a tarball.
"""
result = BytesIO()
tarball = TarFile(fileobj=result, mode="w")
for child in self.path.children():
tarball.add(child.path, arcname=child.basename(), recursive=True)
tarball.close()
# You can append anything to the end of a tar stream without corrupting
# it. Smuggle some data about the snapshots through here. This lets
# tests verify that an incremental stream is really being produced
# without forcing us to implement actual incremental streams on top of
# dumb directories.
if remote_snapshots:
result.write(
u"\nincremental stream based on\n{}".format(
u"\n".join(snapshot.name for snapshot in remote_snapshots)
).encode("ascii")
)
result.seek(0, 0)
yield result
示例9: test_can_put_extracted_file_from_tar
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def test_can_put_extracted_file_from_tar(self):
tempdir = self.make_tempdir()
tarname = os.path.join(tempdir, "mytar.tar")
filename = os.path.join(tempdir, "foo")
# Set up a file to add the tarfile.
with open(filename, "w") as f:
f.write("bar")
# Setup the tar file by adding the file to it.
# Note there is no context handler for TarFile in python 2.6
try:
tar = TarFile(tarname, "w")
tar.add(filename, "foo")
finally:
tar.close()
# See if an extracted file can be uploaded to s3.
try:
tar = TarFile(tarname, "r")
with closing(tar.extractfile("foo")) as f:
self.assert_can_put_object(body=f)
finally:
tar.close()
示例10: build_tar
# 需要导入模块: from tarfile import TarFile [as 别名]
# 或者: from tarfile.TarFile import add [as 别名]
def build_tar(fileobj, mode="w"):
tarfile = TarFile(mode=mode, fileobj=fileobj)
tarfile.add(schema_for("data/address.json"))
tarfile.add(schema_for("data/name.json"))
tarfile.add(schema_for("data/record.json"))