]> arthur.barton.de Git - bup.git/blob - cmd-split.py
'bup split' takes a list of filenames on the command line.
[bup.git] / cmd-split.py
1 #!/usr/bin/env python
2 import sys, os, subprocess, errno, zlib, time
3 import hashsplit, git, options
4 from helpers import *
5
6 BLOB_LWM = 8192*2
7 BLOB_MAX = BLOB_LWM*2
8 BLOB_HWM = 1024*1024
9
10
11 class Buf:
12     def __init__(self):
13         self.data = ''
14         self.start = 0
15
16     def put(self, s):
17         #log('oldsize=%d+%d adding=%d\n' % (len(self.data), self.start, len(s)))
18         if s:
19             self.data = buffer(self.data, self.start) + s
20             self.start = 0
21             
22     def peek(self, count):
23         return buffer(self.data, self.start, count)
24     
25     def eat(self, count):
26         self.start += count
27
28     def get(self, count):
29         v = buffer(self.data, self.start, count)
30         self.start += count
31         return v
32
33     def used(self):
34         return len(self.data) - self.start
35
36
37 def splitbuf(buf):
38     b = buf.peek(buf.used())
39     ofs = hashsplit.splitbuf(b)
40     if ofs:
41         buf.eat(ofs)
42         return buffer(b, 0, ofs)
43     return None
44
45
46 def hashsplit_iter(f):
47     ofs = 0
48     buf = Buf()
49     blob = 1
50
51     eof = 0
52     lv = 0
53     while blob or not eof:
54         if not eof and (buf.used() < BLOB_LWM or not blob):
55             bnew = f.read(BLOB_HWM)
56             if not len(bnew): eof = 1
57             #log('got %d, total %d\n' % (len(bnew), buf.used()))
58             buf.put(bnew)
59
60         blob = splitbuf(buf)
61         if eof and not blob:
62             blob = buf.get(buf.used())
63         if not blob and buf.used() >= BLOB_MAX:
64             blob = buf.get(BLOB_MAX)  # limit max blob size
65         if not blob and not eof:
66             continue
67
68         if blob:
69             yield (ofs, len(blob), git.hash_blob(blob))
70             ofs += len(blob)
71           
72         nv = (ofs + buf.used())/1000000
73         if nv != lv:
74             log('%d\t' % nv)
75             lv = nv
76
77
78 def autofiles(filenames):
79     if not filenames:
80         yield sys.stdin
81     else:
82         for n in filenames:
83             yield open(n)
84
85
86 optspec = """
87 bup split [-t] [filenames...]
88 --
89 b,blobs    output a series of blob ids
90 t,tree     output a tree id
91 c,commit   output a commit id
92 n,name=    name of backup set to update (if any)
93 bench      print benchmark timings to stderr
94 """
95 o = options.Options('bup split', optspec)
96 (opt, flags, extra) = o.parse(sys.argv[1:])
97
98 if not (opt.blobs or opt.tree or opt.commit or opt.name):
99     log("bup split: use one or more of -b, -t, -c, -n\n")
100     o.usage()
101
102 start_time = time.time()
103 shalist = []
104
105 ofs = 0
106
107 for f in autofiles(extra):
108     for (ofs, size, sha) in hashsplit_iter(f):
109         #log('SPLIT @ %-8d size=%-8d\n' % (ofs, size))
110         if opt.blobs:
111             print sha
112         shalist.append(('100644', '%016x.bupchunk' % ofs, sha))
113 tree = git.gen_tree(shalist)
114 if opt.tree:
115     print tree
116 if opt.commit or opt.name:
117     msg = 'Generated by command:\n%r' % sys.argv
118     ref = opt.name and ('refs/heads/%s' % opt.name) or None
119     commit = git.gen_commit_easy(ref, tree, msg)
120     if opt.commit:
121         print commit
122
123 secs = time.time() - start_time
124 if opt.bench:
125     log('\nbup: %.2fkbytes in %.2f secs = %.2f kbytes/sec\n'
126         % (ofs/1024., secs, ofs/1024./secs))