Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,69 +1,137 @@
|
|
1 |
import sys
|
2 |
from pathlib import Path
|
|
|
|
|
|
|
3 |
from config.settings import Settings
|
4 |
from core.git_manager import GitManager
|
5 |
-
from core.file_scanner import FileScanner
|
6 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def main():
|
9 |
-
# コマンドライン引数からパスを取得
|
10 |
if len(sys.argv) != 2:
|
11 |
print("Usage: python main.py <github_url or directory_path>")
|
12 |
return 1
|
13 |
|
14 |
target_path = sys.argv[1]
|
15 |
-
timestamp = Settings.get_timestamp()
|
16 |
-
output_file = Settings.get_output_file(timestamp)
|
17 |
-
|
18 |
-
# GitHubのURLかローカルパスかを判定
|
19 |
-
is_github = target_path.startswith(('http://', 'https://')) and 'github.com' in target_path
|
20 |
|
21 |
try:
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
else:
|
33 |
-
# ローカルディレクトリの場合
|
34 |
-
target_dir = Path(target_path)
|
35 |
-
if not target_dir.exists():
|
36 |
-
print(f"Error: Directory not found: {target_dir}")
|
37 |
-
return 1
|
38 |
-
|
39 |
-
scanner = FileScanner(target_dir)
|
40 |
-
cleanup_needed = False
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
files = scanner.scan_files()
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
writer.write_contents(files)
|
49 |
|
50 |
-
print(f"
|
51 |
-
print(f"
|
|
|
|
|
|
|
52 |
|
53 |
except Exception as e:
|
54 |
-
print(f"
|
55 |
return 1
|
56 |
-
|
57 |
-
finally:
|
58 |
-
# GitHubリポジトリの場合はクリーンアップ
|
59 |
-
if is_github and cleanup_needed and 'git_manager' in locals():
|
60 |
-
try:
|
61 |
-
git_manager.cleanup()
|
62 |
-
print("Cleanup completed")
|
63 |
-
except Exception as e:
|
64 |
-
print(f"Cleanup error: {e}")
|
65 |
-
|
66 |
-
return 0
|
67 |
|
68 |
if __name__ == "__main__":
|
69 |
exit(main())
|
|
|
1 |
import sys
|
2 |
from pathlib import Path
|
3 |
+
import tempfile
|
4 |
+
import git
|
5 |
+
from datetime import datetime
|
6 |
from config.settings import Settings
|
7 |
from core.git_manager import GitManager
|
8 |
+
from core.file_scanner import FileScanner, FileInfo
|
9 |
+
from typing import Tuple, List
|
10 |
+
|
11 |
+
class DirectoryStructureScanner:
|
12 |
+
def __init__(self, root_path: Path):
|
13 |
+
self.root_path = root_path
|
14 |
+
|
15 |
+
def generate_tree(self, path: Path = None, prefix: str = "", is_last: bool = True) -> str:
|
16 |
+
if path is None:
|
17 |
+
path = self.root_path
|
18 |
+
|
19 |
+
output = prefix + ("└── " if is_last else "├── ") + (path.name or str(path)) + "\n"
|
20 |
+
|
21 |
+
entries = list(path.iterdir())
|
22 |
+
dirs = sorted([d for d in entries if d.is_dir()])
|
23 |
+
files = sorted([f for f in entries if f.is_file()])
|
24 |
+
|
25 |
+
dirs = [d for d in dirs if not d.name.startswith('.') and d.name != '__pycache__']
|
26 |
+
files = [f for f in files if not f.name.startswith('.')]
|
27 |
+
|
28 |
+
next_prefix = prefix + (" " if is_last else "│ ")
|
29 |
+
|
30 |
+
for i, dir_path in enumerate(dirs):
|
31 |
+
is_last_entry = (i == len(dirs) - 1) and not files
|
32 |
+
output += self.generate_tree(dir_path, next_prefix, is_last_entry)
|
33 |
+
|
34 |
+
for i, file_path in enumerate(files):
|
35 |
+
is_last_file = i == len(files) - 1
|
36 |
+
output += next_prefix + ("└── " if is_last_file else "├── ") + file_path.name + "\n"
|
37 |
+
|
38 |
+
return output
|
39 |
+
|
40 |
+
class MarkdownGenerator:
|
41 |
+
def __init__(self, target_path: str):
|
42 |
+
self.target_path = target_path
|
43 |
+
self.timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
44 |
+
self.temp_dir = None
|
45 |
+
self.is_github = target_path.startswith(('http://', 'https://')) and 'github.com' in target_path
|
46 |
+
|
47 |
+
def generate_markdowns(self) -> Tuple[str, str, List[FileInfo]]:
|
48 |
+
try:
|
49 |
+
if self.is_github:
|
50 |
+
self.temp_dir = Path(tempfile.mkdtemp())
|
51 |
+
git.Repo.clone_from(self.target_path, self.temp_dir)
|
52 |
+
work_dir = self.temp_dir
|
53 |
+
else:
|
54 |
+
work_dir = Path(self.target_path)
|
55 |
+
if not work_dir.exists():
|
56 |
+
raise ValueError(f"Directory not found: {work_dir}")
|
57 |
+
|
58 |
+
dir_scanner = DirectoryStructureScanner(work_dir)
|
59 |
+
tree_structure = dir_scanner.generate_tree()
|
60 |
+
|
61 |
+
file_scanner = FileScanner(work_dir, selected_extensions)
|
62 |
+
files = file_scanner.scan_files()
|
63 |
+
|
64 |
+
structure_md = self._create_structure_markdown(tree_structure)
|
65 |
+
content_md = self._create_content_markdown(files)
|
66 |
+
|
67 |
+
return content_md, structure_md, files
|
68 |
+
|
69 |
+
finally:
|
70 |
+
if self.is_github and self.temp_dir and Path(self.temp_dir).exists():
|
71 |
+
import shutil
|
72 |
+
shutil.rmtree(self.temp_dir)
|
73 |
+
|
74 |
+
def _create_structure_markdown(self, tree_structure: str) -> str:
|
75 |
+
name = self._get_name()
|
76 |
+
content = f"# {name} - ディレクトリ構造\n\n"
|
77 |
+
content += f"生成日時: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
78 |
+
content += "```\n"
|
79 |
+
content += tree_structure
|
80 |
+
content += "```\n"
|
81 |
+
return content
|
82 |
+
|
83 |
+
def _create_content_markdown(self, files: List[FileInfo]) -> str:
|
84 |
+
content = ""
|
85 |
+
for file in files:
|
86 |
+
content += f"## {file.path}\n"
|
87 |
+
content += "------------\n"
|
88 |
+
if file.content is not None:
|
89 |
+
content += file.content
|
90 |
+
else:
|
91 |
+
content += "# Failed to read content"
|
92 |
+
content += "\n\n"
|
93 |
+
return content
|
94 |
+
|
95 |
+
def _get_name(self) -> str:
|
96 |
+
if self.is_github:
|
97 |
+
return self.target_path.split('/')[-1].replace('.git', '')
|
98 |
+
else:
|
99 |
+
return Path(self.target_path).name
|
100 |
|
101 |
def main():
|
|
|
102 |
if len(sys.argv) != 2:
|
103 |
print("Usage: python main.py <github_url or directory_path>")
|
104 |
return 1
|
105 |
|
106 |
target_path = sys.argv[1]
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
try:
|
109 |
+
print(f"Processing: {target_path}")
|
110 |
+
|
111 |
+
generator = MarkdownGenerator(target_path)
|
112 |
+
content_md, structure_md, _ = generator.generate_markdowns()
|
113 |
+
|
114 |
+
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
115 |
+
name = generator._get_name()
|
116 |
+
|
117 |
+
content_file = f"{name}_content_{timestamp}.md"
|
118 |
+
structure_file = f"{name}_structure_{timestamp}.md"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
with open(content_file, 'w', encoding='utf-8') as f:
|
121 |
+
f.write(content_md)
|
|
|
122 |
|
123 |
+
with open(structure_file, 'w', encoding='utf-8') as f:
|
124 |
+
f.write(structure_md)
|
|
|
125 |
|
126 |
+
print(f"生成完了:")
|
127 |
+
print(f"- ファイル内容: {content_file}")
|
128 |
+
print(f"- ディレクトリ構造: {structure_file}")
|
129 |
+
|
130 |
+
return 0
|
131 |
|
132 |
except Exception as e:
|
133 |
+
print(f"エラーが発生しました: {str(e)}")
|
134 |
return 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
if __name__ == "__main__":
|
137 |
exit(main())
|