ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/ns_dev/Python/NinoCode/Active_prgs/Syncor.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: 7092 byte(s)
Log Message:
Initial Import

File Contents

# User Rev Content
1 ninoborges 8 ## Syncor.py
2     ## This program will sycronize two folders, its subfolders and propmt the user with the
3     ## differences between them. The prompt will allow the user to chose to sync particular
4     ## files.
5     #Eborges
6     #11.17.02
7    
8     import os, sys, Pmw, time
9    
10     def Main(LogFile,RecentFile):
11     root = Pmw.initialise()
12     MainNb = Pmw.NoteBook(root)
13     MainNb.pack()
14     SyncTab = MainNb.add('Syncronize')
15     MainNb.tab('Syncronize').focus_set()
16     RecentTab = MainNb.add('Recent Syncs')
17     groupR = Pmw.Group(RecentTab, tag_text = 'Recent Sync List')
18     groupR.pack(fill = 'both',expand = 1, padx = 10, pady = 10)
19     RecentItems = GetHistList(RecentFile)
20     RecentDisplay = Pmw.ScrolledListBox(groupR.interior(),items = RecentItems)
21     LogTab = MainNb.add('Log')
22     groupL = Pmw.Group(LogTab, tag_text = 'History Log')
23     groupL.pack(fill = 'both',expand = 1, padx = 10, pady = 10)
24     LogDisplay = Pmw.ScrolledText(groupL.interior())
25     LogDisplay.importfile(LogFile)
26     LogDisplay.pack(padx = 10, pady = 10)
27     LogDisplay.configure(text_state = 'disabled')
28     group = Pmw.Group(SyncTab, tag_text = 'Syncronize')
29     group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
30     LocationA = Pmw.EntryField(group.interior(), labelpos = 'w',label_text = 'First Location: ')
31     LocationB = Pmw.EntryField(group.interior(), labelpos = 'w',label_text = 'Second Location: ')
32     LocationA.pack(padx = 20, pady = 10, anchor="w")
33     LocationB.pack(padx = 20, pady = 10, anchor="w")
34     buttonBoxA = Pmw.ButtonBox(group.interior())
35     buttonBoxA.pack()
36     buttonBoxA.add('OK',command =
37     (lambda LocationAObj = LocationA, LocationBObj = LocationB,
38     LogObj = LogDisplay, Logfile = LogFile, RecentListObj = RecentDisplay, Recentfile = RecentFile:
39     runProcess(LocationAObj,LocationBObj,LogObj,Logfile,RecentListObj,Recentfile)))
40     buttonBoxA.add('Cancel',command = root.destroy)
41     RecentDisplay.configure(dblclickcommand=
42     (lambda LocationBObj = LocationB,
43     LocationAObj = LocationA, MainNbObj = MainNb, self =
44     RecentDisplay:SelectFromList(self,MainNbObj,LocationAObj, LocationBObj)))
45     RecentDisplay.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
46     root.title('SyncIT')
47     root.mainloop()
48    
49     def runProcess(LocationAObj,LocationBObj,LogObj,LogFile,RecentListObj,RecentFile):
50     Today = time.asctime()
51     Dir1 = LocationAObj.get()
52     Dir2 = LocationBObj.get()
53     #print "Syncing %s to %s..."%(Dir1,Dir2)
54     compareDirs(Dir1,Dir2)
55     LogOutput = "On %s you synced %s to %s\n" %(Today,Dir1,Dir2)
56     RecentOutput = "%sTO %s\n" %(Dir1,Dir2)
57     UpdateHistList(LogOutput,LogFile,LogObj)
58     UpdateRecentList(RecentOutput,RecentFile,RecentListObj)
59    
60    
61     def SelectFromList(self,MainNbObj,LocationAObj,LocationBObj):
62     Selection = self.getcurselection()
63     Selection = Selection[0]
64     Selection = Selection.split('TO')
65     FirstDir = Selection[0]
66     SecondDir = Selection[1]
67     SecondDir = SecondDir.strip()
68     MainNbObj.selectpage('Syncronize')
69     LocationAObj.setentry(FirstDir)
70     LocationBObj.setentry(SecondDir)
71    
72     def GetHistList(File):
73     o=open(File,'r')
74     output = o.readlines()
75     o.close()
76     output = tuple(output)
77     return output
78    
79     def UpdateHistList(Output,LogFile,LogObj):
80     w=open(LogFile,'a')
81     w.write(Output)
82     w.close()
83     #LogObj.clear()
84     LogObj.importfile(LogFile)
85    
86     def UpdateRecentList(Output,RecentFile,RecentListObj):
87     w=open(RecentFile,'r+')
88     contents = w.readlines()
89     if Output in contents:
90     pass
91     #print "yup"
92     else:
93     w.write(Output)
94     w.close()
95     RecentItems = GetHistList(RecentFile)
96     RecentListObj.setlist(RecentItems)
97    
98     # Actuall comparing section
99    
100     def reportDiffs(unique1,unique2, dir1, dir2):
101     if not (unique1 or unique2):
102     print "directory lists are identical"
103     else:
104     if unique1:
105     print "Files unique to", dir1
106     for file in unique1:
107     print "...", file
108     whatToDo = raw_input('press y to continue ')
109     if unique2:
110     print "files unique to", dir2
111     for file in unique2:
112     print "...", file
113     whatToDo = raw_input('press y to continue ')
114    
115     def unique(seq1, seq2):
116     uniques = []
117     for item in seq1:
118     if item not in seq2:
119     uniques.append(item)
120     return uniques
121    
122     def intersect(seq1, seq2):
123     commons = []
124     for item in seq1:
125     if item in seq2:
126     commons.append(item)
127     return commons
128    
129     def compareDirs(dir1,dir2):
130     print "Comparing" , dir1 , "to", dir2
131     diffs = []
132     files1 = os.listdir(dir1)
133     # JUST FOR THIS TEST
134     newfiles1 = []
135     newfiles2 = []
136     for f in files1:
137     newfiles1.append(f.split('.')[0])
138     files2 = os.listdir(dir2)
139     for g in files2:
140     newfiles2.append(g.split('.')[0])
141     #unique1 = unique(files1, files2)
142     #unique2 = unique(files2, files1)
143     #common = intersect(files1, files2)
144     unique1 = unique(newfiles1, newfiles2)
145     unique2 = unique(newfiles2, newfiles1)
146     common = intersect(newfiles1, newfiles2)
147     if common:
148     for file in common:
149     path1 = os.path.join(dir1, file)
150     path2 = os.path.join(dir2, file)
151     if os.path.isfile(path1) and os.path.isfile(path2):
152     bytes1 = open(path1, 'rb').read()
153     bytes2 = open(path2, 'rb').read()
154     if bytes1 == bytes2:
155     print file, 'matches'
156     else:
157     diffs.append('files differ at %s - %s' %(path1,path2))
158     print file, 'Differs'
159     whatToDo = raw_input('press y to continue ')
160     for file in common:
161     path1 = os.path.join(dir1,file)
162     path2 = os.path.join(dir2,file)
163     if os.path.isdir(path1) and os.path.isdir(path2):
164     compareDirs(path1,path2)
165    
166     reportDiffs(unique1, unique2, dir1, dir2)
167     return not (unique1 or unique2)
168    
169    
170     if __name__ == '__main__':
171     WorkingDir = os.path.exists(r'C:\Documents and Settings\eaborges\My Documents\Python DataFiles\SyncIt')
172     LogFile = r'C:\Documents and Settings\eaborges\My Documents\Python DataFiles\SyncIt\SyncIt.log'
173     RecentFile = r'C:\Documents and Settings\eaborges\My Documents\Python DataFiles\SyncIt\Recent.dat'
174     if WorkingDir == 1:
175     Main(LogFile,RecentFile)
176     #GetHistList(RecentFile)
177     else:
178     os.makedirs(r'C:\Documents and Settings\eaborges\My Documents\Python DataFiles\SyncIt')
179     L = open(r'C:\Documents and Settings\eaborges\My Documents\Python DataFiles\SyncIt\SyncIt.log','w')
180     L.close()
181     R = open(r'C:\Documents and Settings\eaborges\My Documents\Python DataFiles\SyncIt\Recent.dat','w')
182     R.close()
183     Main(LogFile,RecentFile)