|
| 1 | +import re |
| 2 | +import pkg_resources |
| 3 | +from typing import Any |
| 4 | +from semantic_version import Version, NpmSpec |
| 5 | + |
| 6 | +from pyteal.errors import TealPragmaError |
| 7 | + |
| 8 | + |
| 9 | +def __convert_pep440_compiler_version(compiler_version: str): |
| 10 | + """Convert PEP 440 version identifiers to valid NPM versions. |
| 11 | +
|
| 12 | + For example: |
| 13 | + "1.0.0" -> "1.0.0" |
| 14 | + "1.0.0a1" -> "1.0.0-a1" |
| 15 | + "<0.5.0+local || >=1.0.0a9.post1.dev2" -> "<0.5.0 || >=1.0.0-alpha9.1.2" |
| 16 | + """ |
| 17 | + NUMBER = r"(?:x|X|\*|0|[1-9][0-9]*)" |
| 18 | + LOCAL = r"[a-zA-Z0-9.]*" |
| 19 | + TRIM_PREFIX_RE = re.compile( |
| 20 | + r""" |
| 21 | + (?:v)? # Strip optional initial v |
| 22 | + (?P<op><|<=|>=|>|=|\^|~|) # Operator, can be empty |
| 23 | + (?P<major>{nb})(?:\.(?P<minor>{nb})(?:\.(?P<patch>{nb}))?)? |
| 24 | + (?:(?P<prerel_type>a|b|rc)(?P<prerel>{nb}))? # Optional pre-release |
| 25 | + (?:\.post(?P<postrel>{nb}))? # Optional post-release |
| 26 | + (?:\.dev(?P<dev>{nb}))? # Optional dev release |
| 27 | + (?:\+(?P<local>{lcl}))? # Optional local version |
| 28 | + """.format( |
| 29 | + nb=NUMBER, |
| 30 | + lcl=LOCAL, |
| 31 | + ), |
| 32 | + re.VERBOSE, |
| 33 | + ) |
| 34 | + |
| 35 | + def match_replacer(match: re.Match): |
| 36 | + ( |
| 37 | + op, |
| 38 | + major, |
| 39 | + minor, |
| 40 | + patch, |
| 41 | + prerel_type, |
| 42 | + prerel, |
| 43 | + postrel, |
| 44 | + dev, |
| 45 | + local, |
| 46 | + ) = match.groups() |
| 47 | + |
| 48 | + # Base version (major/minor/patch) |
| 49 | + base_version = "{}.{}.{}".format(major or "0", minor or "0", patch or "0") |
| 50 | + |
| 51 | + # Combine prerel, postrel, and dev |
| 52 | + combined_additions = [] |
| 53 | + short_prerel_type_to_long = { |
| 54 | + "a": "alpha", |
| 55 | + "b": "beta", |
| 56 | + "rc": "rc", |
| 57 | + } |
| 58 | + if prerel_type is not None: |
| 59 | + combined_additions.append(short_prerel_type_to_long[prerel_type] + prerel) |
| 60 | + if len(combined_additions) > 0 or postrel is not None or dev is not None: |
| 61 | + combined_additions.append(postrel or "0") |
| 62 | + if len(combined_additions) > 0 or dev is not None: |
| 63 | + combined_additions.append(dev or "0") |
| 64 | + combined_additions_str = ".".join(combined_additions) |
| 65 | + |
| 66 | + # Build full_version |
| 67 | + full_version = base_version |
| 68 | + if len(combined_additions) > 0: |
| 69 | + full_version += "-" + combined_additions_str |
| 70 | + if local is not None: |
| 71 | + full_version += "+" + local.lower() |
| 72 | + |
| 73 | + if op is not None: |
| 74 | + return op + full_version |
| 75 | + return full_version |
| 76 | + |
| 77 | + return re.sub(TRIM_PREFIX_RE, match_replacer, compiler_version) |
| 78 | + |
| 79 | + |
| 80 | +def is_valid_compiler_version(compiler_version: str): |
| 81 | + """Check if the compiler version is valid. |
| 82 | +
|
| 83 | + Args: |
| 84 | + compiler_version: The compiler version to check. |
| 85 | +
|
| 86 | + Returns: |
| 87 | + True if the compiler version is a valid NPM specification range |
| 88 | + using either the PEP 440 or semantic version format, otherwise False. |
| 89 | + """ |
| 90 | + try: |
| 91 | + pep440_converted = __convert_pep440_compiler_version(compiler_version) |
| 92 | + NpmSpec(pep440_converted) |
| 93 | + return True |
| 94 | + except ValueError: |
| 95 | + return False |
| 96 | + |
| 97 | + |
| 98 | +def pragma( |
| 99 | + *, |
| 100 | + compiler_version: str, |
| 101 | + **kwargs: Any, |
| 102 | +) -> None: |
| 103 | + """ |
| 104 | + Specify pragmas for the compiler. |
| 105 | +
|
| 106 | + Args: |
| 107 | + compiler_version: Acceptable versions of the compiler. Will fail if the current PyTeal version |
| 108 | + is not contained in the range. Follows the npm `semver range scheme <https://github.com/npm/node-semver#ranges>`_ |
| 109 | + for specifying compatible versions. |
| 110 | + """ |
| 111 | + pkg_version = pkg_resources.require("pyteal")[0].version |
| 112 | + pyteal_version = Version(__convert_pep440_compiler_version(pkg_version)) |
| 113 | + if pyteal_version not in NpmSpec( |
| 114 | + __convert_pep440_compiler_version(compiler_version) |
| 115 | + ): |
| 116 | + raise TealPragmaError( |
| 117 | + "PyTeal version {} is not compatible with compiler version {}".format( |
| 118 | + pkg_version, compiler_version |
| 119 | + ) |
| 120 | + ) |
0 commit comments