grim/hggravatar

7d4789338ec6
Parents
Children 13a2db7b58db
Initial revision, basics work, needs some additional work though
  • +3 -0
    .hgignore
  • +91 -0
    hggravatar.py
  • --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/.hgignore Thu Dec 20 00:03:27 2018 -0600
    @@ -0,0 +1,3 @@
    +syntax: glob
    +*.pyc
    +
    --- /dev/null Thu Jan 01 00:00:00 1970 +0000
    +++ b/hggravatar.py Thu Dec 20 00:03:27 2018 -0600
    @@ -0,0 +1,91 @@
    +# vi:et:ts=4 st=4 sts=4
    +
    +import errno
    +import hashlib
    +import json
    +import os
    +import os.path
    +
    +from mercurial.i18n import _
    +from mercurial import (
    + cmdutil,
    + registrar,
    + scmutil,
    + util,
    +)
    +from mercurial.utils import stringutil
    +
    +urlreq = util.urlreq
    +
    +cmdtable = {}
    +command = registrar.command(cmdtable)
    +
    +def find_authors(ui, repo, *pats, **opts):
    + ui.write('caching authors ... ')
    +
    + # now walk the dag
    + m = scmutil.match(repo[None], pats, opts)
    +
    + authors = {}
    +
    + def prep(ctx, fns):
    + """callback for cmdutil.walkchangerevs that just adds unique email
    + address to the authors dict."""
    + email = stringutil.email(ctx.user())
    + normalized = email.lower().strip()
    + if not email in authors:
    + authors[email] = hashlib.md5(normalized).hexdigest()
    +
    + for ctx in cmdutil.walkchangerevs(repo, m, opts, prep):
    + # we handle everything in the callback closure so nothing to do here
    + continue
    +
    + try:
    + os.makedirs(opts.get('output'))
    + except os.error as err:
    + if err.errno != errno.EEXIST:
    + raise
    +
    + map_file = os.path.join(opts.get('output'), 'author_map')
    + with open(map_file, 'w') as ofp:
    + json.dump(authors, ofp, indent=2)
    +
    + ui.write('done.\n')
    +
    + return authors
    +
    +
    +def save_avatars(ui, repo, authors, *pats, **opts):
    + """this function downloads all the avatars and stores them in the output
    + directory as pngs. """
    +
    + url = opts.get('url')
    +
    + for author, key in authors.items():
    + ui.write('downloading image for %s ... ' % author)
    +
    + filename = '%s.png' % key
    +
    + uri = os.path.join(url, filename)
    +
    + img = urlreq.urlopen(uri)
    + with open(os.path.join(opts.get('output'), filename), 'w') as ofp:
    + ofp.write(img.read())
    + img.close()
    +
    + ui.write('done.\n')
    +
    +
    +@command(
    + 'gravatar',
    + [
    + ('o', 'output', '.hg/cache/gravatar/', _('output directory'), _('DIR')),
    + ('u', 'url', 'https://www.gravatar.com/avatar', _('gravatar url'), _('URL')),
    + ],
    + _("hg gravatar [-u URL] [-o DIR]"),
    + inferrepo=True,
    +)
    +def gravatar(ui, repo, *pats, **opts):
    + authors = find_authors(ui, repo, *pats, **opts)
    + save_avatars(ui, repo, authors, *pats, **opts)
    +