contrib/hgclient.py
author Martijn Pieters <mjpieters@fb.com>
Sat, 19 Mar 2016 16:37:47 -0700
changeset 28601 cd10171d6c71
parent 28355 897a4bbd578b
child 28836 3f45488d70df
permissions -rw-r--r--
graphmod: allow edges to end early Rather than draw an edge all the way to the bottom of the graph, make it possible to end an edge to parents that are not part of the graph early on. This results in a far cleaner graph. Any edge type can be set to end early; set the ui.graphstyle.<edgetype> parameter to the empty string to enable this. For example, setting the following configuration: [ui] graphstyle.grandparent = : graphstyle.missing = would result in a graph like this: o changeset: 32:d06dffa21a31 |\ parent: 27:886ed638191b | : parent: 31:621d83e11f67 | : o : changeset: 31:621d83e11f67 |\: parent: 21:d42a756af44d | : parent: 30:6e11cd4b648f | : o : changeset: 30:6e11cd4b648f |\ \ parent: 28:44ecd0b9ae99 | ~ : parent: 29:cd9bb2be7593 | / o : changeset: 28:44ecd0b9ae99 |\ \ parent: 1:6db2ef61d156 | ~ : parent: 26:7f25b6c2f0b9 | / o : changeset: 26:7f25b6c2f0b9 |\ \ parent: 18:1aa84d96232a | | : parent: 25:91da8ed57247 | | : | o : changeset: 25:91da8ed57247 | |\: parent: 21:d42a756af44d | | : parent: 24:a9c19a3d96b7 | | : | o : changeset: 24:a9c19a3d96b7 | |\ \ parent: 0:e6eb3150255d | | ~ : parent: 23:a01cddf0766d | | / | o : changeset: 23:a01cddf0766d | |\ \ parent: 1:6db2ef61d156 | | ~ : parent: 22:e0d9cccacb5d | | / | o : changeset: 22:e0d9cccacb5d |/:/ parent: 18:1aa84d96232a | : parent: 21:d42a756af44d | : | o changeset: 21:d42a756af44d | |\ parent: 19:31ddc2c1573b | | | parent: 20:d30ed6450e32 | | | +---o changeset: 20:d30ed6450e32 | | | parent: 0:e6eb3150255d | | ~ parent: 18:1aa84d96232a | | | o changeset: 19:31ddc2c1573b | |\ parent: 15:1dda3f72782d | ~ ~ parent: 17:44765d7c06e0 | o changeset: 18:1aa84d96232a parent: 1:6db2ef61d156 parent: 15:1dda3f72782d The default configuration leaves all 3 types set to |. This is part of the work towards moving smartlog upstream; currently smartlog injects extra nodes into the graph to indicate grandparent relationships (nodes elided).
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     1
# A minimal client for Mercurial's command server
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
     2
28355
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     3
from __future__ import absolute_import, print_function
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     4
import cStringIO
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     5
import os
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     6
import signal
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     7
import socket
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     8
import struct
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
     9
import subprocess
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
    10
import sys
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
    11
import time
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    12
22992
892b2b8c1b50 test-commandserver: allow check() to make connection in different way
Yuya Nishihara <yuya@tcha.org>
parents: 22991
diff changeset
    13
def connectpipe(path=None):
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    14
    cmdline = ['hg', 'serve', '--cmdserver', 'pipe']
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    15
    if path:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    16
        cmdline += ['-R', path]
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    17
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    18
    server = subprocess.Popen(cmdline, stdin=subprocess.PIPE,
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    19
                              stdout=subprocess.PIPE)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    20
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    21
    return server
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    22
22993
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    23
class unixconnection(object):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    24
    def __init__(self, sockpath):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    25
        self.sock = sock = socket.socket(socket.AF_UNIX)
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    26
        sock.connect(sockpath)
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    27
        self.stdin = sock.makefile('wb')
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    28
        self.stdout = sock.makefile('rb')
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    29
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    30
    def wait(self):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    31
        self.stdin.close()
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    32
        self.stdout.close()
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    33
        self.sock.close()
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    34
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    35
class unixserver(object):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    36
    def __init__(self, sockpath, logpath=None, repopath=None):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    37
        self.sockpath = sockpath
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    38
        cmdline = ['hg', 'serve', '--cmdserver', 'unix', '-a', sockpath]
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    39
        if repopath:
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    40
            cmdline += ['-R', repopath]
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    41
        if logpath:
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    42
            stdout = open(logpath, 'a')
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    43
            stderr = subprocess.STDOUT
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    44
        else:
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    45
            stdout = stderr = None
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    46
        self.server = subprocess.Popen(cmdline, stdout=stdout, stderr=stderr)
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    47
        # wait for listen()
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    48
        while self.server.poll() is None:
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    49
            if os.path.exists(sockpath):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    50
                break
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    51
            time.sleep(0.1)
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    52
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    53
    def connect(self):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    54
        return unixconnection(self.sockpath)
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    55
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    56
    def shutdown(self):
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    57
        os.kill(self.server.pid, signal.SIGTERM)
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    58
        self.server.wait()
24c5fd2894f8 test-commandserver: add connector for unix domain socket server
Yuya Nishihara <yuya@tcha.org>
parents: 22992
diff changeset
    59
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    60
def writeblock(server, data):
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    61
    server.stdin.write(struct.pack('>I', len(data)))
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    62
    server.stdin.write(data)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    63
    server.stdin.flush()
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    64
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    65
def readchannel(server):
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    66
    data = server.stdout.read(5)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    67
    if not data:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    68
        raise EOFError
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    69
    channel, length = struct.unpack('>cI', data)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    70
    if channel in 'IL':
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    71
        return channel, length
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    72
    else:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    73
        return channel, server.stdout.read(length)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    74
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    75
def sep(text):
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    76
    return text.replace('\\', '/')
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    77
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    78
def runcommand(server, args, output=sys.stdout, error=sys.stderr, input=None,
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    79
               outfilter=lambda x: x):
28355
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
    80
    print('*** runcommand', ' '.join(args))
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    81
    sys.stdout.flush()
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    82
    server.stdin.write('runcommand\n')
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    83
    writeblock(server, '\0'.join(args))
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    84
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    85
    if not input:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    86
        input = cStringIO.StringIO()
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    87
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    88
    while True:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    89
        ch, data = readchannel(server)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    90
        if ch == 'o':
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    91
            output.write(outfilter(data))
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    92
            output.flush()
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    93
        elif ch == 'e':
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    94
            error.write(data)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    95
            error.flush()
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    96
        elif ch == 'I':
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    97
            writeblock(server, input.read(data))
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    98
        elif ch == 'L':
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
    99
            writeblock(server, input.readline(data))
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   100
        elif ch == 'r':
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   101
            ret, = struct.unpack('>i', data)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   102
            if ret != 0:
28355
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
   103
                print(' [%d]' % ret)
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   104
            return ret
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   105
        else:
28355
897a4bbd578b hgclient: use absolute_import and print_function
Pulkit Goyal <7895pulkit@gmail.com>
parents: 22993
diff changeset
   106
            print("unexpected channel %c: %r" % (ch, data))
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   107
            if ch.isupper():
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   108
                return
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   109
22992
892b2b8c1b50 test-commandserver: allow check() to make connection in different way
Yuya Nishihara <yuya@tcha.org>
parents: 22991
diff changeset
   110
def check(func, connect=connectpipe):
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   111
    sys.stdout.flush()
22991
a94594f5d52f test-commandserver: remove unused repopath argument from check()
Yuya Nishihara <yuya@tcha.org>
parents: 22572
diff changeset
   112
    server = connect()
22566
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   113
    try:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   114
        return func(server)
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   115
    finally:
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   116
        server.stdin.close()
480b7fefbb08 test-commandserver: split helper functions to new hgclient module
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
   117
        server.wait()