ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/MCP/Retired/MCP_CopyUp_Request.py
Revision: 8
Committed: Sat May 5 04:21:19 2012 UTC (13 years, 10 months ago) by ninoborges
Content type: text/x-python
File size: 6234 byte(s)
Log Message:
Initial Import

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     """
14    
15     #import win32api
16     import os, tarfile, time, ConcordanceHelperTools, NinoGenTools, shutil,zipfile,MCP_Lib
17     from directorySize2 import pretty_filesize2
18    
19    
20     def ReportAndStore(caseName, totalSize):
21     reportSize = pretty_filesize2(totalSize)
22     #casesDir = r"C:\Documents and Settings\eborges\My Documents\My Dropbox\Documents\Cases"
23     casesList,casesDir = MCP_Lib.GetCaseList()
24     casePath = os.path.join(casesDir,caseName)
25     outputFile = open(os.path.join(casePath,"UploadReport.txt"),'a')
26     outputFile.write(time.strftime('%Y%m%d') + " | " +reportSize + "\n")
27     outputFile.close()
28    
29     def CompressSelected(pathToCompress, workDir, fileName):
30     ## check available space. Acutally this could take too long to calculate the source media.
31     ## warn them to check the available size, for now.
32     #availSize = win32api.GetDiskFreeSpaceEx(rootPath)
33     ## create the tar
34     if os.path.splitext(fileName)[1].upper() in [".ZIP", ".RAR",".TAR"]:
35     print "MCP: It's already compressed. Skipping step."
36     tFileName = pathToCompress
37     else:
38     #print "this is second workdir %s"% workDir
39     tFileName = os.path.join(workDir , fileName) +".tar"
40     tFile = tarfile.open(tFileName,'w')
41     tFile.add(pathToCompress,recursive = True)
42     tFile.close()
43     return tFileName
44    
45    
46     def CopyUp(compressedFile, newLocation):
47     ## WARNING! needs to be changes so it increments a dir. What if this dir already exists.
48     if os.path.exists(os.path.split(newLocation)[0]):
49     pass
50     else:
51     os.makedirs(os.path.split(newLocation)[0])
52    
53     ## if the pre archive is on C, move it. Else just copy it.ie a dvd with a zip on it.
54     drive,null = os.path.splitdrive(compressedFile)
55     if drive.upper() == "C:":
56     print "MCP: copying up %s"%compressedFile
57     print "MCP: to %s"% newLocation
58     os.rename(compressedFile, newLocation)
59     else:
60     shutil.copy2(compressedFile, newLocation)
61     print "MCP: moving up %s"%compressedFile
62     print "MCP: to %s"% newLocation
63    
64    
65     def Process(caseName, workDir, startDir):
66     ##This should be external too.
67     availableShare = "Dis27"
68     connectionTest = os.path.isdir(os.path.join(r'\\lisdisdss01',availableShare))
69     if connectionTest:
70     totalSize = NinoGenTools.Counter()
71     networkDir = GetCopyToLocation(caseName, availableShare)
72     if os.path.isfile(startDir):
73     startDir,indFile = os.path.split(startDir)
74     compressList = [indFile]
75     else:
76     compressList = os.listdir(startDir)
77     for item in compressList:
78     pathToCompress = os.path.join(startDir,item)
79     fileName = item
80     ## TODO: check to see if the file already exists. If so, skip that request and give them a dialog.
81     print "MCP: Compressing %s"% item
82     compressedFile = CompressSelected(pathToCompress, workDir, fileName)
83     #print compressedFile
84     print "MCP: Done!"
85     print "MCP: Gathering size..."
86     if os.path.splitext(fileName)[1].upper() in [".ZIP"]:
87     print "MCP: It's a ZIP, so gathering uncompressed size..."
88     try:
89     tempZipSize = 0
90     tempZip = zipfile.ZipFile(compressedFile,'r')
91     for zipInfo in tempZip.infolist():
92     tempZipSize = tempZipSize + zipInfo.file_size
93     tempZip.close()
94     totalSize.inc(tempZipSize)
95     print "MCP: Uncompressed size gathered."
96     except:
97     print "MCP: Warning: Zip error. Defaulting to compressed size."
98     totalSize.inc(os.path.getsize(compressedFile))
99     else:
100     totalSize.inc(os.path.getsize(compressedFile))
101     print "MCP: Size gathered."
102     print "MCP: Copying up %s"% item
103     #print "comressedFile is %s"% compressedFile
104     ## Copy or move it up.
105     #CopyUp(compressedFile, os.path.join(networkDir+"\\"+time.strftime('%Y%m%d'), os.path.split(compressedFile)[1]))
106     CopyUp(compressedFile, os.path.join(networkDir, os.path.split(compressedFile)[1]))
107     print "MCP: Done!"
108     print "MCP: Saving sizes..."
109     ReportAndStore(caseName, totalSize.count)
110     print "MCP: Saving complete."
111     print "MCP: All Items processed and copied!"
112     else:
113     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"
114     raw_input("\n\nMCP: Press Enter key to exit.\n\n\tEnd Of Line.\n")
115    
116     def GetCopyToLocation(caseName, availableShare):
117     clientMatter = caseName.split("_(")[1]
118     clientMatter = clientMatter[:-1]
119     parsedPath = ConcordanceHelperTools.ParseClientMatter(clientMatter)
120     parsedClientMatter = os.path.split(parsedPath)[1]
121     copyToLocation = os.path.join(r"\\lisdisdss01\%s\C_D"%availableShare,parsedClientMatter)
122     copyToLocation = os.path.join(copyToLocation,time.strftime('%Y%m%d'))
123     ## Check to see if this path already exists. If it does, keep incrementing it until it dosent.
124     dirIncrement = NinoGenTools.Counter(1)
125     if os.path.exists(copyToLocation):
126     copyToLocation = copyToLocation+ "_"+"%0*d"%(4,dirIncrement.count)
127     dirIncrement.inc()
128     while os.path.exists(copyToLocation):
129     copyToLocation = copyToLocation[:-4] +"%0*d"%(4,dirIncrement.count)
130     dirIncrement.inc()
131    
132     return copyToLocation
133    
134     if __name__ == '__main__':
135     caseName = "DaVita-Dallas_(039323-0308)"
136     workDir = r"C:\Documents and Settings\eborges\Desktop\Working"
137     #workDir = r"E:"
138     startDir = r"C:\Documents and Settings\eborges\Desktop\Working\STCL_EML_IMAGE_1101.zip"
139     Process(caseName, workDir, startDir)
140    
141    
142    
143