Calling all server admins
Our server admin was off today and I had to set a site live. We have a dategrep.py script that lists all changed files since a certain date so that those files can then be ftp’d to the live server. This seemed far too tedious for me so I re-wrote the dategrep.py script so that given a date it will zip up all changed files since that date. The zip archive will have the same directory structire so that all you have to do is extract that archive on the live server and you’re done.
[code lang=”python”]
“””
Checks for files modifies after a given date time and then adds them to a zip
file that can be easily extracted
“””
import os, time, sys
import zipfile
import glob
from datetime import datetime
class SimpleZipDateGrep:
today = time.time()
yesterdayTimestamp = today-24*60*60
yesterday = datetime.fromtimestamp(yesterdayTimestamp)
iYear = yesterday.year
iMonth = yesterday.month
iDay = yesterday.day
iHour = yesterday.hour
iHourOrig = iHour
iMin = yesterday.minute
zFileName = “SimpleZipDateGrepChange.log.zip”
logFileName = “SimpleZipDateGrepChange.log”
sExcludeFiles =””
bExcludeFiles = False
lExcludeFiles = [‘list.txt’,’log.txt’]
sYear,sMonth,sDay,sHour,sMin = “”,””,””,””,””
def __init__(self):
self.GetArgs()
self.ZipFiles()
print “Finished”
def GetArgs(self):
self.sYear=str(raw_input(“Year (yyyy)[default: “+str(self.iYear)+”]: “))
self.sYear = self.sYear.strip()
if self.sYear != “”:
try:
self.iYear = int(self.sYear)
except:
print “This is not a valid year”
#exception in casting to int
self.sMonth=str(raw_input(“Month (mm)[default: “+str(self.iMonth)+”]: “))
self.sMonth = self.sMonth.strip()
if self.sMonth != “”:
try:
self.iMonth = int(self.sMonth)
except:
print “This is not a valid month”
#exception in casting to int
self.sDay=str(raw_input(“Day (dd)[default: “+str(self.iDay)+”]: “))
self.sDay = self.sDay.strip()
if self.sDay != “”:
try:
self.iDay = int(self.sDay)
except:
print “This is not a valid day”
#exception in casting to int
self.sHour=str(raw_input(“Hour (hh)[default: “+str(self.iHour)+”]: “))
self.sHour = self.sHour.strip()
if self.sHour != “”:
try:
if int(self.sHour) < 25:
self.iHour = int(self.sHour)
except:
print "This is not a valid hour" + str(self.sHour)
#exception in casting to int
self.sMin=str(raw_input(“Minute (mm)[default: “+str(self.iMin)+”]: “))
self.sMin = self.sMin.strip()
if self.sMin != “”:
try:
if int(self.sMin) < 61:
self.iMin = int(self.sMin)
except:
print "This is not a valid minute" + str(self.sMin)
#exception in casting to int
self.sExcludeFiles = str(raw_input(“Exclude Files (y|n)[default: n]: “))
if self.sExcludeFiles.lower() == ‘y’:
self.bExcludeFiles = True
else:
self.bExcludeFiles = False
def ZipFiles(self):
”’
0 tm_year (for example, 1993)
1 tm_mon range [1,12]
2 tm_mday range [1,31]
3 tm_hour range [0,23]
4 tm_min range [0,59]
5 tm_sec range [0,61]; see (1) in strftime() description
6 tm_wday range [0,6], Monday is 0
7 tm_yday range [1,366]
8 tm_isdst 0, 1 or -1; see below
”’
iBaseTime=time.mktime((self.iYear,self.iMonth,self.iDay,self.iHour,self.iMin,0,0,0,0))
if self.iHour != self.iHourOrig:
iBaseTime = iBaseTime + time.altzone
currentFileName = os.path.basename(str(sys.argv[0]))
f=open(self.logFileName,’w’)
zFile = zipfile.ZipFile(self.zFileName, “w”)
f.write(“- File changed since – %s\n”%time.asctime(time.localtime(iBaseTime)))
#get a list of all the root level directories
curDir = os.curdir
for root,dirs,files in os.walk(curDir):
if root.lower().find(“svn”)>-1 or root.lower().find(“cvs”)>-1 :
continue
for name in files:
if name.lower().find(“svn”) > -1 or name.lower().find(“cvs”)>-1 or name == self.zFileName or name == self.logFileName or name == currentFileName:
continue
iTime=os.path.getmtime(os.path.join(root,name))
if iTime and iTime > iBaseTime:
#add the file to the zip file
if self.bExcludeFiles == True or (self.bExcludeFiles == False and root not in self.lExcludeFiles and name not in self.lExcludeFiles):
#Products
curPath = os.path.join(root,name)
curPath = curPath.replace(“.\\”,””)
if curPath.find(‘.’)==0:
curPath = curPath[1:]
print curPath
f.write(“%s %s\n”%(time.asctime(time.localtime(iTime)),curPath))
zFile.write(curPath, curPath, zipfile.ZIP_DEFLATED)
zFile.close()
f.flush()
f.close()
if __name__ == ‘__main__’:
SimpleZipDateGrep = SimpleZipDateGrep()
[/code]
Download SimpleZipDateGrep.zip {zip 1Kb}. It’s also on the downloads section. 🙂
Update:
Some bug fixes and now written in an OO stylie for reusablility