githubexplorer / app /services /metadata_service.py
Kareman's picture
initial commit: full implemntation of git explorer project
acc643d
Raw
History Blame Contribute Delete
2.33 kB
from pathlib import Path
from git import Repo
from app.models.metadata import (
RepositoryMetadata,
RepositoryInfo,
StructureInfo,
ConfigurationInfo,
)
class MetadataService:
def extract(self, repo_path: Path):
repo = Repo(repo_path)
branch = repo.active_branch.name
latest_commit = repo.head.commit.hexsha
total_files = 0
python_files = 0
directories = 0
size = 0
has_readme = False
has_license = False
has_dockerfile = False
has_docker_compose = False
requirements = []
for path in repo_path.rglob("*"):
if path.is_dir():
directories += 1
continue
total_files += 1
size += path.stat().st_size
if path.suffix == ".py":
python_files += 1
filename = path.name.lower()
if filename.startswith("readme"):
has_readme = True
elif filename.startswith("license"):
has_license = True
elif filename == "dockerfile":
has_dockerfile = True
elif filename == "docker-compose.yml":
has_docker_compose = True
elif filename == "requirements.txt":
with open(path) as f:
for line in f:
line = line.strip()
if line and not line.startswith("#"):
requirements.append(line)
return RepositoryMetadata(
repository=RepositoryInfo(
name=repo_path.name,
path=str(repo_path),
branch=branch,
latest_commit=latest_commit,
size_mb=round(size / 1024 / 1024, 2),
),
structure=StructureInfo(
total_files=total_files,
python_files=python_files,
directories=directories,
),
configuration=ConfigurationInfo(
has_readme=has_readme,
has_license=has_license,
has_dockerfile=has_dockerfile,
has_docker_compose=has_docker_compose,
requirements=requirements,
),
)