Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to upload languages configuration via API. #2170

Merged
merged 2 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
84 changes: 84 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,89 @@ public function updateExecutableActions(Request $request, string $id): void
$this->dj->auditlog('executable', $language->getLangid(), 'updated');
}

#[IsGranted('ROLE_ADMIN')]
vmcj marked this conversation as resolved.
Show resolved Hide resolved
#[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) {
// TODO: Decide how to handle this case, either erroring out or creating a new language.
continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we not indicate this to the one uploading that the language is missing?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and no:

  • Logging is not really possible as we return the updated languages config, so that it can be compared in the script.
  • We could error out, but that's quite brutal.
  • We could try adding a new language, but of course stuff like executables are missing here.

What about this: I will add a TODO comment here for now; and when we decided on the best solution we can replace it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that.

}

// We disallowed submission for all languages above, so we need to enable it for the given languages.
$lang->setAllowSubmit(true);

if (isset($language['name'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use a PropertyAccessor loop here to get rid of most duplication. See also the problem import somewhere (I believe)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this and it was not really less code / more understandable (probably also because of the nested compiler/runner fields). Feel free to submit to this branch.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I’m fine as is

$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
Loading