ยปCore Development>Code coverage>Tools/scripts/treesync.py

Python code coverage for Tools/scripts/treesync.py

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