ddtrace-internal 0.2.0__tar.gz → 0.3.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ddtrace-internal
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Stub internal Datadog Python package (ddtrace-internal).
5
5
  Author-email: "Datadog, Inc." <dev@datadoghq.com>
6
6
  License-Expression: BSD-3-Clause OR Apache-2.0
@@ -10,7 +10,13 @@ Description-Content-Type: text/markdown
10
10
 
11
11
  # ddtrace-internal
12
12
 
13
- Stub Python distribution for code used by multiple products in dd-trace-py
13
+ Pure-Python code shared by multiple products in dd-trace-py, extracted from
14
+ `ddtrace.internal`: the module registry and `codeowners` so far.
15
+
16
+ Pure Python by design -- this installs and imports with nothing but an
17
+ interpreter. The compiled components live in their own distribution, and the
18
+ two are peers: neither imports the other, and anything needing both depends on
19
+ both.
14
20
 
15
21
  ## Local install (editable)
16
22
 
@@ -1,6 +1,12 @@
1
1
  # ddtrace-internal
2
2
 
3
- Stub Python distribution for code used by multiple products in dd-trace-py
3
+ Pure-Python code shared by multiple products in dd-trace-py, extracted from
4
+ `ddtrace.internal`: the module registry and `codeowners` so far.
5
+
6
+ Pure Python by design -- this installs and imports with nothing but an
7
+ interpreter. The compiled components live in their own distribution, and the
8
+ two are peers: neither imports the other, and anything needing both depends on
9
+ both.
4
10
 
5
11
  ## Local install (editable)
6
12
 
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ddtrace-internal"
7
- version = "0.2.0"
7
+ version = "0.3.0"
8
8
  description = "Stub internal Datadog Python package (ddtrace-internal)."
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.9"
@@ -17,6 +17,9 @@ Repository = "https://github.com/DataDog/datadog-apm-python"
17
17
  [tool.setuptools.packages.find]
18
18
  where = ["src"]
19
19
 
20
+ [tool.setuptools.package-data]
21
+ _ddtrace_internal = ["py.typed"]
22
+
20
23
  [tool.pytest.ini_options]
21
24
  # Tests live at tests/<module_path>/test.py, mirroring the source layout so
22
25
  # sibling fixtures/data for a module's tests have a natural home -- pytest's
@@ -0,0 +1,218 @@
1
+ """Parse CODEOWNERS files and match paths against their rules.
2
+
3
+ Extracted from dd-trace-py (``ddtrace/internal/codeowners.py``) as the first
4
+ module of the package split; the supported surface is ``Codeowners`` -- its
5
+ constructor and ``of()``. Everything else here is internal.
6
+ """
7
+
8
+ import re
9
+ from pathlib import Path
10
+ from typing import Optional
11
+
12
+
13
+ def _close_char_class(regex: str, *, in_char_class: bool) -> tuple[str, bool]:
14
+ """Append a closing ``]``: a plain literal outside a character class."""
15
+ if in_char_class:
16
+ return regex + "]", False
17
+ return regex + re.escape("]"), in_char_class
18
+
19
+
20
+ def _is_full_segment_double_star(index: int, pattern: str) -> bool:
21
+ """Whether the ``*`` at ``pattern[index]`` opens a full-segment ``**``.
22
+
23
+ A ``**`` only matches across path segments when it is anchored on both
24
+ sides by pattern edges or slashes (``**/x``, ``a/**/b``, ``a/**``);
25
+ otherwise it degrades to per-segment ``[^/]*`` matching.
26
+ """
27
+ if index + 1 >= len(pattern) or pattern[index + 1] != "*":
28
+ return False
29
+ left_anchored = index == 0
30
+ leading_slash = index > 0 and pattern[index - 1] == "/"
31
+ right_anchored = index + 2 == len(pattern)
32
+ trailing_slash = index + 2 < len(pattern) and pattern[index + 2] == "/"
33
+ return (left_anchored or leading_slash) and (right_anchored or trailing_slash)
34
+
35
+
36
+ def path_to_regex(pattern: str) -> re.Pattern[str]:
37
+ """Translate a CODEOWNERS pattern into an anchored regular expression.
38
+
39
+ Ported from https://github.com/sbdchd/codeowners/blob/c95e13d384ac09cfa1c23be1a8601987f41968ea/codeowners/__init__.py
40
+
41
+ Copyright (c) 2019-2020 Steve Dignam
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining
44
+ a copy of this software and associated documentation files (the
45
+ "Software"), to deal in the Software without restriction, including
46
+ without limitation the rights to use, copy, modify, merge, publish,
47
+ distribute, sublicense, and/or sell copies of the Software, and to
48
+ permit persons to whom the Software is furnished to do so, subject to
49
+ the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be
52
+ included in all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
55
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
57
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
58
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
59
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
60
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
61
+
62
+ ported from https://github.com/hmarr/codeowners/blob/d0452091447bd2a29ee508eebc5a79874fb5d4ff/match.go#L33
63
+
64
+ MIT License
65
+
66
+ Copyright (c) 2020 Harry Marr
67
+ Permission is hereby granted, free of charge, to any person obtaining a copy
68
+ of this software and associated documentation files (the "Software"), to deal
69
+ in the Software without restriction, including without limitation the rights
70
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
71
+ copies of the Software, and to permit persons to whom the Software is
72
+ furnished to do so, subject to the following conditions:
73
+ The above copyright notice and this permission notice shall be included in all
74
+ copies or substantial portions of the Software.
75
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
76
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
77
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
78
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
79
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
80
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
81
+ SOFTWARE.
82
+ """
83
+ regex = ""
84
+
85
+ slash_pos = pattern.find("/")
86
+ anchored = slash_pos > -1 and slash_pos != len(pattern) - 1
87
+
88
+ regex += r"\A" if anchored else r"(?:\A|/)"
89
+
90
+ matches_dir = pattern[-1] == "/"
91
+ pattern_trimmed = pattern.strip("/")
92
+
93
+ in_char_class = False
94
+ escaped = False
95
+
96
+ iterator = enumerate(pattern_trimmed)
97
+ for i, ch in iterator:
98
+ if escaped:
99
+ regex += re.escape(ch)
100
+ escaped = False
101
+ continue
102
+
103
+ if ch == "\\":
104
+ escaped = True
105
+ elif ch == "*":
106
+ if _is_full_segment_double_star(i, pattern_trimmed):
107
+ regex += ".*"
108
+
109
+ next(iterator, None)
110
+ next(iterator, None)
111
+ continue
112
+ regex += "[^/]*"
113
+ elif ch == "?":
114
+ regex += "[^/]"
115
+ elif ch == "[":
116
+ in_char_class = True
117
+ regex += ch
118
+ elif ch == "]":
119
+ regex, in_char_class = _close_char_class(regex, in_char_class=in_char_class)
120
+ else:
121
+ regex += re.escape(ch)
122
+
123
+ if in_char_class:
124
+ raise ValueError(f"unterminated character class in pattern {pattern}")
125
+
126
+ regex += "/" if matches_dir else r"(?:\Z|/)"
127
+ return re.compile(regex)
128
+
129
+
130
+ class Codeowners:
131
+ """Provide interface to parse CODEOWNERS file and match a given path against it."""
132
+
133
+ KNOWN_LOCATIONS = (
134
+ "CODEOWNERS",
135
+ ".github/CODEOWNERS",
136
+ "docs/CODEOWNERS",
137
+ ".gitlab/CODEOWNERS",
138
+ )
139
+
140
+ def __init__(self, path: Optional[str] = None, cwd: Optional[str] = None):
141
+ """Initialize Codeowners object.
142
+
143
+ :param path: path to CODEOWNERS file otherwise try to use any from known locations
144
+ """
145
+ self.patterns: list[tuple[re.Pattern[str], list[str]]] = []
146
+ self.path: Optional[str] = None
147
+
148
+ path = path or self.location(cwd)
149
+
150
+ if path is None:
151
+ raise ValueError("CODEOWNERS file not found")
152
+
153
+ self.path = path
154
+ self.parse()
155
+
156
+ def location(self, cwd: Optional[str] = None) -> Optional[str]:
157
+ """Return the location of the CODEOWNERS file."""
158
+ base = Path(cwd or Path.cwd())
159
+ for location in self.KNOWN_LOCATIONS:
160
+ candidate = base / location
161
+ if candidate.is_file():
162
+ return str(candidate)
163
+ return None
164
+
165
+ def parse(self) -> None:
166
+ """Parse CODEOWNERS file and store the lines and regexes."""
167
+ if self.path is None:
168
+ return
169
+
170
+ with Path(self.path).open() as f:
171
+ patterns: list[tuple[re.Pattern[str], list[str]]] = []
172
+ for raw_line in f:
173
+ line = raw_line.strip()
174
+
175
+ if "#" in line:
176
+ # Strip out the comment from the line
177
+ line = line.split("#", 1)[0].strip()
178
+
179
+ if line == "":
180
+ continue
181
+
182
+ if line.startswith("[") and line.endswith("]"):
183
+ # found a code owners section
184
+ continue
185
+ if line.startswith("^[") and line.endswith("]"):
186
+ # found an optional code owners section
187
+ continue
188
+
189
+ elements = line.split()
190
+ if len(elements) < 2:
191
+ continue
192
+
193
+ path = elements[0]
194
+
195
+ try:
196
+ pattern = path_to_regex(path)
197
+ except (ValueError, IndexError):
198
+ continue
199
+
200
+ owners = [owner for owner in elements[1:] if owner]
201
+
202
+ if not owners:
203
+ continue
204
+ patterns.append((pattern, owners))
205
+ # Order is important. The last matching pattern has the most precedence.
206
+ patterns.reverse()
207
+ self.patterns = patterns
208
+
209
+ def of(self, path: str) -> list[str]:
210
+ """Return code owners for a given path.
211
+
212
+ :param path: path to check
213
+ :return: list of file code owners identified by the given path
214
+ """
215
+ for pattern, owners in self.patterns:
216
+ if pattern.search(path):
217
+ return owners
218
+ return []
File without changes
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ddtrace-internal
3
- Version: 0.2.0
3
+ Version: 0.3.0
4
4
  Summary: Stub internal Datadog Python package (ddtrace-internal).
5
5
  Author-email: "Datadog, Inc." <dev@datadoghq.com>
6
6
  License-Expression: BSD-3-Clause OR Apache-2.0
@@ -10,7 +10,13 @@ Description-Content-Type: text/markdown
10
10
 
11
11
  # ddtrace-internal
12
12
 
13
- Stub Python distribution for code used by multiple products in dd-trace-py
13
+ Pure-Python code shared by multiple products in dd-trace-py, extracted from
14
+ `ddtrace.internal`: the module registry and `codeowners` so far.
15
+
16
+ Pure Python by design -- this installs and imports with nothing but an
17
+ interpreter. The compiled components live in their own distribution, and the
18
+ two are peers: neither imports the other, and anything needing both depends on
19
+ both.
14
20
 
15
21
  ## Local install (editable)
16
22
 
@@ -2,7 +2,9 @@ README.md
2
2
  pyproject.toml
3
3
  setup.py
4
4
  src/_ddtrace_internal/__init__.py
5
+ src/_ddtrace_internal/codeowners.py
5
6
  src/_ddtrace_internal/modules.py
7
+ src/_ddtrace_internal/py.typed
6
8
  src/ddtrace_internal.egg-info/PKG-INFO
7
9
  src/ddtrace_internal.egg-info/SOURCES.txt
8
10
  src/ddtrace_internal.egg-info/dependency_links.txt