| 1 |
"""
|
| 2 |
|
| 3 |
|
| 4 |
MCP_CopyUp_Request.py
|
| 5 |
|
| 6 |
Created by
|
| 7 |
Emanuel Borges
|
| 8 |
08.14.2010
|
| 9 |
|
| 10 |
TODO: Next make it gather the tar size and update your sheet on what's been uploaded. Also make it
|
| 11 |
automatically make the destination point based on the case you select and the date.
|
| 12 |
|
| 13 |
NOTE: What if the ftp connection times out while it's being compressed...?
|
| 14 |
|
| 15 |
"""
|
| 16 |
|
| 17 |
#import win32api
|
| 18 |
import os, tarfile, time, ConcordanceHelperTools, NinoGenTools, shutil,zipfile,MCP_Lib
|
| 19 |
from directorySize2 import pretty_filesize2
|
| 20 |
|
| 21 |
|
| 22 |
def ReportAndStore(caseName, totalSize, reviewPlatform):
|
| 23 |
reportSize = pretty_filesize2(totalSize)
|
| 24 |
epoch = str(int(time.time()))
|
| 25 |
#casesDir = r"C:\Documents and Settings\eborges\My Documents\My Dropbox\Documents\Cases"
|
| 26 |
casesList,officeCases, allCases, casesDir = MCP_Lib.GetCaseList()
|
| 27 |
if caseName in casesList:
|
| 28 |
if casesDir:
|
| 29 |
casePath = os.path.join(casesDir,caseName)
|
| 30 |
outputFile = open(os.path.join(casePath,"UploadReport.txt"),'a')
|
| 31 |
|
| 32 |
outputFile.write(epoch + " | " + reviewPlatform + " | " + time.strftime('%Y%m%d') + " | " +reportSize + "\n")
|
| 33 |
outputFile.close()
|
| 34 |
print 'MCP: Connecting to the Access Database...'
|
| 35 |
clientMatter = caseName.split("_(")[1]
|
| 36 |
clientMatter = clientMatter[:-1]
|
| 37 |
clientMatter = clientMatter.replace('-','.')
|
| 38 |
#accessDB = MCP_Lib.AccessDBConnection(r"W:\Manny\testing\TPM_CaseTracking.mdb")
|
| 39 |
accessDB = MCP_Lib.AccessDBConnection(r"\\chiads01\app\DS_CaseTrack\TPM_CaseTracking.mdb")
|
| 40 |
accessDB.UpdateCaseUpload(clientMatter,reviewPlatform,{epoch:(time.strftime('%m/%d/%Y'),reportSize)})
|
| 41 |
accessDB.CloseAccessConnection()
|
| 42 |
print 'MCP: Access database updated.'
|
| 43 |
|
| 44 |
def CompressSelected(pathToCompress, workDir, fileName):
|
| 45 |
## check available space. Acutally this could take too long to calculate the source media.
|
| 46 |
## warn them to check the available size, for now.
|
| 47 |
#availSize = win32api.GetDiskFreeSpaceEx(rootPath)
|
| 48 |
## create the tar
|
| 49 |
if os.path.splitext(fileName)[1].upper() in [".ZIP", ".RAR",".TAR",".7Z"]:
|
| 50 |
print "MCP: It's already compressed. Skipping step."
|
| 51 |
tFileName = pathToCompress
|
| 52 |
else:
|
| 53 |
#print "this is second workdir %s"% workDir
|
| 54 |
tFileName = os.path.join(workDir , fileName) +".tar"
|
| 55 |
tFile = tarfile.open(tFileName,'w')
|
| 56 |
tFile.add(pathToCompress,arcname = fileName,recursive = True)
|
| 57 |
tFile.close()
|
| 58 |
return tFileName
|
| 59 |
|
| 60 |
def CopyLNToLN(sourceLocationAndFile,newLocation,expeObj):
|
| 61 |
pathExists = expeObj.TestPath(newLocation)
|
| 62 |
if pathExists:
|
| 63 |
pass
|
| 64 |
else:
|
| 65 |
expeObj.CreateNewPath(newLocation)
|
| 66 |
expeObj.MoveOnLN(sourceLocationAndFile,os.path.join(newLocation,os.path.split(sourceLocationAndFile)[1]))
|
| 67 |
print "the file would have been moved to\n%s"%os.path.join(newLocation,os.path.split(sourceLocationAndFile)[1])
|
| 68 |
|
| 69 |
|
| 70 |
def CopyUpRemote(compressedFile,newLocation,expeObj):
|
| 71 |
newTargetPath = os.path.split(newLocation)[0]
|
| 72 |
pathExists = expeObj.TestPath(newTargetPath)
|
| 73 |
tempPath = newTargetPath
|
| 74 |
pathRemainingList = []
|
| 75 |
while pathExists == False:
|
| 76 |
tempPath, tempPart = os.path.split(tempPath)
|
| 77 |
pathRemainingList.append(tempPart)
|
| 78 |
pathExists = expeObj.TestPath(tempPath)
|
| 79 |
if pathRemainingList:
|
| 80 |
pathRemainingList.reverse()
|
| 81 |
for pathPiece in pathRemainingList:
|
| 82 |
tempPath = os.path.join(tempPath,pathPiece)
|
| 83 |
expeObj.CreateNewPath(tempPath)
|
| 84 |
|
| 85 |
|
| 86 |
drive,null = os.path.splitdrive(compressedFile)
|
| 87 |
if drive.upper() == "C:":
|
| 88 |
print "MCP: moving up %s"%compressedFile
|
| 89 |
print "MCP: to %s"% newLocation
|
| 90 |
errCode = expeObj.CopyToLN(compressedFile,newTargetPath)
|
| 91 |
if errCode == True:
|
| 92 |
os.remove(compressedFile)
|
| 93 |
else:
|
| 94 |
print "MCP: copying up %s"%compressedFile
|
| 95 |
print "MCP: to %s"% newLocation
|
| 96 |
errCode = expeObj.CopyToLN(compressedFile,newTargetPath)
|
| 97 |
|
| 98 |
def CopyUpMWESFTP(compressedFile,newLocation,mweSftpObj):
|
| 99 |
newTargetPath = os.path.split(newLocation)[0]
|
| 100 |
pathExists = mweSftpObj.TestPath(newTargetPath)
|
| 101 |
if pathExists:
|
| 102 |
pass
|
| 103 |
else:
|
| 104 |
mweSftpObj.CreateNewPath(newTargetPath)
|
| 105 |
|
| 106 |
drive,null = os.path.splitdrive(compressedFile)
|
| 107 |
if drive.upper() == "C:":
|
| 108 |
print "MCP: moving up %s"%compressedFile
|
| 109 |
print "MCP: to %s"% newLocation
|
| 110 |
mweSftpObj.CopyToDis(compressedFile,newTargetPath)
|
| 111 |
os.remove(compressedFile)
|
| 112 |
else:
|
| 113 |
print "MCP: copying up %s"%compressedFile
|
| 114 |
print "MCP: to %s"% newLocation
|
| 115 |
mweSftpObj.CopyToDis(compressedFile,newTargetPath)
|
| 116 |
|
| 117 |
def CopyUp(compressedFile, newLocation):
|
| 118 |
## WARNING! needs to be changes so it increments a dir. What if this dir already exists.
|
| 119 |
if os.path.exists(os.path.split(newLocation)[0]):
|
| 120 |
pass
|
| 121 |
else:
|
| 122 |
os.makedirs(os.path.split(newLocation)[0])
|
| 123 |
|
| 124 |
## if the pre archive is on C, move it. Else just copy it.ie a dvd with a zip on it.
|
| 125 |
drive,null = os.path.splitdrive(compressedFile)
|
| 126 |
if drive.upper() == "C:":
|
| 127 |
print "MCP: copying up %s"%compressedFile
|
| 128 |
print "MCP: to %s"% newLocation
|
| 129 |
os.rename(compressedFile, newLocation)
|
| 130 |
else:
|
| 131 |
shutil.copy2(compressedFile, newLocation)
|
| 132 |
print "MCP: moving up %s"%compressedFile
|
| 133 |
print "MCP: to %s"% newLocation
|
| 134 |
|
| 135 |
|
| 136 |
def Process(caseName, workDir, startDir, platform = 'Concordance DIS', sourceMediaLocation = 'local'):
|
| 137 |
##This should be external too.
|
| 138 |
availableShare = "Dis29"
|
| 139 |
expeObj = None
|
| 140 |
mweSftpObj = None
|
| 141 |
if sourceMediaLocation == 'local':
|
| 142 |
if platform == 'Concordance DIS':
|
| 143 |
mweSftpObj = MCP_Lib.MweFtpConnection()
|
| 144 |
connectionTest = os.path.isdir(os.path.join(r'\\lisdisdss01',availableShare))
|
| 145 |
else:
|
| 146 |
expeObj = MCP_Lib.ExpeDatConnection(platform)
|
| 147 |
connectionTest = True
|
| 148 |
if connectionTest:
|
| 149 |
totalSize = NinoGenTools.Counter()
|
| 150 |
networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform)
|
| 151 |
if os.path.isfile(startDir):
|
| 152 |
startDir,indFile = os.path.split(startDir)
|
| 153 |
compressList = [indFile]
|
| 154 |
else:
|
| 155 |
compressList = os.listdir(startDir)
|
| 156 |
for item in compressList:
|
| 157 |
if item.upper() == 'SYSTEM VOLUME INFORMATION':
|
| 158 |
## If it's the system folder skip it.
|
| 159 |
## Consider changing this to a try or to a list of stuff to skip.
|
| 160 |
pass
|
| 161 |
else:
|
| 162 |
pathToCompress = os.path.join(startDir,item)
|
| 163 |
fileName = item
|
| 164 |
## TODO: check to see if the file already exists. If so, skip that request and give them a dialog.
|
| 165 |
print "MCP: Compressing %s"% item
|
| 166 |
compressedFile = CompressSelected(pathToCompress, workDir, fileName)
|
| 167 |
#print compressedFile
|
| 168 |
print "MCP: Done!"
|
| 169 |
print "MCP: Gathering size..."
|
| 170 |
if os.path.splitext(fileName)[1].upper() in [".ZIP"]:
|
| 171 |
print "MCP: It's a ZIP, so gathering uncompressed size..."
|
| 172 |
try:
|
| 173 |
tempZipSize = 0
|
| 174 |
tempZip = zipfile.ZipFile(compressedFile,'r')
|
| 175 |
for zipInfo in tempZip.infolist():
|
| 176 |
tempZipSize = tempZipSize + zipInfo.file_size
|
| 177 |
tempZip.close()
|
| 178 |
totalSize.inc(tempZipSize)
|
| 179 |
print "MCP: Uncompressed size gathered."
|
| 180 |
except:
|
| 181 |
print "MCP: Warning: Zip error. Defaulting to compressed size."
|
| 182 |
totalSize.inc(os.path.getsize(compressedFile))
|
| 183 |
else:
|
| 184 |
totalSize.inc(os.path.getsize(compressedFile))
|
| 185 |
print "MCP: Size gathered."
|
| 186 |
print "MCP: Copying up %s"% item
|
| 187 |
#print "comressedFile is %s"% compressedFile
|
| 188 |
## Copy or move it up.
|
| 189 |
#CopyUp(compressedFile, os.path.join(networkDir+"\\"+time.strftime('%Y%m%d'), os.path.split(compressedFile)[1]))
|
| 190 |
if platform == 'Concordance DIS':
|
| 191 |
## SFTP coping is now only way.
|
| 192 |
CopyUpMWESFTP(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),mweSftpObj)
|
| 193 |
|
| 194 |
## Wan copying totally disabled.
|
| 195 |
## CopyUp(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]))
|
| 196 |
else:
|
| 197 |
CopyUpRemote(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),expeObj)
|
| 198 |
print "MCP: Done!"
|
| 199 |
print "MCP: Saving sizes..."
|
| 200 |
ReportAndStore(caseName, totalSize.count, platform)
|
| 201 |
print "MCP: Saving complete."
|
| 202 |
print "MCP: All Items processed and copied!"
|
| 203 |
else:
|
| 204 |
print "MCP: ERROR!!! You are not connected to the DIS.\nPlease connect to the DIS and try your request again. \n\n\tEnd Of Line.\n"
|
| 205 |
else:
|
| 206 |
## User is copying from LN to LN
|
| 207 |
expeObj = MCP_Lib.ExpeDatConnection(platform)
|
| 208 |
networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform)
|
| 209 |
print "MCP: Gathering size..."
|
| 210 |
totalSize = expeObj.GatherSize(os.path.join('Uploads',startDir))
|
| 211 |
print "MCP: Size gathered."
|
| 212 |
print "MCP: Moving up %s"% os.path.join('Uploads',startDir)
|
| 213 |
## need to change this to put the file name or folder after networkDir
|
| 214 |
CopyLNToLN(os.path.join('Uploads',startDir),networkDir,expeObj)
|
| 215 |
#expeObj.MoveOnLN(os.path.join('Uploads',startDir),os.path.join(networkDir,os.path.split(startDir)[1]))
|
| 216 |
#print "the file would have been moved to\n%s"%os.path.join(networkDir,os.path.split(startDir)[1])
|
| 217 |
print "MCP: Done!"
|
| 218 |
#print totalSize
|
| 219 |
print "MCP: Saving sizes..."
|
| 220 |
ReportAndStore(caseName, int(totalSize), platform)
|
| 221 |
print "MCP: Saving complete."
|
| 222 |
## Verify that it's there
|
| 223 |
print "MCP: All Items processed and copied!"
|
| 224 |
|
| 225 |
if mweSftpObj:
|
| 226 |
mweSftpObj.CloseConnection()
|
| 227 |
raw_input("\n\nMCP: Press Enter key to exit.\n\n\tEnd Of Line.\n")
|
| 228 |
|
| 229 |
def GetCopyToLocation(caseName, availableShare,expeObj,platform = 'Concordance DIS'):
|
| 230 |
clientMatter = caseName.split("_(")[1]
|
| 231 |
clientMatter = clientMatter[:-1]
|
| 232 |
if platform == 'Concordance DIS':
|
| 233 |
parsedPath = ConcordanceHelperTools.ParseClientMatter(clientMatter)
|
| 234 |
parsedClientMatter = os.path.split(parsedPath)[1]
|
| 235 |
copyToLocation = os.path.join(r"\\lisdisdss01\%s\C_D"%availableShare,parsedClientMatter)
|
| 236 |
|
| 237 |
copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
|
| 238 |
|
| 239 |
## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
|
| 240 |
dirIncrement = NinoGenTools.Counter(1)
|
| 241 |
if os.path.exists(copyToLocation):
|
| 242 |
copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
|
| 243 |
dirIncrement.inc()
|
| 244 |
while os.path.exists(copyToLocation):
|
| 245 |
copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
|
| 246 |
dirIncrement.inc()
|
| 247 |
|
| 248 |
## Now that it's SFTP only, remove the part before c_d
|
| 249 |
copyToLocation = copyToLocation.split("\\\\lisdisdss01\\%s\\"%availableShare)[1]
|
| 250 |
else:
|
| 251 |
parsedPath = ConcordanceHelperTools.ParseClientMatterRemote(clientMatter)
|
| 252 |
copyToLocation = parsedPath
|
| 253 |
|
| 254 |
copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
|
| 255 |
|
| 256 |
## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
|
| 257 |
dirIncrement = NinoGenTools.Counter(1)
|
| 258 |
if expeObj.TestPath(copyToLocation) == True:
|
| 259 |
copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
|
| 260 |
dirIncrement.inc()
|
| 261 |
while expeObj.TestPath(copyToLocation) == True:
|
| 262 |
copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
|
| 263 |
dirIncrement.inc()
|
| 264 |
|
| 265 |
return copyToLocation
|
| 266 |
|
| 267 |
if __name__ == '__main__':
|
| 268 |
caseName = "DaVita-Dallas_(039323-0308)"
|
| 269 |
workDir = r"C:\Documents and Settings\eborges\Desktop\Working"
|
| 270 |
#workDir = r"E:"
|
| 271 |
startDir = r"C:\Documents and Settings\eborges\Desktop\Working\STCL_EML_IMAGE_1101.zip"
|
| 272 |
Process(caseName, workDir, startDir)
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|