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: 234
Committed: Wed Dec 19 21:03:55 2012 UTC (13 years, 3 months ago) by nino.borges
Content type: text/x-python
Original Path: Python/NinoCode/Active_prgs/MCP/MCP_CopyUp_Request.py
File size: 11106 byte(s)
Log Message:
MCP: Changed copy up so that it only does the connection test if it's going to copy to the DIS

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