Source

scripts / bitbucket-tools.py

#!/usr/bin/env python
"""
A simple Bitbucket-centric command-line URL opener.

2011 (c) Andrey Mikhaylenko

Environment variables:

    BB_REPO
        (required) repository at Bitbucket, e.g. ``neithere/argh``.
    BB_URL_DOC
        (optional) URL of project documentation (if any).
    BB_URL_REMOTE
        (optional) URL of the remote instance of the project (if any), e.g. the
        development server or production site.
    BB_URL_LOCAL
        (optional) URL of local development instance of the project. Default is
        ``http://127.0.0.1:8000``.
    BB_URL_LOCAL
        (optional) URL of the discussion group.
    BB_URL_COVERAGE
        (optional) URL of the code coverage results. Defaults to
        ``./coverage/index.html``.

"""
import os
import webbrowser
from argh import *


REPO = os.environ.get('BB_REPO')  # e.g. "neithere/argh"
assert REPO, ('Environment variable "BB_REPO" must be set '
              '(e.g. "neithere/argh")')

REPO_URL = 'https://bitbucket.org/{REPO}'.format(**locals())
DOC_URL = os.environ.get('BB_URL_DOC')
DEV_URL_REMOTE = os.environ.get('BB_URL_REMOTE')
DEV_URL_LOCAL = os.environ.get('BB_URL_LOCAL', 'http://127.0.0.1:8000')
GROUP_URL = os.environ.get('BB_URL_GROUP')
COVERAGE_URL = os.environ.get('BB_URL_COVERAGE',
                              os.path.abspath('./coverage/index.html'))

@arg('numbers', nargs='*')
@arg('-m', '--mine', default=False)
@arg('-o', '--open', default=False)
@arg('-M', '--milestone')
def issues(args):
    """Opens the project's issue tracker."""
    for num in args.numbers:
        print 'Opening ticket #{num}...'.format(**locals())
        url = '{REPO_URL}/issue/{num}/'.format(REPO_URL=REPO_URL, **locals())
        webbrowser.open_new_tab(url)
    if not args.numbers:
        print 'Opening list of issues...'
        url = '{REPO_URL}/issues/'.format(**globals())
        if args.mine:
            url += 'mine/'
        url += '?'
        if args.open:
            url += 'status=new&status=open'
        if args.milestone:
            url += '&milestone={0}'.format(args.milestone)
        url += '&sort=kind'
        webbrowser.open_new_tab(url)

@arg('path', default='index', nargs='?')
def doc(args):
    """Opens the project's documentation."""
    assert DOC_URL
    url = '{DOC_URL}{args.path}.html'.format(DOC_URL=DOC_URL, **locals())
    print 'opening ', url
    webbrowser.open_new_tab(url)

@arg('-r', '--remote', default=False)
@arg('path', default='/', nargs='?')
def dev(args):
    url = DEV_URL_REMOTE if args.remote else DEV_URL_LOCAL
    assert url
    if args.path:
        url += '/' + args.path.lstrip('/')
    print 'opening ', url
    webbrowser.open_new_tab(url)

def group(args):
    assert GROUP_URL
    print 'opening ', GROUP_URL
    webbrowser.open_new_tab(GROUP_URL)

def coverage(args):
    assert COVERAGE_URL
    print 'opening ', COVERAGE_URL
    webbrowser.open_new_tab(COVERAGE_URL)

if __name__=='__main__':
    p = ArghParser()
    p.add_commands([issues, doc, dev, group, coverage])
    p.dispatch()