| 1 | ###################################################################### | ||
| 2 | # Copyright (c)2011, David L. Armstrong. | ||
| 3 | # | ||
| 4 | # P4::OO::_Connection::P4PERL.pm | ||
| 5 | # | ||
| 6 | # See COPYRIGHT AND LICENSE section in pod text below for usage | ||
| 7 | # and distribution rights. | ||
| 8 | # | ||
| 9 | ###################################################################### | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | P4::OO::_Connection::P4PERL - P4::OO interface to P4PERL | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | =head1 ENVIRONMENT | ||
| 20 | |||
| 21 | =head1 SEE ALSO | ||
| 22 | |||
| 23 | =cut | ||
| 24 | |||
| 25 | ###################################################################### | ||
| 26 | # Package Initialization | ||
| 27 | # | ||
| 28 | package P4::OO::_Connection::P4PERL; | ||
| 29 | our $VERSION = '0.00_02'; | ||
| 30 | use strict; | ||
| 31 | use base ( 'P4::OO::_Connection' ); | ||
| 32 | |||
| 33 | |||
| 34 | ###################################################################### | ||
| 35 | # Includes | ||
| 36 | # | ||
| 37 | use Readonly; | ||
| 38 | |||
| 39 | |||
| 40 | ###################################################################### | ||
| 41 | # Globals | ||
| 42 | # | ||
| 43 | # P4PERL_SPEC_COMMANDS helps make the somwhat inconsistent | ||
| 44 | # Perforce output fall into neatly defined and consistent objects | ||
| 45 | Readonly my %P4PERL_SPEC_COMMANDS => { | ||
| 46 | 'branch' => { 'specCmd' => 'branch', | ||
| 47 | 'singularID' => 'branch', | ||
| 48 | 'queryCmd' => 'branches', | ||
| 49 | 'pluralID' => 'branch', | ||
| 50 | 'idAttr' => 'branch', | ||
| 51 | 'p4ooType' => 'P4::OO::Branch', | ||
| 52 | 'pluralType' => 'P4::OO::BranchSet', | ||
| 53 | 'queryOptions' => { 'user' => [ '-u', 1 ], | ||
| 54 | 'nameFilter' => [ '-e', 1 ], | ||
| 55 | }, | ||
| 56 | }, | ||
| 57 | 'change' => { 'specCmd' => 'change', | ||
| 58 | 'singularID' => 'change', | ||
| 59 | 'queryCmd' => 'changes', | ||
| 60 | 'pluralID' => 'change', | ||
| 61 | 'idAttr' => 'change', | ||
| 62 | 'p4ooType' => 'P4::OO::Change', | ||
| 63 | 'pluralType' => 'P4::OO::ChangeSet', | ||
| 64 | 'queryOptions' => { 'client' => [ '-c', 1 ], | ||
| 65 | 'status' => [ '-s', 1 ], | ||
| 66 | 'user' => [ '-u', 1 ], | ||
| 67 | 'files' => undef, | ||
| 68 | }, | ||
| 69 | }, | ||
| 70 | # Changelist is the same as Change | ||
| 71 | 'changelist' => { 'specCmd' => 'change', | ||
| 72 | 'singularID' => 'change', | ||
| 73 | 'queryCmd' => 'changes', | ||
| 74 | 'pluralID' => 'change', | ||
| 75 | 'idAttr' => 'change', | ||
| 76 | 'p4ooType' => 'P4::OO::Change', | ||
| 77 | 'pluralType' => 'P4::OO::ChangeSet', | ||
| 78 | 'queryOptions' => { 'client' => [ '-c', 1 ], | ||
| 79 | 'status' => [ '-s', 1 ], | ||
| 80 | 'user' => [ '-u', 1 ], | ||
| 81 | 'files' => undef, | ||
| 82 | }, | ||
| 83 | }, | ||
| 84 | 'client' => { 'specCmd' => 'client', | ||
| 85 | 'singularID' => 'client', | ||
| 86 | 'queryCmd' => 'clients', | ||
| 87 | 'pluralID' => 'client', | ||
| 88 | 'idAttr' => 'client', | ||
| 89 | 'p4ooType' => 'P4::OO::Client', | ||
| 90 | 'pluralType' => 'P4::OO::ClientSet', | ||
| 91 | 'queryOptions' => { 'user' => [ '-u', 1 ], | ||
| 92 | 'nameFilter' => [ '-e', 1 ], | ||
| 93 | }, | ||
| 94 | }, | ||
| 95 | # Workspace is the same as Client | ||
| 96 | 'workspace' => { 'specCmd' => 'client', | ||
| 97 | 'singularID' => 'client', | ||
| 98 | 'queryCmd' => 'clients', | ||
| 99 | 'pluralID' => 'client', | ||
| 100 | 'idAttr' => 'client', | ||
| 101 | 'p4ooType' => 'P4::OO::Client', | ||
| 102 | 'pluralType' => 'P4::OO::ClientSet', | ||
| 103 | 'queryOptions' => { 'user' => [ '-u', 1 ], | ||
| 104 | 'nameFilter' => [ '-e', 1 ], | ||
| 105 | }, | ||
| 106 | }, | ||
| 107 | 'depot' => { 'specCmd' => 'depot', | ||
| 108 | 'singularID' => 'depot', | ||
| 109 | 'queryCmd' => 'depots', | ||
| 110 | 'pluralID' => 'name', | ||
| 111 | 'idAttr' => 'depot', | ||
| 112 | 'p4ooType' => 'P4::OO::Depot', | ||
| 113 | 'pluralType' => 'P4::OO::DepotSet', | ||
| 114 | 'queryOptions' => {}, | ||
| 115 | }, | ||
| 116 | 'group' => { 'specCmd' => 'group', | ||
| 117 | 'singularID' => 'group', | ||
| 118 | 'queryCmd' => 'groups', | ||
| 119 | 'pluralID' => 'group', | ||
| 120 | 'idAttr' => 'group', | ||
| 121 | 'p4ooType' => 'P4::OO::Group', | ||
| 122 | 'pluralType' => 'P4::OO::GroupSet', | ||
| 123 | 'queryOptions' => { 'user' => undef, | ||
| 124 | 'group' => undef, | ||
| 125 | }, | ||
| 126 | }, | ||
| 127 | 'job' => { 'specCmd' => 'job', | ||
| 128 | 'singularID' => 'job', | ||
| 129 | 'queryCmd' => 'jobs', | ||
| 130 | 'pluralID' => 'job', | ||
| 131 | 'idAttr' => 'job', | ||
| 132 | 'p4ooType' => 'P4::OO::Job', | ||
| 133 | 'pluralType' => 'P4::OO::JobSet', | ||
| 134 | 'queryOptions' => { 'jobview' => [ '-e', 1 ], | ||
| 135 | 'files' => undef, | ||
| 136 | }, | ||
| 137 | }, | ||
| 138 | 'label' => { 'specCmd' => 'label', | ||
| 139 | 'singularID' => 'label', | ||
| 140 | 'queryCmd' => 'labels', | ||
| 141 | 'pluralID' => 'label', | ||
| 142 | 'idAttr' => 'label', | ||
| 143 | 'p4ooType' => 'P4::OO::Label', | ||
| 144 | 'pluralType' => 'P4::OO::LabelSet', | ||
| 145 | 'queryOptions' => { 'user' => [ '-u', 1 ], | ||
| 146 | 'name' => [ '-e', 1 ], | ||
| 147 | 'files' => undef, | ||
| 148 | }, | ||
| 149 | }, | ||
| 150 | 'user' => { 'specCmd' => 'user', | ||
| 151 | 'singularID' => 'user', | ||
| 152 | 'queryCmd' => 'users', | ||
| 153 | 'pluralID' => 'User', | ||
| 154 | 'idAttr' => 'user', | ||
| 155 | 'p4ooType' => 'P4::OO::User', | ||
| 156 | 'pluralType' => 'P4::OO::UserSet', | ||
| 157 | 'queryOptions' => { 'users' => undef, | ||
| 158 | }, | ||
| 159 | }, | ||
| 160 | }; | ||
| 161 | |||
| 162 | |||
| 163 | # other p4 commands that don't return specs natively | ||
| 164 | Readonly my %P4PERL_OTHER_COMMANDS => { | ||
| 165 | # 'counter' => { 'specCmd' => 'counter', | ||
| 166 | # 'singularID' => 'counter', | ||
| 167 | # 'queryCmd' => 'counters', | ||
| 168 | # 'pluralID' => 'counter', | ||
| 169 | # 'idAttr' => 'counter', | ||
| 170 | ##subcommands: | ||
| 171 | ## increment | ||
| 172 | ## delete | ||
| 173 | ## set | ||
| 174 | # }, | ||
| 175 | |||
| 176 | # p4 describe [-d<flags> -s -S -f] changelist# ... | ||
| 177 | 'describe' => { 'queryOptions' => { 'diffOptions' => { 'option' => '-d', | ||
| 178 | 'bundledArgs' => 1, | ||
| 179 | 'multiplicity' => 1, | ||
| 180 | }, | ||
| 181 | 'changes' => { 'type' => [ 'string', | ||
| 182 | 'P4::OO::Change', | ||
| 183 | 'P4::OO::ChangeSet', | ||
| 184 | ], | ||
| 185 | }, | ||
| 186 | 'omitDiffs' => { 'option' => '-s', | ||
| 187 | 'multiplicity' => 0, | ||
| 188 | }, | ||
| 189 | 'shelved' => { 'option' => '-S', | ||
| 190 | 'multiplicity' => 0, | ||
| 191 | }, | ||
| 192 | 'force' => { 'option' => '-f', | ||
| 193 | 'multiplicity' => 0, | ||
| 194 | }, | ||
| 195 | }, | ||
| 196 | }, | ||
| 197 | |||
| 198 | # p4 files [ -a ] [ -A ] [ -m max ] file[revRange] ... | ||
| 199 | 'files' => { 'queryOptions' => { 'allRevisions' => { 'option' => '-a', | ||
| 200 | 'multiplicity' => 0, | ||
| 201 | }, | ||
| 202 | 'archived' => { 'option' => '-A', | ||
| 203 | 'multiplicity' => 0, | ||
| 204 | }, | ||
| 205 | 'max' => { 'option' => '-m', | ||
| 206 | 'type' => [ 'string' ], | ||
| 207 | 'multiplicity' => 1, | ||
| 208 | }, | ||
| 209 | 'files' => { 'type' => [ 'string', | ||
| 210 | 'P4::OO::File', | ||
| 211 | 'P4::OO::FileSet', | ||
| 212 | ], | ||
| 213 | }, | ||
| 214 | }, | ||
| 215 | 'output' => { 'pluralType' => 'P4::OO::FileSet', | ||
| 216 | 'pluralID' => 'depotFile', | ||
| 217 | 'singularType' => 'P4::OO::File', | ||
| 218 | 'singularID' => 'depotFile', | ||
| 219 | }, | ||
| 220 | }, | ||
| 221 | |||
| 222 | # p4 have [file ...] | ||
| 223 | 'have' => { 'queryOptions' => { 'files' => { 'type' => [ 'string', | ||
| 224 | 'P4::OO::File', | ||
| 225 | 'P4::OO::FileSet', | ||
| 226 | ], | ||
| 227 | }, | ||
| 228 | }, | ||
| 229 | 'output' => { 'pluralType' => 'P4::OO::FileSet', | ||
| 230 | 'pluralID' => 'depotFile', | ||
| 231 | 'singularType' => 'P4::OO::File', | ||
| 232 | 'singularID' => 'depotFile', | ||
| 233 | }, | ||
| 234 | }, | ||
| 235 | |||
| 236 | # p4 info [-s] | ||
| 237 | 'info' => { 'queryOptions' => { 'shortOutput' => { 'option' => '-s', | ||
| 238 | 'multiplicity' => 0, | ||
| 239 | }, | ||
| 240 | }, | ||
| 241 | }, | ||
| 242 | # p4 opened [-a -c changelist# -C client -u user -m max] [file ...] | ||
| 243 | 'opened' => { 'queryOptions' => { 'allClients' => { 'option' => '-a', | ||
| 244 | 'multiplicity' => 0, | ||
| 245 | }, | ||
| 246 | 'change' => { 'option' => '-c', | ||
| 247 | 'type' => [ 'string', | ||
| 248 | 'P4::OO::Change', | ||
| 249 | 'P4::OO::ChangeSet', | ||
| 250 | ], | ||
| 251 | 'multiplicity' => 1, | ||
| 252 | }, | ||
| 253 | 'client' => { 'option' => '-C', | ||
| 254 | 'type' => [ 'string', | ||
| 255 | 'P4::OO::Client', | ||
| 256 | 'P4::OO::ClientSet', | ||
| 257 | ], | ||
| 258 | 'multiplicity' => 1, | ||
| 259 | }, | ||
| 260 | 'user' => { 'option' => '-u', | ||
| 261 | 'type' => [ 'string', | ||
| 262 | 'P4::OO::User', | ||
| 263 | 'P4::OO::UserSet', | ||
| 264 | ], | ||
| 265 | 'multiplicity' => 1, | ||
| 266 | }, | ||
| 267 | 'max' => { 'option' => '-m', | ||
| 268 | 'type' => [ 'string' ], | ||
| 269 | 'multiplicity' => 1, | ||
| 270 | }, | ||
| 271 | 'files' => { 'type' => [ 'string', | ||
| 272 | 'P4::OO::File', | ||
| 273 | 'P4::OO::FileSet', | ||
| 274 | ], | ||
| 275 | }, | ||
| 276 | }, | ||
| 277 | 'output' => { 'pluralType' => 'P4::OO::FileSet', | ||
| 278 | 'pluralID' => 'clientFile', | ||
| 279 | 'singularType' => 'P4::OO::File', | ||
| 280 | 'singularID' => 'clientFile', | ||
| 281 | }, | ||
| 282 | }, | ||
| 283 | }; | ||
| 284 | |||
| 285 | |||
| 286 | ###################################################################### | ||
| 287 | # Methods | ||
| 288 | # | ||
| 289 | |||
| 290 | sub readSpec | ||
| 291 | { | ||
| 292 | my $self = shift(); | ||
| 293 | my( $specObj ) = @_; | ||
| 294 | |||
| 295 | my $specType = $specObj->SPECOBJ_TYPE(); | ||
| 296 | my $specID = $specObj->_getAttr( 'id' ); | ||
| 297 | |||
| 298 | if( ! exists( $P4PERL_SPEC_COMMANDS{$specType} ) ) | ||
| 299 | { | ||
| 300 | throw E_P4Fatal "Unsupported Spec type '$specType'.\n"; | ||
| 301 | } | ||
| 302 | |||
| 303 | my $specCmd = $P4PERL_SPEC_COMMANDS{$specType}->{'specCmd'}; | ||
| 304 | my $p4Output = $self->_execCmd( $specCmd, "-o", $specID ); | ||
| 305 | |||
| 306 | # Only care about the first output result (there's only one) | ||
| 307 | my $p4Spec = $p4Output->[0]; | ||
| 308 | $specObj->_setAttr( 'p4Spec', $p4Spec ); | ||
| 309 | |||
| 310 | if( ! defined( $specID ) ) | ||
| 311 | { | ||
| 312 | # We'll reset the object's specID if it was not defined. | ||
| 313 | my $singularID = $P4PERL_SPEC_COMMANDS{$specType}->{'singularID'}; | ||
| 314 | |||
| 315 | # At this point the SpecObj is initialized enough for this to work... | ||
| 316 | # TODO This is REALLY awkward... a paradox??? Chicken, meet egg. | ||
| 317 | $specObj->_setAttr( 'id', $specObj->_getSpecAttr( $singularID ) ); | ||
| 318 | } | ||
| 319 | |||
| 320 | return( 1 ); | ||
| 321 | } | ||
| 322 | |||
| 323 | sub saveSpec | ||
| 324 | { | ||
| 325 | my $self = shift(); | ||
| 326 | my( $specType, $specID ) = @_; | ||
| 327 | |||
| 328 | if( ! exists( $P4PERL_SPEC_COMMANDS{$specType} ) ) | ||
| 329 | { | ||
| 330 | throw E_P4Fatal "Unsupported Spec type '$specType'.\n"; | ||
| 331 | } | ||
| 332 | |||
| 333 | my $p4Out = $self->_execCmd( 'clients' ); | ||
| 334 | #TODO... | ||
| 335 | |||
| 336 | } | ||
| 337 | |||
| 338 | sub query | ||
| 339 | { | ||
| 340 | my $self = shift(); | ||
| 341 | my( $specType, @inputFilter ) = @_; | ||
| 342 | my $subName = ( caller( 0 ) )[3]; | ||
| 343 | |||
| 344 | if( ! exists( $P4PERL_SPEC_COMMANDS{$specType} ) ) | ||
| 345 | { | ||
| 346 | throw E_P4Fatal "$subName: Invalid query type.\n"; | ||
| 347 | } | ||
| 348 | |||
| 349 | my $inputFilterHash = $self->_argsToHash( $subName, @inputFilter ); | ||
| 350 | |||
| 351 | my $allowedFilters = $P4PERL_SPEC_COMMANDS{$specType}->{'queryOptions'}; | ||
| 352 | |||
| 353 | my @execArgs; | ||
| 354 | foreach my $origFilterKey ( keys( %{$inputFilterHash} ) ) | ||
| 355 | { | ||
| 356 | my $lcFilterKey = lc( $origFilterKey ); | ||
| 357 | if( ! exists( $allowedFilters->{$lcFilterKey} ) ) | ||
| 358 | { | ||
| 359 | throw E_P4Fatal "$subName: Invalid filter key: $origFilterKey.\n"; | ||
| 360 | } | ||
| 361 | |||
| 362 | if( ! defined( $allowedFilters->{$lcFilterKey} ) ) | ||
| 363 | { | ||
| 364 | # For undef filter options, we'll push them onto the end of the command. | ||
| 365 | push( @execArgs, $inputFilterHash->{$origFilterKey} ); | ||
| 366 | } | ||
| 367 | else | ||
| 368 | { | ||
| 369 | # defined cmdline options go at the front | ||
| 370 | if( $allowedFilters->{$lcFilterKey}->[1] | ||
| 371 | && ! defined( $inputFilterHash->{$origFilterKey} ) ) | ||
| 372 | { | ||
| 373 | throw E_P4Fatal "$subName: Filter key: $origFilterKey requires argument.\n"; | ||
| 374 | } | ||
| 375 | unshift( @execArgs, $allowedFilters->{$lcFilterKey}->[0], $inputFilterHash->{$origFilterKey} ); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | my $queryCmd = $P4PERL_SPEC_COMMANDS{$specType}->{'queryCmd'}; | ||
| 380 | my $pluralSpecID = $P4PERL_SPEC_COMMANDS{$specType}->{'pluralID'}; | ||
| 381 | my $p4ooType = $P4PERL_SPEC_COMMANDS{$specType}->{'p4ooType'}; | ||
| 382 | my $pluralType = $P4PERL_SPEC_COMMANDS{$specType}->{'pluralType'}; | ||
| 383 | my $idAttr = $P4PERL_SPEC_COMMANDS{$specType}->{'idAttr'}; | ||
| 384 | |||
| 385 | my $p4Out = $self->_execCmd( $queryCmd, @execArgs ); | ||
| 386 | |||
| 387 | # Make sure the caller is properly equipped to use any objects | ||
| 388 | # we bless here. | ||
| 389 | require P4::OO::_Set; | ||
| 390 | require P4::OO::_SpecObj; | ||
| 391 | eval "require $p4ooType;"; | ||
| 392 | eval "require $pluralType;"; | ||
| 393 | |||
| 394 | my $objectList = []; | ||
| 395 | |||
| 396 | # Don't really care about the content of the output, just the specIDs. | ||
| 397 | foreach my $p4OutHash ( @{$p4Out} ) | ||
| 398 | { | ||
| 399 | if( ! exists( $p4OutHash->{$pluralSpecID} ) ) | ||
| 400 | { | ||
| 401 | throw E_P4Fatal "Unexpected output from Perforce.\n"; | ||
| 402 | } | ||
| 403 | |||
| 404 | # Copy the pluralID output value to the id attribute | ||
| 405 | # if they aren't one and the same already | ||
| 406 | if( $idAttr ne $pluralSpecID ) | ||
| 407 | { | ||
| 408 | $p4OutHash->{$idAttr} = $p4OutHash->{$pluralSpecID}; | ||
| 409 | } | ||
| 410 | |||
| 411 | # HACK - Instead of eval'ing this through the type's | ||
| 412 | # constructor, we'll just use the base class and bless | ||
| 413 | my $specObj = P4::OO::_SpecObj->new( 'p4Spec' => $p4OutHash, | ||
| 414 | 'id' => $p4OutHash->{$pluralSpecID}, | ||
| 415 | '_p4Conn' => $self, # Make sure each of these objects can reuse this conection too | ||
| 416 | ); | ||
| 417 | bless( $specObj, $p4ooType ); | ||
| 418 | push( @{$objectList}, $specObj ); | ||
| 419 | } | ||
| 420 | |||
| 421 | # Wrap it with a bow | ||
| 422 | my $setObj = P4::OO::_Set->new( '_p4Conn', $self ); | ||
| 423 | bless( $setObj, $pluralType ); | ||
| 424 | $setObj->addObjects( @{$objectList} ); | ||
| 425 | |||
| 426 | return( $setObj ); | ||
| 427 | } | ||
| 428 | |||
| 429 | sub runCommand | ||
| 430 | { | ||
| 431 | my $self = shift(); | ||
| 432 | my( $cmdName, @inputFilter ) = @_; | ||
| 433 | my $subName = ( caller( 0 ) )[3]; | ||
| 434 | |||
| 435 | if( ! exists( $P4PERL_OTHER_COMMANDS{$cmdName} ) ) | ||
| 436 | { | ||
| 437 | throw E_P4Fatal "$subName: Invalid command.\n"; | ||
| 438 | } | ||
| 439 | |||
| 440 | my $inputFilterHash = $self->_argsToHash( $subName, @inputFilter ); | ||
| 441 | |||
| 442 | my $allowedFilters = $P4PERL_OTHER_COMMANDS{$cmdName}->{'queryOptions'}; | ||
| 443 | |||
| 444 | my @execArgs; | ||
| 445 | foreach my $origFilterKey ( keys( %{$inputFilterHash} ) ) | ||
| 446 | { | ||
| 447 | my $lcFilterKey = lc( $origFilterKey ); | ||
| 448 | if( ! exists( $allowedFilters->{$lcFilterKey} ) ) | ||
| 449 | { | ||
| 450 | throw E_P4Fatal "$subName: Invalid filter key: $origFilterKey.\n"; | ||
| 451 | } | ||
| 452 | |||
| 453 | my @optionArgs; | ||
| 454 | if( defined( $inputFilterHash->{$origFilterKey} ) ) | ||
| 455 | { | ||
| 456 | if( ref( $inputFilterHash->{$origFilterKey} ) eq "ARRAY" ) | ||
| 457 | { | ||
| 458 | push( @optionArgs, @{$inputFilterHash->{$origFilterKey}} ); | ||
| 459 | } | ||
| 460 | else | ||
| 461 | { | ||
| 462 | push( @optionArgs, $inputFilterHash->{$origFilterKey} ); | ||
| 463 | } | ||
| 464 | } | ||
| 465 | |||
| 466 | # Check option argument types, and replace option args with IDs for P4::OO objects passed in | ||
| 467 | # Take the opportunity to expand any Set objects we find. | ||
| 468 | my @cmdOptionArgs; | ||
| 469 | if( defined( $allowedFilters->{$lcFilterKey}->{'type'} ) ) | ||
| 470 | { | ||
| 471 | # Check type on each argument | ||
| 472 | ARG: foreach my $optionArg ( @optionArgs ) | ||
| 473 | { | ||
| 474 | foreach my $checkType ( @{$allowedFilters->{$lcFilterKey}->{'type'}} ) | ||
| 475 | { | ||
| 476 | if( $checkType eq "string" | ||
| 477 | && ref( $optionArg ) eq "" ) | ||
| 478 | { | ||
| 479 | push( @cmdOptionArgs, $optionArg ); | ||
| 480 | next ARG; | ||
| 481 | } | ||
| 482 | elsif( UNIVERSAL::isa( $optionArg, $checkType ) ) | ||
| 483 | { | ||
| 484 | # Special Set expansion...this gets weird, eh? | ||
| 485 | if( UNIVERSAL::isa( $optionArg, "P4::OO::_Set" ) ) | ||
| 486 | { | ||
| 487 | # Here we assume the set contents are kosher. | ||
| 488 | push( @cmdOptionArgs, $optionArg->listObjectIDs() ); | ||
| 489 | } | ||
| 490 | else | ||
| 491 | { | ||
| 492 | push( @cmdOptionArgs, $optionArg->_uniqueID() ); | ||
| 493 | } | ||
| 494 | next ARG; | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | # Looped through all types, didn't find a match | ||
| 499 | throw E_P4Fatal "$subName: Filter key: $origFilterKey accepts arguments of only these types: " . join( ", ", @{$allowedFilters->{$lcFilterKey}->{'type'}} ) . ".\n"; | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | # defined cmdline options go at the front | ||
| 504 | if( $allowedFilters->{$lcFilterKey}->{'multiplicity'} == 0 ) | ||
| 505 | { | ||
| 506 | if( scalar( @cmdOptionArgs ) != 0 ) | ||
| 507 | { | ||
| 508 | throw E_P4Fatal "$subName: Filter key: $origFilterKey accepts no arguments.\n"; | ||
| 509 | } | ||
| 510 | |||
| 511 | unshift( @execArgs, $allowedFilters->{$lcFilterKey}->{'option'} ); | ||
| 512 | } | ||
| 513 | elsif( $allowedFilters->{$lcFilterKey}->{'multiplicity'} == 1 ) | ||
| 514 | { | ||
| 515 | if( scalar( @cmdOptionArgs ) != 1 ) | ||
| 516 | { | ||
| 517 | throw E_P4Fatal "$subName: Filter key: $origFilterKey accepts exactly 1 argument.\n"; | ||
| 518 | } | ||
| 519 | |||
| 520 | if( $allowedFilters->{$lcFilterKey}->{'bundledArgs'} ) | ||
| 521 | { | ||
| 522 | unshift( @execArgs, join( "", $allowedFilters->{$lcFilterKey}->{'option'}, @cmdOptionArgs ) ); | ||
| 523 | } | ||
| 524 | else | ||
| 525 | { | ||
| 526 | unshift( @execArgs, $allowedFilters->{$lcFilterKey}->{'option'}, @cmdOptionArgs ); | ||
| 527 | } | ||
| 528 | } | ||
| 529 | else | ||
| 530 | { | ||
| 531 | if( defined( $allowedFilters->{$lcFilterKey}->{'option'} ) ) | ||
| 532 | { | ||
| 533 | push( @execArgs, $allowedFilters->{$lcFilterKey}->{'option'} ); | ||
| 534 | } | ||
| 535 | push( @execArgs, @cmdOptionArgs ); | ||
| 536 | } | ||
| 537 | } | ||
| 538 | |||
| 539 | my $p4Out = $self->_execCmd( $cmdName, @execArgs ); | ||
| 540 | |||
| 541 | |||
| 542 | # TODO... subcommands? | ||
| 543 | # 'counter' => { 'specCmd' => 'counter', | ||
| 544 | # 'singularID' => 'counter', | ||
| 545 | # 'queryCmd' => 'counters', | ||
| 546 | # 'pluralID' => 'counter', | ||
| 547 | # 'idAttr' => 'counter', | ||
| 548 | # p4 counter name | ||
| 549 | # p4 counter [-f] name value | ||
| 550 | # p4 counter [-f] -d name | ||
| 551 | # p4 counter [-i] name | ||
| 552 | # | ||
| 553 | ##subcommands: | ||
| 554 | ## increment | ||
| 555 | ## delete | ||
| 556 | ## set | ||
| 557 | # }, | ||
| 558 | |||
| 559 | # If no special output massaging is needed, we're done! | ||
| 560 | if( ! $P4PERL_OTHER_COMMANDS{$cmdName}->{'output'} ) | ||
| 561 | { | ||
| 562 | return( $p4Out ); | ||
| 563 | } | ||
| 564 | |||
| 565 | my $pluralType = $P4PERL_OTHER_COMMANDS{$cmdName}->{'output'}->{'pluralType'}; | ||
| 566 | my $pluralID = $P4PERL_OTHER_COMMANDS{$cmdName}->{'output'}->{'pluralID'}; | ||
| 567 | my $singularType = $P4PERL_OTHER_COMMANDS{$cmdName}->{'output'}->{'singularType'}; | ||
| 568 | my $singularID = $P4PERL_OTHER_COMMANDS{$cmdName}->{'output'}->{'singularID'}; | ||
| 569 | |||
| 570 | |||
| 571 | # Make sure the caller is properly equipped to use any objects | ||
| 572 | # we bless here. | ||
| 573 | require P4::OO::_Set; | ||
| 574 | require P4::OO::_SpecObj; | ||
| 575 | eval "require $singularType;"; | ||
| 576 | eval "require $pluralType;"; | ||
| 577 | |||
| 578 | my $objectList = []; | ||
| 579 | |||
| 580 | # Don't really care about the content of the output, just the specIDs. | ||
| 581 | foreach my $p4OutHash ( @{$p4Out} ) | ||
| 582 | { | ||
| 583 | if( ! exists( $p4OutHash->{$pluralID} ) ) | ||
| 584 | { | ||
| 585 | throw E_P4Fatal "Unexpected output from Perforce.\n"; | ||
| 586 | } | ||
| 587 | |||
| 588 | # Copy the pluralID output value to the id attribute | ||
| 589 | # if they aren't one and the same already | ||
| 590 | if( $singularID ne $pluralID ) | ||
| 591 | { | ||
| 592 | $p4OutHash->{$singularID} = $p4OutHash->{$pluralID}; | ||
| 593 | } | ||
| 594 | |||
| 595 | # HACK - Instead of eval'ing this through the type's | ||
| 596 | # constructor, we'll just use the base class and bless | ||
| 597 | my $specObj = P4::OO::_SpecObj->new( 'p4Spec' => $p4OutHash, | ||
| 598 | 'id' => $p4OutHash->{$pluralID}, | ||
| 599 | '_p4Conn' => $self, # Make sure each of these objects can reuse this conection too | ||
| 600 | ); | ||
| 601 | bless( $specObj, $singularType ); | ||
| 602 | push( @{$objectList}, $specObj ); | ||
| 603 | } | ||
| 604 | |||
| 605 | # Wrap it with a bow | ||
| 606 | my $setObj = P4::OO::_Set->new( '_p4Conn', $self ); | ||
| 607 | bless( $setObj, $pluralType ); | ||
| 608 | $setObj->addObjects( @{$objectList} ); | ||
| 609 | |||
| 610 | return( $setObj ); | ||
| 611 | } | ||
| 612 | |||
| 613 | ###################################################################### | ||
| 614 | # Internal Methods | ||
| 615 | # | ||
| 616 | |||
| 617 | # _execCmd() | ||
| 618 | sub _execCmd | ||
| 619 | { | ||
| 620 | my $self = shift(); | ||
| 621 | my( $p4SubCmd, @args ) = @_; | ||
| 622 | my $subName = ( caller( 0 ) )[3]; | ||
| 623 | |||
| 624 | # $self->_printDebug( "$subName entered.\n" ); | ||
| 625 | |||
| 626 | my $p4PerlObj = $self->_connect(); | ||
| 627 | |||
| 628 | if( ! defined( $p4SubCmd ) ) | ||
| 629 | { | ||
| 630 | throw E_P4Fatal( "$subName: called with invalid subcommand!" ); | ||
| 631 | } | ||
| 632 | |||
| 633 | # strip undef args from the tail, P4PERL don't like them | ||
| 634 | while( ! defined( $args[$#args] ) ) | ||
| 635 | { | ||
| 636 | # maybe there aren't any args | ||
| 637 | if( $#args == -1 ) | ||
| 638 | { | ||
| 639 | last; | ||
| 640 | } | ||
| 641 | pop( @args ); | ||
| 642 | } | ||
| 643 | |||
| 644 | $self->_printDebug( "$subName: Executing: " . join( " ", $p4SubCmd, @args ) ); | ||
| 645 | |||
| 646 | #TODO ping server before each command? | ||
| 647 | my $p4Out = $p4PerlObj->Run( $p4SubCmd, @args ); | ||
| 648 | |||
| 649 | #TODO Should do something to detect disconnects, etc. | ||
| 650 | # If we have errors and warnings, we want to give both to caller | ||
| 651 | |||
| 652 | if( $p4PerlObj->ErrorCount() > 0 ) | ||
| 653 | { | ||
| 654 | my $errMsg = "ERROR: " . join( "", $p4PerlObj->Errors() ); | ||
| 655 | |||
| 656 | if( $p4PerlObj->WarningCount() > 0 ) | ||
| 657 | { | ||
| 658 | $errMsg .= "WARNING: " . join( "", $p4PerlObj->Warnings() ); | ||
| 659 | } | ||
| 660 | |||
| 661 | throw E_P4Fatal( "${subName}: $errMsg" ); | ||
| 662 | } | ||
| 663 | elsif( $p4PerlObj->WarningCount() > 0 ) | ||
| 664 | { | ||
| 665 | my $warnMsg = "WARNING: " . join( "", $p4PerlObj->Warnings() ); | ||
| 666 | throw E_P4Warning( "${subName}: $warnMsg" ); | ||
| 667 | } | ||
| 668 | |||
| 669 | return( $p4Out ); | ||
| 670 | } | ||
| 671 | |||
| 672 | |||
| 673 | sub _connect | ||
| 674 | { | ||
| 675 | my $self = shift(); | ||
| 676 | |||
| 677 | my $p4PerlObj = $self->_getAttr( 'p4PerlObj' ); | ||
| 678 | |||
| 679 | if( ! defined( $p4PerlObj ) ) | ||
| 680 | { | ||
| 681 | require P4; | ||
| 682 | $p4PerlObj = P4->new(); | ||
| 683 | |||
| 684 | if( ! $p4PerlObj->Connect() ) | ||
| 685 | { | ||
| 686 | my $subName = ( caller( 0 ) )[3]; | ||
| 687 | throw E_P4Fatal ( "${subName}: Failed to connect to Perforce Server" ); | ||
| 688 | } | ||
| 689 | |||
| 690 | $self->_setAttr( 'p4PerlObj', $p4PerlObj ); | ||
| 691 | $self->_setAttr( '_ownP4PerlObj', 1 ); | ||
| 692 | } | ||
| 693 | |||
| 694 | return( $p4PerlObj ); | ||
| 695 | } | ||
| 696 | |||
| 697 | |||
| 698 | sub _disconnect | ||
| 699 | { | ||
| 700 | my $self = shift(); | ||
| 701 | |||
| 702 | my $ownP4PerlObj = $self->_getAttr( '_ownP4PerlObj' ); | ||
| 703 | |||
| 704 | if( $ownP4PerlObj ) | ||
| 705 | { | ||
| 706 | # We instantiated the connection, so we'll tear it down too | ||
| 707 | my $p4PerlObj = $self->_getAttr( 'p4PerlObj' ); | ||
| 708 | |||
| 709 | if( defined( $p4PerlObj ) ) | ||
| 710 | { | ||
| 711 | $p4PerlObj->Disconnect(); | ||
| 712 | } | ||
| 713 | } | ||
| 714 | |||
| 715 | # In any case we'll dereference the P4 object, maybe we're the last reference. | ||
| 716 | $self->_setAttr( 'p4PerlObj', undef ); | ||
| 717 | |||
| 718 | return( 1 ); | ||
| 719 | } | ||
| 720 | |||
| 721 | |||
| 722 | sub DESTROY | ||
| 723 | { | ||
| 724 | my $self = shift(); | ||
| 725 | |||
| 726 | # Call _disconnect, it'll do the right thing | ||
| 727 | $self->_disconnect(); | ||
| 728 | |||
| 729 | return( 1 ); | ||
| 730 | } | ||
| 731 | |||
| 732 | |||
| 733 | ###################################################################### | ||
| 734 | # Standard authorship and copyright for documentation | ||
| 735 | # | ||
| 736 | |||
| 737 | =head1 AUTHOR | ||
| 738 | |||
| 739 | David L. Armstrong <armstd@cpan.org> | ||
| 740 | |||
| 741 | =head1 COPYRIGHT AND LICENSE | ||
| 742 | |||
| 743 | P4::OO::_Connection::P4PERL is Copyright (c)2011, David L. Armstrong. | ||
| 744 | |||
| 745 | This module is free software; you can redistribute it and/or | ||
| 746 | modify it under the same terms as Perl itself, either Perl | ||
| 747 | version 5.8.8 or, at your option, any later version of Perl 5 | ||
| 748 | you may have available. | ||
| 749 | |||
| 750 | =head1 SUPPORT AND WARRANTY | ||
| 751 | |||
| 752 | This program is distributed in the hope that it will be | ||
| 753 | useful, but it is provided "as is" and without any expressed | ||
| 754 | or implied warranties. | ||
| 755 | |||
| 756 | =cut | ||
| 757 | |||
| 758 | 1; |