Skip to content

Commit

Permalink
Add public symbols checker script
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed May 7, 2021
1 parent 81d80aa commit 9977b5f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
97 changes: 97 additions & 0 deletions scripts/public_symbols_checker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright The OpenTelemetry Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from difflib import unified_diff
from os import getcwd
from pathlib import Path
from re import match

from git import Repo

repo = Repo(getcwd())

active_branch = repo.active_branch.name
diff_index = repo.commit("main").diff(repo.active_branch.name)

symbol = r"[a-zA-Z][_\w]+"

file_path_symbols = {}


def get_symbols(change_type, diff_lines_getter, prefix):
for diff_lines in diff_index.iter_change_type(change_type):

b_file_path = diff_lines.b_blob.path

if (
Path(b_file_path).suffix != ".py"
or "opentelemetry" not in b_file_path
):
continue

for diff_line in diff_lines_getter(diff_lines):
matching_line = match(
r"{prefix}({symbol})\s=\s.+|"
r"{prefix}def\s({symbol})|"
r"{prefix}class\s({symbol})".format(
symbol=symbol, prefix=prefix
),
diff_line,
)

if matching_line is not None:
if b_file_path not in file_path_symbols.keys():
file_path_symbols[b_file_path] = []

file_path_symbols[b_file_path].append(
next(filter(bool, matching_line.groups()))
)


def a_diff_lines_getter(diff_lines):
return diff_lines.b_blob.data_stream.read().decode("utf-8").split("\n")


def m_diff_lines_getter(diff_lines):
return unified_diff(
diff_lines.a_blob.data_stream.read().decode("utf-8").split("\n"),
diff_lines.b_blob.data_stream.read().decode("utf-8").split("\n"),
)


get_symbols("A", a_diff_lines_getter, r"")
get_symbols("M", m_diff_lines_getter, r"\+")

if file_path_symbols:
print(
"The {} branch adds the following public symbols:".format(
active_branch
)
)
print()
for file_path, symbols in file_path_symbols.items():
print("- {}".format(file_path))
for symbol in symbols:
print("\t{}".format(symbol))
print()

print(
"Please make sure that all of them are strictly necessary, if not, "
"please consider prefixing them with an underscore to make them "
"private."
)
else:
print(
"The {} branch does not add any public symbols".format(active_branch)
)
12 changes: 11 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ envlist =

; opentelemetry-exporter-jaeger-proto-grpc
py3{6,7,8,9}-test-exporter-jaeger-proto-grpc

; opentelemetry-exporter-jaeger-thrift
py3{6,7,8,9}-test-exporter-jaeger-thrift

Expand Down Expand Up @@ -78,6 +78,7 @@ envlist =
mypy,mypyinstalled
docs
docker-tests
public-symbols-check

[testenv]
deps =
Expand Down Expand Up @@ -186,6 +187,7 @@ deps =
psutil
readme_renderer
httpretty
GitPython

commands_pre =
python -m pip install -e {toxinidir}/opentelemetry-api[test]
Expand Down Expand Up @@ -268,3 +270,11 @@ commands =
pytest {posargs}
commands_post =
docker-compose down -v

[testenv:public-symbols-check]
basepython: python3.8
recreate = True
deps =
GitPython
commands =
python {toxinidir}/scripts/public_symbols_checker.py

0 comments on commit 9977b5f

Please sign in to comment.