Removed /usr/local from CDPATH
[clearscm.git] / cq / convertList.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4
5 =pod
6
7 =head1 NAME $RCSfile: convertList.pl,v $
8
9 This script allows you to convert a Clearquest Dynamic List to a stateless
10 table. You must specify what the dynamic list name is, the stateless table name
11 you wish to convert it to and the field name that serves as the key.
12
13 This script will note duplicate and skip them. It will not remove the dynamic
14 list.
15
16 =head1 VERSION
17
18 =over
19
20 =item Author
21
22 Andrew DeFaria <Andrew@ClearSCM.com>
23
24 =item Revision
25
26 $Revision: 2.2 $
27
28 =item Created:
29
30 Mon Oct 24 16:19:15 PDT 2011
31
32 =item Modified:
33
34 $Date: 2012/12/18 19:44:10 $
35
36 =back
37
38 =head1 SYNOPSIS
39
40  Usage: convertList.pl -list <list> -table <table> -field <field>
41                        [-u|sage] [-v|erbose] [-d|ebug]
42                        [-username <username>] [-password <password>]
43                        [-database <database>] [-dbset <dbset>]
44                        [-module] [-server <server>] [-port <port>]
45
46  Where:
47    -l|ist:      Dynamic list name to convert
48    -t|able:     Name of the stateless table to convert the dynamic
49                 list to
50    -field:      Name of the field to fill in with the values from
51                 -list
52
53    -usa|ge:     Displays usage
54    -v|erbose:   Be verbose
55    -de|bug:     Output debug messages
56
57    -use|rname:  Username to open database with (Default: from config file) 
58    -p|assword:  Password to open database with (Default: from config file) 
59    -da|tabase:  Database to open (Default: from config file)
60    -db|set:     Database Set to use (Default: from config file)
61    -m|odule:    Type of Clearquest module to use. Must be one of 'api', 
62                 'client', or 'rest'. The 'api' module can only be used if
63                 Clearquest is installed locally. The 'client' module can
64                 only be successful if a corresponding server is running. And
65                 the 'rest' module can only be used if a CQ Web server has
66                 been set up and configured (Default: rest)
67    -s|erver:    For module = client or rest this is the name of the server 
68                 that will be providing the service
69    -p|ort:      For module = client, this is the point on the server to talk
70                 through.
71
72 =head1 Options
73
74 Options are keep in the cq.conf file in etc. They specify the default options
75 listed below. Or you can export the option name to the env(1) to override the
76 defaults in cq.conf. Finally you can programmatically set the options when you
77 call new by passing in a %parms hash. To specify the %parms hash key remove the
78 CQ_ portion and lc the rest.
79
80 =for html <blockquote>
81
82 =over
83
84 =item CQ_WEBHOST
85
86 The web host to contact with leading http://
87
88 =item CQ_DATABASE
89
90 Name of database to connect to (Default: from config file)
91
92 =item CQ_USERNAME
93
94 User name to connect as (Default: from config file)
95
96 =item CQ_PASSWORD
97
98 Password for CQ_USERNAME
99
100 =item CQ_DBSET
101
102 Database Set name (Default: from config file)
103
104 =item CQ_SERVER
105
106 Clearquest::Server name to connect to (Default: from config file)
107
108 =item CQ_PORT
109
110 Clearquest::Server port to connect to (Default: from config file)
111
112 =back
113
114 =cut
115
116 use FindBin;
117 use Getopt::Long;
118
119 use lib "$FindBin::Bin/../lib";
120
121 use Clearquest;
122 use Display;
123 use Logger;
124 use TimeUtils;
125 use Utils;
126
127 my $VERSION  = '$Revision: 2.2 $';
128   ($VERSION) = ($VERSION =~ /\$Revision: (.*) /);
129
130 my (%opts, $cq, $log, %totals);
131
132 ## Main
133 local $| = 1;
134
135 my $startTime = time;
136
137 GetOptions (
138   \%opts,
139   usage   => sub { Usage },
140   verbose => sub { set_verbose },
141   debug   => sub { set_debug },
142   'module=s',
143   'username=s',
144   'database=s',
145   'password=s',
146   'dbset=s',
147   'list=s',
148   'table=s',
149   'field=s',
150   'server=s',
151   'port=i',
152 ) || Usage;
153
154 $log = Logger->new;
155
156 $log->msg ("$FindBin::Script v$VERSION");
157
158 Usage 'Must specify -list'  unless $opts{list};
159 Usage 'Must specify -table' unless $opts{table};
160 Usage 'Must specify -field' unless $opts{field};
161
162 # Translate any options to ones that the lib understands
163 $opts{CQ_USERNAME} = delete $opts{username};
164 $opts{CQ_PASSWORD} = delete $opts{password};
165 $opts{CQ_DATABASE} = delete $opts{database};
166 $opts{CQ_DBSET}    = delete $opts{dbset};
167 $opts{CQ_SERVER}   = delete $opts{server};
168 $opts{CQ_PORT}     = delete $opts{port};
169 $opts{CQ_MODULE}   = delete $opts{module};
170
171 $cq = Clearquest->new (%opts);
172
173 my $connection  = $cq->username . '@' . $cq->database . '/' . $cq->dbset; 
174    $connection .= ' (Server: ' . $cq->host . ':' . $cq->port . ')'
175      if ref $cq eq 'Clearquest::Client';
176
177 $log->msg ("Connecting to $connection...", 1);
178      
179 $cq->connect;
180
181 $log->msg (' connected');
182
183 foreach ($cq->getDynamicList ($opts{list})) {
184   verbose_nolf '.';
185
186   $totals{Processed}++;
187   
188   my $errmsg = $cq->add ($opts{table}, ($opts{field} => $_));
189   
190   if ($errmsg ne '') {
191     if ($errmsg =~ /duplicate entries in the database/ or
192         $errmsg =~ /Record with same displayname exists/) {
193       $totals{Duplicates}++;
194     } else {
195       $log->err ($errmsg);
196     } # if
197   } else {
198     $totals{Added}++;
199   } # if
200 } # foreach
201
202 $totals{Errors} = $log->errors;
203
204 error 'Errors occured - check ' . $log->fullname . ' for more info' 
205   if $totals{Errors};
206
207 Stats \%totals, $log;
208
209 display_duration $startTime, $log;
210
211 $cq->disconnect;
212
213 exit $log->errors;