Skip to content

Commit db3beac

Browse files
committed
Move predefined webapp dir into dj_utils.py
Also initialize `domjudge_api_url = None` and simply call the API via CLI if `domjudge_api_url` is unset.
1 parent ee81fdf commit db3beac

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

misc-tools/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
/dj_make_chroot_docker
66
/dj_run_chroot
77
/dj_judgehost_cleanup
8+
/dj_utils.py
89
/force-passwords

misc-tools/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ include $(TOPDIR)/Makefile.global
99
TARGETS =
1010
OBJECTS =
1111

12-
SUBST_DOMSERVER = fix_permissions configure-domjudge import-contest force-passwords
12+
SUBST_DOMSERVER = fix_permissions configure-domjudge dj_utils.py \
13+
import-contest force-passwords
1314

1415
SUBST_JUDGEHOST = dj_make_chroot dj_run_chroot dj_make_chroot_docker \
1516
dj_judgehost_cleanup

misc-tools/configure-domjudge.in

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ from typing import List, Set
2424
sys.path.append('@domserver_libdir@')
2525
import dj_utils
2626

27-
webappdir = '@domserver_webappdir@'
28-
2927
def usage():
3028
print(f'Usage: {sys.argv[0]} [<domjudge-api-url>]')
3129
exit(1)
@@ -57,11 +55,9 @@ def _keyify_list(l: List) -> Set:
5755
return { elem['id']: elem for elem in l }
5856

5957

60-
if len(sys.argv) == 1:
61-
dj_utils.domjudge_webapp_folder_or_api_url = webappdir
62-
elif len(sys.argv) == 2:
63-
dj_utils.domjudge_webapp_folder_or_api_url = sys.argv[1]
64-
else:
58+
if len(sys.argv) == 2:
59+
dj_utils.domjudge_api_url = sys.argv[1]
60+
elif len(sys.argv) != 1:
6561
usage()
6662

6763
user_data = dj_utils.do_api_request('user')

misc-tools/dj_utils.py renamed to misc-tools/dj_utils.py.in

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
_myself = os.path.basename(sys.argv[0])
1717
_default_user_agent = requests.utils.default_user_agent()
1818
headers = {'user-agent': f'dj_utils/{_myself} ({_default_user_agent})'}
19-
domjudge_webapp_folder_or_api_url = 'unset'
19+
domjudge_webapp_dir = '@domserver_webappdir@'
20+
domjudge_api_url = None
2021
ca_check = True
2122

2223

@@ -50,8 +51,8 @@ def parse_api_response(name: str, response: requests.Response) -> bytes:
5051
def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode: bool = True):
5152
'''Perform an API call to the given endpoint and return its data.
5253

53-
Based on whether `domjudge_webapp_folder_or_api_url` is a folder or URL this
54-
will use the DOMjudge CLI or HTTP API.
54+
Based on whether `domjudge_api_url` is set, this will call the DOMjudge
55+
API via HTTP or CLI.
5556

5657
Parameters:
5758
name (str): the endpoint to call
@@ -67,12 +68,12 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
6768
cannot be JSON decoded.
6869
'''
6970

70-
if os.path.isdir(domjudge_webapp_folder_or_api_url):
71+
if domjudge_api_url is None:
7172
result = api_via_cli(name, method, {}, {}, jsonData)
7273
else:
7374
global ca_check
74-
url = f'{domjudge_webapp_folder_or_api_url}/{name}'
75-
parsed = urlparse(domjudge_webapp_folder_or_api_url)
75+
url = f'{domjudge_api_url}/{name}'
76+
parsed = urlparse(domjudge_api_url)
7677
auth = None
7778
if parsed.username and parsed.password:
7879
auth = (parsed.username, parsed.password)
@@ -109,8 +110,8 @@ def do_api_request(name: str, method: str = 'GET', jsonData: dict = {}, decode:
109110
def upload_file(name: str, apifilename: str, file: str, data: dict = {}):
110111
'''Upload the given file to the API at the given path with the given name.
111112

112-
Based on whether `domjudge_webapp_folder_or_api_url` is a folder or URL this
113-
will use the DOMjudge CLI or HTTP API.
113+
Based on whether `domjudge_api_url` is set, this will call the DOMjudge
114+
API via HTTP or CLI.
114115

115116
Parameters:
116117
name (str): the endpoint to call
@@ -124,13 +125,13 @@ def upload_file(name: str, apifilename: str, file: str, data: dict = {}):
124125
RuntimeError when the HTTP status code is non 2xx.
125126
'''
126127

127-
if os.path.isdir(domjudge_webapp_folder_or_api_url):
128+
if domjudge_api_url is None:
128129
response = api_via_cli(name, 'POST', data, {apifilename: file})
129130
else:
130131
global ca_check
131132
files = [(apifilename, open(file, 'rb'))]
132133

133-
url = f'{domjudge_webapp_folder_or_api_url}/{name}'
134+
url = f'{domjudge_api_url}/{name}'
134135

135136
try:
136137
response = requests.post(
@@ -166,7 +167,7 @@ def api_via_cli(name: str, method: str = 'GET', data: dict = {}, files: dict = {
166167
'''
167168

168169
command = [
169-
f'{domjudge_webapp_folder_or_api_url}/bin/console',
170+
f'{domjudge_webapp_dir}/bin/console',
170171
'api:call',
171172
'-m',
172173
method

misc-tools/import-contest.in

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ sys.path.append('@domserver_libdir@')
2929
import dj_utils
3030

3131
cid = None
32-
webappdir = '@domserver_webappdir@'
3332

3433

3534
def usage():
@@ -134,11 +133,9 @@ def import_contest_problemset_document(cid: str):
134133
else:
135134
print('Skipping contest problemset import.')
136135

137-
if len(sys.argv) == 1:
138-
dj_utils.domjudge_webapp_folder_or_api_url = webappdir
139-
elif len(sys.argv) == 2:
140-
dj_utils.domjudge_webapp_folder_or_api_url = sys.argv[1]
141-
else:
136+
if len(sys.argv) == 2:
137+
dj_utils.domjudge_api_url = sys.argv[1]
138+
elif len(sys.argv) != 1:
142139
usage()
143140

144141
user_data = dj_utils.do_api_request('user')

0 commit comments

Comments
 (0)