|
| 1 | +import os |
| 2 | +import sys |
| 3 | + |
| 4 | +from cogapp import Cog |
| 5 | + |
| 6 | + |
| 7 | +def find_cog_files() -> set[str]: |
| 8 | + """Find files to process with cog. |
| 9 | +
|
| 10 | + Looks for the first file that exists of `.cogfiles`, `README.md` or `README`. |
| 11 | + - If `.cogfiles` exists, use it as a list of files to check |
| 12 | + - If a README file exists, process just that file |
| 13 | + - If neither exists, exit with an error |
| 14 | + """ |
| 15 | + try: |
| 16 | + # Check for .cogfiles first |
| 17 | + if os.path.exists(".cogfiles"): |
| 18 | + print("Using .cogfiles to determine which files to process") |
| 19 | + with open(".cogfiles") as f: |
| 20 | + # Read the file list, strip whitespace, and filter out empty lines |
| 21 | + files = {line.strip() for line in f if line.strip()} |
| 22 | + if not files: |
| 23 | + print("Error: .cogfiles exists but is empty", file=sys.stderr) |
| 24 | + sys.exit(1) |
| 25 | + return files |
| 26 | + |
| 27 | + # Check for README.md next |
| 28 | + elif os.path.exists("README.md"): |
| 29 | + print("Processing README.md") |
| 30 | + return {"README.md"} |
| 31 | + |
| 32 | + # Check for README last |
| 33 | + elif os.path.exists("README"): |
| 34 | + print("Processing README") |
| 35 | + return {"README"} |
| 36 | + |
| 37 | + # If none of the files exist, exit with an error |
| 38 | + else: |
| 39 | + print( |
| 40 | + "Error: Could not find .cogfiles, README.md, or README", file=sys.stderr |
| 41 | + ) |
| 42 | + sys.exit(1) |
| 43 | + |
| 44 | + except Exception as e: |
| 45 | + print(f"Error finding cog files: {e}", file=sys.stderr) |
| 46 | + sys.exit(1) |
| 47 | + |
| 48 | + |
| 49 | +def run_cog_on_files(files: set[str]) -> bool: |
| 50 | + """Run cog on the specified files. |
| 51 | +
|
| 52 | + Returns True if all files were processed successfully, False otherwise. |
| 53 | + """ |
| 54 | + if not files: |
| 55 | + print("No files with cog markers found.") |
| 56 | + return True |
| 57 | + |
| 58 | + print(f"Running cog on {len(files)} files...") |
| 59 | + success = True |
| 60 | + |
| 61 | + # Create a Cog instance with the appropriate options |
| 62 | + cog_instance = Cog() |
| 63 | + |
| 64 | + # Set options equivalent to command-line flags |
| 65 | + cog_instance.options.replace = True # -r: replace in-place |
| 66 | + cog_instance.options.checksum = True # -c: checksum |
| 67 | + cog_instance.options.verbosity = 3 |
| 68 | + |
| 69 | + # Add imports to globals (equivalent to -p option) |
| 70 | + cog_instance.options.defines = { |
| 71 | + "subprocess": __import__("subprocess"), |
| 72 | + "sp": __import__("subprocess"), |
| 73 | + "re": __import__("re"), |
| 74 | + "os": __import__("os"), |
| 75 | + "sys": __import__("sys"), |
| 76 | + "pathlib": __import__("pathlib"), |
| 77 | + "pl": __import__("pathlib"), |
| 78 | + "cog": __import__("cogapp.cogapp"), |
| 79 | + } |
| 80 | + |
| 81 | + for file in files: |
| 82 | + try: |
| 83 | + # Process the content using process_string |
| 84 | + cog_instance.process_one_file(file) |
| 85 | + |
| 86 | + except Exception as e: |
| 87 | + print(f"Error processing {file}: {e}", file=sys.stderr) |
| 88 | + success = False |
| 89 | + |
| 90 | + return success |
| 91 | + |
| 92 | + |
| 93 | +def main() -> None: |
| 94 | + """Find files with cog markers and run cog on them.""" |
| 95 | + files = find_cog_files() |
| 96 | + success = run_cog_on_files(files) |
| 97 | + |
| 98 | + if not success: |
| 99 | + sys.exit(1) |
0 commit comments