Exemple #1
0
class Command(object):
    def __init__(self, name):
        self.name = name
        self.args = []
        self.opts = {}

    def __bytes__(self):
        cmd = "hg " + self.name
        if self.opts:
            for k, values in sorted(self.opts.iteritems()):
                for v in values:
                    if v:
                        cmd += " %s %s" % (k, v)
                    else:
                        cmd += " %s" % (k, )
        if self.args:
            cmd += " "
            cmd += " ".join(self.args)
        return cmd

    __str__ = encoding.strmethod(__bytes__)

    def append(self, value):
        self.args.append(value)

    def extend(self, values):
        self.args.extend(values)

    def __setitem__(self, key, value):
        values = self.opts.setdefault(key, [])
        values.append(value)

    def __and__(self, other):
        return AndCommand(self, other)
Exemple #2
0
class Unavailable(Exception):
    def __init__(self, msg, warn=True, invalidate=False):
        self.msg = msg
        self.warn = warn
        if self.msg == b'timed out waiting for response':
            self.warn = False
        self.invalidate = invalidate

    def __bytes__(self):
        if self.warn:
            return b'warning: Watchman unavailable: %s' % self.msg
        else:
            return b'Watchman unavailable: %s' % self.msg

    __str__ = encoding.strmethod(__bytes__)
Exemple #3
0
class journalentry(
        collections.namedtuple(
            u'journalentry',
            u'timestamp user command namespace name oldhashes newhashes')):
    """Individual journal entry

    * timestamp: a mercurial (time, timezone) tuple
    * user: the username that ran the command
    * namespace: the entry namespace, an opaque string
    * name: the name of the changed item, opaque string with meaning in the
      namespace
    * command: the hg command that triggered this record
    * oldhashes: a tuple of one or more binary hashes for the old location
    * newhashes: a tuple of one or more binary hashes for the new location

    Handles serialisation from and to the storage format. Fields are
    separated by newlines, hashes are written out in hex separated by commas,
    timestamp and timezone are separated by a space.

    """
    @classmethod
    def fromstorage(cls, line):
        (time, user, command, namespace, name, oldhashes,
         newhashes) = line.split('\n')
        timestamp, tz = time.split()
        timestamp, tz = float(timestamp), int(tz)
        oldhashes = tuple(node.bin(hash) for hash in oldhashes.split(','))
        newhashes = tuple(node.bin(hash) for hash in newhashes.split(','))
        return cls((timestamp, tz), user, command, namespace, name, oldhashes,
                   newhashes)

    def __bytes__(self):
        """bytes representation for storage"""
        time = ' '.join(map(pycompat.bytestr, self.timestamp))
        oldhashes = ','.join([node.hex(hash) for hash in self.oldhashes])
        newhashes = ','.join([node.hex(hash) for hash in self.newhashes])
        return '\n'.join((time, self.user, self.command, self.namespace,
                          self.name, oldhashes, newhashes))

    __str__ = encoding.strmethod(__bytes__)