### ConfigNode get() Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of using the get() method with a default value. ```python points = node.get('points', 1) ``` -------------------------------- ### Starting Judge with API Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Example command to start the judge server with the API enabled and listening on a specific port. ```bash dmoj -c judge.yml -a 9998 -A 0.0.0.0 localhost ``` -------------------------------- ### Compile and Execute example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/executors.md A comprehensive example showing how to load executors, get an executor instance, compile source code, and launch it with resource limits. ```python from dmoj import executors import subprocess # Load executors executors.load_executors() # Get executor cpp = executors.executors['CPP17'].Executor # Create executor instance executor = cpp( problem_id='test', source_code=b'int main() { return 0; }', fs=[], # Filesystem rules ) # Compile executor = executor.compile() # Launch with resource limits process = executor.launch( time=1.0, # 1 second memory=65536, # 64 MB stdin=open('input.txt', 'rb'), stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) # Wait for completion stdout, stderr = process.communicate() print(f"Exit code: {process.returncode}") ``` -------------------------------- ### LocalJudge Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example of initializing LocalJudge and preparing to start the CLI loop. ```python from dmoj.cli import LocalJudge judge = LocalJudge() # Now use judge.listen() to start CLI loop ``` -------------------------------- ### submit / test Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example commands for submitting and testing solutions. ```bash dmoj-cli> submit aplusb CPP17 /home/user/solution.cpp dmoj-cli> test myprob PY3 solution.py ``` -------------------------------- ### validate Command Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example command to validate a problem setup. ```bash dmoj-cli> validate aplusb ``` -------------------------------- ### help Command Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example commands for using the 'help' command. ```bash help [command] dmoj-cli> help submit dmoj-cli> help ``` -------------------------------- ### GET /metrics Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/endpoints.md Examples of how to fetch metrics using curl, Python, and Prometheus configuration. ```bash # Using curl curl http://judge-host:9998/metrics ``` ```python # Using Python import requests response = requests.get('http://judge-host:9998/metrics') print(response.text) ``` ```yaml # In monitoring systems (Prometheus) scrape_configs: - job_name: 'dmoj-judge' static_configs: - targets: ['judge-host:9998'] ``` -------------------------------- ### ConfigNode unwrap() Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of unwrapping a ConfigNode to get the raw dictionary. ```python raw = node.unwrap() ``` -------------------------------- ### show Command Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example command to display problem information. ```bash dmoj-cli> show aplusb ``` -------------------------------- ### run Method Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/packet.md Example of calling the run method. ```python pm.run() # Blocking call ``` -------------------------------- ### Example of using by_ext Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/executors.md Shows how to get executors for C++ and Java using their file extensions. ```python cpp_executor = executors.by_ext('cpp') # CPP17, CPP14, etc. java_executor = executors.by_ext('java') # JAVA8 or JAVA ``` -------------------------------- ### launch Method Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/executors.md Example of launching an executor with resource limits. ```python process = executor.launch( time=2.0, memory=65536, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) ``` -------------------------------- ### get_problem_root Function Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example showing how to get the root directory path for a specific problem. ```python root = judgeenv.get_problem_root('aplusb') print(root) # /problems/aplusb ``` -------------------------------- ### Filesystem Configuration Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Example of configuring filesystem access rules for different languages (Perl and Ruby). ```yaml extra_fs: PERL: - exact_file: /dev/dtrace/helper - recursive_dir: /usr/share/perl RUBY: - exact_dir: /usr/lib/ruby - recursive_dir: /usr/share/ruby ``` -------------------------------- ### Getting Metrics Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Example curl command to retrieve Prometheus metrics from the Judge API. ```bash curl http://localhost:9998/metrics ``` -------------------------------- ### Server Mode Examples Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Examples demonstrating how to connect the DMOJ judge to a server using various configurations, including TLS and debug mode. ```bash # Connect to judge server dmoj -c /etc/judge.yml -p 9999 judge.example.com judge-1 secret-key # With TLS dmoj -c /etc/judge.yml -s -T /etc/ssl/certs/ca.pem judge.example.com:9999 # Debug mode with limited executors dmoj -c /etc/judge.yml -d -e CPP17,JAVA8 localhost ``` -------------------------------- ### Item Access Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of accessing a configuration key using item access. ```python node['key-name'] node[0] # For lists ``` -------------------------------- ### InvalidInitException Constructor and Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Demonstrates the constructor signature and provides an example of catching the InvalidInitException. ```python InvalidInitException(message: str) ``` ```python try: problem = Problem('nonexistent', 1.0, 65536, {}) except InvalidInitException as e: print(f"Failed to load problem: {e}") ``` -------------------------------- ### ConfigNode Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example usage of ConfigNode with a configuration dictionary. ```python from dmoj.config import ConfigNode config_dict = { 'output_prefix_length': 5, 'test_cases': [ {'in': 'test1.in', 'out': 'test1.out', 'points': 10}, {'in': 'test2.in', 'out': 'test2.out', 'points': 20} ] } node = ConfigNode(config_dict) print(node.output_prefix_length) # 5 print(node['test_cases'][0]['points']) # 10 ``` -------------------------------- ### Example of using from_filename Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/executors.md Demonstrates getting an executor from a given filename. ```python executor_module = executors.from_filename('solution.cpp') executor_module = executors.from_filename('Main.java') ``` -------------------------------- ### get_runtime_versions Function Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example demonstrating how to fetch and display available runtime versions for different executors. ```python versions = judgeenv.get_runtime_versions() if 'CPP17' in versions: for version_str, version_tuple in versions['CPP17']: print(f"C++ 17: {version_str}") ``` -------------------------------- ### Dynamic Keys - Full Code Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of dynamic key evaluation using full code execution in YAML. ```yaml time_limit++: | exec(""" node['time_limit'] = get_time_limit(node) """) ``` -------------------------------- ### PacketManager Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/packet.md Example of how to instantiate and configure a PacketManager. ```python from dmoj.packet import PacketManager from dmoj.judge import Judge judge = Judge(None) # Will be set pm = PacketManager( host='judge.example.com', port=9999, judge=judge, name='judge-1', key='secret-key', secure=True ) judge.packet_manager = pm ``` -------------------------------- ### CLI Mode Examples Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Examples demonstrating how to use the DMOJ judge in CLI mode, including interactive mode and running a single command. ```bash # Interactive CLI dmoj-cli -c /etc/judge.yml # Run single command dmoj-cli -c /etc/judge.yml submit aplusb CPP17 /path/to/solution.cpp ``` -------------------------------- ### resubmit Command Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example command to re-submit a solution with the same code. ```bash dmoj-cli> resubmit 5 ``` -------------------------------- ### get_supported_problems_and_mtimes Function Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of how to retrieve a dictionary of supported problems and their last modification times. ```python problems = judgeenv.get_supported_problems_and_mtimes() for problem_id, mtime in problems.items(): print(f"{problem_id}: modified at {mtime}") ``` -------------------------------- ### ConfigNode items() Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of iterating over key-value pairs using the items() method. ```python for key, value in node.items(): print(f"{key}: {value}") ``` -------------------------------- ### Judge.listen Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/judge.md Starts the judge event loop. Attempts to connect to the judge server and process submissions. ```python try: judge.listen() except KeyboardInterrupt: pass ``` -------------------------------- ### ConfigNode update() Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of updating a ConfigNode with new key-value pairs. ```python node.update({'points': 100}) ``` -------------------------------- ### ProblemDataManager.open Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/problem.md Example of how to use the open method to read a file. ```python with problem_data.open('input.txt') as f: data = f.read() ``` -------------------------------- ### CLI Environment Variables Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Examples of running the DMOJ CLI with specific configurations or flags. ```bash # Run a single command without interactive shell dmoj-cli -c config.yml submit problem CPP17 solution.cpp # With extended history file dmoj-cli --history /tmp/judge_history -c config.yml ``` -------------------------------- ### Dynamic Keys - Expression Evaluation Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example demonstrating dynamic expression evaluation for a ConfigNode. ```python config = { 'points+': 'len(node["test_cases"]) * 10' } node = ConfigNode(config) # node.points will be calculated dynamically ``` -------------------------------- ### Attribute Access Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of accessing a configuration key using attribute access. ```python node.key_name ``` -------------------------------- ### CLI Configuration Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example configuration file (~/.dmojrc) for the DMOJ CLI. ```yaml # Problem directories problem_globs: - ~/problems/* # CLI-specific settings # (Most settings from server config still apply) ``` -------------------------------- ### close Method Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/packet.md Example of calling the close method. ```python pm.close() ``` -------------------------------- ### Submission Object Construction Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/types.md An example of how to construct a Submission object in Python. ```python from dmoj.judge import Submission submission = Submission( id=12345, problem_id='aplusb', language='CPP17', source='#include \nint main() { int a, b; cin >> a >> b; cout << a+b; }', time_limit=1.0, memory_limit=65536, short_circuit=False, meta={'user': 'user123'} ) ``` -------------------------------- ### load_env Function Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example usage of the load_env function for both server and CLI modes. ```python # Server mode from dmoj import judgeenv judgeenv.load_env(cli=False) print(judgeenv.server_host) print(judgeenv.env.compiler_time_limit) # CLI mode judgeenv.load_env(cli=True) ``` -------------------------------- ### rejudge Command Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example command to re-grade a submission. ```bash dmoj-cli> rejudge 5 ``` -------------------------------- ### Example Usage Source: https://github.com/dmoj/judge-server/blob/master/contrib/vm-host-problem-watch/README.md Demonstrates how to use the vm-host-problem-watch.sh script, including setting up a judge hosts list, listing problems, and running the script to watch for updates and notify judges. ```bash $ cat /code/judge-hosts-list judge1.example.com:9998 judge2.example.com:9998 judge3.example.com:9998 $ ls /export/problems | head -n 5 apio bbc bkoi broi btoi $ ./vm-host-problem-watch.sh /export/problems /code/judge-host-list Tue Oct 6 22:17:37 UTC 2020: Start watching /export/problems, notifying /code/judge-host-list Tue Oct 6 22:17:46 UTC 2020: Update problems [/export/problems/rgss/17/rgpc17p5/ MOVED_FROM init.yml] Tue Oct 6 22:17:46 UTC 2020 [judge1.example.com:9998]: As you wish. Tue Oct 6 22:17:46 UTC 2020 [judge2.example.com:9998]: As you wish. Tue Oct 6 22:17:46 UTC 2020 [judge3.example.com:9998]: As you wish. ``` -------------------------------- ### Install stable build Source: https://github.com/dmoj/judge-server/blob/master/README.md Installs the DMOJ judge using pip. ```bash $ pip install dmoj ``` -------------------------------- ### diff Command Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Example command to show verdict differences for a submission. ```bash dmoj-cli> diff 5 ``` -------------------------------- ### ProblemConfig Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/problem.md Example of creating a ProblemConfig object and accessing its time_limit attribute. ```python config = ProblemConfig(problem_data, meta={'source': 'contest'}) print(config.time_limit) # From init.yml ``` -------------------------------- ### Dynamic Keys - Expression Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Example of dynamic key evaluation using Python expressions in YAML. ```yaml wall_time_factor+: 3 time_limit: 2 wall_time_factor+: "node['time_limit'] * 3" ``` -------------------------------- ### Problem Configuration Example (init.yml) Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/types.md An example YAML configuration file for defining a problem on the DMOJ judge server, including test cases, custom checkers, batched test cases, interactive problems, signature grading, custom judges, and various output/performance settings. ```yaml # Required: Test cases test_cases: - {in: input.txt, out: output.txt, points: 10} - {in: input2.txt, out: output2.txt, points: 20} # Or with archive: archive: problems.zip test_cases: - {in: input.txt, out: output.txt, points: 10} # Optional: Custom checker checker: function: check # Optional: Batched test cases (all-or-nothing) test_cases: - batched: - {in: input1.txt, out: output1.txt} - {in: input2.txt, out: output2.txt} points: 15 - {in: input3.txt, out: output3.txt, points: 10} # Optional: Interactive problem interactive: judge: interactor.cpp feedback: true # Optional: Signature graded (C++ function) signature_grader: entry: main header: header.h # Optional: Custom judge custom_judge: judge.cpp # Optional: Output prefix length output_prefix_length: 100 # Optional: Wall time factor (real time / cpu time) wall_time_factor: 3 # Optional: Output size limit (bytes) output_limit_length: 25165824 # Optional: Binary data (no newline normalization) binary_data: false # Optional: Short circuit (stop on first non-AC) short_circuit: true # Optional: Pretest cases pretest_test_cases: - {in: pretest1.txt, out: pretest1.txt, points: 5} ``` -------------------------------- ### Result Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/result.md An example demonstrating how to instantiate the Result class with specific values. ```python result = Result( case=test_case, result_flag=Result.AC, execution_time=0.123, wall_clock_time=0.150, max_memory=5000, proc_output=b'42\n', points=test_case.points ) ``` -------------------------------- ### Executor Configuration Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Example of configuring executors, including version and flags for C++ and memory limit for Java. ```yaml runtime: CPP17: version: 'g++ (Debian 10.2.0-5) 10.2.0' flags: - '-O2' JAVA8: memory_limit: 262144 # 256MB ``` -------------------------------- ### output Property Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/result.md Example of printing the program output obtained from the output property. ```python print(f"Output: {result.output}") ``` -------------------------------- ### Integration Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/MANIFEST.md An example demonstrating how to integrate the DMOJ judge using its documentation. ```python # 1. Load configuration (see config.md) from dmoj.judgeenv import load_env load_env() # 2. Create judge instance (see judge.md) from dmoj.judge import Judge from dmoj.packet import PacketManager pm = PacketManager( host='judge.example.com', port=9999, judge=None, name='judge-1', key='secret-key' ) judge = Judge(pm) pm.judge = judge # 3. Load languages (see executors.md) from dmoj import executors executors.load_executors() # 4. Load problems (see problem.md) from dmoj.problem import Problem problem = Problem('test', 1.0, 65536, {}) ``` -------------------------------- ### Example of loading executors Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/executors.md Demonstrates how to load executors and print their keys. ```python from dmoj import executors executors.load_executors() print(executors.executors.keys()) # [\'CPP17\', \'JAVA8\', \'PY3\', ...] ``` -------------------------------- ### get_main_code Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/result.md Example usage of the get_main_code method to check if a result is Accepted. ```python main_verdict = result.get_main_code() if main_verdict == Result.AC: print("Correct!") ``` -------------------------------- ### LocalPacketManager Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Demonstrates the usage of LocalPacketManager within LocalJudge, highlighting its no-op nature. ```python from dmoj.cli import LocalPacketManager, LocalJudge judge = LocalJudge() pm = judge.packet_manager pm.test_case_status_packet(0, result) # Does nothing ``` -------------------------------- ### POST /update/problems Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/endpoints.md Examples of how to trigger the POST /update/problems endpoint using curl and Python. ```bash # Using curl curl -X POST http://judge-host:9998/update/problems ``` ```python # In Python import requests response = requests.post('http://judge-host:9998/update/problems') assert response.status_code == 200 ``` -------------------------------- ### Example judge.yml Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md A comprehensive example of a judge.yml configuration file, illustrating various settings for judge name, authentication, problem directories, compilation, testing, and runtime environments. ```yaml # Judge name and authentication # (Can be overridden via command line) judge_name: judge-1 judge_key: secret-key-123 # Problem directories (glob patterns) # Multiple patterns can be specified problem_globs: - /problems/* - /extra-problems/* # Watch for problem directory changes problem_watches: - /problems # Regex filters for problems and cases # (Used in testing/debug mode) # problem_regex: pattern # case_regex: pattern # Temporary directory for compilation and execution # Defaults to system temp directory tempdir: /tmp/judge # Compiler configuration compiler_time_limit: 10 # Seconds compiler_size_limit: 131072 # KB (128MB) compiler_output_character_limit: 65536 # Compiled binary cache compiled_binary_cache_dir: /var/cache/judge/binaries compiled_binary_cache_size: 100 # LRU # Test runner configuration selftest_time_limit: 10 # Seconds selftest_memory_limit: 131072 # KB (128MB) # Generator configuration generator_compiler_time_limit: 30 # Seconds generator_time_limit: 20 # Seconds generator_memory_limit: 524288 # KB (512MB) # Validator configuration validator_compiler_time_limit: 30 # Seconds validator_time_limit: 20 # Seconds validator_memory_limit: 524288 # KB (512MB) # CPU affinity (list of core IDs, or null for any) # submission_cpu_affinity: [0, 1, 2, 3] submission_cpu_affinity: null # Per-executor runtime configuration runtime: CPP17: # Executor-specific options JAVA8: # Executor-specific options # Per-executor filesystem configuration extra_fs: PERL: - exact_file: /dev/dtrace/helper RUBY: - recursive_dir: /usr/lib/ruby - exact_dir: /usr/share/ruby # Update notification URLs # Judge will POST to /update/problems on these URLs update_pings: - http://main-judge:9998 - http://backup-judge:9998 ``` -------------------------------- ### Start Judge with API Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/endpoints.md Command to start the DMOJ judge server with API enabled, listening on a specific port and host. ```bash dmoj -c judge.yml -a 9998 -A 0.0.0.0 server.example.com judge-name key ``` -------------------------------- ### submit / test Command Usage Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Usage examples for the 'submit' and 'test' CLI commands. ```bash submit test ``` -------------------------------- ### InvalidInitException Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/errors.md Demonstrates handling InvalidInitException during problem initialization. ```python from dmoj.problem import Problem from dmoj.config import InvalidInitException try: problem = Problem('nonexistent', 1.0, 65536, {}) except InvalidInitException as e: print(f"Invalid problem: {e}") judge.log_internal_error(message=str(e)) ``` -------------------------------- ### Example CLI Workflow Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md Illustrates a typical command-line interaction with the DMOJ CLI, from listing problems to submitting a solution and viewing submissions. ```bash $ dmoj-cli -c ~/.dmojrc Running local judge... dmoj> problems Problem ID | Time Limit | Memory Limit | Cases -----------|------------|--------------|------- aplusb | 1.0 s | 64 MB | 5 sum | 2.0 s | 128 MB | 10 dmoj> show aplusb Problem: aplusb (A Plus B) Time Limit: 1.0 seconds Memory Limit: 64 MB Test Cases: 5 Checker: standard dmoj> submit aplusb CPP17 /tmp/solution.cpp Start grading #ansi[aplusb](#yellow)/#ansi[0](green|bold) in CPP17... Test case 0 AC [0.001s (0.002s wall) | 1024kb | 0 switches] Test case 1 AC [0.001s (0.002s wall) | 1024kb | 0 switches] Test case 2 AC [0.001s (0.002s wall) | 1024kb | 0 switches] Test case 3 AC [0.002s (0.003s wall) | 2048kb | 1 switches] Test case 4 AC [0.001s (0.002s wall) | 1024kb | 0 switches] Done grading aplusb/0. dmoj> submissions ID | Problem | Language | Verdict | Points | Time ---|---------|----------|---------|--------|------ 0 | aplusb | CPP17 | AC | 5/5 | 0.008 dmoj> exit ``` -------------------------------- ### StandardGrader Example Usage Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/graders.md Example of how to instantiate and use the StandardGrader to grade a test case. ```python from dmoj.graders.standard import StandardGrader grader = StandardGrader(judge_worker, problem, 'CPP17', source_code) result = grader.grade(test_case) print(f"Verdict: {result.get_main_code()}") print(f"Points: {result.points}") ``` -------------------------------- ### Import Examples Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/README.md Examples of common imports for the DMOJ judge server, covering various modules like judge, problems, grading, configuration, network, executors, CLI, and errors. ```python # Main judge from dmoj.judge import Judge, Submission, JudgeWorker # Problems and test cases from dmoj.problem import Problem, TestCase, BatchedTestCase # Grading from dmoj.graders import StandardGrader, InteractiveGrader from dmomoj.result import Result, CheckerResult # Configuration from dmoj.config import ConfigNode from dmoj.judgeenv import env, load_env # Network from dmoj.packet import PacketManager # Executors from dmoj.executors import load_executors from dmoj.executors.base_executor import BaseExecutor # CLI from dmoj.cli import LocalJudge, cli_main # Errors from dmoj.error import CompileError, InternalError from dmoj.config import InvalidInitException ``` -------------------------------- ### Example Alert Rules (alert.rules.yml) Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/endpoints.md Example Prometheus alert rules defined in an alert.rules.yml file for DMOJ judge monitoring. ```yaml groups: - name: dmoj_judge interval: 30s rules: - alert: JudgeDown expr: dmoj_judge_up == 0 for: 5m annotations: summary: "Judge {{ $labels.instance }} is down" - alert: JudgeBacklog expr: rate(dmoj_judge_submission_graded_total[5m]) < 0.1 for: 10m annotations: summary: "Judge {{ $labels.instance }} is not grading" - alert: HighErrorRate expr: | ( increase(dmoj_judge_case_verdicts_total{verdict=~"IE|RTE"}[1h]) ) > 100 annotations: summary: "High error rate on judge {{ $labels.instance }}" ``` -------------------------------- ### Example Prometheus Configuration Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/endpoints.md A sample YAML configuration for Prometheus to scrape metrics from the DMOJ judge server. ```yaml global: scrape_interval: 15s scrape_configs: - job_name: 'dmoj-judge' scrape_interval: 5s static_configs: - targets: ['localhost:9998'] relabel_configs: - source_labels: [__address__] target_label: instance # Alert rules example alerting: alertmanagers: - static_configs: - targets: ['localhost:9093'] rule_files: - 'alert.rules.yml' ``` -------------------------------- ### OutputLimitExceeded Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/errors.md Illustrates catching an OutputLimitExceeded error when submission output exceeds limits. ```python from dmoj.error import OutputLimitExceeded try: # Process output if len(output) > output_limit: raise OutputLimitExceeded('stdout', output_limit) except OutputLimitExceeded as e: print(f"Error: {e}") result.result_flag |= Result.OLE ``` -------------------------------- ### Contrib Module Loading Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/contrib-modules.md Python code snippet demonstrating how to load all available contrib modules. ```python from dmoj import contrib # Load all available contrib modules contrib.load_contrib_modules() ``` -------------------------------- ### BaseContribModule parse_return_code Example Implementation Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/contrib-modules.md Example implementation of the parse_return_code class method for a custom module, handling Accepted and Wrong Answer verdicts. ```python class MyModule(BaseContribModule): name = 'mymodule' AC = 0 WA = 1 @classmethod def parse_return_code(cls, proc, executor, point_value, time_limit, memory_limit, feedback, name, stderr): if proc.returncode == cls.AC: return CheckerResult(True, point_value) elif proc.returncode == cls.WA: return CheckerResult(False, 0) else: return CheckerResult(False, 0, feedback=f'{name} crashed}') ``` -------------------------------- ### TestLib Module Example Checker (C++) Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/contrib-modules.md Example of a checker program written in C++ using the testlib library to compare output with the answer. ```cpp #include "testlib.h" #include int main(int argc, char** argv) { registerTestlibCmd(argc, argv); string output = ouf.readString(); string answer = ans.readString(); if (output == answer) { quitf(_ok, "Correct!"); } else { quitf(_wa, "Expected %s, got %s", answer.c_str(), output.c_str()); } return 0; } ``` -------------------------------- ### InvalidCommandException Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/errors.md Shows how to raise and catch an InvalidCommandException for CLI argument errors. ```python from dmoj.error import InvalidCommandException try: if not isinstance(points, float): raise InvalidCommandException("Points must be a number") except InvalidCommandException as e: if e.message: print(f"Error: {e.message}") ``` -------------------------------- ### Judge Server Listening Endpoint Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/MANIFEST.md This snippet shows the basic command to start the judge server listening. ```python judge.listen() ``` -------------------------------- ### Judge.begin_grading Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/judge.md Begins grading a submission. Creates a JudgeWorker and spawns a grading thread. ```python from dmoj.judge import Submission, Judge submission = Submission( id=123, problem_id='aplusb', language='CPP17', source='#include \nint main() { int a, b; cin >> a >> b; cout << a+b; }', time_limit=1.0, memory_limit=65536, short_circuit=False, meta={} ) judge.begin_grading(submission, blocking=False) ``` -------------------------------- ### run Method Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/packet.md Signature for the run method, which starts the packet manager's event loop. ```python def run(self) -> None ``` -------------------------------- ### readable_codes Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/result.md Example of using readable_codes to display all applicable verdicts. ```python verdicts = result.readable_codes() # Returns ['TLE', 'RTE'] if both flags are set ``` -------------------------------- ### Install bleeding-edge build Source: https://github.com/dmoj/judge-server/blob/master/README.md Installs the bleeding-edge version of the DMOJ judge from source. ```bash $ git clone --recursive https://github.com/DMOJ/judge-server.git $ cd judge-server $ pip install -e . ``` -------------------------------- ### BaseGrader.abort_grading Method Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/graders.md Example of how to call the abort_grading method on a grader instance. ```python grader.abort_grading() ``` -------------------------------- ### BaseContribModule get_checker_args_format_string Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/contrib-modules.md Example implementation of the get_checker_args_format_string class method for a custom checker module. ```python class MyModule(BaseContribModule): @classmethod def get_checker_args_format_string(cls) -> str: return '{input} {output} {submission_output}' ``` -------------------------------- ### Example Custom Checker Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/graders.md An example of a custom checker function that compares stripped actual and expected output, returning a CheckerResult. ```python from dmoj.result import CheckerResult def check(process_output: bytes, expected_output: bytes, **kwargs) -> CheckerResult: actual = process_output.decode().strip() expected = expected_output.decode().strip() if actual == expected: return CheckerResult(True, kwargs['point_value']) else: return CheckerResult(False, 0, f"Expected {expected}, got {actual}") ``` -------------------------------- ### launch Method Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/executors.md Launches the compiled program with resource limits. ```python def launch( self, time: float = 2, memory: int = 65536, stdin: IO = None, stdout: IO = None, stderr: IO = None, wall_time: float = None, symlinks: Dict[str, str] = None, ) -> TracedPopen: ``` -------------------------------- ### Problem Constructor Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/problem.md Initializes a new Problem instance. ```python problem = Problem( problem_id='aplusb', time_limit=1.0, memory_limit=65536, meta={} ) ``` -------------------------------- ### TestCase Constructor Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/problem.md Initializes a new TestCase instance. ```python TestCase( count: int, batch_no: int, config: ConfigNode, problem: Problem ) -> None ``` -------------------------------- ### Judge.abort_grading Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/judge.md Aborts the currently grading submission. ```python judge.abort_grading(submission_id=123) ``` -------------------------------- ### CLI Command Format Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/cli.md General format for DMOJ CLI commands. ```bash dmoj-cli> command [arguments] ``` -------------------------------- ### InternalError Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/errors.md Shows how to raise an InternalError for unexpected issues during grading. ```python from dmoj.error import InternalError try: # Grading logic except Exception as e: raise InternalError(f"Unexpected error during grading: {e}") ``` -------------------------------- ### DMOJ Judge Server CLI Help Source: https://github.com/dmoj/judge-server/blob/master/README.md Displays the help message for the 'dmoj' command, outlining its usage and available arguments for running a judge server. ```bash $ dmoj --help usage: dmoj [-h] [-p SERVER_PORT] -c CONFIG [-l LOG_FILE] [--no-watchdog] [-a API_PORT] [-A API_HOST] [-s] [-k] [-T TRUSTED_CERTIFICATES] [-e ONLY_EXECUTORS | -x EXCLUDE_EXECUTORS] [--no-ansi] server_host [judge_name] [judge_key] Spawns a judge for a submission server. positional arguments: server_host host to connect for the server judge_name judge name (overrides configuration) judge_key judge key (overrides configuration) optional arguments: -h, --help show this help message and exit -p SERVER_PORT, --server-port SERVER_PORT port to connect for the server -c CONFIG, --config CONFIG file to load judge configurations from -l LOG_FILE, --log-file LOG_FILE log file to use --no-watchdog disable use of watchdog on problem directories -a API_PORT, --api-port API_PORT port to listen for the judge API (do not expose to public, security is left as an exercise for the reverse proxy) -A API_HOST, --api-host API_HOST IPv4 address to listen for judge API -s, --secure connect to server via TLS -k, --no-certificate-check do not check TLS certificate -T TRUSTED_CERTIFICATES, --trusted-certificates TRUSTED_CERTIFICATES use trusted certificate file instead of system -e ONLY_EXECUTORS, --only-executors ONLY_EXECUTORS only listed executors will be loaded (comma-separated) -x EXCLUDE_EXECUTORS, --exclude-executors EXCLUDE_EXECUTORS prevent listed executors from loading (comma- separated) --no-ansi disable ANSI output --skip-self-test skip executor self-tests ``` -------------------------------- ### CompileError Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/errors.md Demonstrates how to catch and handle a CompileError during the grading process. ```python from dmoj.error import CompileError from dmoj.graders.standard import StandardGrader try: grader = StandardGrader(judge_worker, problem, 'CPP17', source) except CompileError as e: print(f"Compilation failed: {e.message}") # Send to server packet_manager.compile_error_packet(e.message) ``` -------------------------------- ### ConfigNode Constructor Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/config.md Constructor signature for ConfigNode. ```python ConfigNode( raw_config: dict = None, parent: ConfigNode = None, defaults: dict = None, dynamic: bool = True ) -> None ``` -------------------------------- ### Judge.update_problems Example Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/api-reference/judge.md Pushes the current problem set to the judge server. ```python judge = Judge(packet_manager) judge.update_problems() ``` -------------------------------- ### Server Mode Usage Source: https://github.com/dmoj/judge-server/blob/master/_autodocs/configuration.md Command-line usage for running the DMOJ judge in server mode, including positional arguments and available options. ```bash dmoj [options] server_host [judge_name] [judge_key] ``` -------------------------------- ### Docker Tier 1 Judge Image Setup and Run Source: https://github.com/dmoj/judge-server/blob/master/README.md Clones the judge-server repository, builds a tier 1 judge image, and runs it with specified configurations and environment variables. ```bash $ git clone --recursive https://github.com/DMOJ/judge-server.git $ cd judge-server/.docker $ make judge-tier1 $ exec docker run \ --name judge \ -p "$(ip addr show dev enp1s0 | perl -ne 'm@inet (.*)/.*@ and print$1 and exit')":9998:9998 \ -v /mnt/problems:/problems \ --cap-add=SYS_PTRACE \ -d \ --restart=always \ dmoj/judge-tier1:latest \ run -p15001 -s -c /problems/judge.yml \ "$BRIDGE_ADDRESS" "$JUDGE_NAME" "$JUDGE_KEY" ```