-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubmit.py
More file actions
executable file
·158 lines (122 loc) · 4.51 KB
/
submit.py
File metadata and controls
executable file
·158 lines (122 loc) · 4.51 KB
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
#!/usr/bin/env python3
import argparse
import subprocess
import glob
import json
import os
import shutil
REPO_ROOT = os.path.dirname(os.path.realpath(__file__))
SHADOW_REPO_DIR = os.path.join(REPO_ROOT, '.submit-repo')
VERBOSE = False
def git(*args):
if VERBOSE:
print("> {}".format(" ".join(["git"] + list(args))))
subprocess.check_call(" ".join(["git"] + list(args)), cwd=SHADOW_REPO_DIR, shell=True)
else:
subprocess.check_output(["git"] + list(args), cwd=SHADOW_REPO_DIR, stderr=subprocess.PIPE)
def git_output(*args):
if VERBOSE:
print("> {}".format(" ".join(["git"] + list(args))))
return subprocess.check_output(["git"] + list(args)).strip().decode('utf-8')
def set_up_shadow_repo(task_name, user_name, user_email):
# to fix access denied error occuring on Windows
def onerror(func, path, exc_info):
import stat
if not os.access(path, os.W_OK):
os.chmod(path, stat.S_IWUSR)
func(path)
else:
raise
try:
shutil.rmtree(SHADOW_REPO_DIR, onerror=onerror)
except FileNotFoundError:
pass
os.makedirs(SHADOW_REPO_DIR)
git("init")
git("remote", "add", "local", "..")
try:
git("fetch", "local", "+refs/heads/submits/{0}*:refs/heads/submits/{0}*".format(task_name))
except:
pass
private_url = git_output("remote", "get-url", "student")
git("remote", "add", "student", private_url)
with open(os.path.join(SHADOW_REPO_DIR, ".git", "config"), mode="a", encoding="utf-8") as config:
config.write("[user]\n\tname = {}\n\temail = {}\n".format(user_name, user_email))
def create_commits(task_name, files):
git("checkout", "-b", "initial")
shutil.copyfile(os.path.join(REPO_ROOT, ".grader-ci.yml"), os.path.join(SHADOW_REPO_DIR, ".gitlab-ci.yml"))
git("add", ".gitlab-ci.yml")
git("commit", "-m", "initial")
try:
git("checkout", "submits/" + task_name)
except:
git("checkout", "-b", "submits/" + task_name)
try:
os.makedirs(os.path.join(SHADOW_REPO_DIR, task_name))
except:
pass
shutil.copyfile(os.path.join(REPO_ROOT, ".grader-ci.yml"), os.path.join(SHADOW_REPO_DIR, ".gitlab-ci.yml"))
git("add", ".gitlab-ci.yml")
for filename in files:
directory = os.path.join(SHADOW_REPO_DIR, task_name, os.path.dirname(filename))
for path in glob.glob(filename):
try:
os.makedirs(directory)
except:
pass
shutil.copy(path, directory)
git("add", task_name + "/" + filename)
git("commit", "-m", task_name, "--allow-empty")
def push_branches(task_name):
git("push", "local", "submits/" + task_name)
try:
git("push", "-f", "student", "initial")
except:
pass
git(
"push", "-f", "student", "submits/" + task_name,
'-o', 'merge_request.create',
'-o', 'merge_request.target=master',
'-o', 'merge_request.title=' + task_name,
'-o', 'merge_request.label=task/' + task_name
)
def ensure_list(value):
if not isinstance(value, list):
return [value]
return value
def submit(task_name):
if task_name == '':
real_current_path = os.path.realpath(".")
task_group_name = os.path.basename(os.path.dirname(real_current_path))
task_name = task_group_name + "/" + os.path.basename(real_current_path)
task_config = None
try:
task_config = json.load(open(".tester.json"))
except FileNotFoundError:
print("error: Task config not found. Are you running tool from correct directory?")
exit(1)
user_name = git_output("config", "user.name")
user_email = git_output("config", "user.email")
set_up_shadow_repo(task_name, user_name, user_email)
create_commits(task_name, ensure_list(task_config["allow_change"]))
push_branches(task_name)
if __name__ == "__main__":
try:
parser = argparse.ArgumentParser()
parser.add_argument(
"-v", "--verbose",
help="increase output verbosity",
action="store_true"
)
parser.add_argument(
"task_path",
nargs='?',
help="task relative path",
default=''
)
args = parser.parse_args()
VERBOSE = args.verbose
submit(args.task_path)
except:
print("Something went wrong. Most frequent issues are described at https://gitlab.com/danlark/cpp-advanced-hse/-/blob/main/docs/troubleshooting.md")
raise