/hyde/ext/plugins/git.py

http://github.com/hyde/hyde · Python · 54 lines · 38 code · 5 blank · 11 comment · 11 complexity · 458b02caae96eb7300e989527c7f7127 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. """
  3. Contains classes and utilities to extract information from git repository
  4. """
  5. from hyde.plugin import Plugin
  6. import subprocess
  7. import traceback
  8. from dateutil.parser import parse
  9. class GitDatesPlugin(Plugin):
  10. """
  11. Extract creation and last modification date from git and include
  12. them in the meta data if they are set to "git". Creation date
  13. is put in `created` and last modification date in `modified`.
  14. """
  15. def __init__(self, site):
  16. super(GitDatesPlugin, self).__init__(site)
  17. def begin_site(self):
  18. """
  19. Initialize plugin. Retrieve dates from git
  20. """
  21. for node in self.site.content.walk():
  22. for resource in node.resources:
  23. created = None
  24. modified = None
  25. try:
  26. created = resource.meta.created
  27. modified = resource.meta.modified
  28. except AttributeError:
  29. pass
  30. # Everything is already overrided
  31. if created != "git" and modified != "git":
  32. continue
  33. # Run git log --pretty=%ai
  34. try:
  35. commits = subprocess.check_output(["git", "log", "--pretty=%ai",
  36. resource.path]).split("\n")
  37. except subprocess.CalledProcessError:
  38. self.logger.warning("Unable to get git history for [%s]" % resource)
  39. continue
  40. commits = commits[:-1]
  41. if not commits:
  42. self.logger.warning("No git history for [%s]" % resource)
  43. continue
  44. if created == "git":
  45. created = parse(commits[-1].strip())
  46. resource.meta.created = created
  47. if modified == "git":
  48. modified = parse(commits[0].strip())
  49. resource.meta.modified = modified