1 | n/a | #! /usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """Script to synchronize two source trees. |
---|
4 | n/a | |
---|
5 | n/a | Invoke with two arguments: |
---|
6 | n/a | |
---|
7 | n/a | python treesync.py slave master |
---|
8 | n/a | |
---|
9 | n/a | The assumption is that "master" contains CVS administration while |
---|
10 | n/a | slave doesn't. All files in the slave tree that have a CVS/Entries |
---|
11 | n/a | entry in the master tree are synchronized. This means: |
---|
12 | n/a | |
---|
13 | n/a | If the files differ: |
---|
14 | n/a | if the slave file is newer: |
---|
15 | n/a | normalize the slave file |
---|
16 | n/a | if the files still differ: |
---|
17 | n/a | copy the slave to the master |
---|
18 | n/a | else (the master is newer): |
---|
19 | n/a | copy the master to the slave |
---|
20 | n/a | |
---|
21 | n/a | normalizing the slave means replacing CRLF with LF when the master |
---|
22 | n/a | doesn't use CRLF |
---|
23 | n/a | |
---|
24 | n/a | """ |
---|
25 | n/a | |
---|
26 | n/a | import os, sys, stat, getopt |
---|
27 | n/a | |
---|
28 | n/a | # Interactivity options |
---|
29 | n/a | default_answer = "ask" |
---|
30 | n/a | create_files = "yes" |
---|
31 | n/a | create_directories = "no" |
---|
32 | n/a | write_slave = "ask" |
---|
33 | n/a | write_master = "ask" |
---|
34 | n/a | |
---|
35 | n/a | def main(): |
---|
36 | n/a | global always_no, always_yes |
---|
37 | n/a | global create_directories, write_master, write_slave |
---|
38 | n/a | opts, args = getopt.getopt(sys.argv[1:], "nym:s:d:f:a:") |
---|
39 | n/a | for o, a in opts: |
---|
40 | n/a | if o == '-y': |
---|
41 | n/a | default_answer = "yes" |
---|
42 | n/a | if o == '-n': |
---|
43 | n/a | default_answer = "no" |
---|
44 | n/a | if o == '-s': |
---|
45 | n/a | write_slave = a |
---|
46 | n/a | if o == '-m': |
---|
47 | n/a | write_master = a |
---|
48 | n/a | if o == '-d': |
---|
49 | n/a | create_directories = a |
---|
50 | n/a | if o == '-f': |
---|
51 | n/a | create_files = a |
---|
52 | n/a | if o == '-a': |
---|
53 | n/a | create_files = create_directories = write_slave = write_master = a |
---|
54 | n/a | try: |
---|
55 | n/a | [slave, master] = args |
---|
56 | n/a | except ValueError: |
---|
57 | n/a | print("usage: python", sys.argv[0] or "treesync.py", end=' ') |
---|
58 | n/a | print("[-n] [-y] [-m y|n|a] [-s y|n|a] [-d y|n|a] [-f n|y|a]", end=' ') |
---|
59 | n/a | print("slavedir masterdir") |
---|
60 | n/a | return |
---|
61 | n/a | process(slave, master) |
---|
62 | n/a | |
---|
63 | n/a | def process(slave, master): |
---|
64 | n/a | cvsdir = os.path.join(master, "CVS") |
---|
65 | n/a | if not os.path.isdir(cvsdir): |
---|
66 | n/a | print("skipping master subdirectory", master) |
---|
67 | n/a | print("-- not under CVS") |
---|
68 | n/a | return |
---|
69 | n/a | print("-"*40) |
---|
70 | n/a | print("slave ", slave) |
---|
71 | n/a | print("master", master) |
---|
72 | n/a | if not os.path.isdir(slave): |
---|
73 | n/a | if not okay("create slave directory %s?" % slave, |
---|
74 | n/a | answer=create_directories): |
---|
75 | n/a | print("skipping master subdirectory", master) |
---|
76 | n/a | print("-- no corresponding slave", slave) |
---|
77 | n/a | return |
---|
78 | n/a | print("creating slave directory", slave) |
---|
79 | n/a | try: |
---|
80 | n/a | os.mkdir(slave) |
---|
81 | n/a | except OSError as msg: |
---|
82 | n/a | print("can't make slave directory", slave, ":", msg) |
---|
83 | n/a | return |
---|
84 | n/a | else: |
---|
85 | n/a | print("made slave directory", slave) |
---|
86 | n/a | cvsdir = None |
---|
87 | n/a | subdirs = [] |
---|
88 | n/a | names = os.listdir(master) |
---|
89 | n/a | for name in names: |
---|
90 | n/a | mastername = os.path.join(master, name) |
---|
91 | n/a | slavename = os.path.join(slave, name) |
---|
92 | n/a | if name == "CVS": |
---|
93 | n/a | cvsdir = mastername |
---|
94 | n/a | else: |
---|
95 | n/a | if os.path.isdir(mastername) and not os.path.islink(mastername): |
---|
96 | n/a | subdirs.append((slavename, mastername)) |
---|
97 | n/a | if cvsdir: |
---|
98 | n/a | entries = os.path.join(cvsdir, "Entries") |
---|
99 | n/a | for e in open(entries).readlines(): |
---|
100 | n/a | words = e.split('/') |
---|
101 | n/a | if words[0] == '' and words[1:]: |
---|
102 | n/a | name = words[1] |
---|
103 | n/a | s = os.path.join(slave, name) |
---|
104 | n/a | m = os.path.join(master, name) |
---|
105 | n/a | compare(s, m) |
---|
106 | n/a | for (s, m) in subdirs: |
---|
107 | n/a | process(s, m) |
---|
108 | n/a | |
---|
109 | n/a | def compare(slave, master): |
---|
110 | n/a | try: |
---|
111 | n/a | sf = open(slave, 'r') |
---|
112 | n/a | except IOError: |
---|
113 | n/a | sf = None |
---|
114 | n/a | try: |
---|
115 | n/a | mf = open(master, 'rb') |
---|
116 | n/a | except IOError: |
---|
117 | n/a | mf = None |
---|
118 | n/a | if not sf: |
---|
119 | n/a | if not mf: |
---|
120 | n/a | print("Neither master nor slave exists", master) |
---|
121 | n/a | return |
---|
122 | n/a | print("Creating missing slave", slave) |
---|
123 | n/a | copy(master, slave, answer=create_files) |
---|
124 | n/a | return |
---|
125 | n/a | if not mf: |
---|
126 | n/a | print("Not updating missing master", master) |
---|
127 | n/a | return |
---|
128 | n/a | if sf and mf: |
---|
129 | n/a | if identical(sf, mf): |
---|
130 | n/a | return |
---|
131 | n/a | sft = mtime(sf) |
---|
132 | n/a | mft = mtime(mf) |
---|
133 | n/a | if mft > sft: |
---|
134 | n/a | # Master is newer -- copy master to slave |
---|
135 | n/a | sf.close() |
---|
136 | n/a | mf.close() |
---|
137 | n/a | print("Master ", master) |
---|
138 | n/a | print("is newer than slave", slave) |
---|
139 | n/a | copy(master, slave, answer=write_slave) |
---|
140 | n/a | return |
---|
141 | n/a | # Slave is newer -- copy slave to master |
---|
142 | n/a | print("Slave is", sft-mft, "seconds newer than master") |
---|
143 | n/a | # But first check what to do about CRLF |
---|
144 | n/a | mf.seek(0) |
---|
145 | n/a | fun = funnychars(mf) |
---|
146 | n/a | mf.close() |
---|
147 | n/a | sf.close() |
---|
148 | n/a | if fun: |
---|
149 | n/a | print("***UPDATING MASTER (BINARY COPY)***") |
---|
150 | n/a | copy(slave, master, "rb", answer=write_master) |
---|
151 | n/a | else: |
---|
152 | n/a | print("***UPDATING MASTER***") |
---|
153 | n/a | copy(slave, master, "r", answer=write_master) |
---|
154 | n/a | |
---|
155 | n/a | BUFSIZE = 16*1024 |
---|
156 | n/a | |
---|
157 | n/a | def identical(sf, mf): |
---|
158 | n/a | while 1: |
---|
159 | n/a | sd = sf.read(BUFSIZE) |
---|
160 | n/a | md = mf.read(BUFSIZE) |
---|
161 | n/a | if sd != md: return 0 |
---|
162 | n/a | if not sd: break |
---|
163 | n/a | return 1 |
---|
164 | n/a | |
---|
165 | n/a | def mtime(f): |
---|
166 | n/a | st = os.fstat(f.fileno()) |
---|
167 | n/a | return st[stat.ST_MTIME] |
---|
168 | n/a | |
---|
169 | n/a | def funnychars(f): |
---|
170 | n/a | while 1: |
---|
171 | n/a | buf = f.read(BUFSIZE) |
---|
172 | n/a | if not buf: break |
---|
173 | n/a | if '\r' in buf or '\0' in buf: return 1 |
---|
174 | n/a | return 0 |
---|
175 | n/a | |
---|
176 | n/a | def copy(src, dst, rmode="rb", wmode="wb", answer='ask'): |
---|
177 | n/a | print("copying", src) |
---|
178 | n/a | print(" to", dst) |
---|
179 | n/a | if not okay("okay to copy? ", answer): |
---|
180 | n/a | return |
---|
181 | n/a | f = open(src, rmode) |
---|
182 | n/a | g = open(dst, wmode) |
---|
183 | n/a | while 1: |
---|
184 | n/a | buf = f.read(BUFSIZE) |
---|
185 | n/a | if not buf: break |
---|
186 | n/a | g.write(buf) |
---|
187 | n/a | f.close() |
---|
188 | n/a | g.close() |
---|
189 | n/a | |
---|
190 | n/a | def raw_input(prompt): |
---|
191 | n/a | sys.stdout.write(prompt) |
---|
192 | n/a | sys.stdout.flush() |
---|
193 | n/a | return sys.stdin.readline() |
---|
194 | n/a | |
---|
195 | n/a | def okay(prompt, answer='ask'): |
---|
196 | n/a | answer = answer.strip().lower() |
---|
197 | n/a | if not answer or answer[0] not in 'ny': |
---|
198 | n/a | answer = input(prompt) |
---|
199 | n/a | answer = answer.strip().lower() |
---|
200 | n/a | if not answer: |
---|
201 | n/a | answer = default_answer |
---|
202 | n/a | if answer[:1] == 'y': |
---|
203 | n/a | return 1 |
---|
204 | n/a | if answer[:1] == 'n': |
---|
205 | n/a | return 0 |
---|
206 | n/a | print("Yes or No please -- try again:") |
---|
207 | n/a | return okay(prompt) |
---|
208 | n/a | |
---|
209 | n/a | if __name__ == '__main__': |
---|
210 | n/a | main() |
---|