Skip to content

Commit

Permalink
Synchronize sat_semver.py (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp authored Feb 24, 2023
1 parent 8056931 commit 18fc632
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 24 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "bazel_upgradable",
strip_prefix = "bazel_upgradable-0.2.1",
url = "https://github.com/fenollp/bazel_upgradable/archive/0.2.1.zip",
strip_prefix = "bazel_upgradable-0.3.0",
url = "https://github.com/fenollp/bazel_upgradable/archive/0.3.0.zip",
)

load("@bazel_upgradable//:rule.bzl", "upgradable_repository")
Expand All @@ -37,7 +37,7 @@ load("@bazel_upgradable//:rule.bzl", "upgradable_repository")
```python
upgradable_repository(
name = "bazel_skylib",
remote = "git://github.com/bazelbuild/bazel-skylib.git",
remote = "https://github.com/bazelbuild/bazel-skylib.git",
)
```

Expand All @@ -46,7 +46,7 @@ upgradable_repository(
# Locking on major of a GitHub tag
upgradable_repository(
name = "bazel_skylib",
remote = "git://github.com/bazelbuild/bazel-skylib.git",
remote = "https://github.com/bazelbuild/bazel-skylib.git",
tag = "~1",
)

Expand All @@ -66,7 +66,7 @@ upgradable_repository(
build_file = "@//third_party:glfw3_osx.BUILD",
# {tag} and {tag_digits} are expanded during dependency resolution
release = "glfw-{tag_digits}.bin.MACOS.zip",
remote = "git://github.com/glfw/glfw.git",
remote = "https://github.com/glfw/glfw.git",
tag = "~3",
)
```
Expand Down
57 changes: 53 additions & 4 deletions diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import after

IGNORED = [
"android_gmaven_r8",
"android_tools",
"local_config_cc",
"local_config_cc_toolchains",
"local_config_sh",
"local_config_xcode",
"local_jdk",
"remote_coverage_tools",
"remote_java_tools",
"remote_java_tools_darwin",
"remote_java_tools_linux",
"remote_java_tools_windows",
Expand All @@ -24,24 +26,70 @@
"remotejdk10_win",
"remotejdk11_linux",
"remotejdk11_linux_aarch64",
"remotejdk11_linux_aarch64_toolchain_config_repo",
"remotejdk11_linux_ppc64le",
"remotejdk11_linux_ppc64le_toolchain_config_repo",
"remotejdk11_linux_s390x",
"remotejdk11_linux_s390x_toolchain_config_repo",
"remotejdk11_linux_toolchain_config_repo",
"remotejdk11_macos",
"remotejdk11_macos_aarch64",
"remotejdk11_macos_aarch64_toolchain_config_repo",
"remotejdk11_macos_toolchain_config_repo",
"remotejdk11_win",
"remotejdk11_win_arm64",
"remotejdk11_win_arm64_toolchain_config_repo",
"remotejdk11_win_toolchain_config_repo",
"remotejdk14_linux",
"remotejdk14_macos",
"remotejdk14_win",
"remotejdk15_linux",
"remotejdk15_linux_toolchain_config_repo",
"remotejdk15_macos",
"remotejdk15_macos_aarch64",
"remotejdk15_macos_aarch64_toolchain_config_repo",
"remotejdk15_macos_toolchain_config_repo",
"remotejdk15_win",
"remotejdk15_win_toolchain_config_repo",
"remotejdk16_linux",
"remotejdk16_linux_toolchain_config_repo",
"remotejdk16_macos",
"remotejdk16_macos_aarch64",
"remotejdk16_macos_aarch64_toolchain_config_repo",
"remotejdk16_macos_toolchain_config_repo",
"remotejdk16_win",
"remotejdk16_win_toolchain_config_repo",
"remotejdk17_linux",
"remotejdk17_linux_aarch64",
"remotejdk17_linux_aarch64_toolchain_config_repo",
"remotejdk17_linux_toolchain_config_repo",
"remotejdk17_macos",
"remotejdk17_macos_aarch64",
"remotejdk17_macos_aarch64_toolchain_config_repo",
"remotejdk17_macos_toolchain_config_repo",
"remotejdk17_win",
"remotejdk17_win_arm64",
"remotejdk17_win_arm64_toolchain_config_repo",
"remotejdk17_win_toolchain_config_repo",
"remotejdk18_linux",
"remotejdk18_linux_aarch64",
"remotejdk18_linux_aarch64_toolchain_config_repo",
"remotejdk18_linux_toolchain_config_repo",
"remotejdk18_macos",
"remotejdk18_macos_aarch64",
"remotejdk18_macos_aarch64_toolchain_config_repo",
"remotejdk18_macos_toolchain_config_repo",
"remotejdk18_win",
"remotejdk18_win_arm64",
"remotejdk18_win_arm64_toolchain_config_repo",
"remotejdk18_win_toolchain_config_repo",
"remotejdk_linux",
"remotejdk_linux_aarch64",
"remotejdk_macos",
"remotejdk_win",
"rules_cc",
"rules_java",
"rules_license",
"rules_proto",
]

Expand Down Expand Up @@ -76,11 +124,12 @@
xa = a[name]
d = {k: xa[k] for k in set(xa) - set(xb)}
if d:
dump = lambda x: print(json.dumps(x, indent=4, sort_keys=True))
print(name)
print("Before:")
print(json.dumps(xb, indent=4, sort_keys=True))
dump(xb)
print("Before:")
print(json.dumps(xa, indent=4, sort_keys=True))
dump(xa)
print("Difference:")
print(json.dumps(d, indent=4, sort_keys=True))
exit(len(2))
dump(d)
exit(2)
2 changes: 1 addition & 1 deletion example_upgradable_github_archive_HEAD/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ load("@bazel_upgradable//:rule.bzl", "upgradable_repository")
upgradable_repository(
name = "bazel_skylib",
branch = "main",
remote = "git://github.com/bazelbuild/bazel-skylib.git",
remote = "https://github.com/bazelbuild/bazel-skylib.git",
)

load("@bazel_skylib//lib:versions.bzl", "versions")
Expand Down
4 changes: 2 additions & 2 deletions example_upgradable_github_archive_HEAD/resolved.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ resolved = [
"original_attributes": {
"name": "bazel_skylib",
"branch": "master",
"remote": "git://github.com/bazelbuild/bazel-skylib.git"
"remote": "https://github.com/bazelbuild/bazel-skylib.git"
},
"repositories": [
{
Expand All @@ -120,7 +120,7 @@ resolved = [
"workspace_file_content": "",
"branch": "master",
"release": "",
"remote": "git://github.com/bazelbuild/bazel-skylib.git",
"remote": "https://github.com/bazelbuild/bazel-skylib.git",
"sha256": "a89898b28fb596ba8df76fe361597d08659dd58b7f54cb5ece5251f522de0691",
"strip_prefix": "bazel-skylib-feb52960ebd8797421b599194ad6ac7da3fc7600",
"tag": "",
Expand Down
2 changes: 1 addition & 1 deletion example_upgradable_github_archive_constrained/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ load("@bazel_upgradable//:rule.bzl", "upgradable_repository")

upgradable_repository(
name = "bazel_skylib",
remote = "git://github.com/bazelbuild/bazel-skylib.git",
remote = "https://github.com/bazelbuild/bazel-skylib.git",
tag = "~1",
)

Expand Down
4 changes: 2 additions & 2 deletions example_upgradable_github_archive_constrained/resolved.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ resolved = [
"original_rule_class": "@bazel_upgradable//:rule.bzl%upgradable_repository",
"original_attributes": {
"name": "bazel_skylib",
"remote": "git://github.com/bazelbuild/bazel-skylib.git",
"remote": "https://github.com/bazelbuild/bazel-skylib.git",
"tag": "~1"
},
"repositories": [
Expand All @@ -120,7 +120,7 @@ resolved = [
"workspace_file_content": "",
"branch": "",
"release": "",
"remote": "git://github.com/bazelbuild/bazel-skylib.git",
"remote": "https://github.com/bazelbuild/bazel-skylib.git",
"sha256": "839ee2a0ee5b728b7af73eac87b5e207ed2c8651b7bcf7c6142cdf4dd1ea738b",
"strip_prefix": "bazel-skylib-e59b620b392a8ebbcf25879fc3fde52b4dc77535",
"tag": "~1",
Expand Down
45 changes: 37 additions & 8 deletions sat_semver.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ def __init__(

self.partial = partial

# Cached precedence keys
# _cmp_precedence_key is used for semver-precedence comparison
self._cmp_precedence_key = self._build_precedence_key(with_build=False)
# _sort_precedence_key is used for self.precedence_key, esp. for sorted(...)
self._sort_precedence_key = self._build_precedence_key(with_build=True)

@classmethod
def _coerce(cls, value, allow_none=False):
if value is None and allow_none:
Expand Down Expand Up @@ -294,7 +300,8 @@ def coerce(cls, version_string, partial=False):

@classmethod
def parse(cls, version_string, partial=False, coerce=False):
"""Parse a version string into a Version() object.
"""Parse a version string into a tuple of components:
(major, minor, patch, prerelease, build).
Args:
version_string (str), the version string to parse
Expand Down Expand Up @@ -411,25 +418,47 @@ def __hash__(self):
# at least a field being `None`.
return hash((self.major, self.minor, self.patch, self.prerelease, self.build))

@property
def precedence_key(self):
def _build_precedence_key(self, with_build=False):
"""Build a precedence key.
The "build" component should only be used when sorting an iterable
of versions.
"""
if self.prerelease:
prerelease_key = tuple(
NumericIdentifier(part) if re.match(r'^[0-9]+$', part) else AlphaIdentifier(part)
NumericIdentifier(part) if part.isdigit() else AlphaIdentifier(part)
for part in self.prerelease
)
else:
prerelease_key = (
MaxIdentifier(),
)

if not with_build:
return (
self.major,
self.minor,
self.patch,
prerelease_key,
)

build_key = tuple(
NumericIdentifier(part) if part.isdigit() else AlphaIdentifier(part)
for part in self.build or ()
)

return (
self.major,
self.minor,
self.patch,
prerelease_key,
build_key,
)

@property
def precedence_key(self):
return self._sort_precedence_key

def __cmp__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
Expand Down Expand Up @@ -461,22 +490,22 @@ def __ne__(self, other):
def __lt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.precedence_key < other.precedence_key
return self._cmp_precedence_key < other._cmp_precedence_key

def __le__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.precedence_key <= other.precedence_key
return self._cmp_precedence_key <= other._cmp_precedence_key

def __gt__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.precedence_key > other.precedence_key
return self._cmp_precedence_key > other._cmp_precedence_key

def __ge__(self, other):
if not isinstance(other, self.__class__):
return NotImplemented
return self.precedence_key >= other.precedence_key
return self._cmp_precedence_key >= other._cmp_precedence_key


class SpecItem(object):
Expand Down
2 changes: 1 addition & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ for workspace in example_*; do
cp $L after.py

set +e
python ../diff.py "$PWD"
python3 ../diff.py "$PWD"
diffed=$?
set -e
rm before.py after.py
Expand Down

0 comments on commit 18fc632

Please sign in to comment.