Example #1
0
def main(appinfo, args):
    """Show details of a ticket"""
    parser = optparse.OptionParser(
        usage='%prog show [OPTS] [TICKET]',
        )
    parser.add_option(
        '-v', '--verbose',
        help='show more information',
        action='count',
        )
    parser.add_option(
        '--format',
        help='show output in this format',
        )
    (options, args) = parser.parse_args(args)

    if options.verbose:
        raise NotImplementedError
    if options.format:
        raise NotImplementedError

    if not args:
        # TODO check for stored default ticket
        parser.error('no default ticket set')

    requested = list(args)
    with storage.Snapshot(repo='.') as s:
        while True:
            requested_ticket = requested.pop(0)
            try:
                ticket = lookup.match(
                    snapshot=s,
                    requested_ticket=requested_ticket,
                    )
            except lookup.TicketLookupError, e:
                print >>sys.stderr, '%s: %s' % (
                    os.path.basename(sys.argv[0]),
                    e,
                    )
                sys.exit(1)

            serialize.serialize(
                snapshot=s,
                ticket=ticket,
                fp=sys.stdout,
                )

            if not requested:
                break

            print '---'
Example #2
0
def main(appinfo, args):
    """Edit a ticket"""
    parser = optparse.OptionParser(
        usage='%prog edit [TICKET]',
        )
    parser.add_option(
        '--replace',
        help='replace ticket fully with input',
        action='store_true',
        dest='replace',
        )
    parser.add_option(
        '--update',
        help='update ticket based on input, preserving anything not included',
        action='store_false',
        dest='replace',
        )
    (options, args) = parser.parse_args(args)

    ticket = None
    if args:
        try:
            (ticket,) = args
        except ValueError:
            pass

    replace = options.replace

    with storage.Transaction(repo='.') as transaction:

        if ticket is not None:
            try:
                ticket = lookup.match(
                    snapshot=transaction,
                    requested_ticket=ticket,
                    )
            except lookup.TicketLookupError, e:
                print >>sys.stderr, '%s edit: %s' % (
                    os.path.basename(sys.argv[0]),
                    e,
                    )
                sys.exit(1)

        if sys.stdin.isatty():
            if ticket is None:
                print >>sys.stderr, \
                    '%s edit: Missing ticket argument for interactive editing' % (
                    os.path.basename(sys.argv[0]),
                        )
                sys.exit(1)

            print >>sys.stderr, 'bugit edit: editing ticket %s ...' % ticket
            if replace is None:
                replace = True
            filename = os.path.join(
                '.git',
                'bugit',
                'edit.%s.%d.ticket' % (
                    ticket,
                    os.getpid(),
                    ),
                )

            with file(filename, 'w+') as f:
                serialize.serialize(
                    snapshot=transaction,
                    ticket=ticket,
                    fp=f,
                    )
                f.seek(0)
                sha_before = _sha_fp(f)

            try:
                editor.run(
                    appinfo=appinfo,
                    filename=filename,
                    )
            except subprocess.CalledProcessError, e:
                print >>sys.stderr, \
                    '%s edit: editor failed with exit status %r' % (
                    os.path.basename(sys.argv[0]),
                    e.returncode,
                    )
                sys.exit(1)
            # now read back the file contents

            # non-optimal but who cares right now
            with file(filename) as f:
                sha_after = _sha_fp(f)
                if sha_before == sha_after:
                    print >>sys.stderr, \
                        '%s edit: file was not changed, discarding' % (
                        os.path.basename(sys.argv[0]),
                        )
                    sys.exit(0)

            def parse_file(filename):
                with file(filename) as f:
                    for x in parse.parse_ticket(f):
                        yield x
                os.unlink(filename)
            content = parse_file(filename)