# Perforce Defect Tracking Integration Project
#
#
# TEAMTRACK_QUERY.PY -- QUERY TEAMTRACK DATABASE
#
# Gareth Rees, Ravenbrook Limited, 2001-11-14
#
#
# 1. INTRODUCTION
#
# This script shows the contents of a table in TeamTrack. The purpose
# is to allow P4DTI Administrators to write configuration functions for
# replication and migration, for example replicate_p. See [RB
# 2000-08-10] for instructions as to its use.
#
# The intended readership is project developers.
#
# This document is not confidential.
from config_loader import config
import catalog
import getopt
import string
import sys
import teamtrack
def usage():
tables = teamtrack.table.keys()
tables.sort()
# "Usage:
#
# python teamtrack_query.py
# Print usage and list of tables.
#
# python teamtrack_query.py [options] TABLE [FIELD1 FIELD2 ...]
# Show contents of TABLE.
# Specify optional FIELDs to restrict the output.
#
# Options:
# -q QUERY Restrict output to records matching QUERY.
# -r Format output as series of records.
# -t Format output as table.
#
# TABLE should be one of %s."
print str(catalog.msg(1100, string.join(tables, ', ')))
sys.exit(1)
def main(argv):
options, args = getopt.getopt(argv[1:], 'q:rt',
['query=', 'record', 'table'])
query = ''
style = 'table'
for o, a in options:
if o in ['-q', '--query']:
query = a
elif o in ['-r', '--record']:
style = 'record'
elif o in ['-t', '--table']:
style = 'table'
else:
# "Unknown option: '%s'."
print str(catalog.msg(1103, o))
usage()
if len(args) == 0:
usage()
if not teamtrack.table.has_key(args[0]):
# "Table '%s' not recognized."
print str(catalog.msg(1101, args[0]))
usage()
server = teamtrack.connect(config.teamtrack_user,
config.teamtrack_password,
config.teamtrack_server)
if args[0] == 'CASES':
table = server.case_table_id()
else:
table = teamtrack.table[args[0]]
record = server.new_record(table)
if len(args) == 1:
fields = record.keys()
else:
fields = args[1:]
for f in fields:
if not record.has_key(f):
# "Table '%s' has no field named '%s'."
msg = catalog.msg(1102, (args[0], f))
print str(msg)
sys.exit(1)
records = server.query(table, query)
if style == 'record':
for r in records:
print r
else:
max_field_len = {}
for f in fields:
max_field_len[f] = len(f)
for r in records:
for f in fields:
try:
l = len(str(r[f]))
except:
l = 1
max_field_len[f] = max(max_field_len[f], l)
total_len = (reduce((lambda x,y: x+y), max_field_len.values())
+ 5 * len(fields) + 1)
separator = []
for f in fields:
separator.append('+')
separator.append('-' * (max_field_len[f] + 4))
separator.append('+\n')
separator = string.join(separator, '')
sys.stdout.write(separator)
for f in fields:
map(sys.stdout.write,
['| ', f, ' ' * (max_field_len[f] - len(f) + 2)])
sys.stdout.write('|\n')
sys.stdout.write(separator)
for r in records:
for f in fields:
try:
v = str(r[f])
except:
v = '?'
map(sys.stdout.write,
['| ', v, ' ' * (max_field_len[f] - len(v) + 2)])
sys.stdout.write('|\n')
sys.stdout.write(separator)
if __name__ == '__main__':
main(sys.argv)
# A. REFERENCES
#
# [RB 2000-08-10] "Perforce Defect Tracking Integration Administrator's
# Guide"; Richard Brooksby; Ravenbrook Limited; 2000-08-10;
# .
#
#
# B. DOCUMENT HISTORY
#
# 2001-11-14 GDR Created.
#
#
# C. COPYRIGHT AND LICENCE
#
# This file is copyright (c) 2001 Perforce Software, Inc. All
# rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.
#
#
# $Id: //info.ravenbrook.com/project/p4dti/branch/2001-11-22/bugzilla-parameters/code/replicator/teamtrack_query.py#3 $