1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# -*- coding: utf-8 -*-
from git_taskrepo.command import Command
from git_taskrepo.testinfo import ParserError
from git_taskrepo.taskrepo import update_taskrepo, parse_testinfo, TRX_Parse, TRX_TestInfo
import sys, os, commands
import sqlite3
def update_file(filename, line_to_add):
seen=False
updated=False
if os.path.exists(filename):
with open(filename) as f:
for line in f:
line = line.rstrip()
if line == line_to_add:
seen=True
break
if seen == False:
with open(filename,'a') as f:
f.write("%s\n" % line_to_add)
updated=True
return updated
class Init(Command):
"""Init Task Repo"""
enabled = True
def options(self):
self.parser.usage = "%%prog %s" % self.normalized_name
self.parser.add_option(
"--origin",
default=None,
help="Specify a read only origin. This is needed if your current origin is ssh://"
)
self.parser.add_option(
"--no-import",
default=False,
action="store_true",
help="Do not automatically import all tasks."
)
def run(self, *args, **kwargs):
# get our repo handler
self.set_repo(**kwargs)
# make sure origin is usable without authentication
if not kwargs.get("origin") and not getattr(self.repo.remotes, 'origin', None):
self.parser.error("Your git repo doesn't have a origin specified. use --origin")
remote = kwargs.get("origin") or self.repo.remotes.origin.url
if remote.startswith("ssh://") or remote.startswith("git+ssh://"):
self.parser.error("remote origin is %s, you must specify an origin that doesn't need authentication. use --origin" % remote)
print("Initializing taskrepo:")
# get our taskrepo handler
self.set_taskrepo(init=True)
# Initialize the DB with our tables
with self.taskrepo:
cur = self.taskrepo.cursor()
# Create tables if needed
cur.execute("CREATE TABLE IF NOT EXISTS config(origin TEXT NOT NULL)")
cur.execute("CREATE TABLE IF NOT EXISTS tasks(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, description TEXT, owner TEXT)")
cur.execute("CREATE TABLE IF NOT EXISTS types(task_id INTEGER, value TEXT NOT NULL)")
cur.execute("CREATE TABLE IF NOT EXISTS runfor(task_id INTEGER, value TEXT NOT NULL)")
cur.execute("CREATE TABLE IF NOT EXISTS bugs(task_id INTEGER, value TEXT NOT NULL)")
cur.execute("DELETE FROM config")
cur.execute("INSERT INTO config(origin) VALUES (?)", (remote,))
index = self.repo.index
# Add taskrepo.db to .gitignore
gitignore = "%s/.gitignore" % self.repo.working_tree_dir
if update_file(gitignore, "taskrepo.db"):
print(" - Added taskrepo.db to .gitignore")
index.add([gitignore])
# Add testinfo.desc to .gitignore
gitignore = "%s/.gitignore" % self.repo.working_tree_dir
if update_file(gitignore, "testinfo.desc"):
print(" - Added testinfo.desc to .gitignore")
index.add([gitignore])
# if we updated the repo then commit it
if self.repo.is_dirty(working_tree=False):
assert index.commit("Initialized to use git taskrepo.").type == 'commit'
print(" - committed to git")
# Add git hooks to automatically update taskrepo.db
post_commit_hook = """\
#!/bin/sh
git diff-tree -r --name-only --no-commit-id HEAD@{1} HEAD | \
while read file; do
echo "$file" | grep 'Makefile$' -q
if [ $? -eq 0 ]; then
dirname=$(dirname $file)
git taskrepo update $dirname
fi
done
"""
if not os.path.exists("%s/hooks/post-commit" % self.repo.git_dir):
with open("%s/hooks/post-commit" % self.repo.git_dir,'w') as f:
f.write(post_commit_hook)
os.chmod("%s/hooks/post-commit" % self.repo.git_dir, 0755)
print(" - Installed post-commit hook")
post_checkout_hook = """\
#!/bin/sh
previous_head=$1
new_head=$2
is_branch_checkout=$3
git diff-tree -r --name-only --no-commit-id $1 $2 | \
while read file; do
echo "$file" | grep 'Makefile$' -q
if [ $? -eq 0 ]; then
dirname=$(dirname $file)
git taskrepo update $dirname
fi
done
"""
if not os.path.exists("%s/hooks/post-checkout" % self.repo.git_dir):
with open("%s/hooks/post-checkout" % self.repo.git_dir,'w') as f:
f.write(post_checkout_hook)
os.chmod("%s/hooks/post-checkout" % self.repo.git_dir, 0755)
print(" - Installed post-checkout hook")
post_merge_hook = """\
#!/bin/sh
git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD | \
while read file; do
echo "$file" | grep 'Makefile$' -q
if [ $? -eq 0 ]; then
dirname=$(dirname $file)
git taskrepo update $dirname
fi
done
"""
if not os.path.exists("%s/hooks/post-merge" % self.repo.git_dir):
with open("%s/hooks/post-merge" % self.repo.git_dir,'w') as f:
f.write(post_merge_hook)
os.chmod("%s/hooks/post-merge" % self.repo.git_dir, 0755)
print(" - Installed post-merge hook")
# walk the git repo from working_tree_dir and import all tasks
# unless option --no-import was passed in.
if kwargs.get("no_import") is False:
print(" - Importing tasks into taskrepo")
for (dirpath, dirnames, filenames) in os.walk(self.repo.working_tree_dir):
try:
update_taskrepo(self.repo, self.taskrepo, dirpath)
except ParserError, e:
print >> sys.stderr, (" - %s FAIL (%s)." % (dirpath, e))
except TRX_TestInfo:
pass
else:
print(" - %s Imported." % dirpath)
print("Done!")
|