PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/hyde/ext/plugins/git.py

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