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: 189
Committed: Fri Nov 2 15:18:39 2012 UTC (13 years, 4 months ago) by nino.borges
Content type: text/x-python
File size: 11071 byte(s)
Log Message:
Added backend 'view my case notes' ability to view edit and added same to ui and back end of copyUp.

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'):
127 ##This should be external too.
128 availableShare = "Dis29"
129 expeObj = None
130 mweSftpObj = None
131 connectionTest = os.path.isdir(os.path.join(r'\\lisdisdss01',availableShare))
132 if platform == 'Concordance DIS':
133 mweSftpObj = MCP_Lib.MweFtpConnection()
134 else:
135 expeObj = MCP_Lib.ExpeDatConnection(platform)
136 if connectionTest:
137 totalSize = NinoGenTools.Counter()
138 networkDir = GetCopyToLocation(caseName, availableShare,expeObj,platform)
139 if os.path.isfile(startDir):
140 startDir,indFile = os.path.split(startDir)
141 compressList = [indFile]
142 else:
143 compressList = os.listdir(startDir)
144 for item in compressList:
145 if item.upper() == 'SYSTEM VOLUME INFORMATION':
146 ## If it's the system folder skip it.
147 ## Consider changing this to a try or to a list of stuff to skip.
148 pass
149 else:
150 pathToCompress = os.path.join(startDir,item)
151 fileName = item
152 ## TODO: check to see if the file already exists. If so, skip that request and give them a dialog.
153 print "MCP: Compressing %s"% item
154 compressedFile = CompressSelected(pathToCompress, workDir, fileName)
155 #print compressedFile
156 print "MCP: Done!"
157 print "MCP: Gathering size..."
158 if os.path.splitext(fileName)[1].upper() in [".ZIP"]:
159 print "MCP: It's a ZIP, so gathering uncompressed size..."
160 try:
161 tempZipSize = 0
162 tempZip = zipfile.ZipFile(compressedFile,'r')
163 for zipInfo in tempZip.infolist():
164 tempZipSize = tempZipSize + zipInfo.file_size
165 tempZip.close()
166 totalSize.inc(tempZipSize)
167 print "MCP: Uncompressed size gathered."
168 except:
169 print "MCP: Warning: Zip error. Defaulting to compressed size."
170 totalSize.inc(os.path.getsize(compressedFile))
171 else:
172 totalSize.inc(os.path.getsize(compressedFile))
173 print "MCP: Size gathered."
174 print "MCP: Copying up %s"% item
175 #print "comressedFile is %s"% compressedFile
176 ## Copy or move it up.
177 #CopyUp(compressedFile, os.path.join(networkDir+"\\"+time.strftime('%Y%m%d'), os.path.split(compressedFile)[1]))
178 if platform == 'Concordance DIS':
179 ## SFTP coping is now only way.
180 CopyUpMWESFTP(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),mweSftpObj)
181
182 ## Wan copying totally disabled.
183 ## CopyUp(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]))
184 else:
185 CopyUpRemote(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]),expeObj)
186 print "MCP: Done!"
187 print "MCP: Saving sizes..."
188 ReportAndStore(caseName, totalSize.count, platform)
189 print "MCP: Saving complete."
190 print "MCP: All Items processed and copied!"
191 else:
192 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"
193 if mweSftpObj:
194 mweSftpObj.CloseConnection()
195 raw_input("\n\nMCP: Press Enter key to exit.\n\n\tEnd Of Line.\n")
196
197 def GetCopyToLocation(caseName, availableShare,expeObj,platform = 'Concordance DIS'):
198 clientMatter = caseName.split("_(")[1]
199 clientMatter = clientMatter[:-1]
200 if platform == 'Concordance DIS':
201 parsedPath = ConcordanceHelperTools.ParseClientMatter(clientMatter)
202 parsedClientMatter = os.path.split(parsedPath)[1]
203 copyToLocation = os.path.join(r"\\lisdisdss01\%s\C_D"%availableShare,parsedClientMatter)
204
205 copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
206
207 ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
208 dirIncrement = NinoGenTools.Counter(1)
209 if os.path.exists(copyToLocation):
210 copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
211 dirIncrement.inc()
212 while os.path.exists(copyToLocation):
213 copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
214 dirIncrement.inc()
215
216 ## Now that it's SFTP only, remove the part before c_d
217 copyToLocation = copyToLocation.split("\\\\lisdisdss01\\%s\\"%availableShare)[1]
218 else:
219 parsedPath = ConcordanceHelperTools.ParseClientMatterRemote(clientMatter)
220 copyToLocation = parsedPath
221
222 copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
223
224 ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
225 dirIncrement = NinoGenTools.Counter(1)
226 if expeObj.TestPath(copyToLocation) == True:
227 copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
228 dirIncrement.inc()
229 while expeObj.TestPath(copyToLocation) == True:
230 copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
231 dirIncrement.inc()
232
233 return copyToLocation
234
235 if __name__ == '__main__':
236 caseName = "DaVita-Dallas_(039323-0308)"
237 workDir = r"C:\Documents and Settings\eborges\Desktop\Working"
238 #workDir = r"E:"
239 startDir = r"C:\Documents and Settings\eborges\Desktop\Working\STCL_EML_IMAGE_1101.zip"
240 Process(caseName, workDir, startDir)
241
242
243
244