2016-08-20 01:48:00 +03:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
2013-10-02 07:32:53 +03:00
|
|
|
import os
|
2015-12-09 04:38:09 +02:00
|
|
|
import posixpath
|
2013-10-08 06:06:54 +03:00
|
|
|
import re
|
2016-08-20 01:48:00 +03:00
|
|
|
|
2013-10-03 17:58:07 +03:00
|
|
|
import ghdiff
|
2014-09-07 19:54:51 +03:00
|
|
|
import yaml
|
2016-07-06 03:29:01 +03:00
|
|
|
from dulwich.object_store import tree_lookup_path
|
2016-07-09 19:56:26 +03:00
|
|
|
from dulwich.repo import Repo, NotGitRepository
|
2016-08-20 01:48:00 +03:00
|
|
|
|
2014-08-30 18:06:12 +03:00
|
|
|
from realms import cache
|
2014-10-09 06:42:29 +03:00
|
|
|
from realms.lib.hook import HookMixin
|
2016-08-20 20:37:22 +03:00
|
|
|
from realms.lib.util import cname_to_filename, filename_to_cname
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-09-29 00:33:00 +03:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
class PageNotFound(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-10-09 06:42:29 +03:00
|
|
|
class Wiki(HookMixin):
|
2013-09-29 00:09:02 +03:00
|
|
|
path = None
|
|
|
|
base_path = '/'
|
|
|
|
default_ref = 'master'
|
|
|
|
default_committer_name = 'Anon'
|
|
|
|
default_committer_email = 'anon@anon.anon'
|
2013-10-02 04:50:48 +03:00
|
|
|
index_page = 'home'
|
2013-09-29 00:33:00 +03:00
|
|
|
repo = None
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2013-10-01 07:10:10 +03:00
|
|
|
def __init__(self, path):
|
|
|
|
try:
|
2016-07-09 19:56:26 +03:00
|
|
|
self.repo = Repo(path)
|
2013-10-05 00:42:45 +03:00
|
|
|
except NotGitRepository:
|
2016-07-09 19:56:26 +03:00
|
|
|
self.repo = Repo.init(path, mkdir=True)
|
2013-10-01 07:10:10 +03:00
|
|
|
|
2013-09-29 00:09:02 +03:00
|
|
|
self.path = path
|
|
|
|
|
2014-09-03 17:26:53 +03:00
|
|
|
def __repr__(self):
|
|
|
|
return "Wiki: %s" % self.path
|
|
|
|
|
2016-07-09 01:56:40 +03:00
|
|
|
def commit(self, name, email, message, files):
|
|
|
|
"""Commit to the underlying git repo.
|
|
|
|
|
|
|
|
:param name: Committer name
|
|
|
|
:param email: Committer email
|
|
|
|
:param message: Commit message
|
2016-07-09 19:56:26 +03:00
|
|
|
:param files: list of file names that will be staged for commit
|
2016-07-09 01:56:40 +03:00
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if isinstance(name, unicode):
|
|
|
|
name = name.encode('utf-8')
|
|
|
|
if isinstance(email, unicode):
|
|
|
|
email = email.encode('utf-8')
|
|
|
|
if isinstance(message, unicode):
|
|
|
|
message = message.encode('utf-8')
|
2016-07-09 19:56:26 +03:00
|
|
|
author = committer = "%s <%s>" % (name, email)
|
|
|
|
self.repo.stage(files)
|
|
|
|
return self.repo.do_commit(message=message,
|
|
|
|
committer=committer,
|
|
|
|
author=author)
|
2016-07-09 01:56:40 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def get_page(self, name, sha='HEAD'):
|
|
|
|
"""Get page data, partials, commit info.
|
2014-10-22 00:06:27 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
:param name: Name of page.
|
|
|
|
:param sha: Commit sha.
|
|
|
|
:return: dict
|
2014-10-22 00:06:27 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
"""
|
|
|
|
return WikiPage(name, self, sha=sha)
|
2015-12-11 08:56:27 +02:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def get_index(self):
|
|
|
|
"""Get repo index of head.
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
:return: list -- List of dicts
|
2014-10-21 01:27:38 +03:00
|
|
|
|
|
|
|
"""
|
2016-07-05 05:22:12 +03:00
|
|
|
rv = []
|
|
|
|
index = self.repo.open_index()
|
|
|
|
for name in index:
|
|
|
|
rv.append(dict(name=filename_to_cname(name),
|
|
|
|
filename=name,
|
|
|
|
ctime=index[name].ctime[0],
|
|
|
|
mtime=index[name].mtime[0],
|
|
|
|
sha=index[name].sha,
|
|
|
|
size=index[name].size))
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
return rv
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2013-10-15 23:32:17 +03:00
|
|
|
|
2016-07-08 20:53:09 +03:00
|
|
|
class WikiPage(HookMixin):
|
2016-07-05 05:22:12 +03:00
|
|
|
def __init__(self, name, wiki, sha='HEAD'):
|
|
|
|
self.name = name
|
|
|
|
self.filename = cname_to_filename(name)
|
|
|
|
self.sha = sha.encode('latin-1')
|
|
|
|
self.wiki = wiki
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
@property
|
|
|
|
def data(self):
|
|
|
|
cache_key = self._cache_key('data')
|
|
|
|
cached = cache.get(cache_key)
|
|
|
|
if cached:
|
|
|
|
return cached
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-09 19:56:26 +03:00
|
|
|
mode, sha = tree_lookup_path(self.wiki.repo.get_object, self.wiki.repo[self.sha].tree, self.filename)
|
|
|
|
data = self.wiki.repo[sha].data
|
2016-07-05 05:22:12 +03:00
|
|
|
cache.set(cache_key, data)
|
|
|
|
return data
|
2015-12-09 04:38:09 +02:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
@property
|
2016-07-07 09:49:52 +03:00
|
|
|
def history(self):
|
2016-07-05 05:22:12 +03:00
|
|
|
"""Get page history.
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-08 00:30:49 +03:00
|
|
|
History can take a long time to generate for repositories with many commits.
|
2016-07-10 08:00:43 +03:00
|
|
|
This returns an iterator to avoid having to load them all at once, and caches
|
|
|
|
as it goes.
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-08 00:30:49 +03:00
|
|
|
:return: iter -- Iterator over dicts
|
2016-07-09 19:56:26 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
"""
|
2016-07-10 08:00:43 +03:00
|
|
|
cache_head = []
|
|
|
|
cache_tail = cache.get(self._cache_key('history')) or [{'_cache_missing': True}]
|
|
|
|
while True:
|
|
|
|
if not cache_tail:
|
|
|
|
return
|
|
|
|
for index, cached_rev in enumerate(cache_tail):
|
|
|
|
if cached_rev.get("_cache_missing"):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
yield cached_rev
|
|
|
|
cache_head.extend(cache_tail[:index])
|
|
|
|
cache_tail = cache_tail[index+1:]
|
|
|
|
start_sha = cached_rev.get('sha')
|
|
|
|
end_sha = cache_tail[0].get('sha') if cache_tail else None
|
|
|
|
for rev in self._iter_revs(start_sha=start_sha, end_sha=end_sha, filename=cached_rev.get('filename')):
|
|
|
|
cache_head.append(rev)
|
|
|
|
placeholder = {
|
|
|
|
'_cache_missing': True,
|
|
|
|
'sha': rev['sha'],
|
|
|
|
'filename': rev['new_filename']
|
|
|
|
}
|
|
|
|
cache.set(self._cache_key('history'), cache_head + [placeholder] + cache_tail)
|
|
|
|
yield rev
|
|
|
|
cache.set(self._cache_key('history'), cache_head + cache_tail)
|
|
|
|
|
|
|
|
def _iter_revs(self, start_sha=None, end_sha=None, filename=None):
|
|
|
|
if end_sha:
|
|
|
|
end_sha = [end_sha]
|
2016-07-08 00:30:49 +03:00
|
|
|
if not len(self.wiki.repo.open_index()):
|
|
|
|
# Index is empty, no commits
|
|
|
|
return
|
2016-07-10 08:00:43 +03:00
|
|
|
filename = filename or self.filename
|
|
|
|
walker = iter(self.wiki.repo.get_walker(paths=[filename],
|
|
|
|
include=start_sha,
|
|
|
|
exclude=end_sha,
|
|
|
|
follow=True))
|
2016-07-08 00:30:49 +03:00
|
|
|
if start_sha:
|
|
|
|
# If we are not starting from HEAD, we already have the start commit
|
|
|
|
next(walker)
|
|
|
|
filename = self.filename
|
2016-07-05 05:22:12 +03:00
|
|
|
for entry in walker:
|
|
|
|
change_type = None
|
|
|
|
for change in entry.changes():
|
2016-07-10 08:00:43 +03:00
|
|
|
if change.new.path == filename:
|
|
|
|
filename = change.old.path
|
|
|
|
change_type = change.type
|
|
|
|
break
|
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
author_name, author_email = entry.commit.author.rstrip('>').split('<')
|
2016-07-10 08:00:43 +03:00
|
|
|
r = dict(author=author_name.strip(),
|
|
|
|
author_email=author_email,
|
|
|
|
time=entry.commit.author_time,
|
|
|
|
message=entry.commit.message,
|
|
|
|
sha=entry.commit.id,
|
|
|
|
type=change_type,
|
|
|
|
new_filename=change.new.path,
|
|
|
|
old_filename=change.old.path)
|
|
|
|
yield r
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-09 22:50:07 +03:00
|
|
|
@property
|
|
|
|
def history_cache(self):
|
|
|
|
"""Get info about the history cache.
|
|
|
|
|
|
|
|
:return: tuple -- (cached items, cache complete?)
|
|
|
|
"""
|
2016-07-10 08:00:43 +03:00
|
|
|
cached_revs = cache.get(self._cache_key('history'))
|
|
|
|
if not cached_revs:
|
|
|
|
return 0, False
|
|
|
|
elif any(rev.get('_cache_missing') for rev in cached_revs):
|
|
|
|
return len(cached_revs) - 1, False
|
|
|
|
return len(cached_revs), True
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
@property
|
2016-08-09 07:52:08 +03:00
|
|
|
def imports(self):
|
|
|
|
"""Names"""
|
2016-08-10 03:14:29 +03:00
|
|
|
meta = self._get_meta(self.data) or {}
|
2016-08-09 07:52:08 +03:00
|
|
|
return meta.get('import', [])
|
2016-07-05 05:22:12 +03:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _get_meta(content):
|
|
|
|
"""Get metadata from page if any.
|
2014-08-30 18:06:12 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
:param content: Page content
|
|
|
|
:return: dict
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
"""
|
2016-07-05 05:22:12 +03:00
|
|
|
if not content.startswith("---"):
|
2014-09-12 01:35:17 +03:00
|
|
|
return None
|
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
meta_end = re.search("\n(\.{3}|\-{3})", content)
|
2014-09-12 01:35:17 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
if not meta_end:
|
|
|
|
return None
|
2014-10-22 00:06:27 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
try:
|
|
|
|
return yaml.safe_load(content[0:meta_end.start()])
|
|
|
|
except Exception as e:
|
|
|
|
return {'error': e.message}
|
2014-10-22 00:06:27 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def _cache_key(self, property):
|
|
|
|
return 'page/{0}[{1}].{2}'.format(self.name, self.sha, property)
|
2014-09-12 01:35:17 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def _get_user(self, username, email):
|
|
|
|
if not username:
|
|
|
|
username = self.wiki.default_committer_name
|
2014-09-12 01:35:17 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
if not email:
|
|
|
|
email = self.wiki.default_committer_email
|
2014-09-12 01:35:17 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
return username, email
|
2013-09-29 00:09:02 +03:00
|
|
|
|
2016-07-10 08:00:43 +03:00
|
|
|
def _invalidate_cache(self, save_history=None):
|
|
|
|
cache.delete(self._cache_key('data'))
|
|
|
|
if save_history:
|
|
|
|
if not save_history[0].get('_cache_missing'):
|
|
|
|
save_history = [{'_cache_missing': True}] + save_history
|
|
|
|
cache.set(self._cache_key('history'), save_history)
|
|
|
|
else:
|
|
|
|
cache.delete(self._cache_key('history'))
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def delete(self, username=None, email=None, message=None):
|
2014-10-21 01:27:38 +03:00
|
|
|
"""Delete page.
|
2014-10-22 00:06:27 +03:00
|
|
|
:param username: Committer name
|
|
|
|
:param email: Committer email
|
2014-10-21 01:27:38 +03:00
|
|
|
:return: str -- Commit sha1
|
|
|
|
|
|
|
|
"""
|
2014-10-22 00:06:27 +03:00
|
|
|
username, email = self._get_user(username, email)
|
|
|
|
|
|
|
|
if not message:
|
2016-07-05 05:22:12 +03:00
|
|
|
message = "Deleted %s" % self.name
|
2015-09-28 07:57:56 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
os.remove(os.path.join(self.wiki.path, self.filename))
|
2016-07-09 01:56:40 +03:00
|
|
|
commit = self.wiki.commit(name=username,
|
|
|
|
email=email,
|
|
|
|
message=message,
|
|
|
|
files=[self.filename])
|
2016-07-10 08:00:43 +03:00
|
|
|
self._invalidate_cache()
|
2014-10-21 01:27:38 +03:00
|
|
|
return commit
|
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def rename(self, new_name, username=None, email=None, message=None):
|
|
|
|
"""Rename page.
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
:param new_name: New name of page.
|
|
|
|
:param username: Committer name
|
|
|
|
:param email: Committer email
|
|
|
|
:return: str -- Commit sha1
|
2014-10-21 01:27:38 +03:00
|
|
|
|
|
|
|
"""
|
2016-07-05 05:22:12 +03:00
|
|
|
assert self.sha == 'HEAD'
|
|
|
|
old_filename, new_filename = self.filename, cname_to_filename(new_name)
|
2016-07-09 19:56:26 +03:00
|
|
|
if old_filename not in self.wiki.repo.open_index():
|
2016-07-05 05:22:12 +03:00
|
|
|
# old doesn't exist
|
|
|
|
return None
|
|
|
|
elif old_filename == new_filename:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
# file is being overwritten, but that is ok, it's git!
|
|
|
|
pass
|
2014-09-03 17:26:53 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
username, email = self._get_user(username, email)
|
2014-08-20 18:28:25 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
if not message:
|
|
|
|
message = "Moved %s to %s" % (self.name, new_name)
|
2013-10-02 04:50:48 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
os.rename(os.path.join(self.wiki.path, old_filename), os.path.join(self.wiki.path, new_filename))
|
2016-07-09 01:56:40 +03:00
|
|
|
commit = self.wiki.commit(name=username,
|
|
|
|
email=email,
|
|
|
|
message=message,
|
|
|
|
files=[old_filename, new_filename])
|
2016-07-05 05:22:12 +03:00
|
|
|
|
2016-07-10 08:00:43 +03:00
|
|
|
old_history = cache.get(self._cache_key('history'))
|
|
|
|
self._invalidate_cache()
|
2016-07-05 05:22:12 +03:00
|
|
|
self.name = new_name
|
|
|
|
self.filename = new_filename
|
|
|
|
# We need to clear the cache for the new name as well as the old
|
2016-07-10 08:00:43 +03:00
|
|
|
self._invalidate_cache(save_history=old_history)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
return commit
|
|
|
|
|
2016-07-09 19:56:26 +03:00
|
|
|
def write(self, content, message=None, username=None, email=None):
|
2016-07-05 05:22:12 +03:00
|
|
|
"""Write page to git repo
|
|
|
|
|
|
|
|
:param content: Content of page.
|
|
|
|
:param message: Commit message.
|
|
|
|
:param username: Commit Name.
|
|
|
|
:param email: Commit Email.
|
|
|
|
:return: Git commit sha1.
|
2014-10-21 01:27:38 +03:00
|
|
|
"""
|
2016-07-05 05:22:12 +03:00
|
|
|
assert self.sha == 'HEAD'
|
|
|
|
dirname = posixpath.join(self.wiki.path, posixpath.dirname(self.filename))
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
if not os.path.exists(dirname):
|
|
|
|
os.makedirs(dirname)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
with open(self.wiki.path + "/" + self.filename, 'w') as f:
|
|
|
|
f.write(content)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
if not message:
|
|
|
|
message = "Updated %s" % self.name
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
username, email = self._get_user(username, email)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-09 01:56:40 +03:00
|
|
|
ret = self.wiki.commit(name=username,
|
|
|
|
email=email,
|
|
|
|
message=message,
|
|
|
|
files=[self.filename])
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-10 08:00:43 +03:00
|
|
|
old_history = cache.get(self._cache_key('history'))
|
|
|
|
self._invalidate_cache(save_history=old_history)
|
2016-07-05 05:22:12 +03:00
|
|
|
return ret
|
2013-10-03 17:58:07 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def revert(self, commit_sha, message, username, email):
|
|
|
|
"""Revert page to passed commit sha1
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
:param commit_sha: Commit Sha1 to revert to.
|
|
|
|
:param message: Commit message.
|
|
|
|
:param username: Committer name.
|
|
|
|
:param email: Committer email.
|
|
|
|
:return: Git commit sha1
|
2014-10-21 01:27:38 +03:00
|
|
|
|
|
|
|
"""
|
2016-07-05 05:22:12 +03:00
|
|
|
assert self.sha == 'HEAD'
|
|
|
|
new_page = self.wiki.get_page(self.name, commit_sha)
|
|
|
|
if not new_page:
|
|
|
|
raise PageNotFound('Commit not found')
|
2014-10-09 23:47:12 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
if not message:
|
2016-07-09 19:56:26 +03:00
|
|
|
message = "Revert '%s' to %s" % (self.name, commit_sha[:7])
|
2014-10-09 23:47:12 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
return self.write(new_page.data, message=message, username=username, email=email)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def compare(self, old_sha):
|
|
|
|
"""Compare two revisions of the same page.
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
:param old_sha: Older sha.
|
|
|
|
:return: str - Raw markup with styles
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
"""
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
# TODO: This could be effectively done in the browser
|
|
|
|
old = self.wiki.get_page(self.name, sha=old_sha)
|
|
|
|
return ghdiff.diff(old.data, self.data)
|
2014-09-12 01:35:17 +03:00
|
|
|
|
2016-07-05 05:22:12 +03:00
|
|
|
def __nonzero__(self):
|
2016-07-06 03:29:01 +03:00
|
|
|
# Verify this file is in the tree for the given commit sha
|
|
|
|
try:
|
|
|
|
tree_lookup_path(self.wiki.repo.get_object, self.wiki.repo[self.sha].tree, self.filename)
|
|
|
|
except KeyError:
|
|
|
|
# We'll get a KeyError if self.sha isn't in the repo, or if self.filename isn't in the tree of our commit
|
|
|
|
return False
|
|
|
|
return True
|