Skip to content

Commit

Permalink
Add support for stepik submit
Browse files Browse the repository at this point in the history
  • Loading branch information
asaldele1 committed Jan 10, 2024
1 parent 165345b commit fca85d4
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 4 deletions.
40 changes: 40 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for c"
},
"cph.language.c.SubmissionCompilerStepik": {
"type": "string",
"default": "c++11",
"description": "The compiler chosen during Stepik submission for c"
},
"cph.language.c.Command": {
"type": "string",
"default": "gcc",
Expand Down Expand Up @@ -125,6 +130,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for cpp"
},
"cph.language.cpp.SubmissionCompilerStepik": {
"type": "string",
"default": "c++11",
"description": "The compiler chosen during Stepik submission for cpp"
},
"cph.language.cpp.Command": {
"type": "string",
"default": "g++",
Expand All @@ -148,6 +158,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for python"
},
"cph.language.python.SubmissionCompilerStepik": {
"type": "string",
"default": "python3",
"description": "The compiler chosen during Stepik submission for python"
},
"cph.language.python.Command": {
"type": "string",
"default": "python3",
Expand All @@ -167,6 +182,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for rust"
},
"cph.language.rust.SubmissionCompilerStepik": {
"type": "string",
"default": "rust",
"description": "The compiler chosen during Stepik submission for rust"
},
"cph.language.rust.Command": {
"type": "string",
"default": "rustc",
Expand All @@ -186,6 +206,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for go"
},
"cph.language.go.SubmissionCompilerStepik": {
"type": "string",
"default": "go",
"description": "The compiler chosen during Stepik submission for go"
},
"cph.language.go.Command": {
"type": "string",
"default": "go",
Expand All @@ -205,6 +230,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for haskell"
},
"cph.language.haskell.SubmissionCompilerStepik": {
"type": "string",
"default": "haskel 7.10",
"description": "The compiler chosen during Stepik submission for haskell"
},
"cph.language.haskell.Command": {
"type": "string",
"default": "ghc",
Expand All @@ -225,6 +255,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for java"
},
"cph.language.java.SubmissionCompilerStepik": {
"type": "string",
"default": "java8",
"description": "The compiler chosen during Stepik submission for java"
},
"cph.language.java.Command": {
"type": "string",
"default": "javac",
Expand All @@ -244,6 +279,11 @@
],
"description": "The compiler chosen in the drop down during Codeforces submission for js"
},
"cph.language.js.SubmissionCompilerStepik": {
"type": "string",
"default": "javascript",
"description": "The compiler chosen during Stepik submission for js"
},
"cph.language.js.Command": {
"type": "string",
"default": "node",
Expand Down
48 changes: 48 additions & 0 deletions src/companion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { isCodeforcesUrl, randomId } from './utils';
import {
getDefaultLangPref,
getLanguageId,
getLanguageIdStepik,
useShortCodeForcesName,
getMenuChoices,
getDefaultLanguageTemplateFileLocation,
Expand All @@ -23,6 +24,7 @@ import os from 'os';
const emptyResponse: CphEmptyResponse = { empty: true };
let savedResponse: CphEmptyResponse | CphSubmitResponse = emptyResponse;
const COMPANION_LOGGING = false;
export let stepikResult = "";

Check failure on line 27 in src/companion.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `""` with `''`

export const submitKattisProblem = (problem: Problem) => {
globalThis.reporter.sendTelemetryEvent(telmetry.SUBMIT_TO_KATTIS);
Expand Down Expand Up @@ -75,6 +77,52 @@ export const submitKattisProblem = (problem: Problem) => {
});
};

export const submitStepikProblem = (problem: Problem) => {
const srcPath = problem.srcPath;
const homedir = os.homedir();
let submitPath = `${homedir}/.stepik/submitter.py`;
if (process.platform == 'win32') {
if (
!existsSync(`${homedir}\\.stepik\\.client_file`) ||
!existsSync(`${homedir}\\.stepik\\submitter.py`)
) {
vscode.window.showErrorMessage(
`Please ensure .client_file and submitter.py are present in ${homedir}\\.stepik`,
);
return;
} else {
submitPath = `${homedir}\\.stepik\\submitter.py`;
}
} else {
if (
!existsSync(`${homedir}/.stepik/.client_file`) ||
!existsSync(`${homedir}/.stepik/submitter.py`)
) {
vscode.window.showErrorMessage(
`Please ensure .client_file and submitter.py are present in ${homedir}/.stepik`,
);
return;
} else {
submitPath = `${homedir}/.stepik/submitter.py`;
}
}
const pyshell = spawn('python', [submitPath, 'submit', srcPath, '-l', getLanguageIdStepik(problem.srcPath), '--link', problem.url, "--silent"]);

Check failure on line 109 in src/companion.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `submitPath,·'submit',·srcPath,·'-l',·getLanguageIdStepik(problem.srcPath),·'--link',·problem.url,·"--silent"` with `⏎········submitPath,⏎········'submit',⏎········srcPath,⏎········'-l',⏎········getLanguageIdStepik(problem.srcPath),⏎········'--link',⏎········problem.url,⏎········'--silent',⏎····`

pyshell.stdout.on('data', function (data) {
console.log(data.toString());
stepikResult += data.toString();
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'new-problem',
problem,
});
({ command: 'submit-finished' });
});
pyshell.stderr.on('data', function (data) {
console.log(data.tostring());
vscode.window.showErrorMessage(data);
});
};

/** Stores a response to be submitted to CF page soon. */
export const storeSubmitProblem = (problem: Problem) => {
const srcPath = problem.srcPath;
Expand Down
9 changes: 8 additions & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
editorClosed,
checkLaunchWebview,
} from './webview/editorChange';
import { submitToCodeForces, submitToKattis } from './submit';
import { submitToCodeForces, submitToKattis, submitToStepik } from './submit';
import JudgeViewProvider from './webview/JudgeView';
import { getRetainWebviewContextPref } from './preferences';
import TelemetryReporter from '@vscode/extension-telemetry';
Expand Down Expand Up @@ -47,6 +47,12 @@ const registerCommands = (context: vscode.ExtensionContext) => {
submitToKattis();
},
);
const disposable5 = vscode.commands.registerCommand(
'cph.submitToStepik',
() => {
submitToStepik();
},
);

judgeViewProvider = new JudgeViewProvider(context.extensionUri);

Expand All @@ -65,6 +71,7 @@ const registerCommands = (context: vscode.ExtensionContext) => {
context.subscriptions.push(disposable2);
context.subscriptions.push(disposable3);
context.subscriptions.push(disposable4);
context.subscriptions.push(disposable5);
globalThis.reporter = new TelemetryReporter(config.telemetryKey);
context.subscriptions.push(globalThis.reporter);

Expand Down
54 changes: 54 additions & 0 deletions src/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,57 @@ export const getLanguageId = (srcPath: string): number => {
console.error("Couldn't find id for compiler " + compiler);
return -1;
};

export const getLanguageIdStepik = (srcPath: string): number => {
const extension = path.extname(srcPath);
let compiler = null;
switch (extension) {
case '.cpp': {
compiler = getPreference('language.cpp.SubmissionCompilerStepik');
break;
}

case '.java': {
compiler = getPreference('language.java.SubmissionCompilerStepik');
break;
}

case '.js': {
compiler = getPreference('language.js.SubmissionCompilerStepik');
break;
}

case '.c': {
compiler = getPreference('language.c.SubmissionCompilerStepik');
break;
}

case '.rs': {
compiler = getPreference('language.rust.SubmissionCompilerStepik');
break;
}

case '.py': {
compiler = getPreference('language.python.SubmissionCompilerStepik');

Check failure on line 198 in src/preferences.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `'language.python.SubmissionCompilerStepik'` with `⏎················'language.python.SubmissionCompilerStepik',⏎············`
break;
}

case '.go': {
compiler = getPreference('language.go.SubmissionCompilerStepik');
break;
}

case '.hs': {
compiler = getPreference('language.haskell.SubmissionCompilerStepik');

Check failure on line 208 in src/preferences.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `'language.haskell.SubmissionCompilerStepik'` with `⏎················'language.haskell.SubmissionCompilerStepik',⏎············`
break;
}
}
if (compiler == null) return -1;
for (const [_compiler, id] of Object.entries(config.compilerToId)) {
if (_compiler === compiler) {
return id;
}
}
console.error("Couldn't find id for compiler " + compiler);
return -1;
};

Check failure on line 220 in src/preferences.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Insert `⏎`
42 changes: 41 additions & 1 deletion src/submit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getProblem } from './parser';
import * as vscode from 'vscode';
import { storeSubmitProblem, submitKattisProblem } from './companion';
import { storeSubmitProblem, submitKattisProblem, submitStepikProblem } from './companion';

Check failure on line 3 in src/submit.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `·storeSubmitProblem,·submitKattisProblem,·submitStepikProblem·` with `⏎····storeSubmitProblem,⏎····submitKattisProblem,⏎····submitStepikProblem,⏎`
import { getJudgeViewProvider } from './extension';
import telmetry from './telmetry';

Expand Down Expand Up @@ -45,6 +45,46 @@ export const submitToKattis = async () => {
});
};

export const submitToStepik = async () => {
const srcPath = vscode.window.activeTextEditor?.document.fileName;
if (!srcPath) {
vscode.window.showErrorMessage(
'Active editor is not supported for submission',
);
return;
}

const textEditor = await vscode.workspace.openTextDocument(srcPath);
await vscode.window.showTextDocument(textEditor, vscode.ViewColumn.One);
await textEditor.save();

const problem = getProblem(srcPath);

if (!problem) {
vscode.window.showErrorMessage('Failed to parse current code.');
return;
}

let url: URL;
try {
url = new URL(problem.url);
} catch (err) {
console.error(err);
vscode.window.showErrorMessage('Not a kattis problem.');
return;
}

if (url.hostname !== 'stepik.org') {
vscode.window.showErrorMessage('Not a kattis problem.');
return;
}

submitStepikProblem(problem);
getJudgeViewProvider().extensionToJudgeViewMessage({
command: 'waiting-for-submit',
});
};

export const submitToCodeForces = async () => {
const srcPath = vscode.window.activeTextEditor?.document.fileName;

Expand Down
15 changes: 14 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,35 @@ export type prefSection =
| 'general.menuChoices'
| 'language.c.Args'
| 'language.c.SubmissionCompiler'
| 'language.c.SubmissionCompilerStepik'
| 'language.c.Command'
| 'language.cpp.Args'
| 'language.cpp.SubmissionCompiler'
| 'language.cpp.SubmissionCompilerStepik'
| 'language.cpp.Command'
| 'language.go.Args'
| 'language.go.SubmissionCompiler'
| 'language.go.SubmissionCompilerStepik'
| 'language.go.Command'
| 'language.rust.Args'
| 'language.rust.SubmissionCompiler'
| 'language.rust.SubmissionCompilerStepik'
| 'language.rust.Command'
| 'language.java.Args'
| 'language.java.SubmissionCompiler'
| 'language.java.SubmissionCompilerStepik'
| 'language.java.Command'
| 'language.js.Args'
| 'language.js.SubmissionCompiler'
| 'language.js.SubmissionCompilerStepik'
| 'language.js.Command'
| 'language.python.Args'
| 'language.python.SubmissionCompiler'
| 'language.python.SubmissionCompilerStepik'
| 'language.python.Command'
| 'language.haskell.Args'
| 'language.haskell.SubmissionCompiler'
| 'language.haskell.SubmissionCompilerStepik'
| 'language.haskell.Command'
| 'general.retainWebviewContext'
| 'general.autoShowJudge'
Expand Down Expand Up @@ -133,6 +141,10 @@ export type SubmitKattis = {
command: 'submitKattis';
} & WebviewMessageCommon;

export type SubmitStepik = {
command: 'submitStepik';
} & WebviewMessageCommon;

export type GetInitialProblem = {
command: 'get-initial-problem';
};
Expand All @@ -151,7 +163,8 @@ export type WebviewToVSEvent =
| DeleteTcsCommand
| SubmitCf
| OnlineJudgeEnv
| SubmitKattis;
| SubmitKattis
| SubmitStepik;

export type RunningCommand = {
command: 'running';
Expand Down
6 changes: 5 additions & 1 deletion src/webview/JudgeView.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import { storeSubmitProblem, submitKattisProblem } from '../companion';
import { storeSubmitProblem, submitKattisProblem, submitStepikProblem } from '../companion';

Check failure on line 2 in src/webview/JudgeView.ts

View workflow job for this annotation

GitHub Actions / build (20.x)

Replace `·storeSubmitProblem,·submitKattisProblem,·submitStepikProblem·` with `⏎····storeSubmitProblem,⏎····submitKattisProblem,⏎····submitStepikProblem,⏎`
import { killRunning } from '../executions';
import { saveProblem } from '../parser';
import { VSToWebViewMessage, WebviewToVSEvent } from '../types';
Expand Down Expand Up @@ -84,6 +84,10 @@ class JudgeViewProvider implements vscode.WebviewViewProvider {
submitKattisProblem(message.problem);
break;
}
case 'submitStepik': {
submitStepikProblem(message.problem);
break;
}

case 'online-judge-env': {
setOnlineJudgeEnv(message.value);
Expand Down
Loading

0 comments on commit fca85d4

Please sign in to comment.