]> arthur.barton.de Git - bup.git/blob - cmd-server.py
Fix some problems running on older Debian.
[bup.git] / cmd-server.py
1 #!/usr/bin/env python2.5
2 import sys, struct, mmap
3 import options, git
4 from helpers import *
5
6
7 def list_indexes(conn):
8     for f in os.listdir(git.repo('objects/pack')):
9         if f.endswith('.idx'):
10             conn.write('%s\n' % f)
11     conn.ok()
12
13
14 def send_index(conn, name):
15     assert(name.find('/') < 0)
16     assert(name.endswith('.idx'))
17     idx = git.PackIndex(git.repo('objects/pack/%s' % name))
18     conn.write(struct.pack('!I', len(idx.map)))
19     conn.write(idx.map)
20     conn.ok()
21     
22             
23 def receive_objects(conn):
24     w = git.PackWriter()
25     while 1:
26         ns = conn.read(4)
27         if not ns:
28             w.abort()
29             raise Exception('object read: expected length header, got EOF\n')
30         n = struct.unpack('!I', ns)[0]
31         #log('expecting %d bytes\n' % n)
32         if not n:
33             log('bup server: received %d object%s.\n' 
34                 % (w.count, w.count!=1 and "s" or ''))
35             w.close()
36             return
37         buf = conn.read(n)  # object sizes in bup are reasonably small
38         #log('read %d bytes\n' % n)
39         if len(buf) < n:
40             w.abort()
41             raise Exception('object read: expected %d bytes, got %d\n'
42                             % (n, len(buf)))
43         w._raw_write(buf)
44     w.close()
45
46
47 optspec = """
48 bup server
49 """
50 o = options.Options('bup server', optspec)
51 (opt, flags, extra) = o.parse(sys.argv[1:])
52
53 if extra:
54     log('bup server: no arguments expected\n')
55     o.usage()
56
57 log('bup server: reading from stdin.\n')
58
59 # FIXME: this protocol is totally lame and not at all future-proof
60 conn = Conn(sys.stdin, sys.stdout)
61 lr = linereader(conn)
62 for _line in lr:
63     line = _line.strip()
64     if not line:
65         continue
66     log('bup server: command: %r\n' % line)
67     if line == 'quit':
68         break
69     elif line.startswith('set-dir '):
70         git.repodir = line[8:]
71         git.check_repo_or_die()
72         log('bup server: bupdir is %r\n' % git.repodir)
73         conn.ok()
74     elif line == 'list-indexes':
75         list_indexes(conn)
76     elif line.startswith('send-index '):
77         send_index(conn, line[11:])
78     elif line == 'receive-objects':
79         git.check_repo_or_die()
80         receive_objects(conn)
81         conn.ok()
82     else:
83         raise Exception('unknown server command: %r\n' % line)
84
85 log('bup server: done\n')