def archive_model(project):
"""
Use 'pyb -P model_name="<model_name>" archive_model' to create '<model_name>_CURRENTDATE.tar.gz'
in 'build' directory. If model_name == 'deeppavlov_docs', then documentation from build/docs will be archived.
"""
import tarfile, datetime
os.chdir('build')
model_name = project.get_property('model_name')
archive_name = model_name + '_' + datetime.date.today().strftime("%y%m%d")
if model_name == 'deeppavlov_docs':
import shutil
shutil.make_archive(archive_name, 'gztar', 'docs', 'deeppavlov')
os.chdir('..')
return
with tarfile.open(archive_name + '.tar.gz', "w:gz") as archive:
os.chdir(model_name)
for f in os.listdir('.'):
if os.path.isfile(f) and (('h5' in f) or ('json' in f) or ('pkl' in f)or ('dict' in f) or ('threshold' in f)
or ('data' in f) or ('index' in f) or ('meta' in f) or ('checkpoint' in f)):
archive.add(f)
os.chdir('..')
os.chdir('..')
python类make_archive()的实例源码
def send_log():
ldir = options['logsfolder']
dirs = [d for d in os.listdir(ldir) if os.path.isdir(os.path.join(ldir, d))]
dirs = [os.path.join(ldir, d) for d in dirs]
latest_subdir = max(dirs, key=os.path.getmtime)
logfolder = latest_subdir
logfile = os.path.join(ldir, 'compressedlogs')
shutil.make_archive(logfile, 'zip', logfolder)
logfile = logfile + '.zip'
log_content = open(logfile, 'rb').read()
encoded_log = base64.b64encode(bytes(log_content))
data = {'encoded_log': encoded_log, 'sample_hash': options['sample_hash']}
request = urllib2.Request(options['log-server-url'])
request.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(request, json.dumps(data))
if response.getcode() != 200:
print 'Unable to send data'
def _create(self, fixture_file_path, less_verbose=0):
try:
self.write_info('Creating fixture %s' % fixture_file_path, 1+less_verbose)
fixture_file_path = re.sub(r'\.zip$', '', fixture_file_path) # we strip away .zip if given
tmp_dir = tempfile.mkdtemp()
# copy media root
shutil.copytree(self._media_root, join(tmp_dir, 'MEDIA_ROOT'))
# database dump
with open(join(tmp_dir, 'db.sql'), 'w') as fp:
return_code = subprocess.call(['pg_dump', '--clean', '--no-owner', self._database_name], stdout=fp)
if return_code != 0:
raise CommandError('pg_dump failed with exit code {}'.format(return_code))
# creating the fixture archive
archive_name = shutil.make_archive(fixture_file_path, 'zip', root_dir=tmp_dir)
self.write_debug(subprocess.check_output(['unzip', '-l', archive_name]))
except:
self.write_debug('Temporary directory %s kept due to exception.' % tmp_dir)
raise
else:
self.write_info('... fixture created', 1+less_verbose)
shutil.rmtree(tmp_dir)
def archive(self, temp_dir):
"""Archive source directory to temp directory.
Return archive full path.
"""
# We want to achive `/my/sub/{some dir}` under unique temp dir:
# `/tmp/dEdjnr/{name from config}`
#
# So we make archive from base path `/my/sub/{some dir}`,
# root path `/my/sub/` and archive name
# `/tmp/dEdjnr/{name from config}`
from_path = self.settings['path']
base_name = self._prepare_data_path(temp_dir)
echo("... archive directory %s" % from_path)
arch_path = shutil.make_archive(
base_name=base_name,
root_dir=os.path.dirname(from_path),
base_dir=from_path,
# logger=log,
format=self.get_archive_format())
echo("... archived %s" % arch_path)
return arch_path
def save_schema(keyspace_arg=None):
host = get_rpc_address()
save_path = sys.path[0] + '/.snapshots/schemas'
keyspaces = get_keyspaces(host)
if keyspace_arg:
for ks in keyspace_arg:
if ks not in keyspaces:
print('ERROR: Invalid keyspace argument')
exit(1)
print('Saving schema . . .')
print_save_path = write_schema(host, save_path)
print('Saved schema as %s' % print_save_path)
for ks in keyspaces:
print_save_path = write_schema(host, save_path, ks)
print('Saved keyspace schema as %s' % print_save_path)
print('Compressing schema file')
shutil.make_archive(save_path, 'zip', save_path)
print('Saving ring information . . .')
write_ring_info(sys.path[0] + '/.snapshots')
def zipfile_make_archive(dirname, to=None, **kwargs):
"""
Thin layer over shutil.make_archive. Just defaults
the to to the same name as the directory by default.
:param dirname: (str)
The directory name
:param to: (str, default None)
The file path to create the archive in.
:param kwargs:
shutil.make_archive(**kwargs)
:return: (str)
The :param to file path.
"""
if to is None:
to = dirname
return shutil.make_archive(to, "zip", root_dir=dirname, **kwargs)
def ZipAttachments(f_name):
arch_name = "C:\Users\Public\Intel\Logs\\" + f_name + "Attachments"
files = os.listdir(dir_zip)
try:
shutil.make_archive(arch_name, 'zip', dir_zip)
except Exception as e:
pass
for j in range(len(files)):
try:
os.remove(dir_zip + "\\" + files[j])
except Exception as e:
print e
#Function to take screenshot
def get_backup():
experiment = request.args.get('xp')
try:
experiment = to_unicode(experiment)
except:
return wrong_argument("Experiment name should be a non-empty string or unicode instead of '{}'".format(type(experiment)))
if not experiment:
return wrong_argument("xp argument is required")
folder_path = tensorboard_folder.format(experiment)
if not path.isdir(folder_path):
return wrong_argument("Requested experiment '{}' does not exist".format(experiment))
zip_file = shutil.make_archive("/tmp/{}".format(experiment), 'zip', folder_path)
return send_file(zip_file, mimetype='application/zip')
def _create_codebase_snapshot(self):
def ignored_files(path, names): # see https://docs.python.org/2/library/shutil.html#shutil.copytree
if os.path.abspath(path) == self.export_directory or ".git" in path or ".idea" in path:
return names
print "\t", path
include = [".gml", ".py"]
only_files = (name for name in names if os.path.isfile(os.path.join(path, name)))
ignored = [name for name in only_files
if not any(name.endswith(ext) for ext in include)]
return ignored
code_source_directory = self.local_base_path
code_export_directory = os.path.join(self.export_directory, self.code_base_id)
if os.path.exists(code_export_directory):
raise DeploymentError("The export directory exists!")
code_subdir_name = "sca"
src_dir = os.path.join(code_export_directory, code_subdir_name)
shutil.copytree(code_source_directory, src_dir, ignore=ignored_files)
tar = shutil.make_archive(code_export_directory,
format="gztar",
root_dir=self.export_directory,
base_dir=os.path.join(self.code_base_id, code_subdir_name))
if self.cleanup:
shutil.rmtree(code_export_directory)
self._generated_files.add(tar)
def __Step_MakePackage(self, overwrite_existing):
print 'Making %s package...' % (self.PACKAGE_NAME or '<NONE>')
if self.RELEASE_NAME_FREE_FORMAT:
release_name = self.__ParseMacros(self.RELEASE_NAME_FREE_FORMAT).format(*self.VERSION)
else:
release_name = (self.VERSION[3]
and self.__ParseMacros(self.RELEASE_NAME_WITH_BUILD_FMT % self.VERSION)
or self.__ParseMacros(self.RELEASE_NAME_FMT % self.VERSION[:3]))
package_file_name = self.__MakeSrcPath(os.path.join('/', self.ARCHIVE_DEST, release_name))
archive_name = package_file_name + '.zip'
if os.path.exists(archive_name):
if not overwrite_existing:
print 'ERROR: Package for this version already exists: %s' % archive_name
exit(-1)
print '=> package already exists. DELETING.'
os.remove(archive_name)
shutil.make_archive(package_file_name, 'zip', self.__GetAbsReleaseFolder(), 'GameData')
print '=> stored in:', package_file_name
# Fills VERSION given the string or int compinents. The patch and build could be "*".
def zip_built(outdir):
"""Packages the build folder into a zip"""
print("Zipping the built files!")
config_file_dir = os.path.join(cwd, "config.py")
if not os.path.exists(config_file_dir):
sys.exit(
"There dosen't seem to be a configuration file. Have you run the init command?")
else:
sys.path.insert(0, cwd)
try:
from config import website_name
except:
sys.exit(
"Some of the configuration values could not be found! Maybe your config.py is too old. Run 'blended init' to fix.")
# Remove the build folder
build_dir = os.path.join(cwd, outdir)
zip_dir = os.path.join(cwd, website_name.replace(" ", "_") + "-build-" +
str(datetime.now().date()))
if os.path.exists(build_dir):
shutil.make_archive(zip_dir, 'zip', build_dir)
else:
print("The " + outdir +
"/ folder could not be found! Have you run 'blended build' yet?")
def zip(temp_package_path):
"""Create the StreamAlert Lambda deployment package archive.
Zips all dependency files to run the function,
and names this zipfile based on the current date/time,
along with the Lambda function module version.
example filename: stream_alert_1.0.0_20161010_00:11:22.zip
Only package in the `.py` files per AWS's instructions
for creation of lambda functions.
Args:
temp_package_path (str): the temporary file path to store the zip.
Returns:
str: Deployment package full path
"""
LOGGER_CLI.debug('Creating Lambda package: %s', temp_package_path + '.zip')
package_path = shutil.make_archive(temp_package_path, 'zip', temp_package_path)
LOGGER_CLI.info('Package successfully created')
return package_path
def repo(name):
name = os.path.basename(name)
pack_name = os.path.splitext(name)[0]
print("Fetching zip archive for: {}".format(pack_name))
if not os.path.exists(os.path.join(cache_path, name)):
print("Creating zip package for cache")
zip_name = os.path.join(cache_path, pack_name)
dir_name = os.path.join(package_path, pack_name)
if not os.path.exists(dir_name):
print("Such package was not found")
abort(404)
shutil.make_archive(zip_name, 'zip', dir_name)
return send_from_directory(cache_path, name)
def cZip(cDir,tName,zName):
'''
creates a zip file of the temporary directory
'''
os.chdir(cDir)
#zName = 'pipelign.' + time.strftime('%Y-%m-%d-%H%M%S')
try:
shutil.make_archive(zName,'zip',tName)
except OSError as e:
sys.exit(e)
print('\nArchive for all temporary files created in %s.zip\n' % zName)
sys.exit()
#******************************************
def asynchronous():
threads = []
threads.append(gevent.spawn(loader))
for i in xrange(0, workers):
threads.append(gevent.spawn(worker, i))
threads.append(gevent.spawn(writer_valid))
threads.append(gevent.spawn(state))
if invunma:
threads.append(gevent.spawn(writer_invalid))
threads.append(gevent.spawn(writer_unmatched))
if grabactiv:
threads.append(gevent.spawn(writer_grabber))
start = timer()
gevent.joinall(threads)
end = timer()
if grabactiv:
if snap_shot:
output_filename = "grabbed_" + time.strftime("%Y%m%d-%H%M%S")
shutil.make_archive(output_filename, 'zip', "grabbed")
print "[INFO]Time elapsed: " + str(end - start)[:5], "seconds."
print "[INFO] Done."
evt.set() # cleaning up
def download(self, path=None):
"""
Download view method.
:param path:
File path.
"""
if not self.can_download:
abort(404)
logger = logging.getLogger(self.__class__.__name__)
base_path, directory, path = self._normalize_path(path)
# backward compatibility with base_url
base_url = self.get_base_url()
if base_url:
base_url = urljoin(self.get_url('.index'), base_url)
return redirect(urljoin(base_url, path))
if op.isdir(directory):
logger.debug("Directory download asked for: %s" %path)
shutil.make_archive(directory,'zip',directory)
return redirect(self._get_dir_url('.index', op.dirname(path)))
return send_file(directory)
def _build_downloader(target_directory):
"""Build the downloader Lambda deployment package."""
print('Creating downloader deploy package...')
pathlib.Path(DOWNLOAD_SOURCE).touch()
temp_package_dir = os.path.join(tempfile.gettempdir(), 'tmp_yara_downloader.pkg')
if os.path.exists(temp_package_dir):
shutil.rmtree(temp_package_dir)
# Extract cbapi library.
with zipfile.ZipFile(DOWNLOAD_DEPENDENCIES, 'r') as deps:
deps.extractall(temp_package_dir)
# Pip install backoff library (has no native dependencies).
pip.main(['install', '--quiet', '--target', temp_package_dir, 'backoff'])
# Copy Lambda code into the package.
shutil.copy(DOWNLOAD_SOURCE, temp_package_dir)
# Zip up the package and remove temporary directory.
shutil.make_archive(os.path.join(target_directory, DOWNLOAD_ZIPFILE), 'zip', temp_package_dir)
shutil.rmtree(temp_package_dir)
def archive(self):
timestr = time.strftime("%Y%m%d-%H%M%S")
archive_name = "{0}_{1}".format(self.work_dir, timestr)
shutil.make_archive(archive_name, "zip",
str(self.local_root_dir),
str(self.work_dir.name))
def export():
"""
Create a zip file from the item content
"""
item = application.getItemByUUID(request.args(0))
export_dir = tempfile.mkdtemp()
application.exportItem(item.unique_id, export_dir)
tmpdir = tempfile.mkdtemp()
try:
tmparchive = os.path.join(tmpdir, item.slugline)
archive = shutil.make_archive(tmparchive, 'zip', export_dir)
response.stream(
archive,
chunk_size=4096,
request=request,
attachment=True,
filename="{}.zip".format(item.slugline)
)
finally:
shutil.rmtree(tmpdir)
shutil.rmtree(export_dir)
return ''
def create_zipdir(zipname, path, extn='zip'):
"""zip the path and output to same level"""
output_filepath = path + os.sep + zipname
zip_file_path = shutil.make_archive(output_filepath, extn, path)
return zip_file_path
def zipdir(self):
shutil.make_archive("media", 'zip', safe_join(
settings.BASE_DIR, "media"))
def get_source():
"""Download the source of this application."""
# from http://stackoverflow.com/questions/458436/adding-folders-to-a-zip-file-using-python#6511788
directory = tempfile.mkdtemp()
temp_path = os.path.join(directory, APPLICATION)
zip_path = shutil.make_archive(temp_path, "zip", HERE)
return static_file(APPLICATION + ".zip", root=directory)
def execute(self, context):
ps = context.scene.ge_publish_settings
if ps.publish_default_platform:
print("Publishing default platform")
blender_bin_path = bpy.app.binary_path
blender_bin_dir = os.path.dirname(blender_bin_path)
ext = os.path.splitext(blender_bin_path)[-1].lower()
WriteRuntime(os.path.join(blender_bin_dir, 'blenderplayer' + ext),
os.path.join(ps.output_path, 'default', ps.runtime_name),
ps.asset_paths,
True,
True,
True,
ps.make_archive,
self.report
)
else:
print("Skipping default platform")
for platform in ps.platforms:
if platform.publish:
print("Publishing", platform.name)
WriteRuntime(platform.player_path,
os.path.join(ps.output_path, platform.name, ps.runtime_name),
ps.asset_paths,
True,
True,
True,
ps.make_archive,
self.report
)
else:
print("Skipping", platform.name)
return {'FINISHED'}
def move_to_out_folder(project, job, config):
""" Moves the final project into the tmp folder """
build_file = config.get("out")
if os.path.isfile(build_file):
flash("Could not find output file", category="error")
return
build_folder = os.path.join(project.path, os.path.dirname(build_file))
output_folder = os.path.join(project.output, job.name)
shutil.make_archive(output_folder, "zip", build_folder)
remove_tree(build_folder)
return output_folder + ".zip"
def _make_archive(self, dir_name):
"""Make archive of the specified directory near that directory.
"""
return shutil.make_archive(dir_name,
root_dir=dir_name,
base_dir=None,
format=self.get_archive_format())
def zip_folder(folder, name, format='zip'):
print('zip %s into %s compressed file' % (folder, name))
shutil.make_archive(name, format, '.', folder)
print('remove %s folder' % folder)
shutil.rmtree(folder)
def archive(env):
print('Archiving LOVE project \'%s\'...' % env.conf.identifier)
# Archives the project directory into a .love file
if not os.path.exists(env.dist_dir):
os.makedirs(env.dist_dir)
# Creates the .zip file
tmp_dir = tempfile.mkdtemp()
copytree(env.project_dir, tmp_dir, ignore=ignored_files)
shutil.make_archive(env.love_file, 'zip', tmp_dir)
shutil.rmtree(tmp_dir)
# Renames to .love, since shutil.make_archive() adds .zip
os.rename(env.love_file + '.zip', env.love_file)
print('Archival complete!')
def main(src_dir, new_dir, map_d, archer):
""" Call out the necessary functions."""
print "Creating a copy of %s in %s" % (src_dir, new_dir)
shutil.copytree(src_dir, new_dir, symlinks=False, ignore=shutil.ignore_patterns('*.pyc'))
print "Changing the paths according to the mapping table."
for key in map_d.keys():
grep_and_sed(key, new_dir, map_d[key])
fn = shutil.make_archive(new_dir, 'tar', new_dir)
print "%s can now be copied elsewhere and used." %(fn)
def _create_backup(self, version):
logger.debug('Creating backup for {}'.format(version))
shutil.make_archive(self._backup_name(version),
self.backup_format, self.backup_target)
def zip_dir(root_path, save_path, title): # use shutil.make_archive in python2.7
rootlength = len(root_path)
z = zipfile.ZipFile(save_path + '/' + title + '.zip',
'w', zipfile.ZIP_DEFLATED)
for root, dirs, files in os.walk(root_path):
for f in files:
filename = os.path.join(root_path, f)
z.write(filename, filename[rootlength:])
z.close()