"Fossies" - the Fresh Open Source Software Archive

Member "bup-0.30/cmd/server-cmd.py" (28 Sep 2019, 9262 Bytes) of package /linux/privat/bup-0.30.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "server-cmd.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 0.29.3_vs_0.30.

    1 #!/bin/sh
    2 """": # -*-python-*-
    3 bup_python="$(dirname "$0")/bup-python" || exit $?
    4 exec "$bup_python" "$0" ${1+"$@"}
    5 """
    6 # end of bup preamble
    7 
    8 from __future__ import absolute_import
    9 import os, sys, struct, subprocess
   10 
   11 from bup import options, git, vfs, vint
   12 from bup.git import MissingObject
   13 from bup.helpers import (Conn, debug1, debug2, linereader, lines_until_sentinel,
   14                          log)
   15 from bup.repo import LocalRepo
   16 
   17 
   18 suspended_w = None
   19 dumb_server_mode = False
   20 repo = None
   21 
   22 def do_help(conn, junk):
   23     conn.write('Commands:\n    %s\n' % '\n    '.join(sorted(commands)))
   24     conn.ok()
   25 
   26 
   27 def _set_mode():
   28     global dumb_server_mode
   29     dumb_server_mode = os.path.exists(git.repo('bup-dumb-server'))
   30     debug1('bup server: serving in %s mode\n' 
   31            % (dumb_server_mode and 'dumb' or 'smart'))
   32 
   33 
   34 def _init_session(reinit_with_new_repopath=None):
   35     global repo
   36     if reinit_with_new_repopath is None and git.repodir:
   37         if not repo:
   38             repo = LocalRepo()
   39         return
   40     git.check_repo_or_die(reinit_with_new_repopath)
   41     if repo:
   42         repo.close()
   43     repo = LocalRepo()
   44     # OK. we now know the path is a proper repository. Record this path in the
   45     # environment so that subprocesses inherit it and know where to operate.
   46     os.environ['BUP_DIR'] = git.repodir
   47     debug1('bup server: bupdir is %r\n' % git.repodir)
   48     _set_mode()
   49 
   50 
   51 def init_dir(conn, arg):
   52     git.init_repo(arg)
   53     debug1('bup server: bupdir initialized: %r\n' % git.repodir)
   54     _init_session(arg)
   55     conn.ok()
   56 
   57 
   58 def set_dir(conn, arg):
   59     _init_session(arg)
   60     conn.ok()
   61 
   62     
   63 def list_indexes(conn, junk):
   64     _init_session()
   65     suffix = ''
   66     if dumb_server_mode:
   67         suffix = ' load'
   68     for f in os.listdir(git.repo('objects/pack')):
   69         if f.endswith('.idx'):
   70             conn.write('%s%s\n' % (f, suffix))
   71     conn.ok()
   72 
   73 
   74 def send_index(conn, name):
   75     _init_session()
   76     assert(name.find('/') < 0)
   77     assert(name.endswith('.idx'))
   78     idx = git.open_idx(git.repo('objects/pack/%s' % name))
   79     conn.write(struct.pack('!I', len(idx.map)))
   80     conn.write(idx.map)
   81     conn.ok()
   82 
   83 
   84 def receive_objects_v2(conn, junk):
   85     global suspended_w
   86     _init_session()
   87     suggested = set()
   88     if suspended_w:
   89         w = suspended_w
   90         suspended_w = None
   91     else:
   92         if dumb_server_mode:
   93             w = git.PackWriter(objcache_maker=None)
   94         else:
   95             w = git.PackWriter()
   96     while 1:
   97         ns = conn.read(4)
   98         if not ns:
   99             w.abort()
  100             raise Exception('object read: expected length header, got EOF\n')
  101         n = struct.unpack('!I', ns)[0]
  102         #debug2('expecting %d bytes\n' % n)
  103         if not n:
  104             debug1('bup server: received %d object%s.\n' 
  105                 % (w.count, w.count!=1 and "s" or ''))
  106             fullpath = w.close(run_midx=not dumb_server_mode)
  107             if fullpath:
  108                 (dir, name) = os.path.split(fullpath)
  109                 conn.write('%s.idx\n' % name)
  110             conn.ok()
  111             return
  112         elif n == 0xffffffff:
  113             debug2('bup server: receive-objects suspended.\n')
  114             suspended_w = w
  115             conn.ok()
  116             return
  117             
  118         shar = conn.read(20)
  119         crcr = struct.unpack('!I', conn.read(4))[0]
  120         n -= 20 + 4
  121         buf = conn.read(n)  # object sizes in bup are reasonably small
  122         #debug2('read %d bytes\n' % n)
  123         _check(w, n, len(buf), 'object read: expected %d bytes, got %d\n')
  124         if not dumb_server_mode:
  125             oldpack = w.exists(shar, want_source=True)
  126             if oldpack:
  127                 assert(not oldpack == True)
  128                 assert(oldpack.endswith('.idx'))
  129                 (dir,name) = os.path.split(oldpack)
  130                 if not (name in suggested):
  131                     debug1("bup server: suggesting index %s\n"
  132                            % git.shorten_hash(name))
  133                     debug1("bup server:   because of object %s\n"
  134                            % shar.encode('hex'))
  135                     conn.write('index %s\n' % name)
  136                     suggested.add(name)
  137                 continue
  138         nw, crc = w._raw_write((buf,), sha=shar)
  139         _check(w, crcr, crc, 'object read: expected crc %d, got %d\n')
  140     # NOTREACHED
  141     
  142 
  143 def _check(w, expected, actual, msg):
  144     if expected != actual:
  145         w.abort()
  146         raise Exception(msg % (expected, actual))
  147 
  148 
  149 def read_ref(conn, refname):
  150     _init_session()
  151     r = git.read_ref(refname)
  152     conn.write('%s\n' % (r or '').encode('hex'))
  153     conn.ok()
  154 
  155 
  156 def update_ref(conn, refname):
  157     _init_session()
  158     newval = conn.readline().strip()
  159     oldval = conn.readline().strip()
  160     git.update_ref(refname, newval.decode('hex'), oldval.decode('hex'))
  161     conn.ok()
  162 
  163 def join(conn, id):
  164     _init_session()
  165     try:
  166         for blob in git.cp().join(id):
  167             conn.write(struct.pack('!I', len(blob)))
  168             conn.write(blob)
  169     except KeyError as e:
  170         log('server: error: %s\n' % e)
  171         conn.write('\0\0\0\0')
  172         conn.error(e)
  173     else:
  174         conn.write('\0\0\0\0')
  175         conn.ok()
  176 
  177 def cat_batch(conn, dummy):
  178     _init_session()
  179     cat_pipe = git.cp()
  180     # For now, avoid potential deadlock by just reading them all
  181     for ref in tuple(lines_until_sentinel(conn, '\n', Exception)):
  182         ref = ref[:-1]
  183         it = cat_pipe.get(ref)
  184         info = next(it)
  185         if not info[0]:
  186             conn.write('missing\n')
  187             continue
  188         conn.write('%s %s %d\n' % info)
  189         for buf in it:
  190             conn.write(buf)
  191     conn.ok()
  192 
  193 def refs(conn, args):
  194     limit_to_heads, limit_to_tags = args.split()
  195     assert limit_to_heads in ('0', '1')
  196     assert limit_to_tags in ('0', '1')
  197     limit_to_heads = int(limit_to_heads)
  198     limit_to_tags = int(limit_to_tags)
  199     _init_session()
  200     patterns = tuple(x[:-1] for x in lines_until_sentinel(conn, '\n', Exception))
  201     for name, oid in git.list_refs(patterns=patterns,
  202                                    limit_to_heads=limit_to_heads,
  203                                    limit_to_tags=limit_to_tags):
  204         assert '\n' not in name
  205         conn.write('%s %s\n' % (oid.encode('hex'), name))
  206     conn.write('\n')
  207     conn.ok()
  208 
  209 def rev_list(conn, _):
  210     _init_session()
  211     count = conn.readline()
  212     if not count:
  213         raise Exception('Unexpected EOF while reading rev-list count')
  214     count = None if count == '\n' else int(count)
  215     fmt = conn.readline()
  216     if not fmt:
  217         raise Exception('Unexpected EOF while reading rev-list format')
  218     fmt = None if fmt == '\n' else fmt[:-1]
  219     refs = tuple(x[:-1] for x in lines_until_sentinel(conn, '\n', Exception))
  220     args = git.rev_list_invocation(refs, count=count, format=fmt)
  221     p = subprocess.Popen(git.rev_list_invocation(refs, count=count, format=fmt),
  222                          preexec_fn=git._gitenv(git.repodir),
  223                          stdout=subprocess.PIPE)
  224     while True:
  225         out = p.stdout.read(64 * 1024)
  226         if not out:
  227             break
  228         conn.write(out)
  229     conn.write('\n')
  230     rv = p.wait()  # not fatal
  231     if rv:
  232         msg = 'git rev-list returned error %d' % rv
  233         conn.error(msg)
  234         raise GitError(msg)
  235     conn.ok()
  236 
  237 def resolve(conn, args):
  238     _init_session()
  239     (flags,) = args.split()
  240     flags = int(flags)
  241     want_meta = bool(flags & 1)
  242     follow = bool(flags & 2)
  243     have_parent = bool(flags & 4)
  244     parent = vfs.read_resolution(conn) if have_parent else None
  245     path = vint.read_bvec(conn)
  246     if not len(path):
  247         raise Exception('Empty resolve path')
  248     try:
  249         res = list(vfs.resolve(repo, path, parent=parent, want_meta=want_meta,
  250                                follow=follow))
  251     except vfs.IOError as ex:
  252         res = ex
  253     if isinstance(res, vfs.IOError):
  254         conn.write(b'\0')  # error
  255         vfs.write_ioerror(conn, res)
  256     else:
  257         conn.write(b'\1')  # success
  258         vfs.write_resolution(conn, res)
  259     conn.ok()
  260 
  261 optspec = """
  262 bup server
  263 """
  264 o = options.Options(optspec)
  265 (opt, flags, extra) = o.parse(sys.argv[1:])
  266 
  267 if extra:
  268     o.fatal('no arguments expected')
  269 
  270 debug2('bup server: reading from stdin.\n')
  271 
  272 commands = {
  273     'quit': None,
  274     'help': do_help,
  275     'init-dir': init_dir,
  276     'set-dir': set_dir,
  277     'list-indexes': list_indexes,
  278     'send-index': send_index,
  279     'receive-objects-v2': receive_objects_v2,
  280     'read-ref': read_ref,
  281     'update-ref': update_ref,
  282     'join': join,
  283     'cat': join,  # apocryphal alias
  284     'cat-batch' : cat_batch,
  285     'refs': refs,
  286     'rev-list': rev_list,
  287     'resolve': resolve
  288 }
  289 
  290 # FIXME: this protocol is totally lame and not at all future-proof.
  291 # (Especially since we abort completely as soon as *anything* bad happens)
  292 conn = Conn(sys.stdin, sys.stdout)
  293 lr = linereader(conn)
  294 for _line in lr:
  295     line = _line.strip()
  296     if not line:
  297         continue
  298     debug1('bup server: command: %r\n' % line)
  299     words = line.split(' ', 1)
  300     cmd = words[0]
  301     rest = len(words)>1 and words[1] or ''
  302     if cmd == 'quit':
  303         break
  304     else:
  305         cmd = commands.get(cmd)
  306         if cmd:
  307             cmd(conn, rest)
  308         else:
  309             raise Exception('unknown server command: %r\n' % line)
  310 
  311 debug1('bup server: done\n')