Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1470a8c

Browse files
authored
Add public symbols checker script (open-telemetry#1816)
1 parent 3dbbd1b commit 1470a8c

File tree

5 files changed

+156
-3
lines changed

5 files changed

+156
-3
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Public API check
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, reopened, labeled, unlabeled]
6+
branches:
7+
- main
8+
9+
jobs:
10+
publicAPICheck:
11+
12+
runs-on: ubuntu-latest
13+
14+
if: "!contains(github.event.pull_request.labels.*.name, 'Skip Public API check')"
15+
16+
steps:
17+
- name: Checkout the repo
18+
uses: actions/checkout@v2
19+
with:
20+
fetch-depth: 0
21+
22+
- name: Checkout main
23+
run: git checkout main
24+
25+
- name: Pull origin
26+
run: git pull --rebase=false origin main
27+
28+
- name: Checkout pull request
29+
run: git checkout ${{ github.event.pull_request.head.sha }}
30+
31+
- name: Set up Python
32+
uses: actions/setup-python@v2
33+
with:
34+
python-version: 3.9
35+
36+
- name: Install tox
37+
run: pip install -U tox-factor
38+
39+
- name: Public API Check
40+
run: tox -e public-symbols-check

.pylintrc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,9 @@ disable=missing-docstring,
7373
exec-used,
7474
super-with-arguments, # temp-pylint-upgrade
7575
isinstance-second-argument-not-valid-type, # temp-pylint-upgrade
76-
raise-missing-from, # temp-pylint-upgrade
77-
unused-argument, # temp-pylint-upgrade
76+
raise-missing-from, # temp-pylint-upgrade
77+
unused-argument, # temp-pylint-upgrade
78+
redefined-builtin,
7879

7980
# Enable the message, report, category or checker with the given id(s). You can
8081
# either give multiple identifier separated by comma (,) or put this option

CONTRIBUTING.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ You can run:
6969
Python version
7070
- `tox -e lint` to run lint checks on all code
7171

72+
We try to keep the amount of _public symbols_ in our code minimal. A public symbol is any Python identifier that does not start with an underscore.
73+
Every public symbol is something that has to be kept in order to maintain backwards compatibility, so we try to have as few as possible.
74+
75+
To check if your PR is adding public symbols, run `tox -e public-symbols-check`. This will always fail if public symbols are being added. The idea
76+
behind this is that every PR that adds public symbols fails in CI, forcing reviewers to check the symbols to make sure they are strictly necessary.
77+
If after checking them, it is considered that they are indeed necessary, the PR will be labeled with `Skip Public API check` so that this check is not
78+
run.
79+
7280
See
7381
[`tox.ini`](https://github.com/open-telemetry/opentelemetry-python/blob/main/tox.ini)
7482
for more detail on available tox commands.

scripts/public_symbols_checker.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from difflib import unified_diff
16+
from pathlib import Path
17+
from re import match
18+
from sys import exit
19+
20+
from git import Repo
21+
from git.db import GitDB
22+
23+
repo = Repo(__file__, odbt=GitDB, search_parent_directories=True)
24+
25+
26+
file_path_symbols = {}
27+
28+
29+
def get_symbols(change_type, diff_lines_getter, prefix):
30+
for diff_lines in (
31+
repo.commit("main")
32+
.diff(repo.head.commit)
33+
.iter_change_type(change_type)
34+
):
35+
36+
b_file_path = diff_lines.b_blob.path
37+
38+
if (
39+
Path(b_file_path).suffix != ".py"
40+
or "opentelemetry" not in b_file_path
41+
):
42+
continue
43+
44+
for diff_line in diff_lines_getter(diff_lines):
45+
matching_line = match(
46+
r"{prefix}({symbol_re})\s=\s.+|"
47+
r"{prefix}def\s({symbol_re})|"
48+
r"{prefix}class\s({symbol_re})".format(
49+
symbol_re=r"[a-zA-Z][_\w]+", prefix=prefix
50+
),
51+
diff_line,
52+
)
53+
54+
if matching_line is not None:
55+
if b_file_path not in file_path_symbols.keys():
56+
file_path_symbols[b_file_path] = []
57+
58+
file_path_symbols[b_file_path].append(
59+
next(filter(bool, matching_line.groups()))
60+
)
61+
62+
63+
def a_diff_lines_getter(diff_lines):
64+
return diff_lines.b_blob.data_stream.read().decode("utf-8").split("\n")
65+
66+
67+
def m_diff_lines_getter(diff_lines):
68+
return unified_diff(
69+
diff_lines.a_blob.data_stream.read().decode("utf-8").split("\n"),
70+
diff_lines.b_blob.data_stream.read().decode("utf-8").split("\n"),
71+
)
72+
73+
74+
get_symbols("A", a_diff_lines_getter, r"")
75+
get_symbols("M", m_diff_lines_getter, r"\+")
76+
77+
if file_path_symbols:
78+
print("The code in this branch adds the following public symbols:")
79+
print()
80+
for file_path, symbols in file_path_symbols.items():
81+
print("- {}".format(file_path))
82+
for symbol in symbols:
83+
print("\t{}".format(symbol))
84+
print()
85+
86+
print(
87+
"Please make sure that all of them are strictly necessary, if not, "
88+
"please consider prefixing them with an underscore to make them "
89+
'private. After that, please label this PR with "Skip Public API '
90+
'check".'
91+
)
92+
exit(1)
93+
else:
94+
print("The code in this branch will not add any public symbols")

tox.ini

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ envlist =
3333

3434
; opentelemetry-exporter-jaeger-proto-grpc
3535
py3{6,7,8,9}-test-exporter-jaeger-proto-grpc
36-
36+
3737
; opentelemetry-exporter-jaeger-thrift
3838
py3{6,7,8,9}-test-exporter-jaeger-thrift
3939

@@ -78,6 +78,7 @@ envlist =
7878
mypy,mypyinstalled
7979
docs
8080
docker-tests
81+
public-symbols-check
8182

8283
[testenv]
8384
deps =
@@ -186,6 +187,7 @@ deps =
186187
psutil
187188
readme_renderer
188189
httpretty
190+
GitPython
189191

190192
commands_pre =
191193
python -m pip install -e {toxinidir}/opentelemetry-api[test]
@@ -268,3 +270,11 @@ commands =
268270
pytest {posargs}
269271
commands_post =
270272
docker-compose down -v
273+
274+
[testenv:public-symbols-check]
275+
basepython: python3.8
276+
recreate = True
277+
deps =
278+
GitPython
279+
commands =
280+
python {toxinidir}/scripts/public_symbols_checker.py

0 commit comments

Comments
 (0)