Spaces:
Sleeping
Sleeping
File size: 5,419 Bytes
230b1a5 c1b3404 230b1a5 c1b3404 72ef40c c1b3404 85f48fb c1b3404 85f48fb c1b3404 85f48fb c1b3404 85f48fb c1b3404 85f48fb c1b3404 85f48fb c1b3404 85f48fb c1b3404 230b1a5 c1b3404 230b1a5 c1b3404 230b1a5 c1b3404 230b1a5 c1b3404 230b1a5 c1b3404 230b1a5 |
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 |
import sys
from pathlib import Path
import tempfile
import git
from datetime import datetime
from config.settings import Settings
from core.git_manager import GitManager
from core.file_scanner import FileScanner, FileInfo
from typing import Tuple, List, List, Set, Optional
class DirectoryStructureScanner:
def __init__(self, root_path: Path):
self.root_path = root_path
def generate_tree(self, path: Path = None, prefix: str = "", is_last: bool = True) -> str:
if path is None:
path = self.root_path
output = prefix + ("└── " if is_last else "├── ") + (path.name or str(path)) + "\n"
entries = list(path.iterdir())
dirs = sorted([d for d in entries if d.is_dir()])
files = sorted([f for f in entries if f.is_file()])
dirs = [d for d in dirs if not d.name.startswith('.') and d.name != '__pycache__']
files = [f for f in files if not f.name.startswith('.')]
next_prefix = prefix + (" " if is_last else "│ ")
for i, dir_path in enumerate(dirs):
is_last_entry = (i == len(dirs) - 1) and not files
output += self.generate_tree(dir_path, next_prefix, is_last_entry)
for i, file_path in enumerate(files):
is_last_file = i == len(files) - 1
output += next_prefix + ("└── " if is_last_file else "├── ") + file_path.name + "\n"
return output
class MarkdownGenerator:
def __init__(self, target_path: str, target_extensions: Set[str] = None):
self.target_path = target_path
self.timestamp = Settings.get_timestamp()
self.temp_dir = None
self.is_github = target_path.startswith(('http://', 'https://')) and 'github.com' in target_path
# Settings.pyのデフォルト拡張子を使用
self.target_extensions = target_extensions or Settings.DEFAULT_EXTENSIONS
def generate_markdowns(self) -> Tuple[str, str, List[FileInfo]]:
"""リポジトリの内容とディレクトリ構造のMarkdownを生成"""
try:
if self.is_github:
self.temp_dir = Path(tempfile.mkdtemp())
git.Repo.clone_from(self.target_path, self.temp_dir)
work_dir = self.temp_dir
else:
work_dir = Path(self.target_path)
if not work_dir.exists():
raise ValueError(f"Directory not found: {work_dir}")
# ディレクトリ構造のスキャン
dir_scanner = DirectoryStructureScanner(work_dir)
tree_structure = dir_scanner.generate_tree()
# ファイル内容のスキャン
file_scanner = FileScanner(work_dir, self.target_extensions)
files = file_scanner.scan_files()
# Markdown生成
structure_md = self._create_structure_markdown(tree_structure)
content_md = self._create_content_markdown(files)
return content_md, structure_md, files
finally:
if self.is_github and self.temp_dir and Path(self.temp_dir).exists():
import shutil
shutil.rmtree(self.temp_dir)
def _create_structure_markdown(self, tree_structure: str) -> str:
name = self._get_name()
content = f"# {name} - ディレクトリ構造\n\n"
content += f"生成日時: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
content += "```\n"
content += tree_structure
content += "```\n"
return content
def _create_content_markdown(self, files: List[FileInfo]) -> str:
content = ""
for file in files:
content += f"## {file.path}\n"
content += "------------\n"
if file.content is not None:
content += file.content
else:
content += "# Failed to read content"
content += "\n\n"
return content
def _get_name(self) -> str:
if self.is_github:
return self.target_path.split('/')[-1].replace('.git', '')
else:
return Path(self.target_path).name
def main():
if len(sys.argv) != 2:
print("Usage: python main.py <github_url or directory_path>")
return 1
target_path = sys.argv[1]
try:
print(f"Processing: {target_path}")
generator = MarkdownGenerator(target_path)
content_md, structure_md, _ = generator.generate_markdowns()
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
name = generator._get_name()
content_file = f"{name}_content_{timestamp}.md"
structure_file = f"{name}_structure_{timestamp}.md"
with open(content_file, 'w', encoding='utf-8') as f:
f.write(content_md)
with open(structure_file, 'w', encoding='utf-8') as f:
f.write(structure_md)
print(f"生成完了:")
print(f"- ファイル内容: {content_file}")
print(f"- ディレクトリ構造: {structure_file}")
return 0
except Exception as e:
print(f"エラーが発生しました: {str(e)}")
return 1
if __name__ == "__main__":
exit(main()) |