-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil
executable file
·43 lines (35 loc) · 1.17 KB
/
util
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
#!/usr/bin/python3
import contextlib
from sys import argv
import os
import time
doc = """
This utility generates new problem folders quickly. New features may be added,
but for now it only supports the `new` command.
`new` <path: example/problem_name> <extension: py | cpp>
This command makes a new directory for a problem, and fails if it already exists.
""".strip()
def subcommand_new(path, extension):
if os.getcwd().split("/")[-1] != "competitive-coding":
print("This command should only be called from the project root.")
exit(1)
path_list = path.split("/")
for i in range(len(path_list) - 1):
with contextlib.suppress(FileExistsError):
os.mkdir(path_list[i])
os.chdir(path_list[i])
try:
os.mkdir(path_list[-1])
except FileExistsError:
print("Problem directory already exists, stopping.")
exit(1)
os.chdir(path_list[-1])
open("".join(path_list) + "." + extension, "x")
open("input.txt", "x")
if __name__ == "__main__":
if len(argv) == 1 or argv[1] in ["-h", "--help"]:
print(doc)
if argv[1] == "new":
subcommand_new(argv[2], argv[3])
else:
print(doc)