|
| 1 | +import itertools |
| 2 | + |
| 3 | +from pkgcore.ebuild.atom import atom |
| 4 | +from snakeoil.sequences import iflatten_instance |
| 5 | +from snakeoil.strings import pluralism |
| 6 | + |
| 7 | +from .. import results |
| 8 | +from . import Check |
| 9 | + |
| 10 | + |
| 11 | +IUSE_PREFIX = "ruby_targets_" |
| 12 | + |
| 13 | + |
| 14 | +class RubyCompatUpdate(results.VersionResult, results.Info): |
| 15 | + """``USE_RUBY`` can be updated to support newer ruby version(s).""" |
| 16 | + |
| 17 | + def __init__(self, updates, **kwargs): |
| 18 | + super().__init__(**kwargs) |
| 19 | + self.updates = tuple(updates) |
| 20 | + |
| 21 | + @property |
| 22 | + def desc(self): |
| 23 | + s = pluralism(self.updates) |
| 24 | + updates = ", ".join(self.updates) |
| 25 | + return f"USE_RUBY update{s} available: {updates}" |
| 26 | + |
| 27 | + |
| 28 | +class RubyCompatCheck(Check): |
| 29 | + """Check ruby ebuilds for possible ``USE_RUBY`` updates. |
| 30 | +
|
| 31 | + Supports ebuilds inheriting ``ruby-ng``. |
| 32 | + """ |
| 33 | + |
| 34 | + known_results = frozenset({RubyCompatUpdate}) |
| 35 | + |
| 36 | + whitelist_categories = frozenset({"virtual"}) |
| 37 | + |
| 38 | + def __init__(self, *args): |
| 39 | + super().__init__(*args) |
| 40 | + repo = self.options.target_repo |
| 41 | + # sorter for ruby targets leveraging USE_EXPAND flag ordering from repo |
| 42 | + self.sorter = repo.use_expand_sorter("ruby_targets") |
| 43 | + |
| 44 | + # determine available USE_RUBY use flags |
| 45 | + targets = [] |
| 46 | + for target, _desc in repo.use_expand_desc.get(IUSE_PREFIX[:-1], ()): |
| 47 | + if target[len(IUSE_PREFIX) :].startswith("ruby"): |
| 48 | + targets.append(target[len(IUSE_PREFIX) :]) |
| 49 | + self.multi_targets = tuple(sorted(targets, key=self.sorter)) |
| 50 | + |
| 51 | + def ruby_deps(self, deps, prefix): |
| 52 | + for dep in (x for x in deps if x.use): |
| 53 | + for x in dep.use: |
| 54 | + if x.startswith(("-", "!")): |
| 55 | + continue |
| 56 | + if x.startswith(prefix): |
| 57 | + yield dep.no_usedeps |
| 58 | + break |
| 59 | + |
| 60 | + def deps(self, pkg): |
| 61 | + """Set of dependencies for a given package's attributes.""" |
| 62 | + return { |
| 63 | + p |
| 64 | + for attr in (x.lower() for x in pkg.eapi.dep_keys) |
| 65 | + for p in iflatten_instance(getattr(pkg, attr), atom) |
| 66 | + if not p.blocks |
| 67 | + } |
| 68 | + |
| 69 | + def feed(self, pkg): |
| 70 | + if pkg.category in self.whitelist_categories or "ruby-ng" not in pkg.inherited: |
| 71 | + return |
| 72 | + |
| 73 | + deps = self.deps(pkg) |
| 74 | + |
| 75 | + try: |
| 76 | + # determine the latest supported ruby version |
| 77 | + latest_target = sorted( |
| 78 | + ( |
| 79 | + f"ruby{x.slot.replace('.', '')}" |
| 80 | + for x in deps |
| 81 | + if x.key == "dev-lang/ruby" and x.slot is not None |
| 82 | + ), |
| 83 | + key=self.sorter, |
| 84 | + )[-1] |
| 85 | + except IndexError: |
| 86 | + return |
| 87 | + |
| 88 | + # determine ruby impls to target |
| 89 | + targets = set( |
| 90 | + itertools.takewhile(lambda x: x != latest_target, reversed(self.multi_targets)) |
| 91 | + ) |
| 92 | + |
| 93 | + if targets: |
| 94 | + try: |
| 95 | + # determine if deps support missing ruby targets |
| 96 | + for dep in self.ruby_deps(deps, IUSE_PREFIX): |
| 97 | + # TODO: use query caching for repo matching? |
| 98 | + latest = sorted(self.options.search_repo.match(dep))[-1] |
| 99 | + targets.intersection_update( |
| 100 | + f"ruby{x.rsplit('ruby', 1)[-1]}" |
| 101 | + for x in latest.iuse_stripped |
| 102 | + if x.startswith(IUSE_PREFIX) |
| 103 | + ) |
| 104 | + if not targets: |
| 105 | + return |
| 106 | + except IndexError: |
| 107 | + return |
| 108 | + |
| 109 | + yield RubyCompatUpdate(sorted(targets, key=self.sorter), pkg=pkg) |
0 commit comments