| 1 | from pysqlite2 import dbapi2 as sqlite |
|---|
| 2 | from datetime import datetime, timedelta |
|---|
| 3 | from time import mktime |
|---|
| 4 | import os |
|---|
| 5 | |
|---|
| 6 | tracDir = '/var/trac' |
|---|
| 7 | projectDBLocationFormat = '/var/trac/%s/db/trac.db' |
|---|
| 8 | # all directories in the trac directory |
|---|
| 9 | projects = [f for f in os.listdir(tracDir) if os.path.isdir('/'.join ([tracDir, f]))] |
|---|
| 10 | |
|---|
| 11 | def makeDb(proj): |
|---|
| 12 | return projectDBLocationFormat % proj |
|---|
| 13 | |
|---|
| 14 | def collectResultsFromAllTracs( sql ): |
|---|
| 15 | lst = [] |
|---|
| 16 | for proj in projects: |
|---|
| 17 | try: |
|---|
| 18 | lst.extend([( proj , getResultSet( proj, sql ))]) |
|---|
| 19 | except Exception, e: |
|---|
| 20 | print "collectResultsFromAllTracs: sql failed to execute on %s : %s " % (proj, e.args) |
|---|
| 21 | |
|---|
| 22 | return lst |
|---|
| 23 | |
|---|
| 24 | def executeAgainstAllTracs( sql ): |
|---|
| 25 | for proj in projects: |
|---|
| 26 | try: |
|---|
| 27 | executeNonQuery( proj, sql ) |
|---|
| 28 | except: |
|---|
| 29 | print "executeAgainstAllTracs: sql failed to execute on %s" % proj |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | def executeNonQuery(proj, sql, *params): |
|---|
| 33 | """Executes the query on the given project""" |
|---|
| 34 | con = sqlite.connect(makeDb(proj)) |
|---|
| 35 | cur = con.cursor() |
|---|
| 36 | try: |
|---|
| 37 | cur.execute(sql, params) |
|---|
| 38 | con.commit() |
|---|
| 39 | finally: |
|---|
| 40 | cur.close() |
|---|
| 41 | con.close() |
|---|
| 42 | |
|---|
| 43 | def getColumnAsList(db, sql, col=0, *params): |
|---|
| 44 | return [valueList[col] for valueList in get_all(db, sql, *params)[1]] |
|---|
| 45 | |
|---|
| 46 | def getScalar(proj , sql, col=0, *params): |
|---|
| 47 | db = sqlite.connect(makeDb(proj)) |
|---|
| 48 | cur = db.cursor() |
|---|
| 49 | try: |
|---|
| 50 | cur.execute(sql, params) |
|---|
| 51 | data = cur.fetchone() |
|---|
| 52 | finally: |
|---|
| 53 | cur.close() |
|---|
| 54 | return data[col] |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | def getVector(proj, sql, *params): |
|---|
| 58 | db = sqlite.connect(makeDb(proj)) |
|---|
| 59 | cur = db.cursor() |
|---|
| 60 | try: |
|---|
| 61 | cur.execute(sql, params) |
|---|
| 62 | data = cur.fetchone() |
|---|
| 63 | finally: |
|---|
| 64 | cur.close() |
|---|
| 65 | return data |
|---|
| 66 | |
|---|
| 67 | def getAll(proj, sql, *params): |
|---|
| 68 | """Executes the query and returns the (description, data)""" |
|---|
| 69 | con = sqlite.connect(makeDb(proj)) |
|---|
| 70 | cur = con.cursor() |
|---|
| 71 | try: |
|---|
| 72 | cur.execute(sql, params) |
|---|
| 73 | data = cur.fetchall() |
|---|
| 74 | desc = cur.description |
|---|
| 75 | finally: |
|---|
| 76 | cur.close() |
|---|
| 77 | con.close() |
|---|
| 78 | return (desc, data) |
|---|
| 79 | |
|---|
| 80 | def getResultSet(proj, sql, *params): |
|---|
| 81 | """Executes the query and returns a Result Set""" |
|---|
| 82 | return ResultSet(getAll(proj, sql, *params)) |
|---|
| 83 | |
|---|
| 84 | def _columnName( columnDescription ): |
|---|
| 85 | """ given a the columnHeader from the result set from getAll gives you the column Headers """ |
|---|
| 86 | return columnDescription[0]; |
|---|
| 87 | |
|---|
| 88 | class ResultSet: |
|---|
| 89 | """ the result of calling getResultSet """ |
|---|
| 90 | def __init__ (self, (columnDescription, rows)): |
|---|
| 91 | self.columnDescription, self.rows = columnDescription, rows |
|---|
| 92 | self.columnNames = [_columnName(_) for _ in self.columnDescription] |
|---|
| 93 | self.columnMap = self.getColumnMap() |
|---|
| 94 | |
|---|
| 95 | def getColumnMap ( self ): |
|---|
| 96 | """This function will take the result set from getAll and will |
|---|
| 97 | return a hash of the column names to their index """ |
|---|
| 98 | h = {} |
|---|
| 99 | i = 0 |
|---|
| 100 | if self.columnDescription: |
|---|
| 101 | for col in self.columnNames: |
|---|
| 102 | h[ col ] = i |
|---|
| 103 | i+=1 |
|---|
| 104 | return h; |
|---|
| 105 | |
|---|
| 106 | def value(self, col, row ): |
|---|
| 107 | """ given a row(list or idx) and a column( name or idx ), retrieve the appropriate value""" |
|---|
| 108 | tcol = type(col) |
|---|
| 109 | trow = type(row) |
|---|
| 110 | if tcol == str: |
|---|
| 111 | if(trow == list or trow == tuple): |
|---|
| 112 | return row[self.columnMap[col]] |
|---|
| 113 | elif(trow == int): |
|---|
| 114 | return self.rows[row][self.columnMap[col]] |
|---|
| 115 | else: |
|---|
| 116 | print ("rs.value Type Failed col:%s row:%s" % (type(col), type(row))) |
|---|
| 117 | elif tcol == int: |
|---|
| 118 | if(trow == list or trow == tuple): |
|---|
| 119 | return row[col] |
|---|
| 120 | elif(trow == int): |
|---|
| 121 | return self.rows[row][col] |
|---|
| 122 | else: |
|---|
| 123 | print ("rs.value Type Failed col:%s row:%s" % (type(col), type(row))) |
|---|
| 124 | else: |
|---|
| 125 | print ("rs.value Type Failed col:%s row:%s" % (type(col), type(row))) |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|