Back to home page

Project CMSSW displayed by LXR

 
 

    


Warning, /DQM/Integration/scripts/igfilemgr/zipmerge is written in an unsupported language. File is not indexed.

0001 #! /usr/bin/env python3
0002 """
0003 zipmerge [-sret] [-n size ] [--help] [-o name] file1 
0004   file2 [filen...]
0005   
0006 Options:  
0007   -s      Sort file entries by file name in merged files.
0008   -r      If s is  specifyed,  will  append to the filename the name of the last
0009           file and the first file.
0010   -e      Erase source files.If process interrupted in the midle it could render
0011           the files useless. Always make backup copys of your files before using
0012           this option.
0013   -t      Test print out the inner structure but don't take any action.
0014   -n size Specify  the  maximum file size in Kb. If the merge exeeds the maximum size, 
0015           additional files will be created also not exeeding this size.
0016   -o name Name to be used for resulting merged file(s).
0017   file 1  list of files to be merged, the program accepts a minimum of 2  files, 
0018   file 2  wild cards are accepted.
0019   filen 
0020  
0021 """
0022 
0023 
0024 import sys
0025 import os.path
0026 import getopt as gop
0027 import zipfile as zp
0028 
0029 SORTED=False
0030 RANGE=False
0031 KEEP=True
0032 TEST=False
0033 MFILENAME="merged.zip"
0034 CHUNKSIZE=2097152
0035 ZFHANDLES=[]
0036 OUTFILES=[]    
0037 MEMBERS=[]
0038 #FILETREE={}
0039 TOTALSIZE=0.0
0040 ALL=False
0041 def sortMembers():
0042     MEMBERS.sort(key=lambda x:x[1].filename)
0043 def openOutputZipFile(filename):
0044   global ALL,OUTFILES
0045   try:
0046     if os.path.exists(filename):
0047       key="x"
0048       while  key not in ["y","Y","n","N","a","A","all","All",""] and not ALL:
0049         print "\nOutput file %s already exist and will be overwriten!!!" % filename
0050         key=input("Do you want to proceed? (y,n,[all])")
0051         ALL=key in ["a","A","all","All",""]
0052       if key in ["y","Y",""] or ALL: 
0053         os.remove(filename)
0054       else:
0055         sys.exit()
0056     zpout=zp.ZipFile(filename,"w")
0057     zpout.close()
0058     zpout=zp.ZipFile(filename,"a")
0059     OUTFILES.append(zpout)
0060     return zpout
0061   except:
0062     a,b,c=sys.exc_info()
0063     b.code == None or sys.stderr.write("Could not create output file\n")
0064     sys.exit(3)
0065 def cleanup():
0066   global OUTFILES,ZFHANDLES
0067   for handle in ZFHANDLES:
0068       try:
0069         handle.close()
0070       except:
0071         pass
0072   for handle in OUTFILES:
0073       try:
0074         handle.close()
0075       except:
0076         pass
0077 
0078 if __name__ == "__main__": 
0079   try:
0080     (args,filenames)=gop.getopt(sys.argv[1:],"srn:o:",["help"])
0081   except getopt.GetoptError:
0082     sys.stderr.write(  "Sintax Error unrecognised option" )
0083     sys.stderr.write( __doc__ )
0084     sys.exit(2)
0085   
0086   for item in args:
0087     if item[0]=="-s":
0088       SORTED=True
0089     elif item[0]=="-r":
0090       RANGE=True
0091     elif item[0]=="-e":
0092       KEEP=False
0093     elif item[0]=="-n":
0094       CHUNKSIZE=int(item[1])
0095     elif item[0]=="-o":
0096       MFILENAME=item[1]
0097     elif item[0]=="--help":
0098       sys.stdout.write(__doc__)
0099       sys.exit(1)
0100 
0101   if len(filenames) < 2 :
0102     sys.stdout.write(__doc__)
0103     sys.exit(2)
0104     
0105   basename,extension=MFILENAME.rsplit(".",1)
0106   for filename in filenames:
0107     print "Opening %s" % filename
0108     ZFHANDLES.append(zp.ZipFile(filename,"r"))
0109   
0110   print "gathering archived files statistics"
0111   for handle in ZFHANDLES:
0112     for info in handle.infolist():
0113       MEMBERS.append([handle,info])
0114       #FILETREE.setdefault(handle,[]).append(info)
0115       TOTALSIZE+=info.compress_size/1024
0116   print "found %d archived files in %d archives" % (len(MEMBERS),len(ZFHANDLES))
0117   numfiles=TOTALSIZE/CHUNKSIZE
0118   numfiles=int(round(numfiles) + (numfiles-round(numfiles)>0 and 1 or 0 ))
0119   print "The Number of expected requiered files is: %d (could be more)" % numfiles  
0120   
0121   print "Starting Merging process..."
0122   
0123   print "Creating file structure"
0124   print "\n".join([key[1].filename for key in MEMBERS[:10]])
0125   not SORTED or sortMembers()
0126   print "Sorted"
0127   print "\n".join([key[1].filename for key in MEMBERS[:10]])
0128   i=1
0129   while len(MEMBERS) > 0:
0130     filesize=0
0131     m=0
0132     filename="%s_%03d.%s" % (basename,i,extension)
0133     print "opening file %s" % filename
0134     outfile=openOutputZipFile(filename)
0135     while len(MEMBERS) > 0 and  m < len(MEMBERS):
0136       member=MEMBERS[m]
0137       if (member[1].compress_size+filesize)/1024 < CHUNKSIZE:
0138         try:
0139           print member[1].filename
0140           outfile.writestr(member[1],member[0].read(member[1].filename))
0141         except zp.BadZipfile:
0142           print member[1].filename
0143           print "Problem with file member: %s skiping" % member[1].filename
0144           del member
0145           del MEMBERS[m]
0146           continue
0147         filesize+=member[1].compress_size
0148         del member
0149         del MEMBERS[m]
0150       elif SORTED:
0151         break
0152       else:
0153         m+=1
0154     i+=1
0155     print "Closing file %s" % filename 
0156     outfile.close()
0157   cleanup()
0158   
0159