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))
66 def progress_report(n):
67 global count, lastremain
69 pct = total and (count*100.0/total) or 0
71 elapsed = now - tstart
72 kps = elapsed and int(count/1024./elapsed)
73 kps_frac = 10 ** int(math.log(kps+1, 10) - 1)
74 kps = int(kps/kps_frac)*kps_frac
76 remain = elapsed*1.0/count * (total-count)
79 if (lastremain and (remain > lastremain)
80 and ((remain - lastremain)/lastremain < 0.05)):
84 hours = int(remain/60/60)
85 mins = int(remain/60 - hours*60)
86 secs = int(remain - hours*60*60 - mins*60)
91 kpsstr = '%dk/s' % kps
93 remainstr = '%dh%dm' % (hours, mins)
95 remainstr = '%dm%d' % (mins, secs)
97 remainstr = '%ds' % secs
98 progress('Saving: %.2f%% (%d/%dk, %d/%d files) %s %s \r'
99 % (pct, count/1024, total/1024, fcount, ftotal,
103 r = index.Reader(git.repo('bupindex'))
107 for (transname,ent) in r.filter(extra):
108 if not (ftotal % 10024):
109 progress('Reading index: %d\r' % ftotal)
110 exists = (ent.flags & index.IX_EXISTS)
111 hashvalid = (ent.flags & index.IX_HASHVALID) and w.exists(ent.sha)
112 if exists and not hashvalid:
115 progress('Reading index: %d, done.\n' % ftotal)
116 hashsplit.progress_callback = progress_report
120 for (transname,ent) in r.filter(extra):
121 (dir, file) = os.path.split(ent.name)
122 exists = (ent.flags & index.IX_EXISTS)
123 hashvalid = (ent.flags & index.IX_HASHVALID) and w.exists(ent.sha)
128 if ent.sha == index.EMPTY_SHA:
134 if opt.verbose >= 2 or stat.S_ISDIR(ent.mode):
135 log('%s %-70s\n' % (status, ent.name))
144 assert(dir.startswith('/'))
145 dirp = dir.split('/')
149 for part in dirp[len(parts):]:
153 # sub/parentdirectories already handled in the pop/push() part above.
154 ent.validate(040000, _pop())
161 mode = '%o' % ent.gitmode
163 shalists[-1].append((mode, file, id))
164 elif opt.smaller and ent.size >= opt.smaller:
165 add_error('skipping large file "%s"' % ent.name)
167 if stat.S_ISREG(ent.mode):
175 (mode, id) = hashsplit.split_to_blob_or_tree(w, [f])
177 if stat.S_ISDIR(ent.mode):
178 assert(0) # handled above
179 elif stat.S_ISLNK(ent.mode):
181 rl = os.readlink(ent.name)
187 (mode, id) = ('120000', w.new_blob(rl))
189 add_error(Exception('skipping special file "%s"' % ent.name))
192 ent.validate(int(mode, 8), id)
194 shalists[-1].append((mode, file, id))
197 pct = total and count*100.0/total or 100
198 progress('Saving: %.2f%% (%d/%dk, %d/%d files), done. \n'
199 % (pct, count/1024, total/1024, fcount, ftotal))
201 #log('parts out: %r\n' % parts)
202 #log('stk out: %r\n' % shalists)
203 while len(parts) > 1:
205 #log('parts out: %r\n' % parts)
206 #log('stk out: %r\n' % shalists)
207 assert(len(shalists) == 1)
208 tree = w.new_tree(shalists[-1])
210 print tree.encode('hex')
211 if opt.commit or opt.name:
212 msg = 'bup save\n\nGenerated by command:\n%r' % sys.argv
213 ref = opt.name and ('refs/heads/%s' % opt.name) or None
214 commit = w.new_commit(oldref, tree, msg)
216 print commit.encode('hex')
218 w.close() # must close before we can update the ref
222 cli.update_ref(refname, commit, oldref)
224 git.update_ref(refname, commit, oldref)
230 log('WARNING: %d errors encountered while saving.\n' % len(saved_errors))