]> arthur.barton.de Git - bup.git/blob - cmd/index-cmd.py
options: remove unused 'exe' parameter
[bup.git] / cmd / index-cmd.py
1 #!/usr/bin/env python
2 import sys, stat, time, os
3 from bup import options, git, index, drecurse
4 from bup.helpers import *
5
6
7 def merge_indexes(out, r1, r2):
8     for e in index.MergeIter([r1, r2]):
9         # FIXME: shouldn't we remove deleted entries eventually?  When?
10         out.add_ixentry(e)
11
12
13 class IterHelper:
14     def __init__(self, l):
15         self.i = iter(l)
16         self.cur = None
17         self.next()
18
19     def next(self):
20         try:
21             self.cur = self.i.next()
22         except StopIteration:
23             self.cur = None
24         return self.cur
25
26
27 def check_index(reader):
28     try:
29         log('check: checking forward iteration...\n')
30         e = None
31         d = {}
32         for e in reader.forward_iter():
33             if e.children_n:
34                 if opt.verbose:
35                     log('%08x+%-4d %r\n' % (e.children_ofs, e.children_n,
36                                             e.name))
37                 assert(e.children_ofs)
38                 assert(e.name.endswith('/'))
39                 assert(not d.get(e.children_ofs))
40                 d[e.children_ofs] = 1
41             if e.flags & index.IX_HASHVALID:
42                 assert(e.sha != index.EMPTY_SHA)
43                 assert(e.gitmode)
44         assert(not e or e.name == '/')  # last entry is *always* /
45         log('check: checking normal iteration...\n')
46         last = None
47         for e in reader:
48             if last:
49                 assert(last > e.name)
50             last = e.name
51     except:
52         log('index error! at %r\n' % e)
53         raise
54     log('check: passed.\n')
55
56
57 def update_index(top, excluded_paths):
58     ri = index.Reader(indexfile)
59     wi = index.Writer(indexfile)
60     rig = IterHelper(ri.iter(name=top))
61     tstart = int(time.time())
62
63     hashgen = None
64     if opt.fake_valid:
65         def hashgen(name):
66             return (0100644, index.FAKE_SHA)
67
68     total = 0
69     bup_dir = os.path.abspath(git.repo())
70     for (path,pst) in drecurse.recursive_dirlist([top], xdev=opt.xdev,
71                                                  bup_dir=bup_dir,
72                                                  excluded_paths=excluded_paths):
73         if opt.verbose>=2 or (opt.verbose==1 and stat.S_ISDIR(pst.st_mode)):
74             sys.stdout.write('%s\n' % path)
75             sys.stdout.flush()
76             progress('Indexing: %d\r' % total)
77         elif not (total % 128):
78             progress('Indexing: %d\r' % total)
79         total += 1
80         while rig.cur and rig.cur.name > path:  # deleted paths
81             if rig.cur.exists():
82                 rig.cur.set_deleted()
83                 rig.cur.repack()
84             rig.next()
85         if rig.cur and rig.cur.name == path:    # paths that already existed
86             if pst:
87                 rig.cur.from_stat(pst, tstart)
88             if not (rig.cur.flags & index.IX_HASHVALID):
89                 if hashgen:
90                     (rig.cur.gitmode, rig.cur.sha) = hashgen(path)
91                     rig.cur.flags |= index.IX_HASHVALID
92             if opt.fake_invalid:
93                 rig.cur.invalidate()
94             rig.cur.repack()
95             rig.next()
96         else:  # new paths
97             wi.add(path, pst, hashgen = hashgen)
98     progress('Indexing: %d, done.\n' % total)
99     
100     if ri.exists():
101         ri.save()
102         wi.flush()
103         if wi.count:
104             wr = wi.new_reader()
105             if opt.check:
106                 log('check: before merging: oldfile\n')
107                 check_index(ri)
108                 log('check: before merging: newfile\n')
109                 check_index(wr)
110             mi = index.Writer(indexfile)
111             merge_indexes(mi, ri, wr)
112             ri.close()
113             mi.close()
114             wr.close()
115         wi.abort()
116     else:
117         wi.close()
118
119
120 optspec = """
121 bup index <-p|m|u> [options...] <filenames...>
122 --
123 p,print    print the index entries for the given names (also works with -u)
124 m,modified print only added/deleted/modified files (implies -p)
125 s,status   print each filename with a status char (A/M/D) (implies -p)
126 H,hash     print the hash for each object next to its name (implies -p)
127 l,long     print more information about each file
128 u,update   (recursively) update the index entries for the given filenames
129 x,xdev,one-file-system  don't cross filesystem boundaries
130 fake-valid mark all index entries as up-to-date even if they aren't
131 fake-invalid mark all index entries as invalid
132 check      carefully check index file integrity
133 f,indexfile=  the name of the index file (normally BUP_DIR/bupindex)
134 exclude=   a path to exclude from the backup (can be used more than once)
135 exclude-from= a file that contains exclude paths (can be used more than once)
136 v,verbose  increase log output (can be used more than once)
137 """
138 o = options.Options(optspec)
139 (opt, flags, extra) = o.parse(sys.argv[1:])
140
141 if not (opt.modified or opt['print'] or opt.status or opt.update or opt.check):
142     o.fatal('supply one or more of -p, -s, -m, -u, or --check')
143 if (opt.fake_valid or opt.fake_invalid) and not opt.update:
144     o.fatal('--fake-{in,}valid are meaningless without -u')
145 if opt.fake_valid and opt.fake_invalid:
146     o.fatal('--fake-valid is incompatible with --fake-invalid')
147
148 git.check_repo_or_die()
149 indexfile = opt.indexfile or git.repo('bupindex')
150
151 handle_ctrl_c()
152
153 if opt.check:
154     log('check: starting initial check.\n')
155     check_index(index.Reader(indexfile))
156
157 excluded_paths = drecurse.parse_excludes(flags)
158
159 paths = index.reduce_paths(extra)
160
161 if opt.update:
162     if not extra:
163         o.fatal('update (-u) requested but no paths given')
164     for (rp,path) in paths:
165         update_index(rp, excluded_paths)
166
167 if opt['print'] or opt.status or opt.modified:
168     for (name, ent) in index.Reader(indexfile).filter(extra or ['']):
169         if (opt.modified 
170             and (ent.is_valid() or ent.is_deleted() or not ent.mode)):
171             continue
172         line = ''
173         if opt.status:
174             if ent.is_deleted():
175                 line += 'D '
176             elif not ent.is_valid():
177                 if ent.sha == index.EMPTY_SHA:
178                     line += 'A '
179                 else:
180                     line += 'M '
181             else:
182                 line += '  '
183         if opt.hash:
184             line += ent.sha.encode('hex') + ' '
185         if opt.long:
186             line += "%7s %7s " % (oct(ent.mode), oct(ent.gitmode))
187         print line + (name or './')
188
189 if opt.check and (opt['print'] or opt.status or opt.modified or opt.update):
190     log('check: starting final check.\n')
191     check_index(index.Reader(indexfile))
192
193 if saved_errors:
194     log('WARNING: %d errors encountered.\n' % len(saved_errors))
195     sys.exit(1)