Browse Source

Refactor vcs dates to use a base plugin class.

main
Lakshmi Vyasarajan 11 years ago
parent
commit
b16e9a49d1
1 changed files with 77 additions and 71 deletions
  1. +77
    -71
      hyde/ext/plugins/vcs.py

+ 77
- 71
hyde/ext/plugins/vcs.py View File

@@ -11,24 +11,15 @@ import os.path
import subprocess 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): def begin_site(self):
"""
Initialize plugin. Retrieve dates from git
"""
for node in self.site.content.walk(): for node in self.site.content.walk():
for resource in node.resources: for resource in node.resources:
created = None created = None
@@ -38,80 +29,95 @@ class GitDatesPlugin(Plugin):
modified = resource.meta.modified modified = resource.meta.modified
except AttributeError: except AttributeError:
pass pass

# Everything is already overrided # Everything is already overrided
if created != "git" and modified != "git":
if created != self.vcs_name and modified != self.vcs_name:
continue 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 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) created = created.replace(tzinfo=None)
resource.meta.created = created resource.meta.created = created

if modified == "git": 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) modified = modified.replace(tzinfo=None)
resource.meta.modified = modified 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 "<vcs_name>". 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): 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

Loading…
Cancel
Save