If an html file is empty, pretend it has a body
[booki.git] / tools / rcs / core.py
blobf0a2c145cb0b91a7c95b2ef225cc3024ce505e19
1 import sys, os, subprocess
4 class RCSError(Exception):
5 pass
7 class ParseError(Exception):
8 pass
10 class GitContext:
11 def __init__(self, work_tree, git_dir=None, strip_directories=True):
12 self.work_tree = work_tree
13 if git_dir is not None:
14 self.git_dir = git_dir
15 else:
16 self.git_dir = os.path.join(work_tree, '.git')
17 self.strip_dir = strip_directories
19 def command(self, *args):
20 cmd = ["git", "--git-dir=" + self.git_dir, "--work-tree=" + self.work_tree,]
21 cmd.extend(args)
22 subprocess.check_call(cmd)
25 class Version:
26 contents = ''
27 gitref = 'HEAD'
28 branches = set()
30 def __init__(self, name, revision, date, author, context=None):
31 self.name = os.path.normpath(name)
32 self.revision = revision
33 self.author = author
34 self.set_date(date)
35 self.branch = self.gitref
36 self.git = context
38 def _data_blob(self, data, write=sys.stdout.write):
39 write("data %s\n" % len(data))
40 write(data)
41 write('\n')
43 def to_git(self, branch=None, write=sys.stdout.write, strip_dir=False):
44 if strip_dir:
45 filename = os.path.basename(self.name)
46 else:
47 filename = self.name
49 if branch is None:
50 branch = self.branch
51 write("commit %s\n" % branch)
52 write("committer %s <%s@flossmanuals.net> %s +0000\n" %
53 (self.author, self.author, self.date))
54 self._data_blob("TWiki import: %s revision %s" % (self.name, self.revision))
55 write('M 644 inline %s\n' % (filename))
56 self._data_blob(self.contents)
57 write("\n")
59 def to_git_slow(self, branch=None, write=None, strip_dir=False):
60 """Write to git via the working tree"""
61 if strip_dir:
62 filename = os.path.basename(self.name)
63 else:
64 filename = self.name
66 if branch is not None and branch != self.branch:
67 if branch in self.branches:
68 self.git.command("checkout", branch)
69 else:
70 self.git.command("checkout", "-l", "-b", branch)
71 self.branches.add(branch)
73 # put the new version there
74 f = open(filename, 'w')
75 f.write(self.contents)
76 f.close()
77 subprocess.check_call(["git", "--git-dir=%s"% self.gitdir,
78 "--work-tree=%s" % self.worktree,
79 "commit",
80 "--author=%s@flossmanuals.net" % self.author,
81 "-m", "TWiki import: %s rev. %s" % (self.name, self.revision),
82 filename])
85 def __str__(self):
86 return ("<File %s revision %s branch: %s (%s, %s)>" %
87 (self.name, self.revision, self.branch, self.author, self.date))
89 __repr__ = __str__
91 def set_date(self, date):
92 raise NotImplementedError("subclass me, please")
95 def twiki_clean(lines):
96 """Remove TWiki Metadata"""
97 data = []
98 meta = []
99 for line in lines:
100 if not line.startswith('%META:'):
101 data.append(line)
102 else:
103 meta.append(line)
104 return data, meta
107 def thoeny_filter(revision):
108 """Reject old commits and commits by known twiki authors"""
109 if (revision.author in ('PeterThoeny', 'thoeny')
110 or int(revision.date) < 1050000000):
111 return False
112 return True