def unZipInto(_zip,target,isVerbose=False,callback=None): try: iterable = None typ = ObjectTypeName.typeClassName(_zip) if (typ == 'zipfile.ZipFile'): iterable = (f.filename for f in _zip.filelist) else: raise AttributeError('Invalid _zip attribute cann be of type "%s".' % (typ)) if (isVerbose): print '*** iterable = %s' % (str(iterable)) if (iterable): for f in iterable: _f_ = __normalize__(f) fname = os.path.join(target,_f_) if (f.endswith('/')): if (not os.path.exists(fname)): os.makedirs(fname) if (callable(callback)): try: callback(EntityType.folder,f) except: pass else: __bytes__ = _zip.read(f) if (isVerbose): print '%s -> %s [%s]' % (f,fname,__bytes__) _utils.writeFileFrom(fname,__bytes__,mode='wb') if (callable(callback)): try: callback(EntityType.file,f,fname) except: pass except Exception, _details: if (isVerbose): print _utils.formattedException(details=_details)
def process_records(fnameIn,fnameOut): from vyperlogix.parsers.CSV import CSV print 'Reading %s...' % (fnameIn) reader = CSV(filename=fnameIn) records = reader.rowsAsRecords() ioBuf = _utils.stringIO() print >> ioBuf, '<root>' n = 1 m = len(records) print 'Writing %s records...' % (m) for aRec in records: print >> ioBuf, '<node>' for k,v in aRec.iteritems(): print >> ioBuf, '<%s>%s</%s>' % (k,v,k) print >> ioBuf, '</node>' print 'Writing record #%s of %s...' % (n,m) n += 1 print >> ioBuf, '</root>' _utils.writeFileFrom(fnameOut,ioBuf.getvalue()) print 'Done !!!'
def unZipInto(_zip, target, isVerbose=False, callback=None): import os from vyperlogix.misc import ObjectTypeName from vyperlogix.hash import lists from vyperlogix.misc import _utils try: iterable = None typ = ObjectTypeName.typeClassName(_zip) if (typ == 'zipfile.ZipFile'): iterable = (f.filename for f in _zip.filelist) else: raise AttributeError( 'Invalid _zip attribute cann be of type "%s".' % (typ)) if (isVerbose): print '*** iterable = %s' % (str(iterable)) if (iterable): for f in iterable: _f_ = f.replace('/', os.sep) fname = os.path.join(target, _f_) if (f.endswith('/')): if (not os.path.exists(fname)): os.makedirs(fname) if (callable(callback)): try: callback(EntityType.folder, f) except: pass else: __bytes__ = _zip.read(f) if (isVerbose): print '%s -> %s [%s]' % (f, fname, __bytes__) _utils.writeFileFrom(fname, __bytes__, mode='wb') if (callable(callback)): try: callback(EntityType.file, f, fname) except: pass except Exception as _details: if (isVerbose): print _utils.formattedException(details=_details)
def unEgg(module_name, egg_path, top): '''module_name is the name of the module like paramiko, egg_path is the fully qualified path to the egg, top is the folder where the un-egg should be placed. ''' import os, sys, zipfile from vyperlogix.misc import _utils _dirname = '' if (os.path.exists(egg_path)): dirname = top _dirname = _utils.safely_mkdir(fpath=dirname, dirname=module_name) z = zipfile.ZipFile(egg_path, 'r') _files = z.filelist for f in _files: bytes = z.read(f.filename) _f = os.sep.join([_dirname, f.filename]) _utils.safely_mkdir(fpath=os.path.dirname(_f), dirname='') _utils.writeFileFrom(_f, bytes, mode='wb') z.close() return _dirname
def copyZipFile(fname,adjustAnalysis=None,checkTypes=None,adjustTypes=None,adjustContents=None,postProcess=None,cleanup=None,zipPrefix='',acceptable_types=[]): import zipfile, os, sys, tempfile, types from vyperlogix.misc import _utils from vyperlogix.zip import getZipFilesAnalysis def _cleanup(fname,newName): if (os.path.exists(fname)) and (os.path.exists(newName)): targetFolder = os.path.dirname(fname) targetFname = os.sep.join([targetFolder,'copy%s'%(os.path.basename(fname))]) _utils.copyFile(newName,targetFname) else: print >>sys.stderr, 'WARNING: Cannot copy the Zip file from "%s" to the target-folder of "%s".' % (fname,targetFname) pass new_tuple = tempfile.mkstemp('.zip','new') os.close(new_tuple[0]) newName = new_tuple[-1] os.remove(newName) newName = newName.split('.')[0] os.mkdir(newName) tmpFolder = os.sep.join([newName,'zip']) newName = os.sep.join([newName,'new-file.zip']) if (not os.path.exists(tmpFolder)): os.mkdir(tmpFolder) _zip = zipfile.ZipFile(fname,'r',zipfile.ZIP_DEFLATED) newZip = zipfile.ZipFile(newName,'w',zipfile.ZIP_STORED) _analysis = getZipFilesAnalysis.getZipFilesAnalysis(_zip,prefix=zipPrefix,_acceptable_types=acceptable_types) if (callable(adjustAnalysis)): try: _analysis = adjustAnalysis(_analysis) except: pass for f,t in _analysis.iteritems(): bool_t = True if (callable(checkTypes)): try: bool_t = checkTypes(t) except: bool_t = True if (bool_t): if (callable(adjustTypes)): try: t = adjustTypes(t) except: pass for tt in t: _f = '.'.join([f,tt]) if (len(tt) > 0) else f contents = _zip.read(_f) if (callable(adjustContents)): try: contents = adjustContents(tt,_f,contents,_analysis) except: pass tmpFile = os.sep.join([tmpFolder,_f.replace('/',os.sep)]) try: dname = os.path.dirname(tmpFile) if (not os.path.exists(dname)): os.makedirs(dname) _utils.writeFileFrom(tmpFile,contents) newZip.write(tmpFile,_f,zipfile.ZIP_DEFLATED) finally: try: os.remove(tmpFile) except: pass if (callable(postProcess)): try: postProcess(tmpFolder,_zip,newZip,_analysis) except: pass _zip.close() newZip.close() _utils.removeAllFilesUnder(tmpFolder) if (os.path.exists(tmpFolder)): os.remove(tmpFolder) if (callable(cleanup)): try: cleanup(fname,newName) except: pass else: try: _cleanup(fname,newName) except: pass if (os.path.exists(newName)): os.remove(newName) try: newName = os.path.dirname(newName) os.rmdir(newName) except: pass pass