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'
63 def read_ref(conn, refname):
64 git.check_repo_or_die()
65 r = git.read_ref(refname)
66 conn.write('%s\n' % (r or '').encode('hex'))
70 def update_ref(conn, refname):
71 git.check_repo_or_die()
72 newval = conn.readline().strip()
73 oldval = conn.readline().strip()
74 git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
79 git.check_repo_or_die()
80 for blob in git.cat(id):
81 conn.write(struct.pack('!I', len(blob)))
83 conn.write('\0\0\0\0')
90 o = options.Options('bup server', optspec)
91 (opt, flags, extra) = o.parse(sys.argv[1:])
94 log('bup server: no arguments expected\n')
97 log('bup server: reading from stdin.\n')
100 'init-dir': init_dir,
102 'list-indexes': list_indexes,
103 'send-index': send_index,
104 'receive-objects': receive_objects,
105 'read-ref': read_ref,
106 'update-ref': update_ref,
110 # FIXME: this protocol is totally lame and not at all future-proof.
111 # (Especially since we abort completely as soon as *anything* bad happens)
112 conn = Conn(sys.stdin, sys.stdout)
113 lr = linereader(conn)
118 log('bup server: command: %r\n' % line)
119 words = line.split(' ', 1)
121 rest = len(words)>1 and words[1] or ''
125 cmd = commands.get(cmd)
129 raise Exception('unknown server command: %r\n' % line)
131 log('bup server: done\n')