ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/MCP/MCP_CopyUp_Request.py
Revision: 241
Committed: Wed Dec 19 22:42:01 2012 UTC (13 years, 3 months ago) by nino.borges
Content type: text/x-python
File size: 12217 byte(s)
Log Message:
MCP: save

File Contents

# Content
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 CopyUpRemote(compressedFile,newLocation,expeObj):
61 newTargetPath = os.path.split(newLocation)[0]
62 pathExists = expeObj.TestPath(newTargetPath)
63 tempPath = newTargetPath
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 for pathPiece in pathRemainingList:
72 tempPath = os.path.join(tempPath,pathPiece)
73 expeObj.CreateNewPath(tempPath)
74
75
76 drive,null = os.path.splitdrive(compressedFile)
77 if drive.upper() == "C:":
78 print "MCP: moving up %s"%compressedFile
79 print "MCP: to %s"% newLocation
80 errCode = expeObj.CopyToLN(compressedFile,newTargetPath)
81 if errCode == True:
82 os.remove(compressedFile)
83 else:
84 print "MCP: copying up %s"%compressedFile
85 print "MCP: to %s"% newLocation
86 errCode = expeObj.CopyToLN(compressedFile,newTargetPath)
87
88 def CopyUpMWESFTP(compressedFile,newLocation,mweSftpObj):
89 newTargetPath = os.path.split(newLocation)[0]
90 pathExists = mweSftpObj.TestPath(newTargetPath)
91 if pathExists:
92 pass
93 else:
94 mweSftpObj.CreateNewPath(newTargetPath)
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 mweSftpObj.CopyToDis(compressedFile,newTargetPath)
101 os.remove(compressedFile)
102 else:
103 print "MCP: copying up %s"%compressedFile
104 print "MCP: to %s"% newLocation
105 mweSftpObj.CopyToDis(compressedFile,newTargetPath)
106
107 def CopyUp(compressedFile, newLocation):
108 ## WARNING! needs to be changes so it increments a dir. What if this dir already exists.
109 if os.path.exists(os.path.split(newLocation)[0]):
110 pass
111 else:
112 os.makedirs(os.path.split(newLocation)[0])
113
114 ## if the pre archive is on C, move it. Else just copy it.ie a dvd with a zip on it.
115 drive,null = os.path.splitdrive(compressedFile)
116 if drive.upper() == "C:":
117 print "MCP: copying up %s"%compressedFile
118 print "MCP: to %s"% newLocation
119 os.rename(compressedFile, newLocation)
120 else:
121 shutil.copy2(compressedFile, newLocation)
122 print "MCP: moving up %s"%compressedFile
123 print "MCP: to %s"% newLocation
124
125
126 def Process(caseName, workDir, startDir, platform = 'Concordance DIS', sourceMediaLocation = 'local'):
127 ##This should be external too.
128 availableShare = "Dis29"
129 expeObj = None
130 mweSftpObj = None
131 if sourceMediaLocation == 'local':
132 if platform == 'Concordance DIS':
133 mweSftpObj = MCP_Lib.MweFtpConnection()
134 connectionTest = os.path.isdir(os.path.join(r'\\lisdisdss01',availableShare))
135 else:
136 expeObj = MCP_Lib.ExpeDatConnection(platform)
137 connectionTest = True
138 if connectionTest:
139 totalSize = NinoGenTools.Counter()
140 networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform)
141 if os.path.isfile(startDir):
142 startDir,indFile = os.path.split(startDir)
143 compressList = [indFile]
144 else:
145 compressList = os.listdir(startDir)
146 for item in compressList:
147 if item.upper() == 'SYSTEM VOLUME INFORMATION':
148 ## If it's the system folder skip it.
149 ## Consider changing this to a try or to a list of stuff to skip.
150 pass
151 else:
152 pathToCompress = os.path.join(startDir,item)
153 fileName = item
154 ## TODO: check to see if the file already exists. If so, skip that request and give them a dialog.
155 print "MCP: Compressing %s"% item
156 compressedFile = CompressSelected(pathToCompress, workDir, fileName)
157 #print compressedFile
158 print "MCP: Done!"
159 print "MCP: Gathering size..."
160 if os.path.splitext(fileName)[1].upper() in [".ZIP"]:
161 print "MCP: It's a ZIP, so gathering uncompressed size..."
162 try:
163 tempZipSize = 0
164 tempZip = zipfile.ZipFile(compressedFile,'r')
165 for zipInfo in tempZip.infolist():
166 tempZipSize = tempZipSize + zipInfo.file_size
167 tempZip.close()
168 totalSize.inc(tempZipSize)
169 print "MCP: Uncompressed size gathered."
170 except:
171 print "MCP: Warning: Zip error. Defaulting to compressed size."
172 totalSize.inc(os.path.getsize(compressedFile))
173 else:
174 totalSize.inc(os.path.getsize(compressedFile))
175 print "MCP: Size gathered."
176 print "MCP: Copying up %s"% item
177 #print "comressedFile is %s"% compressedFile
178 ## Copy or move it up.
179 #CopyUp(compressedFile, os.path.join(networkDir+"\\"+time.strftime('%Y%m%d'), os.path.split(compressedFile)[1]))
180 if platform == 'Concordance DIS':
181 ## SFTP coping is now only way.
182 CopyUpMWESFTP(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),mweSftpObj)
183
184 ## Wan copying totally disabled.
185 ## CopyUp(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]))
186 else:
187 CopyUpRemote(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),expeObj)
188 print "MCP: Done!"
189 print "MCP: Saving sizes..."
190 ReportAndStore(caseName, totalSize.count, platform)
191 print "MCP: Saving complete."
192 print "MCP: All Items processed and copied!"
193 else:
194 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"
195 else:
196 ## User is copying from LN to LN
197 expeObj = MCP_Lib.ExpeDatConnection(platform)
198 networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform)
199 print "MCP: Gathering size..."
200 totalSize = expeObj.GatherSize(os.path.join('Uploads',startDir))
201 print "MCP: Size gathered."
202 print "MCP: Moving up %s"% os.path.join('Uploads',startDir)
203 ## move the file
204 print "the file would have been moved to\n%s"%networkDir
205 print "MCP: Done!"
206 #print totalSize
207 print "MCP: Saving sizes..."
208 ReportAndStore(caseName, totalSize, platform)
209 print "MCP: Saving complete."
210 ## Verify that it's there
211 print "MCP: All Items processed and copied!"
212
213 if mweSftpObj:
214 mweSftpObj.CloseConnection()
215 raw_input("\n\nMCP: Press Enter key to exit.\n\n\tEnd Of Line.\n")
216
217 def GetCopyToLocation(caseName, availableShare,expeObj,platform = 'Concordance DIS'):
218 clientMatter = caseName.split("_(")[1]
219 clientMatter = clientMatter[:-1]
220 if platform == 'Concordance DIS':
221 parsedPath = ConcordanceHelperTools.ParseClientMatter(clientMatter)
222 parsedClientMatter = os.path.split(parsedPath)[1]
223 copyToLocation = os.path.join(r"\\lisdisdss01\%s\C_D"%availableShare,parsedClientMatter)
224
225 copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
226
227 ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
228 dirIncrement = NinoGenTools.Counter(1)
229 if os.path.exists(copyToLocation):
230 copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
231 dirIncrement.inc()
232 while os.path.exists(copyToLocation):
233 copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
234 dirIncrement.inc()
235
236 ## Now that it's SFTP only, remove the part before c_d
237 copyToLocation = copyToLocation.split("\\\\lisdisdss01\\%s\\"%availableShare)[1]
238 else:
239 parsedPath = ConcordanceHelperTools.ParseClientMatterRemote(clientMatter)
240 copyToLocation = parsedPath
241
242 copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
243
244 ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
245 dirIncrement = NinoGenTools.Counter(1)
246 if expeObj.TestPath(copyToLocation) == True:
247 copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
248 dirIncrement.inc()
249 while expeObj.TestPath(copyToLocation) == True:
250 copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
251 dirIncrement.inc()
252
253 return copyToLocation
254
255 if __name__ == '__main__':
256 caseName = "DaVita-Dallas_(039323-0308)"
257 workDir = r"C:\Documents and Settings\eborges\Desktop\Working"
258 #workDir = r"E:"
259 startDir = r"C:\Documents and Settings\eborges\Desktop\Working\STCL_EML_IMAGE_1101.zip"
260 Process(caseName, workDir, startDir)
261
262
263
264