-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectToAi.py
More file actions
75 lines (64 loc) · 3.34 KB
/
projectToAi.py
File metadata and controls
75 lines (64 loc) · 3.34 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
import os
import sys
import pyperclip
ignored_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp', '.svg',
'.mp3', '.wav', '.ogg', '.flac', '.mp4', '.avi', '.mov', '.mkv',
'.json', '.yaml', '.lock', '.css', '.scss', '.sass', '.md') # Added .json, .yaml, .lock
ignored_folders = ('.env', '.git', '__pycache__', 'node_modules', 'public', 'icons', 'img', 'imgs')
# Size limits (in MB)
MAX_FOLDER_SIZE_MB = 3
MAX_FILE_SIZE_MB = 1
# Convert to bytes
ignored_folder_size = MAX_FOLDER_SIZE_MB * 1024 * 1024
ignored_file_size = MAX_FILE_SIZE_MB * 1024 * 1024
def combine_files(directory_path, output_filename):
"""Recursively opens files in a directory, adds filename comments,
and combines content into a single file, ignoring files starting with '.'
and specific folders and files defined in ignored_folders and ignored_extensions.
Also ignores folders larger than 3 MB and files larger than 1 MB.
"""
output_file = f"{output_filename}.txt"
script_dir = os.path.dirname(os.path.abspath(__file__))
output_path = os.path.join(script_dir, output_file)
try:
with open(output_path, 'w', encoding="utf-8") as outfile:
for root, dirs, files in os.walk(directory_path):
# Filter directories
dirs[:] = [d for d in dirs if not (d.startswith('.') or d in ignored_folders or os.path.getsize(os.path.join(root, d)) > ignored_folder_size)] # Remove unwanted folders and folders larger than 3 MB
for file in files:
if not file.startswith('.') and not file.endswith(ignored_extensions): # Ignore files starting with '.' and common image, audio, video, config, and lock file extensions
filepath = os.path.join(root, file)
# Check file size
if os.path.getsize(filepath) <= ignored_file_size: # 1 MB in bytes
try:
with open(filepath, 'r', encoding="utf-8") as infile:
outfile.write(f'# {filepath}\n')
outfile.write(infile.read())
outfile.write('\n\n') # Add separator between files
except Exception as e:
print(f'Error reading file {filepath}: {e}')
else:
print(f"Skipping file {filepath} - File size exceeds 1 MB")
except Exception as e:
print(f'Error writing to output file {output_path}: {e}')
try:
with open(output_path, 'r', encoding="utf-8") as outfile:
combined_content = outfile.read()
pyperclip.copy(combined_content)
print(f"Combined content copied to clipboard!")
except Exception as e:
print(f'Error copying to clipboard: {e}')
try:
os.remove(output_path)
print(f"Output file {output_path} deleted.")
except Exception as e:
print(f"Error deleting output file {output_path}: {e}")
def main():
if len(sys.argv) != 3:
print("Usage: python combine_files.py <directory_path> <output_filename>")
sys.exit(1)
directory_path = sys.argv[1]
output_filename = sys.argv[2]
combine_files(directory_path, output_filename)
if __name__ == "__main__":
main()