| import os |
| import argparse |
| import glob |
| import re |
|
|
| def preprocess_file(file_path, src): |
| with open(file_path, 'r') as file: |
| contents = file.read() |
|
|
| |
| contents = re.sub(r'//.*', '', contents) |
| contents = re.sub(r'/\*.*?\*/', '', contents, flags=re.DOTALL) |
| contents = re.sub(r'<!--.*?-->', '', contents, flags=re.DOTALL) |
|
|
| |
| contents = contents.replace('\t', ' ') |
|
|
| |
| contents = contents.replace(" ", " ") |
|
|
| |
| contents = '\n'.join([line.rstrip() for line in contents.split('\n')]) |
|
|
| |
| contents = re.sub(r'\n\s*\n', '\n', contents) |
|
|
| |
| file_path = os.path.relpath(file_path, src) |
|
|
| return "\n" + (f"================== {file_path} ==================\n{contents}".strip()) + "\n" |
|
|
| def main(): |
| parser = argparse.ArgumentParser(description='Prepare files for LLM evaluation') |
| parser.add_argument('--include', action='append', default=[], help='Glob pattern to include files') |
| parser.add_argument('--exclude', action='append', default=[], help='Glob pattern to exclude files') |
| parser.add_argument('src', help='Source directory to search for files') |
|
|
| args = parser.parse_args() |
|
|
| include_patterns = args.include or ['*'] |
| exclude_patterns = args.exclude or [] |
|
|
| combined_contents = '' |
|
|
| |
| includes = [] |
| for pattern in include_patterns: |
| includes.extend(glob.glob(os.path.join(args.src, '**', pattern), recursive=True)) |
| |
| |
| excludes = [] |
| for exclude in exclude_patterns: |
| excludes.extend(glob.glob(os.path.join(args.src, '**', exclude), recursive=True)) |
|
|
| |
| includes = [include for include in includes if include not in excludes] |
|
|
| |
| for include in includes: |
| print(include) |
|
|
| |
| input("Press Enter to continue...") |
| |
| for filename in includes: |
| combined_contents += preprocess_file(filename, args.src) |
|
|
| |
| with open('codebase.txt', 'w') as file: |
| file.write(combined_contents) |
| print(combined_contents) |
| os.system('cat codebase.txt | pbcopy') |
|
|
| if __name__ == '__main__': |
| main() |