-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_output.txt
More file actions
179 lines (142 loc) · 6.58 KB
/
example_output.txt
File metadata and controls
179 lines (142 loc) · 6.58 KB
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
--- setup.py ---
from setuptools import setup, find_packages
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setup(
name="code-collector",
version="0.1.0",
author="Your Name",
author_email="your.email@example.com",
description="A tool to collect user-written code from a project",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/yourusername/code-collector",
packages=find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
python_requires=">=3.7",
entry_points={
"console_scripts": [
"code-collector=code_collector.cli:main",
],
},
)
--- example_run.py ---
import os
import asyncio
import sys
from code_collector.collector import collect_user_code
from code_collector.utils import get_default_extensions
async def run_example():
# 현재 스크립트의 디렉토리를 기준으로 상대 경로 설정
current_dir = os.path.dirname(os.path.abspath(__file__))
output_file = os.path.join(current_dir, 'example_output.txt')
# 예제 실행
extensions = ['.py'] # Python 파일만 수집
project_dir = '/Users/everyshare/code_collector'
ignore_file = os.path.join(project_dir, '.gitignore')
# 프로젝트 디렉토리 존재 여부 확인
if not os.path.exists(project_dir):
print(f"프로젝트 디렉토리가 존재하지 않습니다: {project_dir}")
sys.exit(1)
# .gitignore 파일 존재 여부 확인
if not os.path.isfile(ignore_file):
print(f".gitignore 파일이 존재하지 않습니다: {ignore_file}")
sys.exit(1)
print(f"프로젝트 디렉토리: {project_dir}")
print(f"수집할 파일 확장자: {extensions}")
print(f"무시할 파일 목록: {ignore_file}")
print(f"출력 파일: {output_file}")
# 비동기 함수 실행
await collect_user_code(project_dir, extensions, ignore_file, output_file)
print(f"\n예제 실행이 완료되었습니다. 결과는 {output_file}에서 확인할 수 있습니다.")
# 결과 파일의 내용 출력
print("\n--- 결과 파일 내용 ---")
with open(output_file, 'r', encoding='utf-8') as f:
print(f.read())
if __name__ == "__main__":
asyncio.run(run_example())
--- code_collector/collector.py ---
import os
import fnmatch
import asyncio
import aiofiles
from typing import List, Tuple
async def parse_ignore_file(ignore_file_path: str) -> Tuple[List[str], List[str]]:
excluded_extensions = []
excluded_dirs = []
if os.path.exists(ignore_file_path):
async with aiofiles.open(ignore_file_path, 'r') as f:
for line in await f.readlines():
line = line.strip()
if line and not line.startswith('#'):
# '/'로 끝나는 경우는 디렉토리로 처리
if line.endswith('/'):
excluded_dirs.append(line.rstrip('/')) # '/' 제거하고 저장
# '*.'으로 시작하는 경우는 확장자로 처리
elif line.startswith('*.'):
excluded_extensions.append(line)
# 그 외의 경우는 디렉토리로 처리
else:
excluded_dirs.append(line)
return excluded_extensions, excluded_dirs
async def find_user_files(directory: str, included_extensions: List[str], ignore_file: str = '.gitignore') -> List[str]:
excluded_extensions, excluded_dirs = await parse_ignore_file(ignore_file)
matched_files = []
async def process_file(file_path: str) -> None:
# 파일의 상대 경로를 계산하여 제외 패턴과 매칭
relative_path = os.path.relpath(file_path, directory)
# 파일이 제외 디렉토리에 있는지 확인
path_parts = relative_path.split(os.sep)
if any(dir_name in path_parts for dir_name in excluded_dirs):
return
# 확장자 매칭 및 제외 패턴 확인
if any(file_path.endswith(ext) for ext in included_extensions) and not any(
fnmatch.fnmatch(relative_path, pattern) for pattern in excluded_extensions):
matched_files.append(file_path)
tasks = []
for root, dirs, files in os.walk(directory):
# 제외할 디렉토리 필터링
dirs[:] = [d for d in dirs if d not in excluded_dirs]
for file in files:
file_path = os.path.join(root, file)
tasks.append(process_file(file_path))
await asyncio.gather(*tasks)
return matched_files
async def collect_user_code(directory: str, included_extensions: List[str], ignore_file: str = '.gitignore', output_file: str = 'user_code.txt') -> str:
user_files = await find_user_files(directory, included_extensions, ignore_file)
async with aiofiles.open(output_file, 'w', encoding='utf-8') as out_file:
for file_path in user_files:
relative_path = os.path.relpath(file_path, directory)
await out_file.write(f'--- {relative_path} ---\n')
async with aiofiles.open(file_path, 'r', encoding='utf-8') as in_file:
await out_file.write(await in_file.read())
await out_file.write('\n\n')
return output_file
--- code_collector/__init__.py ---
--- code_collector/cli.py ---
import asyncio
import argparse
from code_collector.collector import collect_user_code
from code_collector.utils import get_default_extensions, get_project_root
def main():
parser = argparse.ArgumentParser(description="Collect user-written code from a project directory.")
parser.add_argument("directory", nargs="?", default=get_project_root(), help="Project directory path")
parser.add_argument("-e", "--extensions", nargs="+", default=get_default_extensions(), help="File extensions to include")
parser.add_argument("-i", "--ignore", default=".gitignore", help="Ignore file path")
parser.add_argument("-o", "--output", default="user_code.txt", help="Output file name")
args = parser.parse_args()
asyncio.run(collect_user_code(args.directory, args.extensions, args.ignore, args.output))
print(f"User code collected and saved to {args.output}")
if __name__ == "__main__":
main()
--- code_collector/utils.py ---
import os
from typing import List
def get_default_extensions() -> List[str]:
return ['.py', '.js', '.java', '.cpp', '.h', '.css', '.html']
def get_project_root() -> str:
return os.path.dirname(os.path.dirname(os.path.abspath(__file__)))