Spaces:
Sleeping
Sleeping
File size: 21,333 Bytes
a78e9c2 fbbe7e4 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a fbbe7e4 aaeda9a fbbe7e4 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a fbbe7e4 a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a fbbe7e4 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a fbbe7e4 a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a a78e9c2 fbbe7e4 7ed2afd aaeda9a 6ac3439 71d7133 fbbe7e4 aaeda9a 71d7133 aaeda9a 71d7133 6ac3439 71d7133 6ac3439 aaeda9a 6ac3439 6e357f9 6ac3439 aaeda9a 6ac3439 aaeda9a 6e357f9 aaeda9a 6ac3439 aaeda9a 6ac3439 aaeda9a 6ac3439 71d7133 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a 6ac3439 aaeda9a 6ac3439 71d7133 aaeda9a 71d7133 aaeda9a 6e357f9 71d7133 aaeda9a 71d7133 aaeda9a fbbe7e4 aaeda9a 71d7133 aaeda9a 6e357f9 fbbe7e4 6ac3439 6e357f9 aaeda9a 6e357f9 7ed2afd aaeda9a fbbe7e4 7ed2afd f4b3080 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a 6ac3439 aaeda9a 6ac3439 aaeda9a 6ac3439 aaeda9a a78e9c2 6ac3439 fbbe7e4 aaeda9a fbbe7e4 aaeda9a 6ac3439 fbbe7e4 6ac3439 fbbe7e4 aaeda9a fbbe7e4 7ed2afd fbbe7e4 7ed2afd fbbe7e4 6ac3439 fbbe7e4 6ac3439 fbbe7e4 6ac3439 fbbe7e4 aaeda9a fbbe7e4 6ac3439 fbbe7e4 6ac3439 fbbe7e4 7ed2afd aaeda9a fbbe7e4 a78e9c2 aaeda9a a78e9c2 aaeda9a fbbe7e4 aaeda9a fbbe7e4 aaeda9a fbbe7e4 7ed2afd fbbe7e4 aaeda9a fbbe7e4 9e75bda fbbe7e4 aaeda9a 7ed2afd fbbe7e4 7ed2afd aaeda9a fbbe7e4 aaeda9a fbbe7e4 aaeda9a 9e75bda aaeda9a fbbe7e4 aaeda9a a78e9c2 aaeda9a fbbe7e4 aaeda9a a78e9c2 fbbe7e4 a78e9c2 fbbe7e4 6ac3439 aaeda9a fbbe7e4 aaeda9a fbbe7e4 aaeda9a 240a453 fbbe7e4 240a453 fbbe7e4 240a453 aaeda9a fbbe7e4 aaeda9a 6ac3439 aaeda9a fbbe7e4 6ac3439 fbbe7e4 6ac3439 aaeda9a fbbe7e4 aaeda9a 6ac3439 a78e9c2 fbbe7e4 aaeda9a fbbe7e4 aaeda9a 6e357f9 aaeda9a 6ac3439 fbbe7e4 6ac3439 fbbe7e4 6ac3439 fbbe7e4 6ac3439 aaeda9a 6e357f9 fbbe7e4 aaeda9a a78e9c2 aaeda9a a78e9c2 aaeda9a | 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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 | import os
import gradio as gr
from pathlib import Path
import tempfile
import zipfile
import shutil
from typing import List, Set, Dict, Tuple
class WebProjectAnalyzer:
def __init__(self):
# Diretórios que devem ser completamente ignorados
self.ignored_dirs = {
'__pycache__', '.git', '.vscode', '.idea', 'node_modules',
'vendor', 'dist', 'build', 'cache', 'logs', 'tmp',
'coverage', '.github', '.circleci', '.docker', 'venv',
'env', '.venv', 'virtualenv', 'bin', 'include', 'lib',
'lib64', 'site-packages', 'dist-packages', '.gitlab',
'.svn', '.hg', '.pytest_cache', '.mypy_cache', '__MACOSX',
'.next', '.nuxt', 'out', 'target', '.gradle', '.mvn',
'bower_components', 'jspm_packages', 'web_modules',
'.serverless', '.fusebox', '.dynamodb', '.tern-port',
'.yarn', '.pnp', 'eggs', 'wheels', 'htmlcov', '.tox',
'.hypothesis', '.ruff_cache', 'var', 'sdist', 'develop-eggs',
'.sass-cache', '.cache', 'public/packs', 'public/assets'
}
# Extensões relevantes para projetos web (PRINCIPAIS)
self.relevant_extensions = {
# Frontend
'.html', '.htm', '.css', '.scss', '.sass', '.less',
'.js', '.jsx', '.ts', '.tsx', '.vue', '.svelte',
# Backend
'.py', '.rb', '.php', '.java', '.go', '.rs',
# Config importantes
'.json', '.yml', '.yaml', '.toml', '.xml',
# Docs
'.md', '.txt',
# SQL
'.sql',
# Outros
'.env.example', '.gitignore', '.dockerignore'
}
# Arquivos de configuração CRÍTICOS (sempre incluir)
self.critical_files = {
'package.json', 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml',
'requirements.txt', 'Pipfile', 'Pipfile.lock', 'pyproject.toml',
'composer.json', 'composer.lock', 'Gemfile', 'Gemfile.lock',
'go.mod', 'go.sum', 'Cargo.toml', 'Cargo.lock',
'pom.xml', 'build.gradle', 'settings.gradle',
'CMakeLists.txt', 'Makefile', 'tsconfig.json',
'webpack.config.js', 'vite.config.js', 'rollup.config.js',
'docker-compose.yml', 'Dockerfile', '.dockerignore',
'.env.example', '.gitignore', '.eslintrc', '.prettierrc',
'README.md', 'LICENSE', 'setup.py', 'setup.cfg',
'manage.py', 'wsgi.py', 'asgi.py', 'app.py', 'main.py',
'index.html', 'index.js', 'index.ts', 'App.js', 'App.tsx',
'next.config.js', 'nuxt.config.js', 'vue.config.js',
'angular.json', 'nest-cli.json', 'vercel.json', 'netlify.toml'
}
# Padrões de nomes de arquivos a ignorar
self.ignored_file_patterns = {
'.min.js', '.min.css', '.map', '.log',
'.pyc', '.pyo', '.pyd', '.so', '.dll', '.dylib',
'.class', '.jar', '.war', '.ear', '.tar', '.gz',
'.exe', '.bin', '.dat', '.db', '.sqlite', '.sqlite3',
'thumbs.db', '.DS_Store', 'desktop.ini'
}
def should_ignore(self, name: str, path: str, is_dir: bool) -> bool:
"""Determina se um arquivo/diretório deve ser ignorado"""
# Ignora diretórios específicos
if is_dir and name in self.ignored_dirs:
return True
# Ignora arquivos críticos? NÃO!
if name in self.critical_files:
return False
# Ignora arquivos ocultos (exceto importantes)
if name.startswith('.') and name not in self.critical_files:
if not name.endswith('.example') and name not in ['.gitignore', '.dockerignore', '.env.example']:
return True
# Ignora arquivos temporários
if name.endswith('~') or name.startswith('~') or name.startswith('#'):
return True
# Ignora padrões específicos (exceto .lock de configs)
for pattern in self.ignored_file_patterns:
if pattern in name and name not in self.critical_files:
return True
# Ignora arquivos muito grandes (>10MB)
if not is_dir:
try:
if os.path.getsize(path) > 10 * 1024 * 1024:
return True
except:
pass
return False
def is_relevant_file(self, name: str) -> bool:
"""Determina se um arquivo é relevante para mostrar"""
# Arquivos críticos sempre são relevantes
if name in self.critical_files:
return True
# Ignora padrões indesejados (exceto configs)
for pattern in self.ignored_file_patterns:
if pattern in name and name not in self.critical_files:
return False
# Verifica extensões relevantes
_, ext = os.path.splitext(name)
return ext.lower() in self.relevant_extensions
def get_file_icon(self, filename: str) -> str:
"""Retorna ícone apropriado para o tipo de arquivo"""
_, ext = os.path.splitext(filename)
# Arquivos específicos
if filename in ['package.json', 'package-lock.json']:
return '📦'
if filename in ['requirements.txt', 'Pipfile']:
return '🐍'
if filename in ['docker-compose.yml', 'Dockerfile']:
return '🐳'
if filename == 'README.md':
return '📖'
if filename in ['.gitignore', '.dockerignore']:
return '🚫'
if filename.startswith('.env'):
return '🔐'
# Por extensão
icon_map = {
'.py': '🐍', '.js': '📜', '.jsx': '⚛️', '.ts': '📘', '.tsx': '⚛️',
'.html': '🌐', '.htm': '🌐', '.css': '🎨', '.scss': '🎨', '.less': '🎨',
'.php': '🐘', '.java': '☕', '.rb': '💎', '.json': '📋', '.xml': '📄',
'.sql': '🗃️', '.md': '📖', '.txt': '📄', '.yml': '⚙️', '.yaml': '⚙️',
'.toml': '⚙️', '.vue': '💚', '.svelte': '🟢', '.rs': '🦀', '.go': '🐹'
}
return icon_map.get(ext, '📄')
def extract_zip(self, zip_path: str) -> str:
"""Extrai arquivo ZIP e retorna o diretório"""
try:
# Cria diretório temporário
temp_dir = tempfile.mkdtemp(prefix='project_')
print(f"📦 Extraindo ZIP para: {temp_dir}")
# Extrai ZIP
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
# Verifica se há uma única pasta raiz (comum em ZIPs do GitHub)
items = os.listdir(temp_dir)
if len(items) == 1 and os.path.isdir(os.path.join(temp_dir, items[0])):
root_dir = os.path.join(temp_dir, items[0])
print(f"📁 Pasta raiz encontrada: {items[0]}")
return root_dir
return temp_dir
except Exception as e:
print(f"❌ Erro ao extrair ZIP: {e}")
raise
def analyze_structure(self, root_path: str, max_depth: int = 4) -> Tuple[str, Dict, List[str]]:
"""Analisa a estrutura do projeto"""
if not root_path:
return "❌ Nenhuma pasta para analisar.", {"total_files": 0, "relevant_files": 0, "total_size": 0}, []
if not os.path.exists(root_path):
return "❌ Pasta não encontrada.", {"total_files": 0, "relevant_files": 0, "total_size": 0}, []
if not os.path.isdir(root_path):
return "❌ Caminho inválido.", {"total_files": 0, "relevant_files": 0, "total_size": 0}, []
structure_output = []
relevant_files = []
stats = {
'total_files': 0,
'relevant_files': 0,
'total_size': 0
}
def analyze_directory(current_path: str, level: int = 0, prefix: str = ''):
if level > max_depth:
return
try:
items = sorted(os.listdir(current_path))
dirs = []
files = []
# Separa diretórios e arquivos
for item in items:
full_path = os.path.join(current_path, item)
is_dir = os.path.isdir(full_path)
if self.should_ignore(item, full_path, is_dir):
continue
if is_dir:
dirs.append(item)
else:
stats['total_files'] += 1
if self.is_relevant_file(item):
files.append(item)
stats['relevant_files'] += 1
relevant_files.append(full_path)
try:
stats['total_size'] += os.path.getsize(full_path)
except:
pass
# Renderiza arquivos primeiro
for i, file in enumerate(files):
is_last = (i == len(files) - 1) and len(dirs) == 0
connector = '└── ' if is_last else '├── '
icon = self.get_file_icon(file)
structure_output.append(f"{prefix}{connector}{icon} {file}")
# Renderiza diretórios depois
for i, dir_name in enumerate(dirs):
is_last = i == len(dirs) - 1
connector = '└── ' if is_last else '├── '
structure_output.append(f"{prefix}{connector}📁 {dir_name}/")
# Recursão
extension = ' ' if is_last else '│ '
full_path = os.path.join(current_path, dir_name)
analyze_directory(full_path, level + 1, prefix + extension)
except PermissionError:
structure_output.append(f"{prefix}🚫 [Acesso negado]")
except Exception as e:
structure_output.append(f"{prefix}⚠️ [Erro: {str(e)}]")
try:
# Nome da pasta raiz
project_name = os.path.basename(root_path)
structure_output.append(f"📁 {project_name}/")
analyze_directory(root_path, 0, '')
if not structure_output or len(structure_output) == 1:
return "ℹ️ Nenhum arquivo relevante encontrado.", stats, relevant_files
structure_text = '\n'.join(structure_output)
return structure_text, stats, relevant_files
except Exception as e:
return f"❌ Erro ao analisar: {str(e)}", stats, []
def create_structure_report(self, folder_name: str, structure: str, stats: Dict) -> str:
"""Cria relatório completo da estrutura"""
total_files = stats['total_files']
relevant_files = stats['relevant_files']
total_size = stats['total_size']
from datetime import datetime
current_time = datetime.now().strftime('%d/%m/%Y %H:%M:%S')
report = f"""# 📊 ANÁLISE DO PROJETO: `{folder_name}`
## 📍 Informações
- **Projeto**: `{folder_name}`
- **Data da Análise**: {current_time}
## 📈 Estatísticas
| Métrica | Valor |
|---------|-------|
| 📁 Total de arquivos escaneados | {total_files} |
| ✅ Arquivos relevantes | {relevant_files} |
| 💾 Tamanho total | {total_size / 1024 / 1024:.2f} MB |
| 🎯 Taxa de relevância | {(relevant_files/total_files*100) if total_files > 0 else 0:.1f}% |
---
## 🌳 Estrutura do Projeto
```
{structure}
```
---
## 💡 Legenda
- 📁 Diretório
- 🐍 Python
- 📜 JavaScript
- ⚛️ React (JSX/TSX)
- 📘 TypeScript
- 🌐 HTML
- 🎨 CSS/SCSS
- 📦 Package Config
- 🐳 Docker
- 📖 Documentação
- 🚫 Git/Docker Ignore
- 🔐 Environment
---
**✨ Apenas arquivos estruturais e de configuração principais foram incluídos**
"""
return report
def process_zip(self, zip_file, max_depth: int = 4):
"""Processa arquivo ZIP do projeto"""
if not zip_file:
return (
"**📁 Status**: Aguardando upload de arquivo ZIP...",
"ℹ️ Faça upload do arquivo ZIP do seu projeto para análise...",
None
)
temp_dir = None
try:
# Obtém o caminho do arquivo
if hasattr(zip_file, 'name'):
zip_path = zip_file.name
else:
zip_path = str(zip_file)
print(f"📦 Processando ZIP: {zip_path}")
# Verifica se é um ZIP
if not zip_path.lower().endswith('.zip'):
return "❌ Por favor, envie um arquivo ZIP do projeto.", "", None
# Extrai ZIP
temp_dir = self.extract_zip(zip_path)
# Analisa estrutura
structure, stats, relevant_files = self.analyze_structure(temp_dir, max_depth)
# Nome do projeto (do ZIP)
project_name = Path(zip_path).stem
# Cria relatório
report = self.create_structure_report(project_name, structure, stats)
# Salva relatório
report_file = tempfile.NamedTemporaryFile(mode='w', suffix='.md', delete=False, encoding='utf-8')
report_file.write(report)
report_file.close()
return report, structure, report_file.name
except zipfile.BadZipFile:
return "❌ Arquivo ZIP inválido ou corrompido.", "", None
except Exception as e:
error_msg = f"❌ Erro ao processar: {str(e)}"
import traceback
traceback.print_exc()
return error_msg, "", None
finally:
# Limpa diretório temporário
if temp_dir and os.path.exists(temp_dir):
try:
shutil.rmtree(temp_dir)
print(f"🧹 Limpeza: {temp_dir}")
except:
pass
def create_interface():
"""Cria a interface Gradio"""
analyzer = WebProjectAnalyzer()
with gr.Blocks(
title="🔍 Web Project Structure Analyzer",
theme=gr.themes.Soft(primary_hue="purple"),
) as demo:
gr.Markdown("""
# 🔍 Web Project Structure Analyzer
### Analise a estrutura do seu projeto web automaticamente!
**✨ Recursos:**
- 📦 Upload de arquivo ZIP do projeto completo
- 🎯 Filtragem inteligente (ignora node_modules, .git, cache, etc)
- 📊 Estatísticas detalhadas
- 💾 Download do relatório em Markdown
- 🌳 Visualização em árvore ASCII
---
""")
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### 📦 Upload do Projeto (ZIP)")
zip_input = gr.File(
label="Arraste o arquivo ZIP ou clique para selecionar",
file_count="single",
file_types=[".zip"],
type="filepath",
height=200
)
gr.Markdown("""
**💡 Como usar:**
1. **Comprima seu projeto em ZIP:**
- Windows: Clique direito na pasta → "Enviar para" → "Pasta compactada"
- Mac: Clique direito na pasta → "Comprimir"
- Linux: `zip -r meu-projeto.zip meu-projeto/`
2. **Upload:**
- Arraste o arquivo .zip aqui
- OU clique e selecione o arquivo
3. **Automático:**
- A análise inicia automaticamente!
- Aguarde alguns segundos
**🎯 Download do GitHub:**
- Vá no repositório
- Clique em "Code" → "Download ZIP"
- Faça upload aqui!
""")
max_depth = gr.Slider(
minimum=1,
maximum=8,
value=5,
step=1,
label="🔍 Profundidade de Análise",
info="Níveis de subpastas a escanear"
)
analyze_btn = gr.Button(
"🚀 Re-Analisar",
variant="secondary",
size="lg"
)
gr.Markdown("""
---
### ℹ️ O que é analisado?
**✅ INCLUÍDO:**
- 🐍 Código fonte (.py, .js, .html, .css, .php)
- ⚙️ Arquivos de config (package.json, requirements.txt)
- 📖 Documentação (README.md, LICENSE)
- 🐳 Docker (Dockerfile, docker-compose.yml)
**🚫 IGNORADO AUTOMATICAMENTE:**
- 📦 node_modules, vendor
- 🗂️ .git, .vscode, .idea
- 🐍 venv, __pycache__
- 🏗️ dist, build, cache
- 📊 logs, tmp
- 🗑️ Arquivos temporários
""")
with gr.Column(scale=2):
gr.Markdown("### 📊 Resultado da Análise")
report_output = gr.Markdown(
value="""**📁 Status**: Aguardando upload...
**💡 Dica Rápida:**
1. Comprima sua pasta de projeto em ZIP
2. Arraste o arquivo aqui
3. Pronto! Análise automática.
**🎯 Para projetos do GitHub:**
- Code → Download ZIP → Upload aqui""",
label="Relatório"
)
structure_output = gr.Textbox(
label="🌳 Estrutura em Árvore (Copie e Cole)",
lines=22,
max_lines=30,
value="ℹ️ A estrutura do projeto aparecerá aqui após o upload...",
show_copy_button=True,
container=True
)
download_output = gr.File(
label="💾 Download do Relatório Completo (Markdown)",
visible=False
)
# Eventos
def analyze_wrapper(zip_file, depth):
if not zip_file:
return (
"**📁 Status**: Aguardando upload...",
"ℹ️ Faça upload de um arquivo ZIP para começar...",
gr.File(visible=False)
)
report, structure, file_path = analyzer.process_zip(zip_file, depth)
if file_path:
return report, structure, gr.File(value=file_path, visible=True)
else:
return report, structure, gr.File(visible=False)
# Upload automático
zip_input.change(
fn=analyze_wrapper,
inputs=[zip_input, max_depth],
outputs=[report_output, structure_output, download_output]
)
# Botão manual
analyze_btn.click(
fn=analyze_wrapper,
inputs=[zip_input, max_depth],
outputs=[report_output, structure_output, download_output]
)
# Quando muda a profundidade
max_depth.change(
fn=analyze_wrapper,
inputs=[zip_input, max_depth],
outputs=[report_output, structure_output, download_output]
)
gr.Markdown("""
---
### 🎯 Exemplos de Uso
**React App:**
```bash
cd meu-react-app
zip -r meu-react-app.zip . -x "node_modules/*" ".git/*"
```
**Python/Django:**
```bash
cd meu-projeto-django
zip -r meu-projeto.zip . -x "venv/*" ".git/*" "*.pyc"
```
**Node.js API:**
```bash
cd minha-api
zip -r minha-api.zip . -x "node_modules/*" ".git/*"
```
---
### 📋 Formatos Suportados
- ✅ Arquivos .zip padrão
- ✅ ZIP do GitHub ("Download ZIP")
- ✅ ZIP com ou sem pasta raiz
- ✅ Projetos de qualquer tamanho
---
**Made with ❤️ using Gradio 5.49.1+ | Open Source**
""")
return demo
if __name__ == "__main__":
demo = create_interface()
demo.launch() |