#!/usr/bin/python
import os, sys, glob, re, shutil
####sys.argv = sys.argv[1:]
####path1 = sys.argv[0]
def postfixbuild(tempname):
import md5, os.path, time
md5o = md5.md5()
rw = open(tempname)
while True:
nextbuf=rw.read(8192)
if nextbuf == "":
break
md5o.update( nextbuf )
rw.close()
time1 = os.path.getmtime(tempname)
timestr = time.strftime( "%Y%m%d-%H%M", time.localtime( int(time1)) )
return timestr + "_$_" + md5o.hexdigest()
def execzip(path1):
hasjp = False
whitelist = re.compile( r'[^0-9a-zA-Z@\/\-=\.\n_%~\^\+,\s\-]' )
zipname = os.path.dirname(path1) + "/" + os.path.basename(path1).replace(".","#")
for f1 in glob.glob( path1 + "/*" ):
blackis = whitelist.search(f1)
if blackis != None:
hasjp = True; print blackis.group(0)
if hasjp:
tempname = "/tmp/" + str( os.getpid() ) + ".7z"
try:
os.spawnv( os.P_WAIT, "/usr/bin/7za",
[
"/usr/bin/7za",
"u",
"-mx=1",
tempname,
path1 ] )
postfix = postfixbuild(tempname)
shutil.move( tempname, path1 + ".7z.temp" )
os.rename( path1 + ".7z.temp", path1 + "(" + postfix + ")" + ".7z" )
os.rename( path1, path1 + ".7zed" )
shutil.rmtree( path1 + ".7zed" )
except KeyboardInterrupt:
os.remove( tempname )
else:
try:
tempname = "/tmp/" + str( os.getpid() ) + ".zip"
os.spawnv( os.P_WAIT, "/usr/bin/zip",
[
"/usr/bin/zip",
"-ru",
tempname,
path1
] )
postfix = postfixbuild(tempname)
shutil.move( tempname, path1 + ".zipped.temp" )
os.rename( path1 + ".zipped.temp", path1 + "(" + postfix + ")" + ".zip" )
os.rename( path1, path1 + ".zipped" )
shutil.rmtree( path1 + ".zipped" )
except KeyboardInterrupt:
os.remove( tempname )
for f1 in sys.argv[1:]:
if not os.path.isdir(f1): continue
if re.search( r'\.7zed$', f1 ): continue
execzip(f1)