From b16e9a49d16fe22533483ec343950d08bdc3cce5 Mon Sep 17 00:00:00 2001 From: Lakshmi Vyasarajan Date: Tue, 30 Apr 2013 18:00:02 +0530 Subject: [PATCH] Refactor vcs dates to use a base plugin class. --- hyde/ext/plugins/vcs.py | 148 +++++++++++++++++++++------------------- 1 file changed, 77 insertions(+), 71 deletions(-) diff --git a/hyde/ext/plugins/vcs.py b/hyde/ext/plugins/vcs.py index 4f59949..f6b6734 100644 --- a/hyde/ext/plugins/vcs.py +++ b/hyde/ext/plugins/vcs.py @@ -11,24 +11,15 @@ import os.path import subprocess -# -# Git Dates -# - -class GitDatesPlugin(Plugin): +class VCSDatesPlugin(Plugin): """ - Extract creation and last modification date from git and include - them in the meta data if they are set to "git". Creation date - is put in `created` and last modification date in `modified`. + Base class for getting resource timestamps from VCS. """ - - def __init__(self, site): - super(GitDatesPlugin, self).__init__(site) + def __init__(self, site, vcs_name='vcs'): + super(VCSDatesPlugin, self).__init__(site) + self.vcs_name = vcs_name def begin_site(self): - """ - Initialize plugin. Retrieve dates from git - """ for node in self.site.content.walk(): for resource in node.resources: created = None @@ -38,80 +29,95 @@ class GitDatesPlugin(Plugin): modified = resource.meta.modified except AttributeError: pass + # Everything is already overrided - if created != "git" and modified != "git": + if created != self.vcs_name and modified != self.vcs_name: continue - # Run git log --pretty=%ai - try: - commits = subprocess.Popen(["git", "log", "--pretty=%ai", resource.path], stdout=subprocess.PIPE).communicate() - commits = commits[0].split("\n") - if not commits: - self.logger.warning("No git history for [%s]" % resource) - except subprocess.CalledProcessError: - self.logger.warning("Unable to get git history for [%s]" % resource) - commits = None + + date_created, date_modified = self.get_dates(resource) if created == "git": - if commits: - created = parse(commits[-1].strip()) - else: - created = datetime.utcfromtimestamp(os.path.getctime(resource.path)) + created = date_created or \ + datetime.utcfromtimestamp( + os.path.getctime(resource.path)) created = created.replace(tzinfo=None) resource.meta.created = created + if modified == "git": - if commits: - modified = parse(commits[0].strip()) - else: - modified = datetime.utcfromtimestamp(os.path.getmtime(resource.path)) + modified = date_modified or resource.source.last_modified modified = modified.replace(tzinfo=None) resource.meta.modified = modified + def get_dates(self): + """ + Extract creation and last modification date from the vcs and include + them in the meta data if they are set to "". Creation date + is put in `created` and last modification date in `modified`. + """ + return None, None + # -# Mercurial Dates +# Git Dates # +class GitDatesPlugin(VCSDatesPlugin): + def __init__(self, site): + super(GitDatesPlugin, self).__init__(site, 'git') -class MercurialDatesPlugin(Plugin): - """ - Extract creation and last modification date from mercurial and - include them in the meta data if they are set to "hg". Creation - date is put in `created` and last modification date in `modified`. - """ + def get_dates(self, resource): + """ + Retrieve dates from git + """ + # Run git log --pretty=%ai + try: + commits = subprocess.check_output([ + "git", + "log", + "--pretty=%ai", + resource.path + ]).split("\n") + commits = commits[:-1] + except subprocess.CalledProcessError: + self.logger.warning("Unable to get git history for [%s]" % resource) + commits = None + + if commits: + created = parse(commits[-1].strip()) + modified = parse(commits[0].strip()) + else: + self.logger.warning("No git history for [%s]" % resource) + created, modified = None, None + + self.logger.info("Git dates: %s", (resource, created, modified)) + return created, modified + +# +# Mercurial Dates +# +class MercurialDatesPlugin(VCSDatesPlugin): def __init__(self, site): - super(MercurialDatesPlugin, self).__init__(site) + super(MercurialDatesPlugin, self).__init__(site, 'hg') - def begin_site(self): + def get_dates(self, resource): """ - Initialize plugin. Retrieve dates from mercurial + Retrieve dates from mercurial """ - for node in self.site.content.walk(): - for resource in node.resources: - created = None - modified = None - try: - created = resource.meta.created - modified = resource.meta.modified - except AttributeError: - pass - # Everything is already overrided - if created != "hg" and modified != "hg": - continue - # Run hg log --template={date|isodatesec} - try: - commits = subprocess.check_output(["hg", "log", "--template={date|isodatesec}\n", - resource.path]).split('\n') - except subprocess.CalledProcessError: - self.logger.warning("Unable to get mercurial history for [%s]" % resource) - continue - commits = commits[:-1] - if not commits: - self.logger.warning("No mercurial history for [%s]" % resource) - continue - if created == "hg": - created = parse(commits[-1].strip()) - resource.meta.created = created - if modified == "hg": - modified = parse(commits[0].strip()) - resource.meta.modified = modified + # Run hg log --template={date|isodatesec} + try: + commits = subprocess.check_output([ + "hg", "log", "--template={date|isodatesec}\n", + resource.path]).split('\n') + commits = commits[:-1] + except subprocess.CalledProcessError: + self.logger.warning("Unable to get mercurial history for [%s]" + % resource) + commits = None + + if not commits: + self.logger.warning("No mercurial history for [%s]" % resource) + return None, None + created = parse(commits[-1].strip()) + modified = parse(commits[0].strip()) + return created, modified