Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions python/lib/util/fs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import os
import re
import shutil
import tarfile
import tempfile
from collections.abc import Generator
from collections.abc import Iterator
from datetime import datetime

import lib.exitcode
Expand All @@ -25,7 +26,7 @@ def extract_archive(env: Env, tar_path: str, prefix: str, dir_path: str) -> str:
return extract_path


def iter_all_dir_files(dir_path: str) -> Generator[str, None, None]:
def iter_all_dir_files(dir_path: str) -> Iterator[str]:
"""
Iterate through all the files in a directory recursively, and yield the path of each file
relative to that directory.
Expand Down Expand Up @@ -77,3 +78,39 @@ def remove_empty_directories(dir_path: str):
for subdir_path, _, _ in os.walk(dir_path, topdown=False):
if is_directory_empty(subdir_path):
os.rmdir(subdir_path)


def get_file_extension(file_name: str) -> str:
"""
Get the extension (including multiple extensions) of a file name or path without the leading
dot.
"""

parts = file_name.split('.', maxsplit=1)
if len(parts) == 1:
return ''

return parts[1]


def replace_file_extension(file_name: str, extension: str) -> str:
"""
Replace the extension (including multiple extensions) of a file name or path by another
extension.
"""

parts = file_name.split('.')
return f'{parts[0]}.{extension}'


def search_dir_file_with_regex(dir_path: str, regex: str) -> str | None:
"""
Search for a file within a directory whose name matches a regular expression, or return `None`
if no such file is found.
"""

for file in os.scandir(dir_path):
if re.search(regex, file.name):
return file.name

return None
Loading