提交 c5aa4f8b 编写于 作者: J Jason Park

Move all the compiling/running parts to tracers repository

上级 75308980
const path = require('path');
const fs = require('fs');
const categories = fs.readdirSync(path.resolve(__dirname, '..', 'src', 'backend', 'public', 'algorithms'));
for (const category of categories) {
if (category.startsWith('.')) continue;
const algorithms = fs.readdirSync(path.resolve(__dirname, '..', 'src', 'backend', 'public', 'algorithms', category));
for (const algorithm of algorithms) {
if (algorithm.startsWith('.')) continue;
const dir = path.resolve(__dirname, '..', 'src', 'backend', 'public', 'algorithms', category, algorithm);
try {
fs.renameSync(path.resolve(dir, 'desc.md'), path.resolve(dir, 'README.md'));
} catch (e) {
}
}
}
\ No newline at end of file
文件模式从 100644 更改为 100755
......@@ -3,14 +3,12 @@
"version": "2.0.0",
"description": "Algorithm Visualizer",
"scripts": {
"postinstall": "docker pull openjdk:8 && docker pull gcc:8.1",
"dev": "NODE_ENV=development node bin/www",
"start": "NODE_ENV=production node bin/www",
"dev": "NODE_ENV=development ./bin/www",
"start": "NODE_ENV=production ./bin/www",
"build": "npm run build:frontend && npm run build:backend",
"build:frontend": "NODE_ENV=production node ./node_modules/webpack/bin/webpack --bail --progress --config webpack.frontend.config.js",
"build:backend": "NODE_ENV=production node ./node_modules/webpack/bin/webpack --bail --progress --config webpack.backend.config.js",
"lint": "./node_modules/.bin/eslint --fix src",
"publish": "npm run lint && npm run build && git add . && git commit && git push"
"lint": "./node_modules/.bin/eslint --fix src"
},
"repository": {
"type": "git",
......
......@@ -9,51 +9,36 @@ import { CompileError, RuntimeError } from '/common/error';
const router = express.Router();
const repoPath = path.resolve(__dirname, '..', 'public', 'tracers');
const getLibsPath = (...args) => path.resolve(repoPath, 'libs', ...args);
const getCodesPath = (...args) => path.resolve(__dirname, '..', 'public', 'codes', ...args);
const buildRelease = release => (
fs.pathExistsSync(repoPath) ?
execute(`git fetch && ! git diff-index --quiet ${release.target_commitish}`, repoPath) :
execute(`git clone git@github.com:algorithm-visualizer/tracers ${repoPath}`, __dirname)
).then(() => execute(`git reset --hard ${release.target_commitish} && npm install && npm run build`, repoPath));
).then(() => execute(`git reset --hard ${release.target_commitish} && npm install && npm run build && ./bin/build`, repoPath));
GitHubApi.getLatestRelease('algorithm-visualizer', 'tracers').then(buildRelease);
// TODO: build release when webhooked
const getJsWorker = (req, res, next) => {
res.sendFile(getLibsPath('js', 'tracers.js'));
res.sendFile(path.resolve(repoPath, 'src', 'languages', 'js', 'tracers', 'build', 'tracers.js'));
};
const executeInDocker = (imageId, ext, srcPath, command) => {
// TODO: memory limit + time limit + space limit?
const libsPath = getLibsPath(ext);
const dockerCommand = [
`docker run --rm`,
'-w=/usr/judge',
`-v=${libsPath}:/usr/bin/tracers:ro`,
`-v=${srcPath}:/usr/judge:rw`,
`-e MAX_TRACES=${1e6} -e MAX_TRACERS=${1e2}`,
imageId,
].join(' ');
return execute(`${dockerCommand} ${command}`, repoPath, { stdout: null, stderr: null });
};
const trace = ({ ext, imageId, compileCommand, runCommand }) => (req, res, next) => {
const trace = lang => (req, res, next) => {
const { code } = req.body;
const srcPath = getCodesPath(uuid.v4());
fs.outputFile(path.resolve(srcPath, `Main.${ext}`), code)
.then(() => executeInDocker(imageId, ext, srcPath, compileCommand)
const tempPath = getCodesPath(uuid.v4());
fs.outputFile(path.resolve(tempPath, `Main.${lang}`), code)
.then(() => execute(`LANG=${lang} TEMP_PATH=${tempPath} ./bin/compile`, repoPath, { stdout: null, stderr: null })
.catch(error => {
throw new CompileError(error);
}))
.then(() => executeInDocker(imageId, ext, srcPath, runCommand)
.then(() => execute(`LANG=${lang} TEMP_PATH=${tempPath} ./bin/run`, repoPath, { stdout: null, stderr: null })
.catch(error => {
throw new RuntimeError(error);
}))
.then(() => res.sendFile(path.resolve(srcPath, 'traces.json')))
.then(() => res.sendFile(path.resolve(tempPath, 'traces.json')))
.catch(next)
.finally(() => fs.remove(srcPath));
.finally(() => fs.remove(tempPath));
};
......@@ -61,19 +46,9 @@ router.route('/js')
.get(getJsWorker);
router.route('/java')
.post(trace({
ext: 'java',
imageId: 'openjdk:8',
compileCommand: 'javac -cp /usr/bin/tracers/tracers.jar Main.java',
runCommand: 'java -cp /usr/bin/tracers/tracers.jar:. Main',
}));
.post(trace('java'));
router.route('/cpp')
.post(trace({
ext: 'cpp',
imageId: 'gcc:8.1',
compileCommand: 'g++ Main.cpp -o Main -O2 -std=c++11 -L/usr/bin/tracers/lib -l:tracers.a -I/usr/bin/tracers/include',
runCommand: './Main',
}));
.post(trace('cpp'));
export default router;
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册