summaryrefslogtreecommitdiffstats
path: root/git_taskrepo/sub_commands
diff options
context:
space:
mode:
Diffstat (limited to 'git_taskrepo/sub_commands')
-rw-r--r--git_taskrepo/sub_commands/__init__.py0
-rw-r--r--git_taskrepo/sub_commands/cmd_init.py149
-rw-r--r--git_taskrepo/sub_commands/cmd_list.py45
-rw-r--r--git_taskrepo/sub_commands/cmd_update.py29
4 files changed, 223 insertions, 0 deletions
diff --git a/git_taskrepo/sub_commands/__init__.py b/git_taskrepo/sub_commands/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/git_taskrepo/sub_commands/__init__.py
diff --git a/git_taskrepo/sub_commands/cmd_init.py b/git_taskrepo/sub_commands/cmd_init.py
new file mode 100644
index 0000000..592f7d3
--- /dev/null
+++ b/git_taskrepo/sub_commands/cmd_init.py
@@ -0,0 +1,149 @@
+
+# -*- 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://"):
+ 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)")
+ cur.execute("CREATE TABLE IF NOT EXISTS key_value_inc(task_id INTEGER, key TEXT NOT NULL, value TEXT NOT NULL)")
+ cur.execute("CREATE TABLE IF NOT EXISTS key_value_exc(task_id INTEGER, key TEXT NOT NULL, 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():
+ 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
+
+echo "post-commit"
+
+git diff --name-only 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
+
+echo "post-checkout"
+
+previous_head=$1
+new_head=$2
+is_branch_checkout=$3
+
+git diff --name-only $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")
+
+ # 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!")
diff --git a/git_taskrepo/sub_commands/cmd_list.py b/git_taskrepo/sub_commands/cmd_list.py
new file mode 100644
index 0000000..3381da5
--- /dev/null
+++ b/git_taskrepo/sub_commands/cmd_list.py
@@ -0,0 +1,45 @@
+
+# -*- coding: utf-8 -*-
+
+from git_taskrepo.command import Command
+
+class List(Command):
+ """List Tasks"""
+ enabled = True
+
+ def options(self):
+ self.parser.usage = "%%prog %s" % self.normalized_name
+ self.parser.add_option(
+ "--type",
+ metavar="TYPE",
+ action="append",
+ help="List tasks only of TYPE")
+
+ def run(self, *args, **kwargs):
+ self.set_repo(**kwargs)
+ self.set_taskrepo(**kwargs)
+ conn = self.taskrepo
+ with conn:
+ cur = conn.cursor()
+ cur.execute("SELECT origin FROM config")
+ result = cur.fetchone()
+ origin = result[0]
+
+ values = []
+ joins = []
+ where = []
+ extra = ""
+ if kwargs.get("type"):
+ for x in range(0, len(kwargs.get("type"))):
+ joins.append("LEFT JOIN key_value_inc AS kvi_%d ON kvi_%d.task_id = tasks.id" % (x, x))
+ where.append("kvi_%d.key='types' AND kvi_%d.value=?" % (x, x))
+ values.append(kwargs.get("type")[x])
+
+ if where:
+ extra = ' '.join(joins)
+ extra = "%s WHERE %s" % (extra, ' AND '.join(where))
+
+ cur.execute("SELECT name FROM tasks %s" % extra, values)
+ rows = cur.fetchall()
+ for row in rows:
+ print "%s" % row[0]
diff --git a/git_taskrepo/sub_commands/cmd_update.py b/git_taskrepo/sub_commands/cmd_update.py
new file mode 100644
index 0000000..91f30db
--- /dev/null
+++ b/git_taskrepo/sub_commands/cmd_update.py
@@ -0,0 +1,29 @@
+
+# -*- coding: utf-8 -*-
+
+import sys, os
+from git_taskrepo.command import Command
+from git_taskrepo.taskrepo import update_taskrepo, parse_testinfo, TRX
+
+class Update(Command):
+ """Update Task Repo"""
+ enabled = True
+
+ def options(self):
+ self.parser.usage = "%%prog %s [<path/to/task>]" % self.normalized_name
+
+ def run(self, *args, **kwargs):
+ self.set_repo(**kwargs)
+ self.set_taskrepo(**kwargs)
+ if len(args) >= 1:
+ taskpath = os.path.normpath(os.path.join(os.getcwd(), args[0]))
+ else:
+ taskpath = os.getcwd()
+ sys.stderr.write("[TaskRepo] Updating %s ... " % taskpath)
+ try:
+ update_taskrepo(self.repo, self.taskrepo, taskpath)
+ except TRX, e:
+ sys.stderr.write("FAIL (%s).\n" % e)
+ sys.exit(1)
+ else:
+ sys.stderr.write("done.\n")