Skip to content

Commit

Permalink
Use Path instead of str
Browse files Browse the repository at this point in the history
  • Loading branch information
Werni2A committed Jul 13, 2024
1 parent a94edd0 commit 1711219
Showing 1 changed file with 20 additions and 22 deletions.
42 changes: 20 additions & 22 deletions test/py/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,33 @@

ExtensionsOfInterest = ['*.OBK', '*.OLB', '*.DSN', '*.DBK']

def get_files_of_interest(path: str, file_extensions: List[str]) -> List[str]:
def get_files_of_interest(path: Path, file_extensions: List[str]) -> List[Path]:
"""
Get a list of absolute paths to files with specified extension.
This function searches recursively through the path.
Usage e.g.
>>> files = get_files_of_interest('/home/some_dir', ['*.txt', '*.log'])
>>> files = get_files_of_interest(Path('/home/some_dir'), ['*.txt', '*.log'])
>>> print(files)
>>> ['/home/som_dir/sub_dir/foo.txt', '/home/some_dir/bar.log']
"""

files = []

for extension in file_extensions:
for file in Path(path).rglob(extension):
for file in path.rglob(extension):
files += [file]

return files

def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: str) -> None:
def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: Path) -> None:

author = url.split('.git')[0].split('/')[-2]
project = url.split('.git')[0].split('/')[-1]

base_path = os.path.join(base_path, author, project)
base_path = base_path / author / project

exists_on_fs = os.path.exists(base_path)

if exists_on_fs:
if base_path.exists():
print(f'Repository {base_path} does already exist, do not clone.')
fs_repo = Repo(base_path)
else:
Expand All @@ -60,16 +58,16 @@ def add_new_repo_to_database(db: FileErrorDatabase, url: str, base_path: str) ->
files = get_files_of_interest(base_path, ExtensionsOfInterest)

for file in files:
file = os.path.join('.', os.path.relpath(file, base_path))
file = Path(os.path.join('.', os.path.relpath(file, base_path)))
repo_file = RepoFile(path=file, errors=99999)

if not db.isFileInRepo(repo, repo_file):
db.addFileToRepo(repo, repo_file)

db.addRepo(repo)

def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: str, path_thirdparty_designs: str) -> None:
with open(path_err_cnt_log, 'r') as f:
def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: Path, path_thirdparty_designs: Path) -> None:
with path_err_cnt_log.open() as f:
log = f.readlines()

for line in log:
Expand All @@ -81,23 +79,23 @@ def write_back_err_count(db: FileErrorDatabase, path_err_cnt_log: str, path_thir
expect_err = int(matches[1])
file_path = str(matches[2])

author = file_path[len(path_thirdparty_designs):].split(os.path.sep)[1]
project = file_path[len(path_thirdparty_designs):].split(os.path.sep)[2]
author = file_path[len(str(path_thirdparty_designs)):].split(os.path.sep)[1]
project = file_path[len(str(path_thirdparty_designs)):].split(os.path.sep)[2]

path_repo = os.path.join(path_thirdparty_designs, author, project)
path_repo = path_thirdparty_designs / author / project

# Make path relative
file_path = os.path.relpath(file_path, path_repo)
file_path = os.path.join('.', file_path)
file_path = Path(os.path.join('.', file_path))

repo = db.getRepoByNames(author, project)
file = db.getFileByPath(repo, file_path)

db.updateErrorCounter(file, actual_err)


def create_repo_cpp_unit_test(repo: Repository, path_repo: str, path_unit_test: str) -> None:
with open(path_unit_test, 'w') as f:
def create_repo_cpp_unit_test(repo: Repository, path_repo: Path, path_unit_test: Path) -> None:
with path_unit_test.open('w', encoding='utf-8') as f:
preamble = """// THIS FILE IS AUTOMATICALLY GENERATED! DO NOT MODIFY IT!
Expand Down Expand Up @@ -198,19 +196,19 @@ def create_repo_cpp_unit_test(repo: Repository, path_repo: str, path_unit_test:
do_write_back = args.write_back
do_download = args.do_download

path_thirdparty_designs = args.path_thirdparty_designs
path_unit_tests = args.path_unit_tests
path_thirdparty_designs = Path(args.path_thirdparty_designs)
path_unit_tests = Path(args.path_unit_tests)
path_err_cnt_log = args.path_err_cnt_log

path_db = args.path_db

if not do_generate_unit_tests and not do_write_back and not do_download:
parser.print_help()

def generate_unit_tests(db: FileErrorDatabase, path_thirdparty_designs: str, path_unit_tests: str) -> None:
def generate_unit_tests(db: FileErrorDatabase, path_thirdparty_designs: Path, path_unit_tests: Path) -> None:
for repo in db.data.repositories:
path_repo = os.path.join(path_thirdparty_designs, repo.author, repo.project)
path_unit_test = os.path.join(path_unit_tests, f'Test_{repo.author}_{repo.project}.cpp')
path_repo = path_thirdparty_designs / repo.author / repo.project
path_unit_test = path_unit_tests / f'Test_{repo.author}_{repo.project}.cpp'

create_repo_cpp_unit_test(repo, path_repo, path_unit_test)

Expand Down

0 comments on commit 1711219

Please sign in to comment.