Show More
@@ -1,103 +1,128 | |||
|
1 | 1 | #!/usr/bin/env python |
|
2 | 2 | # |
|
3 | 3 | # Copyright (C) 2007 Brendan Cully <brendan@kublai.com> |
|
4 | 4 | # |
|
5 | 5 | # This software may be used and distributed according to the terms |
|
6 | 6 | # of the GNU General Public License, incorporated herein by reference. |
|
7 | 7 | |
|
8 | 8 | import os, sys |
|
9 | 9 | |
|
10 | 10 | class ConfigError(Exception): pass |
|
11 | 11 | |
|
12 | 12 | def usage(): |
|
13 | 13 | print """relink <source> <destination> |
|
14 | 14 | Recreate hard links between source and destination repositories""" |
|
15 | 15 | |
|
16 | 16 | class Config: |
|
17 | 17 | def __init__(self, args): |
|
18 | 18 | if len(args) != 3: |
|
19 | 19 | raise ConfigError("wrong number of arguments") |
|
20 | 20 | self.src = os.path.abspath(args[1]) |
|
21 | 21 | self.dst = os.path.abspath(args[2]) |
|
22 | 22 | for d in (self.src, self.dst): |
|
23 | 23 | if not os.path.exists(os.path.join(d, '.hg')): |
|
24 | 24 | raise ConfigError("%s: not a mercurial repository" % d) |
|
25 | 25 | |
|
26 | try: | |
|
27 | cfg = Config(sys.argv) | |
|
28 | except ConfigError, inst: | |
|
29 | print str(inst) | |
|
30 | usage() | |
|
31 | sys.exit(1) | |
|
32 | ||
|
33 | 26 | def collect(src): |
|
34 | 27 | seplen = len(os.path.sep) |
|
35 | 28 | candidates = [] |
|
36 | 29 | for dirpath, dirnames, filenames in os.walk(src): |
|
37 | 30 | relpath = dirpath[len(src) + seplen:] |
|
38 | 31 | for filename in filenames: |
|
39 |
if not |
|
|
32 | if not filename.endswith('.i'): | |
|
40 | 33 | continue |
|
41 | 34 | st = os.stat(os.path.join(dirpath, filename)) |
|
42 | 35 | candidates.append((os.path.join(relpath, filename), st)) |
|
43 | 36 | |
|
44 | 37 | return candidates |
|
45 | 38 | |
|
46 | 39 | def prune(candidates, dst): |
|
40 | def getdatafile(path): | |
|
41 | if not path.endswith('.i'): | |
|
42 | return None, None | |
|
43 | df = path[:-1] + 'd' | |
|
44 | try: | |
|
45 | st = os.stat(df) | |
|
46 | except OSError: | |
|
47 | return None, None | |
|
48 | return df, st | |
|
49 | ||
|
50 | def linkfilter(dst, st): | |
|
51 | try: | |
|
52 | ts = os.stat(dst) | |
|
53 | except OSError: | |
|
54 | # Destination doesn't have this file? | |
|
55 | return False | |
|
56 | if st.st_ino == ts.st_ino: | |
|
57 | return False | |
|
58 | if st.st_dev != ts.st_dev: | |
|
59 | # No point in continuing | |
|
60 | raise Exception('Source and destination are on different devices') | |
|
61 | if st.st_size != ts.st_size: | |
|
62 | # TODO: compare revlog heads | |
|
63 | return False | |
|
64 | return st | |
|
65 | ||
|
47 | 66 | targets = [] |
|
48 | 67 | for fn, st in candidates: |
|
49 | 68 | tgt = os.path.join(dst, fn) |
|
50 | try: | |
|
51 | ts = os.stat(tgt) | |
|
52 | except OSError: | |
|
53 | # Destination doesn't have this file? | |
|
54 | continue | |
|
55 | if st.st_ino == ts.st_ino: | |
|
56 | continue | |
|
57 | if st.st_dev != ts.st_dev: | |
|
58 | raise Exception('Source and destination are on different devices') | |
|
59 | if st.st_size != ts.st_size: | |
|
69 | ts = linkfilter(tgt, st) | |
|
70 | if not ts: | |
|
60 | 71 | continue |
|
61 | 72 | targets.append((fn, ts.st_size)) |
|
73 | df, ts = getdatafile(tgt) | |
|
74 | if df: | |
|
75 | targets.append((fn[:-1] + 'd', ts.st_size)) | |
|
62 | 76 | |
|
63 | 77 | return targets |
|
64 | 78 | |
|
65 | 79 | def relink(src, dst, files): |
|
80 | def relinkfile(src, dst): | |
|
81 | bak = dst + '.bak' | |
|
82 | os.rename(dst, bak) | |
|
83 | try: | |
|
84 | os.link(src, dst) | |
|
85 | except OSError: | |
|
86 | os.rename(bak, dst) | |
|
87 | raise | |
|
88 | os.remove(bak) | |
|
89 | ||
|
66 | 90 | CHUNKLEN = 65536 |
|
67 | 91 | relinked = 0 |
|
68 | 92 | savedbytes = 0 |
|
69 | 93 | |
|
70 | 94 | for f, sz in files: |
|
71 | 95 | source = os.path.join(src, f) |
|
72 | 96 | tgt = os.path.join(dst, f) |
|
73 | 97 | sfp = file(source) |
|
74 | 98 | dfp = file(tgt) |
|
75 | 99 | sin = sfp.read(CHUNKLEN) |
|
76 | 100 | while sin: |
|
77 | 101 | din = dfp.read(CHUNKLEN) |
|
78 | 102 | if sin != din: |
|
79 | 103 | break |
|
80 | 104 | sin = sfp.read(CHUNKLEN) |
|
81 | 105 | if sin: |
|
82 | 106 | continue |
|
83 | 107 | try: |
|
84 |
|
|
|
85 | try: | |
|
86 | os.link(source, tgt) | |
|
87 | except OSError: | |
|
88 | os.rename(tgt + '.bak', tgt) | |
|
89 | raise | |
|
108 | relinkfile(source, tgt) | |
|
90 | 109 | print 'Relinked %s' % f |
|
91 | 110 | relinked += 1 |
|
92 | 111 | savedbytes += sz |
|
93 | os.remove(tgt + '.bak') | |
|
94 | 112 | except OSError, inst: |
|
95 | 113 | print '%s: %s' % (tgt, str(inst)) |
|
96 | 114 | |
|
97 | 115 | print 'Relinked %d files (%d bytes reclaimed)' % (relinked, savedbytes) |
|
98 | 116 | |
|
117 | try: | |
|
118 | cfg = Config(sys.argv) | |
|
119 | except ConfigError, inst: | |
|
120 | print str(inst) | |
|
121 | usage() | |
|
122 | sys.exit(1) | |
|
123 | ||
|
99 | 124 | src = os.path.join(cfg.src, '.hg') |
|
100 | 125 | dst = os.path.join(cfg.dst, '.hg') |
|
101 | 126 | candidates = collect(src) |
|
102 | 127 | targets = prune(candidates, dst) |
|
103 | 128 | relink(src, dst, targets) |
General Comments 0
You need to be logged in to leave comments.
Login now