File size: 7,144 Bytes
61759de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""
Script to list documents in specified TFS repositories.
Can receive repository patterns from the command line or use a predefined list.
"""

import sys
import argparse
from pathlib import Path
import logging
import re
from typing import List, Dict, Tuple

# Add the app directory to the path
sys.path.append(str(Path(__file__).parent.parent))

from app.utils.tfs_client import TFSClient
from app.core.config import settings

# --- LISTA DE REPOSITÓRIOS PADRÃO ---
DEFAULT_REPO_PATTERNS = [
    "cpcstools",
    "PEX",
]
DEFAULT_REPO_PATTERNS = []

# --- LISTA DE ARQUIVOS E PASTAS EXCLUÍDOS (Case Insensitive) ---
EXCLUDED_NAMES = [
    "lib_list.txt",
    "pull_request_template.md",
    "contributing.md",
    ".azuredevops"
]

# --- PASTAS PERMITIDAS PARA BUSCA (além da raiz) ---
ALLOWED_DOC_FOLDERS = [
    "doc", "docs", 
    "documentacao", "documentação", 
    "manual", "manuais",
    # "spec", "specs"
]
# ------------------------------------

def is_path_allowed(file_path_str: str) -> bool:
    """
    Verifica se o arquivo deve ser incluído baseado na sua localização.
    Regra: Aceita se estiver na raiz OU se estiver dentro de uma pasta permitida de PRIMEIRO NÍVEL.
    """
    path_str = file_path_str.replace('\\', '/')
    if path_str.startswith('/'):
        path_str = path_str[1:]
        
    path_obj = Path(path_str)
    parts = path_obj.parts
    
    # 1. Se só tem uma parte (ex: "arquivo.doc"), está na raiz -> PERMITIDO
    if len(parts) == 1:
        return True
        
    # 2. Verifica se a PRIMEIRA pasta do caminho está na lista de permitidas
    # Ex: "docs/manual.pdf" -> parts[0] é "docs" -> PERMITIDO
    # Ex: "src/codigo/docs/info.txt" -> parts[0] é "src" -> NEGADO
    first_folder = parts[0]
    if first_folder.lower() in ALLOWED_DOC_FOLDERS:
        return True
            
    return False

def filter_latest_versions(file_paths: List[str]) -> List[str]:
    """
    Filtra arquivos para manter apenas a versão mais recente.
    Trata 'Arquivo.ext' como versão 0 e 'Arquivo_V1.ext' como versão 1.
    """
    version_pattern = re.compile(r'^(.*?)[\s_]*[vV](\d+)$')
    versioned_files: Dict[str, Tuple[int, str]] = {}
    
    for file_path in file_paths:
        path_obj = Path(file_path)
        stem = path_obj.stem
        suffix = path_obj.suffix
        parent = str(path_obj.parent)
        
        match = version_pattern.match(stem)
        
        if match:
            base_name = match.group(1).strip()
            version = int(match.group(2))
        else:
            base_name = stem.strip()
            version = 0
            
        key = f"{parent}/{base_name}{suffix}".lower()
        
        if key in versioned_files:
            current_max_ver, _ = versioned_files[key]
            if version > current_max_ver:
                versioned_files[key] = (version, file_path)
        else:
            versioned_files[key] = (version, file_path)
            
    return sorted([info[1] for info in versioned_files.values()])

def main():
    """Main function to list documents"""
    parser = argparse.ArgumentParser(description="List documents in TFS repositories using name patterns")
    parser.add_argument(
        "patterns",
        nargs='*',
        default=None,
        help="Optional: List of repository name patterns to search for"
    )
    parser.add_argument(
        "--verbose", "-v",
        action="store_true",
        help="Enable verbose logging"
    )
    args = parser.parse_args()
    
    if args.patterns:
        repo_patterns_to_use = args.patterns
        print("🔍 Using repository patterns from command line.")
    elif DEFAULT_REPO_PATTERNS:
        repo_patterns_to_use = DEFAULT_REPO_PATTERNS
        print("🔍 Using predefined repository patterns from the script.")
    else:
        # Se não houver padrões definidos, busca em TODOS os repositórios
        repo_patterns_to_use = [None]
        print("🔍 No patterns provided. Searching ALL repositories.")

    log_level = logging.DEBUG if args.verbose else logging.INFO
    logging.basicConfig(
        level=log_level,
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
    )
    logger = logging.getLogger(__name__)

    if not all([settings.tfs_url, settings.tfs_pat, settings.tfs_project]):
        print("❌ TFS not configured. Please set TFS_URL, TFS_PAT, and TFS_PROJECT in your .env file.")
        return

    print("=" * 60)
    print("  TFS Document Lister (Smart Filter v5)")
    print("=" * 60)
    
    try:
        tfs_client = TFSClient()
        if not tfs_client.connection:
            print("❌ Failed to connect to TFS. Check your configuration and network.")
            return

        supported_extensions = settings.supported_extensions
        print(f"🔍 Searching for files with extensions: {', '.join(supported_extensions)}")
        print(f"📂 Allowed root folders: {', '.join(ALLOWED_DOC_FOLDERS)}")
        print(f"🚫 Excluding names: {', '.join(EXCLUDED_NAMES)}\n")

        for pattern in repo_patterns_to_use:
            if pattern:
                print(f"--- Searching for repositories matching pattern: '{pattern}' ---")
            else:
                print("--- Searching for ALL repositories ---")
            
            matching_repos = tfs_client.get_repositories(name_pattern=pattern)
            
            if not matching_repos:
                msg = f"matching '{pattern}'" if pattern else ""
                print(f"  No repositories found {msg}.")
                continue

            for repo in matching_repos:
                repo_url = getattr(repo, 'web_url', getattr(repo, 'remote_url', 'URL not available'))
                
                print(f"\n  Inspecting repository: {repo.name}")
                print(f"  URL: {repo_url}")
                
                files = tfs_client.list_files_in_repo(repo.name, supported_extensions)
                
                non_excluded_files = [
                    f for f in files 
                    if Path(f).name.lower() not in [e.lower() for e in EXCLUDED_NAMES] and
                       not any(part.lower() in [e.lower() for e in EXCLUDED_NAMES] for part in Path(f).parts)
                ]
                
                location_filtered_files = [f for f in non_excluded_files if is_path_allowed(f)]
                
                final_files = filter_latest_versions(location_filtered_files)
                
                if final_files:
                    print(f"    Found {len(final_files)} supported documents (filtered):")
                    for file_path in final_files:
                        print(f"      - {file_path}")
                else:
                    print("    No supported documents found after filtering.")
            
            print("-" * (len(pattern) + 40 if pattern else 60) + "\n")

    except Exception as e:
        logger.error(f"An unexpected error occurred: {e}", exc_info=True)
        print(f"❌ An unexpected error occurred. Check the logs for details.")

    print("🎉 Done!")

if __name__ == "__main__":
    main()