2 import sys, re, errno, stat, time, math
3 import hashsplit, git, options, index, client
8 bup save [-tc] [-n name] <filenames...>
10 r,remote= remote repository path
11 t,tree output a tree id
12 c,commit output a commit id
13 n,name= name of backup set to update (if any)
14 v,verbose increase log output (can be used more than once)
15 q,quiet don't show progress meter
16 smaller= only back up files smaller than n bytes
18 o = options.Options('bup save', optspec)
19 (opt, flags, extra) = o.parse(sys.argv[1:])
21 git.check_repo_or_die()
22 if not (opt.tree or opt.commit or opt.name):
23 log("bup save: use one or more of -t, -c, -n\n")
26 log("bup save: no filenames given.\n")
29 opt.progress = (istty and not opt.quiet)
31 refname = opt.name and 'refs/heads/%s' % opt.name or None
33 cli = client.Client(opt.remote)
34 oldref = refname and cli.read_ref(refname) or None
35 w = cli.new_packwriter()
38 oldref = refname and git.read_ref(refname) or None
58 assert(len(parts) > 1)
60 shalist = shalists.pop()
61 tree = w.new_tree(shalist)
62 shalists[-1].append(('40000', part, tree))
65 def progress_report(n):
66 global count, lastremain
68 pct = count*100.0/total
70 elapsed = now - tstart
71 kps = elapsed and int(count/1024./elapsed)
72 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
73 kps = int(kps/kps_frac)*kps_frac
75 remain = elapsed*1.0/count * (total-count)
78 if (lastremain and (remain > lastremain)
79 and ((remain - lastremain)/lastremain < 0.05)):
83 hours = int(remain/60/60)
84 mins = int(remain/60 - hours*60)
85 secs = int(remain - hours*60*60 - mins*60)
90 kpsstr = '%dk/s' % kps
92 remainstr = '%dh%dm' % (hours, mins)
94 remainstr = '%dm%d' % (mins, secs)
96 remainstr = '%ds' % secs
97 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s \r'
98 % (pct, count/1024, total/1024, fcount, ftotal,
102 r = index.Reader(git.repo('bupindex'))
106 for (transname,ent) in r.filter(extra):
107 if not (ftotal % 10024):
108 progress('Reading index: %d\r' % ftotal)
109 exists = (ent.flags & index.IX_EXISTS)
110 hashvalid = (ent.flags & index.IX_HASHVALID) and w.exists(ent.sha)
111 if exists and not hashvalid:
114 progress('Reading index: %d, done.\n' % ftotal)
115 hashsplit.progress_callback = progress_report
119 for (transname,ent) in r.filter(extra):
120 (dir, file) = os.path.split(ent.name)
121 exists = (ent.flags & index.IX_EXISTS)
122 hashvalid = (ent.flags & index.IX_HASHVALID) and w.exists(ent.sha)
127 if ent.sha == index.EMPTY_SHA:
133 if opt.verbose >= 2 or stat.S_ISDIR(ent.mode):
134 log('%s %-70s\n' % (status, ent.name))
143 assert(dir.startswith('/'))
144 dirp = dir.split('/')
148 for part in dirp[len(parts):]:
152 # directory already handled.
153 # FIXME: not using the indexed tree sha1's for anything, which is
154 # a waste. That's a potential optimization...
160 mode = '%o' % ent.mode
162 shalists[-1].append((mode, file, id))
163 elif opt.smaller and ent.size >= opt.smaller:
164 add_error('skipping large file "%s"' % ent.name)
166 if stat.S_ISREG(ent.mode):
174 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
176 if stat.S_ISDIR(ent.mode):
177 assert(0) # handled above
178 elif stat.S_ISLNK(ent.mode):
180 rl = os.readlink(ent.name)
186 (mode, id) = ('120000', w.new_blob(rl))
188 add_error(Exception('skipping special file "%s"' % ent.name))
193 shalists[-1].append((mode, file, id))
196 pct = total and count*100.0/total or 100
197 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
198 % (pct, count/1024, total/1024, fcount, ftotal))
200 #log('parts out: %r\n' % parts)
201 #log('stk out: %r\n' % shalists)
202 while len(parts) > 1:
204 #log('parts out: %r\n' % parts)
205 #log('stk out: %r\n' % shalists)
206 assert(len(shalists) == 1)
207 tree = w.new_tree(shalists[-1])
209 print tree.encode('hex')
210 if opt.commit or opt.name:
211 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
212 ref = opt.name and ('refs/heads/%s' % opt.name) or None
213 commit = w.new_commit(oldref, tree, msg)
215 print commit.encode('hex')
217 w.close() # must close before we can update the ref
221 cli.update_ref(refname, commit, oldref)
223 git.update_ref(refname, commit, oldref)
229 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))