Skip to content

Commit

Permalink
Add option to upload languages configuration via API.
Browse files Browse the repository at this point in the history
  • Loading branch information
meisterT committed Sep 30, 2023
1 parent 4391956 commit 24da9ef
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 16 deletions.
39 changes: 23 additions & 16 deletions misc-tools/configure-domjudge.in
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,6 @@ else:
print('Need a "config.json" to update general DOMjudge configuration.')


if os.path.exists('executables'):
executables = []
for file in os.listdir('executables'):
if os.path.isdir(f'executables/{file}'):
executables.append(file)
shutil.make_archive(f'executables/{file}', 'zip', f'executables/{file}')

if executables:
if dj_utils.confirm('Upload language executables (found: ' + ','.join(executables) + ')?', False):
for langid in executables:
dj_utils.upload_file(f'languages/{langid}/executable', 'executable', f'executables/{langid}.zip')
for langid in executables:
os.remove(f'executables/{langid}.zip')


if os.path.exists('languages.json'):
print('Comparing DOMjudge\'s language configuration:')
with open('languages.json') as langConfigFile:
Expand All @@ -132,7 +117,29 @@ if os.path.exists('languages.json'):
if missing_keys:
print(f' - missing keys from expected config = {missing_keys}')
if diffs or new_keys or missing_keys:
print(' - We cannot update the configuration yet, but you might want to change it via the UI.')
if os.path.exists('executables'):
executables = []
for file in os.listdir('executables'):
if os.path.isdir(f'executables/{file}'):
executables.append(file)
shutil.make_archive(f'executables/{file}', 'zip', f'executables/{file}')

if executables:
if dj_utils.confirm('Upload language executables (found: ' + ','.join(executables) + ')?', False):
for langid in executables:
dj_utils.upload_file(f'languages/{langid}/executable', 'executable', f'executables/{langid}.zip')
for langid in executables:
os.remove(f'executables/{langid}.zip')
if dj_utils.confirm(' - Upload configuration changes?', True):
actual_config = _keyify_list(dj_utils.upload_file(f'languages', 'json', f'languages.json'))
diffs, new_keys, missing_keys = compare_configs(
actual_config=actual_config,
expected_config=expected_config
)
if diffs:
print(' - There are still configuration differences after uploading:')
for d in diffs:
print(d)
else:
print(' - Language configuration is already up-to-date, nothing to update.')
else:
Expand Down
79 changes: 79 additions & 0 deletions webapp/src/Controller/API/LanguageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use FOS\RestBundle\Controller\Annotations as Rest;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Attributes as OA;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
Expand Down Expand Up @@ -122,6 +123,84 @@ public function updateExecutableActions(Request $request, string $id): void
$this->dj->auditlog('executable', $language->getLangid(), 'updated');
}

#[IsGranted('ROLE_ADMIN')]
#[Rest\Post('languages')]
#[OA\Response(
response: 200,
description: 'Configure all specified languages.',
)]
public function configureLanguagesAction(Request $request): Response
{
/** @var UploadedFile|null $jsonFile */
$jsonFile = $request->files->get('json');
if (!$jsonFile) {
throw new BadRequestHttpException('No JSON file supplied.');
}
$newLanguages = $this->dj->jsonDecode(file_get_contents($jsonFile->getRealPath()));

// Disable submission for all current languages, we will enable it for all new languages below.
$curLanguages = $this->em->getRepository(Language::class)->findAll();
foreach ($curLanguages as $language) {
/** @var Language $language */
$language->setAllowSubmit(false);
}

$idField = $this->eventLogService->externalIdFieldForEntity(Language::class) ?? 'langid';
foreach ($newLanguages as $language) {
/** @var Language $language */
$lang_id = $language['id'];
$lang = $this->em->getRepository(Language::class)->findOneBy(
[$idField => $lang_id]
);
if (!$lang) {
continue;
}
$lang->setAllowSubmit(true);
if (isset($language['name'])) {
$lang->setName($language['name']);
}
if (isset($language['allow_submit'])) {
$lang->setAllowSubmit($language['allow_submit']);
}
if (isset($language['allow_judge'])) {
$lang->setAllowJudge($language['allow_judge']);
}
if (isset($language['entry_point_required'])) {
$lang->setRequireEntryPoint($language['entry_point_required']);
}
if (isset($language['entry_point_name'])) {
$lang->setEntryPointDescription($language['entry_point_name']);
}
if (isset($language['extensions'])) {
$lang->setExtensions($language['extensions']);
}
if (isset($language['filter_compiler_files'])) {
$lang->setFilterCompilerFiles($language['filter_compiler_files']);
}
if (isset($language['time_factor'])) {
$lang->setTimeFactor($language['time_factor']);
}
if (isset($language['compiler'])) {
if (isset($language['compiler']['version_command'])) {
$lang->setCompilerVersionCommand($language['compiler']['version_command']);
}
if (isset($language['compiler']['version'])) {
$lang->setCompilerVersion($language['compiler']['version']);
}
}
if (isset($language['runner'])) {
if (isset($language['runner']['version_command'])) {
$lang->setRunnerVersionCommand($language['runner']['version_command']);
}
if (isset($language['runner']['version'])) {
$lang->setRunnerVersion($language['runner']['version']);
}
}
}
$this->em->flush();
return parent::performListAction($request);
}

protected function getQueryBuilder(Request $request): QueryBuilder
{
if ($request->attributes->has('cid')) {
Expand Down

0 comments on commit 24da9ef

Please sign in to comment.