• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pantsbuild / pants / 19015773527

02 Nov 2025 05:33PM UTC coverage: 17.872% (-62.4%) from 80.3%
19015773527

Pull #22816

github

web-flow
Merge a12d75757 into 6c024e162
Pull Request #22816: Update Pants internal Python to 3.14

4 of 5 new or added lines in 3 files covered. (80.0%)

28452 existing lines in 683 files now uncovered.

9831 of 55007 relevant lines covered (17.87%)

0.18 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/python/pants/backend/go/util_rules/pkg_pattern.py
1
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md).
2
# Licensed under the Apache License, Version 2.0 (see LICENSE).
UNCOV
3
from __future__ import annotations
×
4

UNCOV
5
import re
×
UNCOV
6
from collections.abc import Callable
×
7

8
# Adapted from Go toolchain:
9
# https://github.com/golang/go/blob/6a70292d1cb3464e5b2c2c03341e5148730a1889/src/cmd/internal/pkgpattern/pkgpattern.go
10
#
11
# // Copyright 2022 The Go Authors. All rights reserved.
12
# // Use of this source code is governed by a BSD-style
13
# // license that can be found in the LICENSE file.
14

15

UNCOV
16
def match_pattern(pattern: str) -> Callable[[str], bool]:
×
17
    """MatchPattern(pattern)(name) reports whether name matches pattern. Pattern is a limited glob
18
    pattern in which '...' means 'any string' and there is no other special syntax. Unfortunately,
19
    there are two special cases. Quoting "go help packages":
20

21
    First, /... at the end of the pattern can match an empty string, so that net/... matches both
22
    net and packages in its subdirectories, like net/http. Second, any slash-separated pattern
23
    element containing a wildcard never participates in a match of the "vendor" element in the path
24
    of a vendored package, so that ./... does not match packages in subdirectories of ./vendor or
25
    ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do. Note, however, that a directory
26
    named vendor that itself contains code is not a vendored package: cmd/vendor would be a command
27
    named vendor, and the pattern cmd/... matches it.
28
    """
UNCOV
29
    return _match_pattern_internal(pattern, True)
×
30

31

UNCOV
32
def match_simple_pattern(pattern: str) -> Callable[[str], bool]:
×
33
    """MatchSimplePattern returns a function that can be used to check whether a given name matches
34
    a pattern, where pattern is a limited glob pattern in which '...' means 'any string', with no
35
    other special syntax.
36

37
    There is one special case for MatchPatternSimple: according to the rules in "go help packages":
38
    a /... at the end of the pattern can match an empty string, so that net/... matches both net and
39
    packages in its subdirectories, like net/http.
40
    """
UNCOV
41
    return _match_pattern_internal(pattern, False)
×
42

43

UNCOV
44
def _match_pattern_internal(pattern: str, vendor_exclude: bool) -> Callable[[str], bool]:
×
45
    # Convert pattern to regular expression.
46
    # The strategy for the trailing /... is to nest it in an explicit ? expression.
47
    # The strategy for the vendor exclusion is to change the unmatchable
48
    # vendor strings to a disallowed code point (vendorChar) and to use
49
    # "(anything but that codepoint)*" as the implementation of the ... wildcard.
50
    # This is a bit complicated, but the obvious alternative,
51
    # namely a handwritten search like in most shell glob matchers,
52
    # is too easy to make accidentally exponential.
53
    # Using package regexp guarantees linear-time matching.
54

UNCOV
55
    vendor_char = chr(0)  # "\x00"
×
56

UNCOV
57
    if vendor_exclude and vendor_char in pattern:
×
58
        return lambda _name: False
×
59

UNCOV
60
    r: str = re.escape(pattern)
×
UNCOV
61
    wild = ".*"
×
UNCOV
62
    if vendor_exclude:
×
UNCOV
63
        wild = rf"[^{vendor_char}]*"
×
UNCOV
64
        r = _replace_vendor(r, vendor_char)
×
65

UNCOV
66
        suffix = rf"/{vendor_char}/\.\.\."
×
UNCOV
67
        if r.endswith(suffix):
×
UNCOV
68
            r = r[0 : -len(suffix)] + rf"(/vendor|/{vendor_char}/\.\.\.)"
×
UNCOV
69
        elif r == rf"{vendor_char}/\.\.\.":
×
70
            r = rf"(/vendor|/{vendor_char}/\.\.\.)"
×
71

UNCOV
72
    suffix = r"/\.\.\."
×
UNCOV
73
    if r.endswith(suffix):
×
UNCOV
74
        r = r[0 : -len(suffix)] + r"(/\.\.\.)?"
×
UNCOV
75
    r = r.replace(r"\.\.\.", wild)
×
76

UNCOV
77
    reg = re.compile(rf"^{r}$")
×
78

UNCOV
79
    def f(name: str) -> bool:
×
UNCOV
80
        if vendor_exclude:
×
UNCOV
81
            if vendor_char in name:
×
82
                return False
×
UNCOV
83
            name = _replace_vendor(name, vendor_char)
×
UNCOV
84
        return bool(reg.match(name))
×
85

UNCOV
86
    return f
×
87

88

89
# replaceVendor returns the result of replacing
90
# non-trailing vendor path elements in x with repl.
UNCOV
91
def _replace_vendor(x: str, repl: str) -> str:
×
UNCOV
92
    if "vendor" not in x:
×
UNCOV
93
        return x
×
94

UNCOV
95
    elems = x.split("/")
×
UNCOV
96
    for i, elem in enumerate(elems[0:-1]):
×
UNCOV
97
        if elem == "vendor":
×
UNCOV
98
            elems[i] = repl
×
UNCOV
99
    return "/".join(elems)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc