Skip to content

Commit

Permalink
Unify check_repo_path
Browse files Browse the repository at this point in the history
We had 4 identical copies of the check_repo_path function. Replace them by a
single copy in the build_tree module where it naturally belongs.

Signed-off-by: Gilles Peskine <[email protected]>
  • Loading branch information
gilles-peskine-arm committed Oct 14, 2022
1 parent 239765a commit 7ff4766
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 22 deletions.
9 changes: 3 additions & 6 deletions scripts/abi_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@

import xml.etree.ElementTree as ET

from mbedtls_dev import build_tree


class AbiChecker:
"""API and ABI checker."""
Expand Down Expand Up @@ -150,11 +152,6 @@ def __init__(self, old_version, new_version, configuration):
self.git_command = "git"
self.make_command = "make"

@staticmethod
def check_repo_path():
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
raise Exception("Must be run from Mbed TLS root")

def _setup_logger(self):
self.log = logging.getLogger()
if self.verbose:
Expand Down Expand Up @@ -540,7 +537,7 @@ def get_abi_compatibility_report(self):
def check_for_abi_changes(self):
"""Generate a report of ABI differences
between self.old_rev and self.new_rev."""
self.check_repo_path()
build_tree.check_repo_path()
if self.check_api or self.check_abi:
self.check_abi_tools_are_installed()
self._get_abi_dump_for_ref(self.old_version)
Expand Down
7 changes: 7 additions & 0 deletions scripts/mbedtls_dev/build_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ def looks_like_mbedtls_root(path: str) -> bool:
return all(os.path.isdir(os.path.join(path, subdir))
for subdir in ['include', 'library', 'programs', 'tests'])

def check_repo_path():
"""
Check that the current working directory is the project root, and throw
an exception if not.
"""
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
raise Exception("This script must be run from Mbed TLS root")

def chdir_to_root() -> None:
"""Detect the root of the Mbed TLS source tree and change to it.
Expand Down
10 changes: 4 additions & 6 deletions tests/scripts/check_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
except ImportError:
pass

import scripts_path # pylint: disable=unused-import
from mbedtls_dev import build_tree


class FileIssueTracker:
"""Base class for file-wide issue tracking.
Expand Down Expand Up @@ -338,7 +341,7 @@ def __init__(self, log_file):
"""Instantiate the sanity checker.
Check files under the current directory.
Write a report of issues to log_file."""
self.check_repo_path()
build_tree.check_repo_path()
self.logger = None
self.setup_logger(log_file)
self.issues_to_check = [
Expand All @@ -353,11 +356,6 @@ def __init__(self, log_file):
MergeArtifactIssueTracker(),
]

@staticmethod
def check_repo_path():
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
raise Exception("Must be run from Mbed TLS root")

def setup_logger(self, log_file, level=logging.INFO):
self.logger = logging.getLogger()
self.logger.setLevel(level)
Expand Down
15 changes: 5 additions & 10 deletions tests/scripts/check_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
import subprocess
import logging

import scripts_path # pylint: disable=unused-import
from mbedtls_dev import build_tree


# Naming patterns to check against. These are defined outside the NameCheck
# class for ease of modification.
MACRO_PATTERN = r"^(MBEDTLS|PSA)_[0-9A-Z_]*[0-9A-Z]$"
Expand Down Expand Up @@ -218,7 +222,7 @@ class CodeParser():
"""
def __init__(self, log):
self.log = log
self.check_repo_path()
build_tree.check_repo_path()

# Memo for storing "glob expression": set(filepaths)
self.files = {}
Expand All @@ -227,15 +231,6 @@ def __init__(self, log):
# Note that "*" can match directory separators in exclude lists.
self.excluded_files = ["*/bn_mul", "*/compat-1.3.h"]

@staticmethod
def check_repo_path():
"""
Check that the current working directory is the project root, and throw
an exception if not.
"""
if not all(os.path.isdir(d) for d in ["include", "library", "tests"]):
raise Exception("This script must be run from Mbed TLS root")

def comprehensive_parse(self):
"""
Comprehensive ("default") function to call each parsing function and
Expand Down

0 comments on commit 7ff4766

Please sign in to comment.