forked from badouralix/adventofcode-2018
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.py
executable file
·162 lines (128 loc) · 4.58 KB
/
create.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/env python3
import argparse
import os.path
import re
import sys
class FileNotEmptyException(Exception):
pass
def mkdirp(path):
"""
Create directory if it does not exist already.
Parameters
----------
path : string
Name of the directory to be created
"""
if not os.path.exists(path):
os.makedirs(path)
def make_dirs(day, parts):
"""
Create day/part directories along with input directory.
Parameters
----------
day : int
Day number for which a directory is created
parts : list[int]
List of parts for which directories is created
Returns
-------
dirs : dict("input": string, "parts": list(string))
Dictionnary containing created directories
"""
dirs = dict()
# Create day directory
day_dir = "./day-{:02d}".format(day)
mkdirp(day_dir)
# Create input directory
dirs["input"] = f"{day_dir}/input"
mkdirp(dirs["input"])
# Create part directories
dirs["parts"] = [f"{day_dir}/part-{part}" for part in parts]
for part_dir in dirs["parts"]:
mkdirp(part_dir)
return dirs
def create_submission(author, path, language):
"""
Create submission file from template according to the chosen language.
Parameters
----------
author : string
Name of the author for whom the input file is created
path : string
Day/part directory in which the submission file is created
language : string
Programming language of the submission
"""
# Build submission file name
submission_file = os.path.join(path, f"{author}.{language}")
# Extract submission template
if language == 'py':
# Create a dedicated class with the author name
class_name = ''.join(x for x in f"{author} submission".title() if not x.isspace())
submission_content = open(os.path.join("templates", "template.py")).read().format(class_name=class_name)
else:
submission_content = open(os.path.join("templates", f"template.{language}")).read()
# Write template to submission file if it is empty
if os.path.exists(submission_file):
raise FileNotEmptyException(f"{submission_file} not empty")
with open(submission_file, 'w') as f:
f.write(submission_content)
# Log success
print(f"[+] created {submission_file}")
# Create an entry in Cargo.toml if it is a Rust project
if language == "rs":
submission_name = f"{re.sub('[^0-9a-zA-Z]+', '-', path[2:])}-{author}"
cargo = open(os.path.join("Cargo.toml"), "a")
cargo.write(f"\n[[bin]]\nname = \"{submission_name}\"\npath = \"{submission_file}\"\n")
print("[+] added submission to Cargo.toml")
def create_input(author, path):
"""
Create input txt file.
Parameters
----------
author : string
Name of the author for whom the input file is created
path : string
Input directory in which the input file is created
"""
# Build input file name
input_file = os.path.join(path, f"{author}.txt")
# Create input file
if os.path.exists(input_file):
raise FileNotEmptyException(f"{input_file} not empty")
with open(input_file, 'a') as f:
f.close()
# Log success
print(f"[+] created {input_file}")
def main():
"""
Create directories and submission files according to commamd line arguments.
"""
# Parse command line arguments
parser = argparse.ArgumentParser(description='Creates new empty submission')
parser.add_argument('author', type=str, help='Name of author (github login)')
parser.add_argument('day', type=int, help='Day of problem (between 1 and 25)')
parser.add_argument('-p', '--part', type=int, help='Create submission for one day part only', choices=[1, 2])
parser.add_argument('-l', '--language', help='Use specified language', default="py", choices=["c", "cpp", "go", "js", "py", "rb", "rs"])
args = parser.parse_args()
# Format author name
author = args.author.lower()
# Create missing directories
dirs = make_dirs(args.day, [1, 2] if not args.part else [args.part])
# Create input file
try:
create_input(author, dirs["input"])
except FileNotEmptyException:
pass
except Exception as e:
print(e, file=sys.stderr)
sys.exit(1)
# Create submission files
for submission_path in dirs["parts"]:
try:
create_submission(author, submission_path, args.language)
except FileNotEmptyException as e:
print(e, file=sys.stderr)
sys.exit(1)
if __name__ == '__main__':
main()