|
| 1 | +from argparse import ArgumentParser |
| 2 | +from pathlib import Path |
| 3 | +import subprocess |
| 4 | + |
| 5 | + |
| 6 | +def create_new_project(name): |
| 7 | + project_folder = Path.cwd().absolute() / name |
| 8 | + project_folder.mkdir() |
| 9 | + (project_folder / "README.md").touch() |
| 10 | + with open(project_folder / ".gitignore", mode="w") as f: |
| 11 | + f.write("\n".join(["venv", "__pycache__"])) |
| 12 | + commands = [ |
| 13 | + [ |
| 14 | + "python", |
| 15 | + "-m", |
| 16 | + "venv", |
| 17 | + f"{project_folder}/venv", |
| 18 | + ], |
| 19 | + ["git", "-C", project_folder, "init"], |
| 20 | + ["git", "-C", project_folder, "add", "."], |
| 21 | + ["git", "-C", project_folder, "commit", "-m", "Initial commit"], |
| 22 | + ] |
| 23 | + for command in commands: |
| 24 | + try: |
| 25 | + subprocess.run(command, check=True, timeout=60) |
| 26 | + except FileNotFoundError as exc: |
| 27 | + print( |
| 28 | + f"Command {command} failed because the process " |
| 29 | + f"could not be found.\n{exc}" |
| 30 | + ) |
| 31 | + except subprocess.CalledProcessError as exc: |
| 32 | + print( |
| 33 | + f"Command {command} failed because the process " |
| 34 | + f"did not return a successful return code.\n{exc}" |
| 35 | + ) |
| 36 | + except subprocess.TimeoutExpired as exc: |
| 37 | + print(f"Command {command} timed out.\n {exc}") |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == "__main__": |
| 41 | + parser = ArgumentParser() |
| 42 | + parser.add_argument("project_name", type=str) |
| 43 | + args = parser.parse_args() |
| 44 | + create_new_project(args.project_name) |
0 commit comments