| 1 |
"""
|
| 2 |
NinoGenTools
|
| 3 |
|
| 4 |
Created by
|
| 5 |
Emanuel Borges
|
| 6 |
06.16.07
|
| 7 |
|
| 8 |
This is just going to be a collection of classes for general tools that I use often. Nothing product
|
| 9 |
specific.
|
| 10 |
|
| 11 |
"""
|
| 12 |
|
| 13 |
import os, shutil, hashlib, time, stat, datetime
|
| 14 |
|
| 15 |
class Counter:
|
| 16 |
def __init__(self, count = 1):
|
| 17 |
"""Nino's main Counter Class. It will allow you to keep the counter in memory and you can
|
| 18 |
count in multiples. For example if you pass it 2 and inc by 2, your counting in multiples
|
| 19 |
of 2"""
|
| 20 |
#self.count = 1
|
| 21 |
self.count = count
|
| 22 |
def inc(self, n=1):
|
| 23 |
self.count += n
|
| 24 |
|
| 25 |
|
| 26 |
class CopyFile:
|
| 27 |
def __init__(self, outputDir):
|
| 28 |
self.outputDir = outputDir
|
| 29 |
self.logsDir = outputDir + "\\_logs_"
|
| 30 |
|
| 31 |
def Copy(self,absInputFile,outputFile,destinationDir,verify):
|
| 32 |
"""This was copied from SummationExt and modified a little to stand alone. It will copy a file,
|
| 33 |
write errors to an error log, etc.
|
| 34 |
This method will do the actual coying IF verify is == to 0. If not it will just populate file to be counted later"""
|
| 35 |
#print self.imagesDir
|
| 36 |
#destinationDir = self.imagesProcessedDir + "\\Printing\\" + projectName.split('\n')[0] + "\\" + os.path.split(absFile.lower().split(self.imagesDir.lower())[1])[0]
|
| 37 |
#print destinationDir
|
| 38 |
print "-"*80
|
| 39 |
print "copying %s\n"% absInputFile
|
| 40 |
|
| 41 |
try:
|
| 42 |
if verify == 0:
|
| 43 |
if os.path.exists(destinationDir) == 0:
|
| 44 |
os.makedirs(destinationDir)
|
| 45 |
if os.path.isfile(destinationDir + "\\" + outputFile):
|
| 46 |
print "duplicate File. Skipping..."
|
| 47 |
else:
|
| 48 |
print destinationDir+"\\"+outputFile
|
| 49 |
shutil.copy2(absInputFile,destinationDir+"\\"+outputFile)
|
| 50 |
print "Done!\n"
|
| 51 |
#copySuccess = open(self.workDir + '\\copySuccess.log','a')
|
| 52 |
#copySuccess.write(absFile+'\n')
|
| 53 |
#copySuccess.close()
|
| 54 |
except:
|
| 55 |
print "This file could not be copied! It either dosent exist or something is wrong with the path..."
|
| 56 |
if os.path.exists(self.logsDir) == 0:
|
| 57 |
os.makedirs(self.logsDir)
|
| 58 |
copyError = open(self.logsDir + '\\copyError.log','a')
|
| 59 |
copyError.write("I can't find or process original file %s or destination %s\n"% (absInputFile,destinationDir+ "\\"+outputFile))
|
| 60 |
copyError.close()
|
| 61 |
|
| 62 |
class HashFileContents:
|
| 63 |
"""This class will handle requests to hash file contents. It will alow you to choose between the
|
| 64 |
algorithms and will have some protection for opening really large files to memory. (eventually)
|
| 65 |
This hash class will always hash the binary contents of the file not the string representation.
|
| 66 |
Very important."""
|
| 67 |
def __init__(self, hashAlgorithm):
|
| 68 |
self.hashAlgorithm = hashAlgorithm
|
| 69 |
|
| 70 |
def HashFile(self, absFilePath):
|
| 71 |
"""Current supported Algorithms are: md5, sha1, sha224, sha256"""
|
| 72 |
binContents = open(absFilePath,'rb').read()
|
| 73 |
print "-"*80
|
| 74 |
print "Creating a %s hash digest from file\n%s"%(self.hashAlgorithm, absFilePath)
|
| 75 |
print "-"*80
|
| 76 |
if self.hashAlgorithm == 'md5':
|
| 77 |
hashVal = hashlib.md5(binContents).hexdigest()
|
| 78 |
elif self.hashAlgorithm == 'sha1':
|
| 79 |
hashVal = hashlib.sha1(binContents).hexdigest()
|
| 80 |
elif self.hashAlgorithm == 'sha224':
|
| 81 |
hashVal = hashlib.sha224(binContents).hexdigest()
|
| 82 |
elif self.hashAlgorithm == 'sha256':
|
| 83 |
hashVal = hashlib.sha256(binContents).hexdigest()
|
| 84 |
else:
|
| 85 |
print "Unsupported algorithm!"
|
| 86 |
hashVal = '0'
|
| 87 |
return hashVal
|
| 88 |
|
| 89 |
|
| 90 |
class FileProperties:
|
| 91 |
"""This class will retrieve file properties (things like created date, etc)in a human readable format
|
| 92 |
"""
|
| 93 |
def GetCreatedDate(self, absFilePath):
|
| 94 |
"""Returns the Created Date for the file."""
|
| 95 |
stats = os.stat(absFilePath)
|
| 96 |
createDate = time.localtime(stats[9])
|
| 97 |
formatedDate = time.strftime("%m/%d/%y", createDate)
|
| 98 |
return formatedDate
|
| 99 |
|
| 100 |
def GetModifiedDate(self, absFilePath):
|
| 101 |
"""Returns the Last Modified Date for the file."""
|
| 102 |
stats = os.stat(absFilePath)
|
| 103 |
lastModDate = time.localtime(stats[8])
|
| 104 |
formatedDate = time.strftime("%m/%d/%y", lastModDate)
|
| 105 |
return formatedDate
|
| 106 |
|
| 107 |
def GetAccessedDate(self, absFilePath):
|
| 108 |
"""Returns the Last Accessed Date for the file."""
|
| 109 |
stats = os.stat(absFilePath)
|
| 110 |
lastAccDate = time.localtime(stats[7])
|
| 111 |
formatedDate = time.strftime("%m/%d/%y", lastAccDate)
|
| 112 |
return formatedDate
|
| 113 |
|
| 114 |
def RemoveReadOnlyFlag(self, absFilePath, removeFlag = True):
|
| 115 |
"""Removes the Read Only flag on a file passed to it. Alternativly, you can add a the
|
| 116 |
flag by making removeFlag = False"""
|
| 117 |
if removeFlag:
|
| 118 |
os.chmod(absFilePath,stat.S_IWRITE)
|
| 119 |
else:
|
| 120 |
os.chmod(absFilePath,stat.S_IREAD)
|
| 121 |
|
| 122 |
class DateUtilities:
|
| 123 |
def EnumerateDateRange(self, startDate, endDate):
|
| 124 |
"""This method will enumerate all the values in a date range. Must be in MM/DD/YYYY format"""
|
| 125 |
m,d,y = startDate.split("/")
|
| 126 |
first=datetime.date(int(y),int(m),int(d))
|
| 127 |
m,d,y = endDate.split("/")
|
| 128 |
last=datetime.date(int(y),int(m),int(d))
|
| 129 |
adate=first
|
| 130 |
dates=[]
|
| 131 |
while adate<=last:
|
| 132 |
dates.append(adate)
|
| 133 |
adate+=datetime.timedelta(1)
|
| 134 |
dateList = []
|
| 135 |
for i in dates:
|
| 136 |
i = str(i)
|
| 137 |
y,m,d = i.split("-")
|
| 138 |
dateList.append("%s/%s/%s"%(m,d,y))
|
| 139 |
return dateList
|
| 140 |
def ConvertDateFormat(self,date, convertToFormat='computer'):
|
| 141 |
"""This method converts between human MM/DD/YYYY and computer yyyymmdd"""
|
| 142 |
if convertToFormat == 'computer':
|
| 143 |
m,d,y = date.split("/")
|
| 144 |
if len(m)<2:
|
| 145 |
m = "0"+m
|
| 146 |
if len(d)<2:
|
| 147 |
d = "0"+d
|
| 148 |
return y+m+d
|
| 149 |
else:
|
| 150 |
y = date[:4]
|
| 151 |
m = date[4:6]
|
| 152 |
d = date[6:]
|
| 153 |
return "%s/%s/%s"% (m,d,y)
|
| 154 |
|
| 155 |
def ReverseDateFormat(self,date):
|
| 156 |
"""This method converts from improper computer format MMDDYYYY to proper YYYYMMDD"""
|
| 157 |
y = date[4:]
|
| 158 |
m = date[:2]
|
| 159 |
d = date[2:4]
|
| 160 |
return y+m+d |