A fork of hyde, the static site generation. Some patches will be pushed upstream.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 lines
2.4 KiB

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