
    Vi:4                        S SK r S SKJr  S SKJr  S SKJr  S SKJr  S SKJr  S SKJ	r	  \ " S S	5      5       r
\ " S
 S5      5       r\ " S S5      5       r\ " S S5      5       rS\\   S\\S4   4S jrS\\   S\4S jrS\S\
4S jrS\S\4S jrS\S\4S jrS\
S\
S\4S jrS\S\S\4S jrS \S!\S\4S" jrS#\S$\S\4S% jrS&\S'\S\4S( jrg))    N)	dataclass)datetime)List)Optional)Union)SemgrepErrorc                   8    \ rS rSr% Sr\\S'   \\S'   \\S'   Srg)GolangVersionCore   zW
Holds integer impressions of the Golang Core Version
assuming <major>:<minor>:<patch>
majorminorpatch N)__name__
__module____qualname____firstlineno____doc__int__annotations____static_attributes__r       h/home/james-whalen/.local/share/pipx/venvs/semgrep/lib/python3.13/site-packages/semdep/golang_version.pyr
   r
      s    
 JJJr   r
   c                   .    \ rS rSr% Sr\\S'   \\S'   Srg)GolangPseudoVersion#   a  
If we see a golang version string which contains a commit hash at the end,
this is referred to as a "pseudo-version". Pseudo-versions have the
following format:

    <core>-YYYYMMDDHHMMSS-<commit-hash>

Note: This type is purely for capturing datetimes and commit hashes which are the sole
modifiers of the core version. If a pre-release version is present, we will parse it as a
pre-release version instead even if a timestamp is present since this will be captured in the
alphanumeric comparison of the pre-release version.

e.g.)
    "1.0.0-20220225172249-27dd8689420f" is parsed as a GolangPsuedoVersion
    "1.0.0-0.20220225172249-27dd8689420f" is parsed as a GolangPreReleaseVersion

	timestampcommit_hashr   N)	r   r   r   r   r   r   r   strr   r   r   r   r   r   #   s    $ r   r   c                   *    \ rS rSr% Sr\\   \S'   Srg)GolangPreReleaseVersion;   ae  
If we see a golang version string which contains a pre-release version at the end,
this is referred to as a "pre-release version". Pre-release versions have the
following format:

    <core>-<a list of pre-release identifiers separated by .>

From the semantic versioning (semver) spec, pre-release versions can be of unlimited
length and can contain any combination of alphanumerics and hyphens. The spec to compare
pre-release versions can be found here: https://semver.org/#spec-item-11

These versions are always prefixed by a future stable release of the core
version, indicated by core in the format above
pre_release_identifiersr   N)	r   r   r   r   r   r   r   r   r   r   r   r   r!   r!   ;   s     "#Y&r   r!   c                   L    \ rS rSr% Sr\\S'   Sr\\	   \S'   Sr
\\   \S'   Srg)ParsedGolangVersionO   zf
Holds the core part an optional PseudoVersion if the raw Golang version is
actually a Pseudo-version
coreNpseudopre_releaser   )r   r   r   r   r   r
   r   r(   r   r   r)   r!   r   r   r   r   r%   r%   O   s/    
 ,0FH()059K129r   r%   split_raw_versionreturnc                 \    U S   n [        U5      n[        X S   S9$ ! [         a     gf = f)z
Tries to parse a raw pseudo version string into a GolangPseudoVersion object. Returns None
if the raw version string does not conform to the pseudo version format

args:
    split_raw_version: A list of strings split by "-" from the raw version string
      )r   r   N)parse_golang_pseudo_datetimer   
ValueError)r*   raw_timestampparsed_timestamps      r   try_parse_golang_pseudor3   [   sG     &a(M7F"&a4H
 	
  s    
++c                 ^    SR                  U SS 5      nUR                  S5      n[        US9$ )zO
Parses a raw pre-release version string into a GolangPreReleaseVersion object
-r-   N.)r#   )joinsplitr!   )r*   raw_pre_releasesplit_pre_releases      r   parse_golang_pre_releaser;   o   s7     hh045O'--c2";LMMr   raw_corec                     U S:X  a  [        SSSS9$ U R                  S5      n[        [        US   5      [        US   5      [        US   5      S9$ )z
Splits the core into major, minor and patch components, turning them into
integers which can be effectively used to compare against other like components
in other GolangVersionCore objects
0r   r   r   r   r6   r-   r.   )r
   r8   r   )r<   split_raw_cores     r   parse_golang_corerA   x   s\     3 q;;^^C(N.#$.#$.#$ r   r1   c                 0    [         R                  " U S5      $ )z`
Casts the raw timestamp into a timestamp which can be compared
temporally to another timestamp
z%Y%m%d%H%M%S)r   strptime)r1   s    r   r/   r/      s    
 ]N;;r   raw_versionc                     U R                  S5      n[        US   5      nSnSn[        U5      S:  a4  [        U5      S:X  a  [        U5      nUc  [	        U5      nO[	        U5      n[        X#US9$ )zx
Parses a raw version string into a ParsedGolangVersion which holds the
core component and an optional pseudo component
r5   r   Nr-      )r'   r(   r)   )r8   rA   lenr3   r;   r%   )rD   r*   parsed_coreparsed_pseudoparsed_pre_releases        r   parse_golang_versionrK      s    
 $))#.#$5a$89KM" !Q& 44EFM$
 &>>O%P" "::K!L<N r   parsed_version_corespecifier_version_corec                     U R                   UR                   -
  nUS:w  a  U$ U R                  UR                  -
  nUS:w  a  U$ U R                  UR                  -
  nUS:w  a  U$ g)a  
Checks which core version is higher between the package version and the spec version
from the rule. Does this by first comparing the major component and if equal, comparing
the minor and vice versa.

If at any point, the encounter a non-zero diff, we know to stop calculations there and
just return the diff

Returns:
 a) 1 if the parsed core is after the specifier core
 b) -1 if the specifier core is after the parsed core
 c) 0 if they have equal precedence
r   r?   )rL   rM   
major_diff
minor_diff
patch_diffs        r   cmp_corerR      ss      %**-C-I-IIJQ$**-C-I-IIJQ$**-C-I-IIJQr   parsed_version_pseudospecifier_version_pseudoc                     U R                   UR                   :X  a  gU R                  UR                  :  nU(       a  gU R                  UR                  :  nU(       a  gg)z
Compares the Pseudo portion of a golang pseudo-version, specifically the timestamp

Returns:
 a) 1 if the parsed pseudo is after the specifier pseudo
 b) -1 if the specifier pseudo is after the parsed pseudo
 c) 0 if they have equal precedence
r   r-   )r   r   )rS   rT   isAfterisBefores       r   
cmp_pseudorY      sY     ((,D,P,PP#--0H0R0RRG(225M5W5WWr   parsed_version_pre_releasespecifier_version_pre_releasec                    [        U R                  5      n[        UR                  5      n[        U R                  UR                  5       Hs  u  pEXE:X  a  M  UR                  5       nUR                  5       nU(       a   U(       a  [	        U5      [	        U5      -
  s  $ U(       a    gU(       a    gXE:  a    gXE:  d  Ms    g   X2:  a  gX#:  a  gg)a/  
Compares the pre-release portion of a golang pre-release version, specifically the milestone and version (if applicable)

Returns:
 a) 1 if the parsed pre-release is after the specifier pre-release
 b) -1 if the specifier pre-release is after the parsed pre-release
 c) 0 if they have equal precedence
rV   r-   r   )rG   r#   zipisdigitr   )rZ   r[   parsed_num_identifiersspecifier_num_identifiersparsed_identifierspecifier_identifieris_parsed_identifier_numberis_specifier_identifier_numbers           r   cmp_pre_releasere      s     !!;!S!ST #%==!
 47"::%==4/ 4 '8&?&?&A#)=)E)E)G& '+I()C0D,EEE(+ 35148 !9		;r   parsed_versionspecifier_versionc                    [        U R                  UR                  5      nUS:w  a  U$ U R                  (       a:  UR                  (       a)  [        U R                  UR                  5      nUS:w  a  U$ O$U R                  (       a  gUR                  (       a  gU R                  (       a;  UR                  (       a*  [        U R                  UR                  5      nUS:w  a  U$  gU R                  (       a  gUR                  (       a  gg)a9  
Compares the package version and the spec version. For a given core version, at a high level, the order
of release between the three major categories of a golang version (pre-release, core and pseudo) is as follows:

pre-release => core => pseudo

e.g.) given a core version of 1.0.0, the following versions are ordered from lowest to highest:

1.0.0-alpha.1 < 1.0.0 < 1.0.0-20220225172249-27dd8689420f

Returns:
 a) 1 if the parsed version is after the specifier version
 b) -1 if the specifier version is after the parsed version
 c) 0 if they have equal precedence
r   r-   rV   )rR   r'   r)   re   r(   rY   )rf   rg   	core_diffpre_release_diffpseudo_diffs        r   cmp_golang_versionsrl     s    & ,,.?.D.DEIA~ !!&7&C&C*&&(9(E(E
 q ## ! 
	#	#		&	& !2!9!9 !6!68I8P8PQ!   
				!	!r   	specifierversionc                    [         R                  " S5      nUR                  U 5      nU(       d  [        SU  35      eUR	                  S5      n[        UR	                  S5      5      n[        U5      nUS:X  a  [        Xe5      S:H  $ US:X  a  [        Xe5      S:g  $ US:X  a  [        Xe5      S:*  $ US	:X  a  [        Xe5      S:  $ US
:X  a  [        Xe5      S:  $ US:X  a  [        Xe5      S:  $ [        SU  35      e)z2
Returns if version satisfies specifier arguments
z1(?P<operator>(==|!=|<=|>=|<|>))\s*(?P<version>.*)z/unknown package version comparison expression: operatorrn   z==r   z!=z<=<z>=>)recompilematchr   grouprK   rl   )rm   rn   specifier_regexmatchedrp   rg   rf   s          r   compare_golang_specifierry   L  s    jj@O ##I.G=i[I
 	
 }}Z(H,W]]9-EF)'2N 4">EJJ	T	">EJJ	T	">EJJ	S">EII	T	">EJJ	S">EII=i[I
 	
r   )rs   dataclassesr   r   typingr   r   r   semgrep.errorr   r
   r   r!   r%   r   r3   r;   rA   r/   rK   r   rR   rY   re   rl   boolry   r   r   r   <module>r~      s   
 !     &      . ' ' '& : : :Cy
$%(NS	 N>U N (9  < < <c .A <*DU8.1 	03 73#:3 	3l1'1<O11h!
 !
c !
d !
r   