From 45019219d6a2c5e5b13b1e9ea7fc029e23f969f7 Mon Sep 17 00:00:00 2001 From: "Anastasiia.Birillo" Date: Fri, 27 Aug 2021 13:32:49 +0300 Subject: [PATCH] Delete analysis part --- requirements.txt | 3 +- src/python/review/common/file_system.py | 148 +----------------- src/python/review/common/language.py | 4 +- src/python/review/common/subprocess_runner.py | 5 - .../reviewers/utils/metadata_exploration.py | 6 +- 5 files changed, 8 insertions(+), 158 deletions(-) diff --git a/requirements.txt b/requirements.txt index 4cfad8e5..484495a5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -27,5 +27,4 @@ radon==4.5.0 # extra libraries and frameworks django==3.2 requests==2.25.1 -argparse==1.4.0 -pyyaml==5.4.1 \ No newline at end of file +argparse==1.4.0 \ No newline at end of file diff --git a/src/python/review/common/file_system.py b/src/python/review/common/file_system.py index 9800d2ce..d787d61a 100644 --- a/src/python/review/common/file_system.py +++ b/src/python/review/common/file_system.py @@ -1,15 +1,10 @@ import linecache import os -import pickle -import re -import shutil import tempfile from contextlib import contextmanager from enum import Enum, unique from pathlib import Path -from typing import Any, Callable, List, Optional, Tuple, Union - -import yaml +from typing import Callable, List, Tuple, Union @unique @@ -34,39 +29,13 @@ class Extension(Enum): KT = '.kt' JS = '.js' KTS = '.kts' - XLSX = '.xlsx' - CSV = '.csv' - PICKLE = '.pickle' - JSON = '.json' - HTML = '.html' - - # Image extensions - PNG = '.png' - JPG = '.jpg' - JPEG = '.jpeg' - WEBP = '.webp' - SVG = '.svg' - PDF = '.pdf' - EPS = '.eps' # Not empty extensions are returned with a dot, for example, '.txt' # If file has no extensions, an empty one ('') is returned @classmethod - def get_extension_from_file(cls, file: str) -> 'Extension': + def get_extension_from_file(cls, file: Union[Path, str]) -> 'Extension': return Extension(os.path.splitext(file)[1]) - @classmethod - def get_image_extensions(cls) -> List['Extension']: - return [ - Extension.PNG, - Extension.JPG, - Extension.JPEG, - Extension.WEBP, - Extension.SVG, - Extension.PDF, - Extension.EPS, - ] - ItemCondition = Callable[[str], bool] @@ -75,13 +44,6 @@ def all_items_condition(name: str) -> bool: return True -def extension_file_condition(extension: Extension) -> ItemCondition: - def has_this_extension(name: str) -> bool: - return get_extension_from_file(name) == extension - - return has_this_extension - - # To get all files or subdirs (depends on the last parameter) from root that match item_condition # Note that all subdirs or files already contain the full path for them def get_all_file_system_items( @@ -105,53 +67,6 @@ def get_all_file_system_items( return items -def match_condition(regex: str) -> ItemCondition: - def does_name_match(name: str) -> bool: - return re.fullmatch(regex, name) is not None - return does_name_match - - -def serialize_data_and_write_to_file(path: Path, data: Any) -> None: - os.makedirs(get_parent_folder(path), exist_ok=True) - with open(path, 'wb') as f: - p = pickle.Pickler(f) - p.dump(data) - - -def deserialize_data_from_file(path: Path) -> Any: - with open(path, 'rb') as f: - u = pickle.Unpickler(f) - return u.load() - - -def parse_yaml(path: Union[Path, str]) -> Any: - with open(path) as file: - return yaml.safe_load(file) - - -# For getting name of the last folder or file -# For example, returns 'folder' for both 'path/data/folder' and 'path/data/folder/' -def get_name_from_path(path: Union[Path, str], with_extension: bool = True) -> str: - head, tail = os.path.split(path) - # Tail can be empty if '/' is at the end of the path - file_name = tail or os.path.basename(head) - if not with_extension: - file_name = os.path.splitext(file_name)[0] - elif get_extension_from_file(Path(file_name)) == Extension.EMPTY: - raise ValueError('Cannot get file name with extension, because the passed path does not contain it') - return file_name - - -def pair_in_and_out_files(in_files: List[Path], out_files: List[Path]) -> List[Tuple[Path, Path]]: - pairs = [] - for in_file in in_files: - out_file = Path(re.sub(r'in(?=[^in]*$)', 'out', str(in_file))) - if out_file not in out_files: - raise ValueError(f'List of out files does not contain a file for {in_file}') - pairs.append((in_file, out_file)) - return pairs - - # TODO: Need testing @contextmanager def new_temp_dir() -> Path: @@ -163,17 +78,6 @@ def new_temp_file(suffix: Extension = Extension.EMPTY) -> Tuple[str, str]: yield tempfile.mkstemp(suffix=suffix.value) -# File should contain the full path and its extension. -# Create all parents if necessary -def create_file(file_path: Union[str, Path], content: str): - file_path = Path(file_path) - - os.makedirs(os.path.dirname(file_path), exist_ok=True) - with open(file_path, 'w+') as f: - f.writelines(content) - yield Path(file_path) - - def get_file_line(path: Path, line_number: int): return linecache.getline( str(path), @@ -188,54 +92,6 @@ def get_content_from_file(file_path: Path, encoding: str = Encoding.ISO_ENCODING return content if not to_strip_nl else content.rstrip('\n') -# Not empty extensions are returned with a dot, for example, '.txt' -# If file has no extensions, an empty one ('') is returned -def get_extension_from_file(file: Union[Path, str]) -> Extension: - return Extension(os.path.splitext(file)[1]) - - -def get_restricted_extension(file_path: Optional[Union[str, Path]] = None, - available_values: List[Extension] = None) -> Extension: - if file_path is None: - return Extension.EMPTY - ext = Extension.get_extension_from_file(file_path) - if available_values is not None and ext not in available_values: - raise ValueError(f'Invalid extension. ' - f'Available values are: {list(map(lambda e: e.value, available_values))}.') - return ext - - -def remove_slash(path: str) -> str: - return path.rstrip('/') - - -def remove_directory(directory: Union[str, Path]) -> None: - if os.path.isdir(directory): - shutil.rmtree(directory, ignore_errors=True) - - -def add_slash(path: str) -> str: - if not path.endswith('/'): - path += '/' - return path - - -def get_parent_folder(path: Union[Path, str], to_add_slash: bool = False) -> Path: - path = remove_slash(str(path)) - parent_folder = '/'.join(path.split('/')[:-1]) - if to_add_slash: - parent_folder = add_slash(parent_folder) - return Path(parent_folder) - - -def copy_directory(source: Union[str, Path], destination: Union[str, Path], dirs_exist_ok: bool = True): - shutil.copytree(source, destination, dirs_exist_ok=dirs_exist_ok) - - -def copy_file(source: Union[str, Path], destination: Union[str, Path]): - shutil.copy(source, destination) - - # Before using it, check that there are no line breaks in the string def __is_line_empty(line: str) -> bool: return len(line.strip()) == 0 diff --git a/src/python/review/common/language.py b/src/python/review/common/language.py index c48944ec..46851ce3 100644 --- a/src/python/review/common/language.py +++ b/src/python/review/common/language.py @@ -3,7 +3,7 @@ from typing import List from src.python.review.application_config import LanguageVersion -from src.python.review.common.file_system import Extension, get_extension_from_file +from src.python.review.common.file_system import Extension @unique @@ -51,7 +51,7 @@ def from_value(cls, value: str, default=None): def guess_file_language(file_path: Path) -> Language: - return EXTENSION_TO_LANGUAGE.get(get_extension_from_file(file_path), Language.UNKNOWN) + return EXTENSION_TO_LANGUAGE.get(Extension.get_extension_from_file(file_path), Language.UNKNOWN) def filter_paths_by_language(file_paths: List[Path], language: Language) -> List[Path]: diff --git a/src/python/review/common/subprocess_runner.py b/src/python/review/common/subprocess_runner.py index 2a89ad42..a25cbdcd 100644 --- a/src/python/review/common/subprocess_runner.py +++ b/src/python/review/common/subprocess_runner.py @@ -21,8 +21,3 @@ def run_in_subprocess(command: List[str]) -> str: logger.debug('%s\'s stderr:\n%s' % (command[0], stderr)) return stdout - - -def run_and_wait(command: List[str]) -> None: - process = subprocess.Popen(command) - process.wait() diff --git a/src/python/review/reviewers/utils/metadata_exploration.py b/src/python/review/reviewers/utils/metadata_exploration.py index f928a1a9..318f5328 100644 --- a/src/python/review/reviewers/utils/metadata_exploration.py +++ b/src/python/review/reviewers/utils/metadata_exploration.py @@ -4,7 +4,7 @@ from pathlib import Path from typing import Dict, List, Set, Union -from src.python.review.common.file_system import get_all_file_system_items, get_extension_from_file +from src.python.review.common.file_system import Extension, get_all_file_system_items from src.python.review.common.language import guess_file_language, Language @@ -16,7 +16,7 @@ class FileMetadata: @property def extension(self) -> str: - return get_extension_from_file(self.path).value + return Extension.get_extension_from_file(self.path).value @dataclass @@ -39,7 +39,7 @@ def size_bytes(self) -> int: def extension_to_files(self) -> Dict[str, List[FileMetadata]]: extension_to_files = defaultdict(list) for file in self.inner_files: - extension_to_files[get_extension_from_file(file.path)].append(file) + extension_to_files[Extension.get_extension_from_file(file.path)].append(file) return extension_to_files @property