cengal.build_tools.remove_pycache.versions.v_0.remove_pycache

 1#!/usr/bin/env python
 2# coding=utf-8
 3
 4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
 5# 
 6# Licensed under the Apache License, Version 2.0 (the "License");
 7# you may not use this file except in compliance with the License.
 8# You may obtain a copy of the License at
 9# 
10#     http://www.apache.org/licenses/LICENSE-2.0
11# 
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17
18
19__all__ = ['pycache_filter_func', 'remove_pycache_from_dir_tree']
20
21
22"""
23Module Docstring
24Docstrings: http://www.python.org/dev/peps/pep-0257/
25"""
26
27__author__ = "ButenkoMS <gtalk@butenkoms.space>"
28__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
29__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
30__license__ = "Apache License, Version 2.0"
31__version__ = "4.4.1"
32__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
33__email__ = "gtalk@butenkoms.space"
34# __status__ = "Prototype"
35__status__ = "Development"
36# __status__ = "Production"
37
38
39import shutil
40import os
41from typing import List, Tuple, Set
42from cengal.file_system.path_manager import RelativePath, get_relative_path_part
43from cengal.file_system.directory_manager import current_src_dir
44from cengal.file_system.directory_manager import file_list_traversal, FilteringEntity
45
46
47pycache_dir_name = '__pycache__'
48
49
50def pycache_filter_func(filtering_entity: FilteringEntity, data) -> bool:
51    if FilteringEntity.dirpath == filtering_entity:
52        return True
53    elif FilteringEntity.dirname == filtering_entity:
54        dirpath, dirname = data
55        if pycache_dir_name == dirname:
56            return True
57    elif FilteringEntity.aggregated == filtering_entity:
58        new_data: List[Tuple[str, List[str], List[str]]] = list()
59        for dirpath, dirnames, filenames in data:
60            if dirnames:
61                new_data.append((dirpath, dirnames, list()))
62        
63        return new_data
64    else:
65        return False
66
67
68def remove_pycache_from_dir_tree(root_dir_path: str):
69    to_be_deleted: Set[str] = set()
70    for dirpath, dirnames, filenames in file_list_traversal(root_dir_path, pycache_filter_func):
71        dirpath_rel: RelativePath = RelativePath(dirpath)
72        for dirname in dirnames:
73            pycache_full_path = dirpath_rel(dirname)
74            to_be_deleted.add(pycache_full_path)
75    
76    for pycache_full_path in to_be_deleted:
77        # print(f'Removing: {pycache_full_path}')
78        if os.path.exists(pycache_full_path):
79            shutil.rmtree(pycache_full_path, ignore_errors=True)
def pycache_filter_func( filtering_entity: cengal.file_system.directory_manager.versions.v_0.directory_manager.FilteringEntity, data) -> bool:
51def pycache_filter_func(filtering_entity: FilteringEntity, data) -> bool:
52    if FilteringEntity.dirpath == filtering_entity:
53        return True
54    elif FilteringEntity.dirname == filtering_entity:
55        dirpath, dirname = data
56        if pycache_dir_name == dirname:
57            return True
58    elif FilteringEntity.aggregated == filtering_entity:
59        new_data: List[Tuple[str, List[str], List[str]]] = list()
60        for dirpath, dirnames, filenames in data:
61            if dirnames:
62                new_data.append((dirpath, dirnames, list()))
63        
64        return new_data
65    else:
66        return False
def remove_pycache_from_dir_tree(root_dir_path: str):
69def remove_pycache_from_dir_tree(root_dir_path: str):
70    to_be_deleted: Set[str] = set()
71    for dirpath, dirnames, filenames in file_list_traversal(root_dir_path, pycache_filter_func):
72        dirpath_rel: RelativePath = RelativePath(dirpath)
73        for dirname in dirnames:
74            pycache_full_path = dirpath_rel(dirname)
75            to_be_deleted.add(pycache_full_path)
76    
77    for pycache_full_path in to_be_deleted:
78        # print(f'Removing: {pycache_full_path}')
79        if os.path.exists(pycache_full_path):
80            shutil.rmtree(pycache_full_path, ignore_errors=True)