ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/MCP/Trunk/MCP_CopyUp_Request.py
Revision: 458
Committed: Tue Sep 17 14:41:23 2013 UTC (12 years, 6 months ago) by nino.borges
Content type: text/x-python
Original Path: Python/NinoCode/Active_prgs/MCP/MCP_CopyUp_Request.py
File size: 14516 byte(s)
Log Message:
Changed the Generic lable to Generic (old cases only) from working dir.  Created the Expedat Walk generator in MCP_Lib to act as a expedat dir walker. Changed the vendor upload dialog so that it now supports unlimited folder depth, by using the generator.

File Contents

# User Rev Content
1 ninoborges 8 """
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 nino.borges 454 casesList,myActiveCases,officeCases, allCases, casesDir = MCP_Lib.GetCaseList()
27 nino.borges 189 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 ninoborges 8 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 nino.borges 266 if os.path.splitext(fileName)[1].upper() in MCP_Lib.GetArchiveFileTypeList():#[".ZIP", ".RAR",".TAR",".7Z"]:
50 ninoborges 8 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 nino.borges 151 tFile.add(pathToCompress,arcname = fileName,recursive = True)
57 ninoborges 8 tFile.close()
58     return tFileName
59 nino.borges 258
60     def CopyLNToLN(sourceLocationAndFile,newLocation,expeObj):
61 nino.borges 453 ## Here new location does not have a file ending.
62 nino.borges 258 pathExists = expeObj.TestPath(newLocation)
63 nino.borges 453 tempPath = newLocation
64     pathRemainingList = []
65     while pathExists == False:
66     tempPath, tempPart = os.path.split(tempPath)
67     pathRemainingList.append(tempPart)
68     pathExists = expeObj.TestPath(tempPath)
69     if pathRemainingList:
70     pathRemainingList.reverse()
71     #print pathRemainingList
72     for pathPiece in pathRemainingList:
73     tempPath = os.path.join(tempPath,pathPiece)
74     expeObj.CreateNewPath(tempPath)
75    
76 nino.borges 258 expeObj.MoveOnLN(sourceLocationAndFile,os.path.join(newLocation,os.path.split(sourceLocationAndFile)[1]))
77     print "the file would have been moved to\n%s"%os.path.join(newLocation,os.path.split(sourceLocationAndFile)[1])
78 ninoborges 8
79 nino.borges 258
80 ninoborges 8 def CopyUpRemote(compressedFile,newLocation,expeObj):
81     newTargetPath = os.path.split(newLocation)[0]
82     pathExists = expeObj.TestPath(newTargetPath)
83     tempPath = newTargetPath
84     pathRemainingList = []
85     while pathExists == False:
86     tempPath, tempPart = os.path.split(tempPath)
87     pathRemainingList.append(tempPart)
88     pathExists = expeObj.TestPath(tempPath)
89     if pathRemainingList:
90     pathRemainingList.reverse()
91     for pathPiece in pathRemainingList:
92     tempPath = os.path.join(tempPath,pathPiece)
93     expeObj.CreateNewPath(tempPath)
94    
95    
96     drive,null = os.path.splitdrive(compressedFile)
97     if drive.upper() == "C:":
98     print "MCP: moving up %s"%compressedFile
99     print "MCP: to %s"% newLocation
100     errCode = expeObj.CopyToLN(compressedFile,newTargetPath)
101     if errCode == True:
102     os.remove(compressedFile)
103     else:
104     print "MCP: copying up %s"%compressedFile
105     print "MCP: to %s"% newLocation
106     errCode = expeObj.CopyToLN(compressedFile,newTargetPath)
107    
108     def CopyUpMWESFTP(compressedFile,newLocation,mweSftpObj):
109     newTargetPath = os.path.split(newLocation)[0]
110     pathExists = mweSftpObj.TestPath(newTargetPath)
111     if pathExists:
112     pass
113     else:
114     mweSftpObj.CreateNewPath(newTargetPath)
115    
116     drive,null = os.path.splitdrive(compressedFile)
117     if drive.upper() == "C:":
118     print "MCP: moving up %s"%compressedFile
119     print "MCP: to %s"% newLocation
120     mweSftpObj.CopyToDis(compressedFile,newTargetPath)
121     os.remove(compressedFile)
122     else:
123     print "MCP: copying up %s"%compressedFile
124     print "MCP: to %s"% newLocation
125     mweSftpObj.CopyToDis(compressedFile,newTargetPath)
126    
127     def CopyUp(compressedFile, newLocation):
128     ## WARNING! needs to be changes so it increments a dir. What if this dir already exists.
129     if os.path.exists(os.path.split(newLocation)[0]):
130     pass
131     else:
132     os.makedirs(os.path.split(newLocation)[0])
133    
134     ## if the pre archive is on C, move it. Else just copy it.ie a dvd with a zip on it.
135     drive,null = os.path.splitdrive(compressedFile)
136     if drive.upper() == "C:":
137     print "MCP: copying up %s"%compressedFile
138     print "MCP: to %s"% newLocation
139     os.rename(compressedFile, newLocation)
140     else:
141     shutil.copy2(compressedFile, newLocation)
142     print "MCP: moving up %s"%compressedFile
143     print "MCP: to %s"% newLocation
144    
145    
146 nino.borges 458 def Process(caseName, workDir, startDir, platform = 'Concordance DIS', sourceMediaLocation = 'local', uploadType = 'Generic (Old Cases Only)'):
147 ninoborges 8 ##This should be external too.
148     availableShare = "Dis29"
149     expeObj = None
150     mweSftpObj = None
151 nino.borges 235 if sourceMediaLocation == 'local':
152     if platform == 'Concordance DIS':
153     mweSftpObj = MCP_Lib.MweFtpConnection()
154     connectionTest = os.path.isdir(os.path.join(r'\\lisdisdss01',availableShare))
155 ninoborges 8 else:
156 nino.borges 235 expeObj = MCP_Lib.ExpeDatConnection(platform)
157     connectionTest = True
158     if connectionTest:
159     totalSize = NinoGenTools.Counter()
160 nino.borges 457 networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform,uploadType)
161 nino.borges 235 if os.path.isfile(startDir):
162     startDir,indFile = os.path.split(startDir)
163     compressList = [indFile]
164 ninoborges 8 else:
165 nino.borges 235 compressList = os.listdir(startDir)
166     for item in compressList:
167     if item.upper() == 'SYSTEM VOLUME INFORMATION':
168     ## If it's the system folder skip it.
169     ## Consider changing this to a try or to a list of stuff to skip.
170     pass
171 ninoborges 8 else:
172 nino.borges 235 pathToCompress = os.path.join(startDir,item)
173     fileName = item
174     ## TODO: check to see if the file already exists. If so, skip that request and give them a dialog.
175     print "MCP: Compressing %s"% item
176     compressedFile = CompressSelected(pathToCompress, workDir, fileName)
177     #print compressedFile
178     print "MCP: Done!"
179     print "MCP: Gathering size..."
180     if os.path.splitext(fileName)[1].upper() in [".ZIP"]:
181     print "MCP: It's a ZIP, so gathering uncompressed size..."
182     try:
183     tempZipSize = 0
184     tempZip = zipfile.ZipFile(compressedFile,'r')
185     for zipInfo in tempZip.infolist():
186     tempZipSize = tempZipSize + zipInfo.file_size
187     tempZip.close()
188     totalSize.inc(tempZipSize)
189     print "MCP: Uncompressed size gathered."
190     except:
191     print "MCP: Warning: Zip error. Defaulting to compressed size."
192     totalSize.inc(os.path.getsize(compressedFile))
193     else:
194     totalSize.inc(os.path.getsize(compressedFile))
195     print "MCP: Size gathered."
196     print "MCP: Copying up %s"% item
197     #print "comressedFile is %s"% compressedFile
198     ## Copy or move it up.
199     #CopyUp(compressedFile, os.path.join(networkDir+"\\"+time.strftime('%Y%m%d'), os.path.split(compressedFile)[1]))
200     if platform == 'Concordance DIS':
201     ## SFTP coping is now only way.
202     CopyUpMWESFTP(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),mweSftpObj)
203    
204     ## Wan copying totally disabled.
205     ## CopyUp(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]))
206     else:
207     CopyUpRemote(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),expeObj)
208     print "MCP: Done!"
209     print "MCP: Saving sizes..."
210     ReportAndStore(caseName, totalSize.count, platform)
211     print "MCP: Saving complete."
212     print "MCP: All Items processed and copied!"
213     else:
214     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"
215 ninoborges 8 else:
216 nino.borges 235 ## User is copying from LN to LN
217     expeObj = MCP_Lib.ExpeDatConnection(platform)
218 nino.borges 240 networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform)
219     print "MCP: Gathering size..."
220 nino.borges 238 totalSize = expeObj.GatherSize(os.path.join('Uploads',startDir))
221 nino.borges 240 print "MCP: Size gathered."
222 nino.borges 241 print "MCP: Moving up %s"% os.path.join('Uploads',startDir)
223 nino.borges 242 ## need to change this to put the file name or folder after networkDir
224 nino.borges 258 CopyLNToLN(os.path.join('Uploads',startDir),networkDir,expeObj)
225     #expeObj.MoveOnLN(os.path.join('Uploads',startDir),os.path.join(networkDir,os.path.split(startDir)[1]))
226     #print "the file would have been moved to\n%s"%os.path.join(networkDir,os.path.split(startDir)[1])
227 nino.borges 240 print "MCP: Done!"
228     #print totalSize
229     print "MCP: Saving sizes..."
230 nino.borges 258 ReportAndStore(caseName, int(totalSize), platform)
231 nino.borges 240 print "MCP: Saving complete."
232     ## Verify that it's there
233     print "MCP: All Items processed and copied!"
234 nino.borges 235
235 ninoborges 8 if mweSftpObj:
236     mweSftpObj.CloseConnection()
237     raw_input("\n\nMCP: Press Enter key to exit.\n\n\tEnd Of Line.\n")
238    
239 nino.borges 458 def GetCopyToLocation(caseName, availableShare,expeObj,platform = 'Concordance DIS', uploadType = 'Generic (Old Cases Only)'):
240 ninoborges 8 clientMatter = caseName.split("_(")[1]
241     clientMatter = clientMatter[:-1]
242     if platform == 'Concordance DIS':
243     parsedPath = ConcordanceHelperTools.ParseClientMatter(clientMatter)
244     parsedClientMatter = os.path.split(parsedPath)[1]
245     copyToLocation = os.path.join(r"\\lisdisdss01\%s\C_D"%availableShare,parsedClientMatter)
246    
247     copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
248    
249     ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
250     dirIncrement = NinoGenTools.Counter(1)
251     if os.path.exists(copyToLocation):
252     copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
253     dirIncrement.inc()
254     while os.path.exists(copyToLocation):
255     copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
256     dirIncrement.inc()
257    
258     ## Now that it's SFTP only, remove the part before c_d
259     copyToLocation = copyToLocation.split("\\\\lisdisdss01\\%s\\"%availableShare)[1]
260     else:
261 nino.borges 458 uploadTypeMatrix = {'Processed Review Data':'Processed_Data\\rev','Hard Copy Review Data':'Hard_Copy\\HC','Loose Unprocessed Review Data':'Loose\\loose','Subset Tiffs':'TIFs\\subset','Other Party Production Data':'Productions\\Other_Party\\[[Party_Name]]','Generic (Old Cases Only)':'Working' }
262 ninoborges 8 parsedPath = ConcordanceHelperTools.ParseClientMatterRemote(clientMatter)
263     copyToLocation = parsedPath
264 nino.borges 457 ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
265     dirIncrement = NinoGenTools.Counter(1)
266 ninoborges 8
267 nino.borges 458 if uploadType == 'Generic (Old Cases Only)':
268 nino.borges 457 #copyToLocation = os.path.join(os.path.join(copyToLocation,uploadTypeMatrix[uploadType]),time.strftime('%Y%m%d'))
269     copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
270     if expeObj.TestPath(copyToLocation) == True:
271     copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
272     dirIncrement.inc()
273     else:
274     copyToLocation = os.path.join(copyToLocation,uploadTypeMatrix[uploadType]+'_0001')
275 ninoborges 8
276     ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
277 nino.borges 457 #dirIncrement = NinoGenTools.Counter(1)
278     ## For new version of MCP, for it to 0001
279     #if expeObj.TestPath(copyToLocation) == True:
280     # copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
281     # dirIncrement.inc()
282 ninoborges 8 while expeObj.TestPath(copyToLocation) == True:
283     copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
284     dirIncrement.inc()
285    
286     return copyToLocation
287    
288     if __name__ == '__main__':
289     caseName = "DaVita-Dallas_(039323-0308)"
290     workDir = r"C:\Documents and Settings\eborges\Desktop\Working"
291     #workDir = r"E:"
292     startDir = r"C:\Documents and Settings\eborges\Desktop\Working\STCL_EML_IMAGE_1101.zip"
293     Process(caseName, workDir, startDir)
294    
295    
296    
297