/******************************************************************************* Copyright (c) 2001-2008, 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 PERFORCE SOFTWARE, INC. 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. *******************************************************************************/ /******************************************************************************* * Name : specmgr.cc * * Author : Tony Smith <tony@perforce.com> or <tony@smee.org> * * Description : Ruby bindings for the Perforce API. Class for handling * Perforce specs. This class provides other classes with * generic support for parsing and formatting Perforce * specs. * ******************************************************************************/ #include <ctype.h> #include <ruby.h> #include "undefdups.h" #include <clientapi.h> #include <strops.h> #include <spec.h> #include <strtable.h> #include "extconf.h" #include "gc_hack.h" #include "p4rubydebug.h" #include "specmgr.h" struct defaultspec { const char *type; const char *spec; } speclist[] = { { "branch", "Branch;code:301;rq;ro;fmt:L;len:32;;" "Update;code:302;type:date;ro;fmt:L;len:20;;" "Access;code:303;type:date;ro;fmt:L;len:20;;" "Owner;code:304;fmt:R;len:32;;" "Description;code:306;type:text;len:128;;" "Options;code:309;type:line;len:32;val:" "unlocked/locked;;" "View;code:311;type:wlist;words:2;len:64;;" }, { "change", "Change;code:201;rq;ro;fmt:L;seq:1;len:10;;" "Date;code:202;type:date;ro;fmt:R;seq:3;len:20;;" "Client;code:203;ro;fmt:L;seq:2;len:32;;" "User;code:204;ro;fmt:L;seq:4;len:32;;" "Status;code:205;ro;fmt:R;seq:5;len:10;;" "Description;code:206;type:text;rq;seq:6;;" "JobStatus;code:207;fmt:I;type:select;seq:8;;" "Jobs;code:208;type:wlist;seq:7;len:32;;" "Files;code:210;type:llist;len:64;;" }, { "client", "Client;code:301;rq;ro;seq:1;len:32;;" "Update;code:302;type:date;ro;seq:2;fmt:L;len:20;;" "Access;code:303;type:date;ro;seq:4;fmt:L;len:20;;" "Owner;code:304;seq:3;fmt:R;len:32;;" "Host;code:305;seq:5;fmt:R;len:32;;" "Description;code:306;type:text;len:128;;" "Root;code:307;rq;type:line;len:64;;" "AltRoots;code:308;type:llist;len:64;;" "Options;code:309;type:line;len:64;val:" "noallwrite/allwrite,noclobber/clobber,nocompress/compress," "unlocked/locked,nomodtime/modtime,normdir/rmdir;;" "SubmitOptions;code:313;type:select;fmt:L;len:25;val:" "submitunchanged/submitunchanged+reopen/revertunchanged/" "revertunchanged+reopen/leaveunchanged/leaveunchanged+reopen;;" "LineEnd;code:310;type:select;fmt:L;len:12;val:" "local/unix/mac/win/share;;" "View;code:311;type:wlist;words:2;len:64;;" }, { "depot", "Depot;code:251;rq;ro;len:32;;" "Owner;code:252;len:32;;" "Date;code:253;type:date;ro;len:20;;" "Description;code:254;type:text;len:128;;" "Type;code:255;rq;len:10;;" "Address;code:256;len:64;;" "Suffix;code:258;len:64;;" "Map;code:257;rq;len:64;;" }, { "group", "Group;code:401;rq;ro;len:32;;" "MaxResults;code:402;type:word;len:12;;" "MaxScanRows;code:403;type:word;len:12;;" "MaxLockTime;code:407;type:word;len:12;;" "Timeout;code:406;type:word;len:12;;" "Subgroups;code:404;type:wlist;len:32;opt:default;;" "Owners;code:408;type:wlist;len:32;opt:default;;" "Users;code:405;type:wlist;len:32;opt:default;;" }, { "job", "Job;code:101;rq;len:32;;" "Status;code:102;type:select;rq;len:10;" "pre:open;val:open/suspended/closed;;" "User;code:103;rq;len:32;pre:$user;;" "Date;code:104;type:date;ro;len:20;pre:$now;;" "Description;code:105;type:text;rq;pre:$blank;;" }, { "label", "Label;code:301;rq;ro;fmt:L;len:32;;" "Update;code:302;type:date;ro;fmt:L;len:20;;" "Access;code:303;type:date;ro;fmt:L;len:20;;" "Owner;code:304;fmt:R;len:32;;" "Description;code:306;type:text;len:128;;" "Options;code:309;type:line;len:64;val:" "unlocked/locked;;" "Revision;code:312;type:word;words:1;len:64;;" "View;code:311;type:wlist;len:64;;" }, { "license", "License;code:451;len:32;;" "License-Expires;code:452;len:10;;" "Support-Expires;code:453;len:10;;" "Customer;code:454;type:line;len:128;;" "Application;code:455;len:32;;" "IPaddress;code:456;len:24;;" "Platform;code:457;len:32;;" "Clients;code:458;len:8;;" "Users;code:459;len:8;;" }, { "protect", "Protections;code:501;type:wlist;words:5;opt:default;len:64;;" }, { "spec", "Fields;code:351;type:wlist;words:5;rq;;" "Words;code:352;type:wlist;words:2;;" "Formats;code:353;type:wlist;words:3;;" "Values;code:354;type:wlist;words:2;;" "Presets;code:355;type:wlist;words:2;;" "Comments;code:356;type:text;;" }, { "triggers", "Triggers;code:551;type:wlist;words:4;len:64;opt:default;" }, { "typemap", "TypeMap;code:601;type:wlist;words:2;len:64;opt:default;" }, { "user", "User;code:651;rq;ro;seq:1;len:32;;" "Email;code:652;fmt:R;rq;seq:3;len:32;;" "Update;code:653;fmt:L;type:date;ro;seq:2;len:20;;" "Access;code:654;fmt:L;type:date;ro;len:20;;" "FullName;code:655;fmt:R;type:line;rq;len:32;;" "JobView;code:656;type:line;len:64;;" "Password;code:657;len:32;;" "Reviews;code:658;type:wlist;len:64;;" }, { 0, 0 } }; SpecMgr::SpecMgr() { debug = 0; specs = 0; Reset(); } SpecMgr::~SpecMgr() { delete specs; } void SpecMgr::AddSpecDef( const char *type, StrPtr &specDef ) { if( specs->GetVar( type ) ) specs->RemoveVar( type ); specs->SetVar( type, specDef ); } void SpecMgr::AddSpecDef( const char *type, const char *specDef ) { if( specs->GetVar( type ) ) specs->RemoveVar( type ); specs->SetVar( type, specDef ); } void SpecMgr::Reset() { delete specs; specs = new StrBufDict; for( struct defaultspec *sp = &speclist[ 0 ]; sp->type; sp++ ) AddSpecDef( sp->type, sp->spec ); } int SpecMgr::HaveSpecDef( const char *type ) { return specs->GetVar( type ) != 0; } // // Convert a Perforce StrDict into a Ruby hash. Convert multi-level // data (Files0, Files1 etc. ) into (nested) array members of the hash. // VALUE SpecMgr::StrDictToHash( StrDict *dict, VALUE hash ) { StrRef var, val; int i; if( hash == Qnil ) hash = rb_hash_new(); for ( i = 0; dict->GetVar( i, var, val ); i++ ) { if ( var == "specdef" || var == "func" || var == "specFormatted" ) continue; InsertItem( hash, &var, &val ); } return hash; } // // Convert a Perforce StrDict into a P4::Spec object // VALUE SpecMgr::StrDictToSpec( StrDict *dict, StrPtr *specDef ) { VALUE spec = NewSpec( specDef ); return StrDictToHash( dict, spec ); } VALUE SpecMgr::StringToSpec( const char *type, const char *form, Error *e ) { SpecDataTable specData; StrPtr * specDef = specs->GetVar( type ); Spec s( specDef->Text(), "", e ); if( !e->Test() ) s.ParseNoValid( form, &specData, e ); if ( e->Test() ) return Qfalse; return StrDictToSpec( specData.Dict(), specDef ); } // // Format routine. updates a StrBuf object with the form content. // The StrBuf can then be converted to a Ruby string where required. // void SpecMgr::SpecToString( const char *type, VALUE hash, StrBuf &b, Error *e ) { StrBuf buf; SpecMgr m; StrPtr * specDef = specs->GetVar( type ); if ( !specDef ) { e->Set( E_FAILED, "No specdef available. Cannot convert hash to a " "Perforce form" ); return; } SpecDataTable specData; Spec s( specDef->Text(), "", e ); if( e->Test() ) return; ID idKeys = rb_intern( "keys" ); ID idLength = rb_intern( "length" ); ID idToS = rb_intern( "to_s" ); VALUE keys = rb_funcall( hash, idKeys, 0 ); int keyCount = NUM2INT( rb_funcall( keys, idLength, 0 ) ); for ( int idx = 0; idx < keyCount; idx++ ) { VALUE key; VALUE val; char * tVal = 0; StrBuf keyStr; key = rb_ary_entry( keys, idx ); if ( key == Qnil ) break; keyStr.Set( STR2CSTR( rb_funcall( key, idToS, 0 ) ) ); val = rb_hash_aref( hash, key ); if ( rb_obj_is_kind_of( val, rb_cArray ) ) { // Need to flatten the array VALUE subVal; for( int idx2 = 0; (subVal = rb_ary_entry( val, idx2 ) ) ; idx2++ ) { if ( subVal == Qnil ) break; StrBuf tKey; tKey.Alloc( 32 ); sprintf( tKey.Text(), "%s%d", keyStr.Text(), idx2 ); tVal = STR2CSTR( rb_funcall( subVal, idToS, 0 ) ); specData.Dict()->SetVar( tKey.Text(), tVal ); if( P4RDB_DATA ) fprintf( stderr, "... %s -> %s\n", tKey.Text(), tVal ); } } else { tVal = STR2CSTR( rb_funcall( val, idToS, 0 ) ); specData.Dict()->SetVar( keyStr.Text(), tVal ); if( P4RDB_DATA ) fprintf( stderr, "... %s -> %s\n", keyStr.Text(), tVal ); } } s.Format( &specData, &b ); } // // This method returns a hash describing the valid fields in the spec. To // make it easy on our users, we map the lowercase name to the name defined // in the spec. Thus, the users can always user lowercase, and if the field // should be in mixed case, it will be. See P4::Spec::method_missing // VALUE SpecMgr::SpecFields( const char *type ) { return SpecFields( specs->GetVar( type ) ); } VALUE SpecMgr::SpecFields( StrPtr *specDef ) { if( !specDef ) return Qnil; // // There's no trivial way to do this using the API (and get it right), so // for now, we parse the string manually. We're ignoring the type of // the field, and any constraints it may be under; what we're interested // in is solely the field name // VALUE hash = rb_hash_new(); const char *b, *e; const char *sep = ";"; const char *fsep = ";;"; const char *seek = sep; for( e = b = specDef->Text(); e && b ; ) { e = strstr( b, seek ); if( e && seek == sep ) { StrBuf k; k.Set( b, e - b ); StrOps::Lower( k ); rb_hash_aset(hash, rb_str_new2( k.Text() ), rb_str_new( b, e - b )); b = ++e; seek = fsep; } else if( e ) { b = e += 2; seek = sep; } } return hash; } // // Split a key into its base name and its index. i.e. for a key "how1,0" // the base name is "how" and they index is "1,0". We work backwards from // the end of the key looking for the first char that is neither a // digit, nor a comma. // void SpecMgr::SplitKey( const StrPtr *key, StrBuf &base, StrBuf &index ) { int i = 0; base = *key; index = ""; for ( i = key->Length(); i; i-- ) { char prev = (*key)[ i-1 ]; if ( !isdigit( prev ) && prev != ',' ) { base.Set( key->Text(), i ); index.Set( key->Text() + i ); break; } } } // // Insert an element into the response structure. The element may need to // be inserted into an array nested deeply within the enclosing hash. // void SpecMgr::InsertItem( VALUE hash, const StrPtr *var, const StrPtr *val ) { VALUE ary = 0; VALUE tary = 0; VALUE key; ID idLength = rb_intern( "length" ); StrBuf base, index; StrRef comma( "," ); SplitKey( var, base, index ); // If there's no index, then we insert into the top level hash // but if the key is already defined then we need to rename the key. This // is probably one of those special keys like otherOpen which can be // both an array element and a scalar. The scalar comes last, so we // just rename it to "otherOpens" to avoid trashing the previous key // value if ( index == "" ) { ID idHasKey = rb_intern( "has_key?"); ID idPlus = rb_intern( "+" ); key = rb_str_new2( var->Text() ); if ( rb_funcall( hash, idHasKey, 1, key ) == Qtrue ) key = rb_funcall( key, idPlus, 1, rb_str_new2( "s" ) ); if( P4RDB_DATA ) fprintf( stderr, "... %s -> %s\n", STR2CSTR( key ), val->Text() ); rb_hash_aset( hash, key, rb_str_new2( val->Text() ) ); return; } // // Get or create the parent array from the hash. // key = rb_str_new2( base.Text() ); ary = rb_hash_aref( hash, key ); if ( Qnil == ary ) { ary = rb_ary_new(); rb_hash_aset( hash, key, ary ); } else if( rb_obj_is_kind_of( ary, rb_cArray ) != Qtrue ) { // // There's an index in our var name, but the name is already defined // and the value it contains is not an array. This means we've got a // name collision. This can happen in 'p4 diff2' for example, when // one file gets 'depotFile' and the other gets 'depotFile2'. In // these cases it makes sense to keep the structure flat so we // just use the raw variable name. // if( P4RDB_DATA ) fprintf( stderr, "... %s -> %s\n", var->Text(), val->Text() ); rb_hash_aset( hash, rb_str_new2( var->Text() ) , rb_str_new2( val->Text() ) ); return; } // The index may be a simple digit, or it could be a comma separated // list of digits. For each "level" in the index, we need a containing // array. if( P4RDB_DATA ) fprintf( stderr, "... %s -> [", base.Text() ); for( const char *c = 0 ; ( c = index.Contains( comma ) ); ) { StrBuf level; level.Set( index.Text(), c - index.Text() ); index.Set( c + 1 ); // Found another level so we need to get/create a nested array // under the current entry. We use the level as an index so that // missing entries are left empty deliberately. tary = rb_ary_entry( ary, level.Atoi() ); if ( ! RTEST( tary ) ) { tary = rb_ary_new(); rb_ary_store( ary, level.Atoi(), tary ); } if( P4RDB_DATA ) fprintf( stderr, "%s][", level.Text() ); ary = tary; } int pos = index.Atoi(); if( P4RDB_DATA ) fprintf( stderr, "%d] = %s\n", pos, val->Text() ); rb_ary_store( ary, pos, rb_str_new2( val->Text() ) ); } // // Create a new P4::Spec object and return it. // VALUE SpecMgr::NewSpec( StrPtr *specDef ) { ID idNew = rb_intern( "new" ); ID idP4 = rb_intern( "P4" ); ID idP4Spec = rb_intern( "Spec" ); VALUE cP4 = rb_const_get_at( rb_cObject, idP4 ); VALUE cP4Spec = rb_const_get_at( cP4, idP4Spec ); VALUE fields = SpecFields( specDef ); return rb_funcall( cP4Spec, idNew, 1, fields ); }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#24 | 14682 | Git Fusion |
Git Fusion branch management Imported from Git ghost-of-change-num: 960958 ghost-of-sha1: 005052ae424bd69f426f7209e741ca1c8c3253c7 ghost-precedes-sha1: ad052c71a568ef12165e143a6866ad9ceffbb4a1 parent-branch: None@960958 push-state: incomplete |
||
#23 | 14660 | tony | Update P4Ruby's spec maps for 2014.1. | ||
#22 | 14653 | jmistry |
Merging p12.2 changes to main Integration only change. |
||
#21 | 14649 | jmistry |
Pull 12.1 fixes to main Integration only change |
||
#20 | 14632 | jmistry | Update P4Ruby spec manager to 12.1 specs. | ||
#19 | 14624 | jmistry | Pull p11.1 changes back to main | ||
#18 | 14622 | jmistry |
Pull 10.2 changes to main Pick up missing changes in p10.2 and integrate to main. As part of the integrate I also moved the unit tests '16_streams.rb' and '17_streaming_handler.rb' because the integration introduced collisions with the unit test names. Updated MANIFEST with new names for unit tests and also added '98_unicode.rb', which was missing from it. |
||
#17 | 14615 | psoccard | Propagated spec field update | ||
#16 | 14608 | jmistry |
Add encoding to Strings As part of adding Ruby 1.9 support we need to associate the encoding for Ruby's strings from the server. This approach is similar to Sven's (in changelist 257263), where everything but the 'content' charset was set to 'utf8'. The content charset is picked up from P4CHARSET and this is used to translate any file content. Also disabled the Ruby 1.9 warning for each compile. User visible change to be documented in release notes. |
||
#15 | 14602 | jmistry |
Update specdefs in SpecMgr to 2011.1 User visible change, to be documented in the release notes. |
||
#14 | 14596 | jmistry |
Include 'extraTag' val in P4::Spec The tagged output of a stream spec includes an 'extraTag<n>' field. This change ensures that the field pointed to by 'extraTag' (such as 'firmerThanParent') is added to the P4::Spec object and it points to the correct value. Test case updated to check for 'firmerThanParent' in stream P4::Spec. The test case needs to disconnect/connect, otherwise P4.run_streams returns with the warning 'No such streams'. |
||
#13 | 14592 | Sven Erik Knop |
Enable P4-Ruby to compile and test with Ruby 1.9. The current solution is far from ideal because it is not possible to compile and test both Ruby 1.8 and Ruby 1.9 in parallel. The Makefile writes both artifacts and binaries to the same location. This means a user/tester/builder needs to choose on Ruby platform or ensure 'make clean' is called first. Many of the test cases also still fail in Ruby 1.9. We also need to investigate the Unicode story with Ruby 1.9 and see if the lessons learned from Python 3 can be applied somehow. Infrastructure change, no functional change yet. |
||
#12 | 14550 | tony | Remove redundant SpecMgr instance spotted by Sven Erik. | ||
#11 | 14549 | tony |
Followup to 191623, which inadvertently caused P4#parse_spec to return a ruby hash instead of a P4::Spec object. This change corrects that. Bug fix to earlier unreleased bug fix. |
||
#10 | 14544 | tony |
Enable P4Ruby to handle jobspec fields with names that end in numbers. Previously these were mistaken for entries in list fields (wlist, llist). This change introduces SpecDataRuby, a subclass of SpecData that reads from/writes to Ruby P4::Spec objects. That makes it a snap to parse and format specs using the same code the server does, and that fixes this bug very neatly, and probably makes it faster too. I've also replaced the manual parsing of the specdef strings with an implementation that uses the Spec, and SpecElem classes. That's also going to be more reliable in the long run. This change will be ported to P4Perl, P4Python, and should probably also go into the upcoming P4PHP. User-visible bug fix documented in p4rubynotes.txt |
||
#9 | 14541 | tony |
Copyright notice housekeeping: update all notices to 2008, and correct start date from 1997 to 2001 when P4Ruby was first released from the public depot. No functional change |
||
#8 | 14529 | tony |
Pull 2007.3 p4-ruby changes back to main. Integration only change |
||
#7 | 14521 | tony | Update copyright notices in all applicable P4Ruby files. | ||
#6 | 14517 | tony |
Dodge Windows porting issue by renaming 'struct spec' to 'struct defaultspec' because Windows' lame-o compiler can't disambiguate 'struct spec' and 'class Spec' because its namespace appears to be case folding. |
||
#5 | 14513 | tony |
Remove all compatibility code with versions of the API older than 2006.2 from both P4Perl and P4Ruby. We're insisting that people use a 2006.2 or later API for this first release, and will support the current API, and the previous two releases going forward. Also stripped out some reference Ruby code that was still lurking in P4Perl (commented out, obviously). Also ensured that both Makefile.PL and p4conf.rb insist on the minimum API level, and warn the user if they're attempting to build with an even newer release of the API. |
||
#4 | 14504 | tony |
Disambiguate my specs: rename AddSpec() to AddSpecDef() and HaveSpec() to HaveSpecDef() since those methods deal with manipulating the specdef cache rather than producing specs themselves. |
||
#3 | 14503 | tony |
Followon to previous change. Remove overloaded SpecMgr::SpecToString() as, now that ClientUserRuby knows which command we're running, we can dispense with it. |
||
#2 | 14502 | tony |
Rework spec handling somewhat so that: (a) P4Ruby knows about the default spec types for 2007.2 so it doesn't have to connect to the server to parse and format specs, and nor does it have to do the ugly hack of running a 'p4 xxx -o' and discarding the result just to get the specDef. (b) If a user's got a custom spec then the spec cache will be updated if they fetch an object of that type. So basically, if the server's given us a specdef for a class of spec, we use it. Otherwise, we fall back on the builtin defaults This reworks SpecMgr quite a bit - renaming methods so it's clearer what they do, and making it own the spec cache. We also pass the SpecMgr object created by P4ClientApi down to ClientUserRuby now, so that whenever the server sends us a spec, we can update the cache. |
||
#1 | 14480 | tony |
Add P4Ruby 1.5944 to main as start-point for the first productized release of P4Ruby |