directory-content-scanner/scan.py

97 lines
4.5 KiB
Python
Raw Normal View History

2023-09-14 07:41:37 +02:00
import os
import argparse
2023-09-14 08:41:20 +02:00
import re
2023-09-14 08:54:21 +02:00
import zlib
2023-09-14 08:41:20 +02:00
def remove_comments(content, file_type):
"""Remove comments from the content based on file type."""
if file_type == ".py":
# Remove Python single line comments
content = re.sub(r'^\s*#.*\n?', '', content, flags=re.MULTILINE)
# Remove triple double-quote and triple single-quote docstrings
content = re.sub(r'\"\"\"(.*?)\"\"\"', '', content, flags=re.DOTALL)
content = re.sub(r"\'\'\'(.*?)\'\'\'", '', content, flags=re.DOTALL)
elif file_type == ".js" or file_type == ".c" or file_type == ".cpp" or file_type == ".h":
# Remove C-style comments (// and /* */)
content = re.sub(r'\s*//.*', '', content)
content = re.sub(r'/\*.*?\*/', '', content, flags=re.DOTALL)
# Add more file types and their comment styles as needed
return content.strip()
2023-09-14 07:41:37 +02:00
2023-09-14 08:54:21 +02:00
def compress_code(content):
"""Compress code using zlib."""
return zlib.compress(content.encode())
2023-09-14 08:02:05 +02:00
def filter_directories(dirs, ignore_strings, ignore_hidden):
2023-09-14 07:41:37 +02:00
"""Filter out directories based on ignore criteria."""
if ignore_hidden:
dirs[:] = [d for d in dirs if not d.startswith('.')]
dirs[:] = [d for d in dirs if not any(ig in d for ig in ignore_strings)]
2023-09-14 07:50:13 +02:00
def should_print_file(file, file_filters, ignore_strings, ignore_hidden):
2023-09-14 07:41:37 +02:00
"""Determine if a file should be printed based on filters."""
2023-09-14 07:50:13 +02:00
if ignore_hidden and file.startswith('.'):
return False
2023-09-14 08:02:05 +02:00
if file_filters and not any(file.endswith(file_type) for file_type in file_filters):
return False
2023-09-14 07:50:13 +02:00
if any(ignore_str in file for ignore_str in ignore_strings):
return False
return True
2023-09-14 07:41:37 +02:00
2023-09-14 08:54:21 +02:00
def print_file_content(file_path, no_comments, compress):
2023-09-14 08:02:05 +02:00
"""Print the content of a file."""
try:
with open(file_path, 'r') as f:
content = f.read()
2023-09-14 08:41:20 +02:00
if no_comments:
file_type = os.path.splitext(file_path)[1]
content = remove_comments(content, file_type)
2023-09-14 08:02:05 +02:00
print(f"======== File Path: {file_path} ========")
2023-09-14 08:54:21 +02:00
if compress:
compressed_content = compress_code(content)
print(f"======== Compressed Code ========")
print(compressed_content)
else:
print(content)
2023-09-14 08:02:05 +02:00
print("==================================\n")
except UnicodeDecodeError:
print(f"Warning: Could not read file due to encoding issues: {file_path}")
exit(1)
2023-09-14 07:41:37 +02:00
2023-09-14 08:54:21 +02:00
def handle_directory(directory, file_filters, ignore_strings, ignore_hidden, verbose, no_comments, compress):
2023-09-14 08:02:05 +02:00
"""Handle scanning and printing for directories."""
2023-09-14 07:41:37 +02:00
for root, dirs, files in os.walk(directory):
2023-09-14 08:02:05 +02:00
filter_directories(dirs, ignore_strings, ignore_hidden)
2023-09-14 07:41:37 +02:00
for file in files:
2023-09-14 07:50:13 +02:00
if should_print_file(file, file_filters, ignore_strings, ignore_hidden):
2023-09-14 08:54:21 +02:00
print_file_content(os.path.join(root, file), no_comments, compress)
2023-09-14 07:41:37 +02:00
elif verbose:
print(f"Skipped file: {file}")
2023-09-14 08:54:21 +02:00
def handle_file(file_path, file_filters, ignore_strings, ignore_hidden, no_comments, compress):
2023-09-14 08:02:05 +02:00
"""Handle scanning and printing for individual files."""
2023-09-14 08:54:21 +02:00
print_file_content(file_path, no_comments, compress)
2023-09-14 08:02:05 +02:00
2023-09-14 07:41:37 +02:00
def main():
2023-09-14 08:54:21 +02:00
parser = argparse.ArgumentParser(description="Scan directories and print/compile file contents.")
2023-09-14 08:02:05 +02:00
parser.add_argument("paths", nargs='+', help="List of files or directories to scan.")
2023-09-14 07:41:37 +02:00
parser.add_argument("-f", "--filetype", nargs='+', default=[], help="Filter by file types (e.g., .txt .log).")
parser.add_argument("-i", "--ignore", nargs='+', default=[], help="Ignore files and folders containing these strings.")
2023-09-14 08:02:05 +02:00
parser.add_argument("--ignore-hidden", action='store_true', help="Ignore hidden directories and files.")
2023-09-14 07:41:37 +02:00
parser.add_argument("-v", "--verbose", action='store_true', help="Enable verbose mode.")
2023-09-14 08:41:20 +02:00
parser.add_argument("--no-comments", action='store_true', help="Remove comments from the displayed content based on file type.")
2023-09-14 08:54:21 +02:00
parser.add_argument("--compress", action='store_true', help="Compress code (for Python files).")
2023-09-14 07:41:37 +02:00
args = parser.parse_args()
2023-09-14 08:02:05 +02:00
for path in args.paths:
if os.path.isdir(path):
2023-09-14 08:54:21 +02:00
handle_directory(path, args.filetype, args.ignore, args.ignore_hidden, args.verbose, args.no_comments, args.compress)
2023-09-14 08:02:05 +02:00
elif os.path.isfile(path):
2023-09-14 08:54:21 +02:00
handle_file(path, args.filetype, args.ignore, args.ignore_hidden, args.no_comments, args.compress)
2023-09-14 08:02:05 +02:00
else:
print(f"Error: {path} is neither a valid file nor a directory.")
2023-09-14 07:41:37 +02:00
exit(1)
if __name__ == "__main__":
main()