def getFullAssetsAndUnknownfiles(apkfilepath, zipfile): pwd = os.getcwd() decodeDir = '_decode_package_' destpath = os.path.dirname(apkfilepath) os.chdir(destpath) if not os.path.exists(decodeDir): os.mkdir(decodeDir) paras = [] paras.append((APK_TOOL_PATH, '')) paras.append(('d', os.path.basename(apkfilepath))) paras.append(('-o', decodeDir)) paras.append(('-f', ' ')) decodeCmd = genCmd(paras) print(decodeCmd) os.system(decodeCmd) pwd3 = os.getcwd() os.chdir(decodeDir) assetsf = __getAllObjFiles('assets', '.*') [zipfile.write(os.path.join(x, y)) for (x, y) in assetsf] if os.path.exists('unknown'): unkownf = __getAllObjFiles('unknown', '.*') pwdunknown = os.getcwd() [zipfile.write(os.path.join(x,y)) for (x, y) in unkownf] os.chdir(pwd3) shutil.rmtree(decodeDir) os.chdir(pwd)
def write_files_in_dir_to_zipfile(sourcepath, arcpath, zipfile): """ <Purpose> Inserts the files in the current directory into the specified zipfile. <Arguments> sourcepath: The source path of the files to add. arcpath: The zip file's internal destination path to write to. zipfile: The zip file to write to. files: If specified, only these files are copied. Only files in the immediate directory can be specified. skipfiles: If specified, these files will be skipped. Only files in the immediate directory can be skipped. <Side Effects> Copies the files that are in sourcepath to arcpath in the zipfile. If files is specified, then only those files are copied. <Exceptions> None <Return> None """ files = os.listdir(sourcepath) for fname in files: sourcefilepath = sourcepath + os.sep + fname targetfilepath = arcpath + os.sep + fname if os.path.isfile(sourcefilepath): zipfile.write(sourcefilepath, targetfilepath) else: write_files_in_dir_to_zipfile(sourcefilepath, targetfilepath, zipfile)
def to_zipfile(obj, zipfile, path=''): for key in obj.keys(): new_path = join_path(path, key) if isinstance(obj[key], h5py.Group): to_zipfile(obj[key], zipfile, new_path) else: fname = join_path(path, key+'.dat') # some members are empty... skip them. if sum(obj[key].shape) == 0: continue value = obj[key].value formats = { 'S': '%s', 'f': '%.8g', 'i': '%d', 'u': '%d' } if value.dtype.kind in formats: fd, fn = tempfile.mkstemp() os.close(fd) # to be opened by name if DEBUG: print fname, value.dtype.kind if len(value.shape) > 2: with open(fn, 'w') as f: simplejson.dump(value.tolist(), f) else: savetxt(fn, value, delimiter='\t', fmt=formats[value.dtype.kind]) zipfile.write(fn, fname) os.remove(fn) else: print "unknown type of array: ", fname, value.dtype
def ZipDirectory(base_dir, zipfile): """ Zip all the files in base_dir into the given opened zipfile object. """ for (root, dirs, files) in os.walk(base_dir, followlinks=True): for f in files: full_name = J(root, f) zipfile.write(full_name, os.path.relpath(full_name, base_dir))
def write_to_zip(zipfile, root_dir, local_dir): files = os.listdir(os.path.join(root_dir, local_dir)) for f in files: if (os.path.isdir(os.path.join(root_dir, local_dir, f))): write_to_zip(zipfile, root_dir, os.path.join(local_dir, f)) else: zipfile.write(os.path.join(root_dir, local_dir, f), os.path.join(local_dir, f))
def get_zip_logs(request): """ Make an archive of logs available to download """ from django.core.servers.basehttp import FileWrapper import zipfile try: compression = zipfile.ZIP_DEFLATED except: compression = zipfile.ZIP_STORED zipPath = "/tmp/logs.zip" zipfile = zipfile.ZipFile(zipPath, mode="w", allowZip64=True) files = ["tsconfig_gui.log", "django.log", "celery_w1.log"] for afile in files: fullpath = os.path.join("/var/log/ion", afile) if os.path.exists(fullpath): zipfile.write(fullpath, arcname=afile, compress_type=compression) zipfile.close() # TODO: Include the server serial number in the archive filename. # One possible source is /etc/torrentserver/tsconf.conf, serialnumber:XXXXXX archive_filename = "ts_update_logs.zip" response = http.HttpResponse(FileWrapper(open(zipPath)), mimetype="application/zip") response["Content-Disposition"] = "attachment; filename=%s" % archive_filename return response
def copy_assets(self, zipfile): for root, dirs, files in os.walk(os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates/css")): for name in files: zipfile.write(os.path.join(root, name), os.path.join(self.username, "css", name)) for root, dirs, files in os.walk(os.path.join(os.path.abspath(os.path.dirname(__file__)), "templates/js")): for name in files: zipfile.write(os.path.join(root, name), os.path.join(self.username, "js", name))
def trav(father,targetdir = ""): # 两个参数,father是父亲节点(遍历的文件夹), # targetdir是记录压缩包中的存放路径 travFile = os.listdir(father) # 用listdir列出文件夹内所有第一级文件 for i in travFile: # 遍历刚才的list if i.find('.') == -1: # 用str类的find方法,检测是否为文件夹 trav(father+os.sep+i,targetdir + os.sep + i) # 若为文件夹,则递归引用,同时调整存放路径 else: zipfile.write(father+os.sep+i,targetdir+os.sep+i)
def zipdir(path, zipfile): os.chdir(path) try: for root, dirs, files in os.walk("."): for fil in files: print(fil) zipfile.write(fil) finally: os.chdir("..")
def addItem(zipfile, item): if os.path.isdir(item): for (archiveDirPath, dirNames, fileNames) in os.walk(item): for fileName in fileNames: filePath = os.path.join(archiveDirPath, fileName) if not fileName in zipfile.namelist(): zipfile.write(filePath, filePath) else: if not item in zipfile.namelist(): zipfile.write(item)
def addtozip(zipfile, dir): if dir: files = os.listdir(dir) else: files = os.listdir(".") for f in files: file = os.path.join(dir, f) if os.path.isdir(file): addtozip(zipfile, file) else: print "Adding %s." % file zipfile.write(file)
def zipdir(path, zipfile): os.chdir(path) try: for root, dirs, files in os.walk("."): for fil in files: print(dirs) print(fil) print(root) print(os.path.join(root, fil)) zipfile.write(os.path.join(root, fil)) finally: os.chdir("..")
def ParseFile(xmlNode, zipfile): filename = os.path.join(options.directory, xmlNode.getAttribute("Name")) if options.verbose: print "Trimming %s" % (filename) #create dictionary of header locations headers = xmlNode.getElementsByTagName("MLSField") allColumns = False headerMap = {} for header in headers: if header.firstChild.data == "*": allColumns = True else: headerMap[header.firstChild.data] = -1 if options.verbose: print "xml file says to save: %s" % (headerMap) try: f = csv.reader(open(filename)) firstLineRead = False tf = tempfile.NamedTemporaryFile() headersToSave = [] for line in f: if not firstLineRead: s1 = set(item for item in line if item != '') s2 = set(headerMap.keys()) if allColumns: s2 = s1 s3 = s1.intersection(s2) headerMap.clear() for i in s3: headerMap[i] = line.index(i) headersToSave = headerMap.keys() if options.verbose: print "The columns that exist in both the xml and csv files are:" % (headersToSave) firstLineRead = True lineToWrite = "" for h in headersToSave: lineToWrite = "%s\"%s\"," % (lineToWrite, line[headerMap[h]]) if options.verbose: print lineToWrite tf.write(lineToWrite + "\n") #if options.verbose: print line tf.flush() zipfile.write(tf.name, xmlNode.getAttribute("Name")) tf.close() except IOError, e: print e sys.exit()
def _install_plugin_zip(self, plugin_name, plugin_data, update=False): # zipped module from download zip_plugin = plugin_name + '.zip' dst = os.path.join(self.plugins_directory, zip_plugin) if update: dst += _UPDATE_SUFFIX if os.path.isfile(dst): os.remove(dst) with tempfile.NamedTemporaryFile(dir=self.plugins_directory) as zipfile: zipfile.write(plugin_data) zipfile.flush() os.fsync(zipfile.fileno()) os.link(zipfile.name, dst) log.debug("Plugin (zipped) saved to %r", dst)
def inject_source(zipfile): for root, dirs, files in os.walk(os.curdir): for file in files: if file.endswith('.zip') or file.startswith('.'): continue full = os.path.join(root, file) zipfile.write(full, SOURCE + os.path.normpath(full)) logger.info('%s written', full) if root == os.curdir: for ex in EXCLUDE: try: dirs.remove(ex) except ValueError: pass # strange new_dirs = [] for dir in dirs: if not dir.startswith('.'): new_dirs.append(dir) dirs[:] = new_dirs
def full_backup(): import zipfile logger.info('Running full_backup') from django.utils import timezone from django.core.management import call_command now = timezone.now() file_name = 'backup-%s-%s-%s-%s-%s.json' % (now.year, now.month, now.day, now.hour, now.minute) file_path = os.path.join(PROJECT_ROOT, 'backups', file_name) dir = os.path.dirname(file_path) try: os.stat(dir) except: os.mkdir(dir) # we will temporarilly redirect stdout to a file to capture the data from the dumpdata cmd stdout = sys.stdout try: sys.stdout = open(file_path, 'w') call_command('dumpdata', use_natural_foreign_keys=True, use_natural_primary_keys=True) sys.stdout.close() except Exception as exc: logger.error(exc) # Put stdout back to what it was sys.stdout = stdout # Now zip the file zip_file_path = os.path.join(PROJECT_ROOT, 'backups', file_name + '.gz') zipfile = gzip.GzipFile(zip_file_path, "wb") try: inputFile = open(file_path,"r") zipfile.write(inputFile.read()) finally: zipfile.close() # Delete the original uncompressed file os.remove(file_path)
def inject_checker_validator(zipfile): pc = PackageConfig.get_config() checker = pc['checker'] val = pc['validator'] zipfile.write(checker) zipfile.write(val) for path in glob.glob('testlib.*'): zipfile.write(path)
def get_zip_logs(request): """ Make an archive of logs available to download """ from django.core.servers.basehttp import FileWrapper import zipfile try: compression = zipfile.ZIP_DEFLATED except: compression = zipfile.ZIP_STORED zipPath = '/tmp/logs.zip' zipfile = zipfile.ZipFile(zipPath, mode='w', allowZip64=True) for afile in ['tsconfig_gui.log', 'django.log', 'celery_w1.log', 'tsconfig_debug.log', 'product_integration.log']: fullpath = os.path.join('/var/log/ion', afile) if os.path.exists(fullpath): zipfile.write(fullpath, arcname=afile, compress_type=compression) zipfile.close() #TODO: Include the server serial number in the archive filename. #One possible source is /etc/torrentserver/tsconf.conf, serialnumber:XXXXXX archive_filename = 'ts_update_logs.zip' response = http.HttpResponse(FileWrapper (open(zipPath)), mimetype='application/zip') response['Content-Disposition'] = 'attachment; filename=%s' % archive_filename return response
def to_zipfile(obj, zipfile, path=''): summary = OrderedDict() for key in obj.keys(): val = obj[key] new_path = join_path(path, key) if isinstance(val, h5py.Group): to_zipfile(val, zipfile, new_path) else: fname = join_path(path, key+'.dat') if 'target' in val.attrs and val.attrs['target'] != join_path('/', path, key): print val.attrs['target'], join_path('/', path, key) summary[key] = OrderedDict([['target', val.attrs['target']]]) #, ['shape', (obj[val.attrs['target']]).shape]]) elif numpy.product(val.shape) <= 1: summary[key] = val.value.tolist() else: value = obj[key].value formats = { 'S': '%s', 'f': '%.8g', 'i': '%d', 'u': '%d' } if value.dtype.kind in formats: fd, fn = tempfile.mkstemp() os.close(fd) # to be opened by name if DEBUG: print fname, value.dtype.kind if len(value.shape) > 2: with open(fn, 'w') as f: simplejson.dump(value.tolist(), f) else: numpy.savetxt(fn, value, delimiter='\t', fmt=formats[value.dtype.kind]) zipfile.write(fn, fname) os.remove(fn) summary[key] = OrderedDict([['target', join_path('/', fname)], ['shape', obj[key].shape]]) else: print "unknown type of array: ", fname, value.dtype zipfile.writestr(os.path.join(path, 'fields.json'), simplejson.dumps(summary, indent=' '))
def inject_tests(zipfile): for test, answer in _own_tg.generate_all(): basetest = os.path.basename(test) baseans = os.path.basename(answer) zipfile.write(test, TESTDIR + basetest) zipfile.write(answer, TESTDIR + baseans)
def zip_with_msg(zipfile, source, target): if not os.path.exists(source): error('{} does not exist! Try build \'binary\' and \'apk\' before zipping!'.format(source)) zipfile.write(source, target) vprint('zip: {} -> {}'.format(source, target))
def zip_with_msg(zipfile, source, target): if not os.path.exists(source): error('{} does not exist! Try build \'binary\' and \'apk\' before zipping!'.format(source)) print('zip: ' + source + ' -> ' + target) zipfile.write(source, target)
def zip_dir(path, zipfile): """Create a zip of contents of path by traversing it.""" for root, dirs, files in os.walk(path): for f in files: zipfile.write(os.path.join(root, f), arcname=os.path.relpath(os.path.join(root, f), path))
import sys import os import zipfile FOLDER = 'lady_rappers' NEW_FOLDER = 'new_lady_rappers' files = os.listdir(FOLDER) zipfile = zipfile.ZipFile(('%s.zip' % NEW_FOLDER), mode='w') for file in files: full_path_to_file = ("%s/%s" % (FOLDER, file)) new_file = ("%s/%s" % (NEW_FOLDER, file)) sys.stderr.write("file = %s\n" % full_path_to_file) try: zipfile.write(full_path_to_file, new_file) except: sys.stderr.write("Error with this file.\n") zipfile.close() sys.stderr.write("** SCRIPT COMPLETE. **\n")
def install_plugin(self, path, update=False, overwrite_confirm=None, plugin_name=None, plugin_data=None): """ path is either: 1) /some/dir/name.py 2) /some/dir/name (directory containing __init__.py) 3) /some/dir/name.zip (containing either 1 or 2) """ zip_plugin = False if not plugin_name: zip_plugin = is_zip(path) if not zip_plugin: plugin_name = _plugin_name_from_path(path) else: plugin_name = os.path.splitext(zip_plugin)[0] if plugin_name: try: if plugin_data and plugin_name: # zipped module from download zip_plugin = plugin_name + '.zip' dst = os.path.join(USER_PLUGIN_DIR, zip_plugin) if update: dst += '.update' if os.path.isfile(dst): os.remove(dst) ziptmp = tempfile.NamedTemporaryFile(delete=False, dir=USER_PLUGIN_DIR).name try: with open(ziptmp, "wb") as zipfile: zipfile.write(plugin_data) zipfile.flush() os.fsync(zipfile.fileno()) os.rename(ziptmp, dst) log.debug("Plugin saved to %r", dst) except BaseException: try: os.remove(ziptmp) except (IOError, OSError): pass raise elif os.path.isfile(path): dst = os.path.join(USER_PLUGIN_DIR, os.path.basename(path)) if update: dst += '.update' if os.path.isfile(dst): os.remove(dst) shutil.copy2(path, dst) elif os.path.isdir(path): dst = os.path.join(USER_PLUGIN_DIR, plugin_name) if update: dst += '.update' if os.path.isdir(dst): shutil.rmtree(dst) shutil.copytree(path, dst) if not update: try: installed_plugin = self.load_plugin(zip_plugin or plugin_name, USER_PLUGIN_DIR) except Exception as e: log.error('Unable to load plugin: %s.\nError occured: %s', plugin_name, e) installed_plugin = None if installed_plugin is not None: self.plugin_installed.emit(installed_plugin, False) else: self.plugin_updated.emit(plugin_name, False) except (OSError, IOError): log.warning("Unable to copy %s to plugin folder %s" % (path, USER_PLUGIN_DIR))
def zipdir(path, zipfile): for root, files in os.walk(path): for f in files: zipfile.write(os.path.join(root, f))
def install_plugin(self, path, overwrite_confirm=None, plugin_name=None, plugin_data=None): """ path is either: 1) /some/dir/name.py 2) /some/dir/name (directory containing __init__.py) 3) /some/dir/name.zip (containing either 1 or 2) """ zip_plugin = False if not plugin_name: zip_plugin = is_zip(path) if not zip_plugin: plugin_name = _plugin_name_from_path(path) else: plugin_name = os.path.splitext(zip_plugin)[0] if plugin_name: try: dirpath, filepaths = self._get_existing_paths(plugin_name) update = dirpath or filepaths if plugin_data and plugin_name: # zipped module from download zip_plugin = plugin_name + '.zip' dst = os.path.join(USER_PLUGIN_DIR, zip_plugin) if update: dst += '.update' if os.path.isfile(dst): os.remove(dst) ziptmp = tempfile.NamedTemporaryFile( delete=False, dir=USER_PLUGIN_DIR).name try: with open(ziptmp, "wb") as zipfile: zipfile.write(plugin_data) zipfile.flush() os.fsync(zipfile.fileno()) os.rename(ziptmp, dst) log.debug("Plugin saved to %r", dst) except: try: os.remove(ziptmp) except (IOError, OSError): pass raise elif os.path.isfile(path): dst = os.path.join(USER_PLUGIN_DIR, os.path.basename(path)) if update: dst += '.update' if os.path.isfile(dst): os.remove(dst) shutil.copy2(path, dst) elif os.path.isdir(path): dst = os.path.join(USER_PLUGIN_DIR, plugin_name) if update: dst += '.update' if os.path.isdir(dst): shutil.rmtree(dst) shutil.copytree(path, dst) if not update: installed_plugin = self.load_plugin( zip_plugin or plugin_name, USER_PLUGIN_DIR) if installed_plugin is not None: self.plugin_installed.emit(installed_plugin, False) else: self.plugin_updated.emit(plugin_name, False) except (OSError, IOError): log.warning("Unable to copy %s to plugin folder %s" % (path, USER_PLUGIN_DIR))
PASS = args.pwd PACK = args.package SAPHUB = args.saphub print("Deploy %s to %s as %s via %s" % (DIR, HOST, PACK, SAPHUB)) kickoff = datetime.datetime.now() archive = io.BytesIO() zipfile = zipfile.ZipFile(archive, 'w') for path, dirs, files in os.walk(DIR): for file in files: if os.path.isfile(os.path.join(path, file)): print("add %s" % os.path.relpath(os.path.join(path, file), DIR)) zipfile.write(os.path.join(path, file), os.path.relpath(os.path.join(path, file), DIR)) zipfile.close() archive.seek(0) try: opener = build_opener(HTTPHandler) request = Request('http://%s/?host=%s&usr=%s&pwd=%s&package=%s' % (SAPHUB, HOST, USER, PASS, PACK), archive.read()) request.add_header('Content-Type', 'application/zip') request.get_method = lambda: 'PUT' response = urlopen(request) print("") print(response.read())
def write2zip(zipfile, path): for fl in glob.glob(path): if os.path.isdir(fl): write2zip(zipfile, fl + '/*') else: zipfile.write(fl)
def inject_solutions(zipfile): pc = PackageConfig.get_config(os.curdir) for sol in pc['solution']: zipfile.write(sol['source']) zipfile.write(pc['main_solution'])
# Remove duplicates. files_for_tarball = set(files_for_tarball) with tarfile.open(os.path.join(html_output_dir, example_name + '.tar.gz'), \ 'w:gz', dereference=True) as tarfile: example_dir_idx = example_dir.index(example_name) def strip_path(tarinfo): tarinfo.name = tarinfo.name[example_dir_idx-1:] # Put the inputs and outputs into the build directory because the test # will not be able to find them otherwise. basename = os.path.basename(tarinfo.name) if basename == 'CMakeLists.txt.tarball': head, tail = os.path.split(tarinfo.name) tarinfo.name = os.path.join(head, 'CMakeLists.txt') return tarinfo for path in files_for_tarball: tarfile.add(path, filter=strip_path) with zipfile.ZipFile(os.path.join(html_output_dir, example_name + '.zip'), \ 'w') as zipfile: example_dir_idx = example_dir.index(example_name) for path in files_for_tarball: arcname = path[example_dir_idx-1:] # Put the inputs and outputs into the build directory because the test # will not be able to find them otherwise. basename = os.path.basename(arcname) if basename == 'CMakeLists.txt.tarball': head, tail = os.path.split(arcname) arcname = os.path.join(head, 'CMakeLists.txt') zipfile.write(path, arcname)
def inject_tex(zipfile): pdf_out = generate_problem() tex = os.path.splitext(pdf_out)[0] + '.tex' zipfile.write(pdf_out, os.path.basename(pdf_out)) zipfile.write(tex, os.path.basename(tex)) # TODO it will be uncompilable zipfile.write(get_template_full_path('olymp.sty'), 'olymp.sty')