@@ -679,10 +679,20 @@ def recurse_properties(prefix, rs, s, parent_ids):
679679
680680# Expects v to be 'v\d+', throws otherwise
681681def to_api_version (v ):
682- assert len (v ) >= 2
683- if v .startswith ('v' ):
684- v = v [1 :]
685- return v .replace ('.' , 'p' )
682+ m = re .search ("_?v(\d(\.\d)*)_?" , v )
683+ assert m , "Expected to find a version within '%s'" % v
684+
685+ tokens = m .group (1 ).split ('.' )
686+ for t in tokens [1 :]:
687+ if t == '0' :
688+ tokens .remove (t )
689+ version = '.' .join (tokens )
690+
691+ version = version .replace ('.' , 'd' )
692+ remainder = v .replace (m .group (0 ), '' )
693+ if remainder :
694+ version = version + '_' + remainder
695+ return version
686696
687697# build a full library name (non-canonical)
688698def library_name (name , version ):
@@ -796,3 +806,29 @@ def size_to_bytes(size):
796806 except KeyError :
797807 raise ValueError ("Invalid unit: '%s'" % unit )
798808 # end handle errors gracefully
809+
810+
811+ if __name__ == '__main__' :
812+ def test_to_version ():
813+ for v , want in (('v1.3' , '1d3' ),
814+ ('v1' , '1' ),
815+ ('directory_v1' , '1_directory' ),
816+ ('directory_v1.3' , '1d3_directory' ),
817+ ('v1beta2' , '1_beta2' ),
818+ ('v1sandbox' , '1_sandbox' ),
819+ ('v2.0' , '2' ),
820+ ('v2.0beta3' , '2_beta3' ),):
821+ res = to_api_version (v )
822+ assert res == want , "%s == %s" % (res , want )
823+ # end for each pair
824+
825+ for iv in ('some_branch_name' , '1.3' ):
826+ try :
827+ to_api_version (iv )
828+ except AssertionError :
829+ pass
830+ else :
831+ AssertionError ("Call should have failed" )
832+ # end for each invalid version
833+ # suite
834+ test_to_version ()
0 commit comments