chatdoc / scripts\list_tfs_docs.py
celiolr's picture
Deploy Docker: scripts\list_tfs_docs.py
61759de verified
Raw History Blame Contribute Delete
7.14 kB
#!/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()