Skip to content

Instantly share code, notes, and snippets.

@shomah4a
Created May 25, 2012 02:33
Show Gist options
  • Save shomah4a/2785431 to your computer and use it in GitHub Desktop.
Save shomah4a/2785431 to your computer and use it in GitHub Desktop.
リポジトリいじってみる
#-*- coding:utf-8 -*-
import time
import datetime
from mercurial import localrepo, match, ui as uimod
def load_repository(path):
u'''
ローカルディスクにあるリポジトリを開く
'''
ui = uimod.ui()
return localrepo.localrepository(ui, path)
def enum_files(repo, patterns=[]):
u'''
リポジトリにあるファイルを列挙
patterns は mercurial.match.match を参照
'''
return repo.walk(match.match('/', '/', patterns))
def _get_file_last_updates(repo):
u'''
ファイルごとの最終更新日を取得
消えているファイルも出ちゃう
'''
def mkupdates(rev):
u'''
リビジョンから更新日リストを取得
'''
ut, _ = rev.date()
date = datetime.datetime(*time.localtime(ut)[:-2])
return dict((x, date) for x in rev.files())
return reduce((lambda x, y: dict(x.items()+y.items())),
[mkupdates(rev) for rev in reversed(repo)], {})
def get_last_update_dict(repo, pattern):
u'''
ファイルごとの最終更新日を取得
pattern は mercurial.match.match を参照
'''
files = set(enum_files(repo, [pattern]))
updates = _get_file_last_updates(repo)
return dict(x for x in updates.iteritems()
if x[0] in files)
if __name__ == '__main__':
repo = load_repository(path_to_repos)
import pprint
pprint.pprint(get_last_update_dict(repo, r're:.*\.rst'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment