1 #!/usr/bin/env python2.5
2 import sys, struct, mmap
7 def init_dir(conn, arg):
9 log('bup server: bupdir initialized: %r\n' % git.repodir)
13 def set_dir(conn, arg):
14 git.check_repo_or_die(arg)
15 log('bup server: bupdir is %r\n' % git.repodir)
19 def list_indexes(conn, junk):
20 git.check_repo_or_die()
21 for f in os.listdir(git.repo('objects/pack')):
22 if f.endswith('.idx'):
23 conn.write('%s\n' % f)
27 def send_index(conn, name):
28 git.check_repo_or_die()
29 assert(name.find('/') < 0)
30 assert(name.endswith('.idx'))
31 idx = git.PackIndex(git.repo('objects/pack/%s' % name))
32 conn.write(struct.pack('!I', len(idx.map)))
37 def receive_objects(conn, junk):
38 git.check_repo_or_die()
44 raise Exception('object read: expected length header, got EOF\n')
45 n = struct.unpack('!I', ns)[0]
46 #log('expecting %d bytes\n' % n)
48 log('bup server: received %d object%s.\n'
49 % (w.count, w.count!=1 and "s" or ''))
52 buf = conn.read(n) # object sizes in bup are reasonably small
53 #log('read %d bytes\n' % n)
56 raise Exception('object read: expected %d bytes, got %d\n'
66 o = options.Options('bup server', optspec)
67 (opt, flags, extra) = o.parse(sys.argv[1:])
70 log('bup server: no arguments expected\n')
73 log('bup server: reading from stdin.\n')
78 'list-indexes': list_indexes,
79 'send-index': send_index,
80 'receive-objects': receive_objects,
83 # FIXME: this protocol is totally lame and not at all future-proof.
84 # (Especially since we abort completely as soon as *anything* bad happens)
85 conn = Conn(sys.stdin, sys.stdout)
91 log('bup server: command: %r\n' % line)
92 words = line.split(' ', 1)
94 rest = len(words)>1 and words[1] or ''
98 cmd = commands.get(cmd)
102 raise Exception('unknown server command: %r\n' % line)
104 log('bup server: done\n')