author | Brendan Cully <brendan@kublai.com> |
Mon, 19 Mar 2007 09:36:06 -0700 | |
changeset 4249 | 7663780b55a7 |
child 4270 | 29eb88bd5c8d |
permissions | -rwxr-xr-x |
4249
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
1 |
#!/usr/bin/env python |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
2 |
# |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
3 |
# Copyright (C) 2007 Brendan Cully <brendan@kublai.com> |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
4 |
# |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
6 |
# of the GNU General Public License, incorporated herein by reference. |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
7 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
8 |
import os, sys |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
9 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
10 |
class ConfigError(Exception): pass |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
11 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
12 |
def usage(): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
13 |
print """relink <source> <destination> |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
14 |
Recreate hard links between source and destination repositories""" |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
15 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
16 |
class Config: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
17 |
def __init__(self, args): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
18 |
if len(args) != 3: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
19 |
raise ConfigError("wrong number of arguments") |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
20 |
self.src = os.path.abspath(args[1]) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
21 |
self.dst = os.path.abspath(args[2]) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
22 |
for d in (self.src, self.dst): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
23 |
if not os.path.exists(os.path.join(d, '.hg')): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
24 |
raise ConfigError("%s: not a mercurial repository" % d) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
25 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
26 |
try: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
27 |
cfg = Config(sys.argv) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
28 |
except ConfigError, inst: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
29 |
print str(inst) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
30 |
usage() |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
31 |
sys.exit(1) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
32 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
33 |
def collect(src): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
34 |
seplen = len(os.path.sep) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
35 |
candidates = [] |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
36 |
for dirpath, dirnames, filenames in os.walk(src): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
37 |
relpath = dirpath[len(src) + seplen:] |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
38 |
for filename in filenames: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
39 |
if not (filename.endswith('.i') or filename.endswith('.d')): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
40 |
continue |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
41 |
st = os.stat(os.path.join(dirpath, filename)) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
42 |
candidates.append((os.path.join(relpath, filename), st)) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
43 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
44 |
return candidates |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
45 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
46 |
def prune(candidates, dst): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
47 |
targets = [] |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
48 |
for fn, st in candidates: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
49 |
tgt = os.path.join(dst, fn) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
50 |
try: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
51 |
ts = os.stat(tgt) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
52 |
except OSError: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
53 |
# Destination doesn't have this file? |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
54 |
continue |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
55 |
if st.st_ino == ts.st_ino: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
56 |
continue |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
57 |
if st.st_dev != ts.st_dev: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
58 |
raise Exception('Source and destination are on different devices') |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
59 |
if st.st_size != ts.st_size: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
60 |
continue |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
61 |
targets.append((fn, ts.st_size)) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
62 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
63 |
return targets |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
64 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
65 |
def relink(src, dst, files): |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
66 |
CHUNKLEN = 65536 |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
67 |
relinked = 0 |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
68 |
savedbytes = 0 |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
69 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
70 |
for f, sz in files: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
71 |
source = os.path.join(src, f) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
72 |
tgt = os.path.join(dst, f) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
73 |
sfp = file(source) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
74 |
dfp = file(tgt) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
75 |
sin = sfp.read(CHUNKLEN) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
76 |
while sin: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
77 |
din = dfp.read(CHUNKLEN) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
78 |
if sin != din: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
79 |
break |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
80 |
sin = sfp.read(CHUNKLEN) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
81 |
if sin: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
82 |
continue |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
83 |
try: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
84 |
os.rename(tgt, tgt + '.bak') |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
85 |
try: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
86 |
os.link(source, tgt) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
87 |
except OSError: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
88 |
os.rename(tgt + '.bak', tgt) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
89 |
raise |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
90 |
print 'Relinked %s' % f |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
91 |
relinked += 1 |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
92 |
savedbytes += sz |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
93 |
os.remove(tgt + '.bak') |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
94 |
except OSError, inst: |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
95 |
print '%s: %s' % (tgt, str(inst)) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
96 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
97 |
print 'Relinked %d files (%d bytes reclaimed)' % (relinked, savedbytes) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
98 |
|
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
99 |
src = os.path.join(cfg.src, '.hg') |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
100 |
dst = os.path.join(cfg.dst, '.hg') |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
101 |
candidates = collect(src) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
102 |
targets = prune(candidates, dst) |
7663780b55a7
Add hg-relink script to contrib
Brendan Cully <brendan@kublai.com>
parents:
diff
changeset
|
103 |
relink(src, dst, targets) |