mr_dot_convict

parser.json.bk

May 29th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const app = require('express')(),
  2.       bodyParser = require('body-parser'),
  3.       fs = require('file-system'),
  4.       url = require('url'),
  5.       sh = require('shelljs'),
  6.       colors = require('colors'),
  7.       os = require('os');
  8.  
  9. colors.setTheme({
  10.    debug : 'cyan',
  11.    display : 'white',
  12.    error: 'red',
  13.    info: 'green',
  14.    warn: 'yellow'
  15. });
  16.  
  17. const user_name = os.userInfo().username.toString(); // get the user_name from os for /home/${user_name}/...
  18. const cur_working_dir = sh.pwd(); // object
  19. console.log(colors.debug(`currently in dir : ${cur_working_dir.toString()}`));
  20.  
  21. var echo_header_done = false;
  22.  
  23. const port = 8080; // port to listen
  24. app.use(bodyParser.json());
  25.  
  26. app.post('/', (req, res) => {
  27.    const data = req.body; // main body
  28.    // console.log(colors.debug(`${JSON.stringify(data, null, 4)}`)); // show main json body
  29.  
  30.    var online_judge = url.parse(data.url).hostname;
  31.    var contest_name = data.group.split(' ').join('_');
  32.    var problem_name = data.name.split(' ').join('_');
  33.    var test_count = data.tests.length;
  34.    var final_path = online_judge + '/' + contest_name + '/' + problem_name
  35.  
  36.    // console.log(colors.debug(`${online_judge}`));
  37.  
  38.    //Display
  39.    if (!echo_header_done) {
  40.       echo_header_done = true;
  41.       console.log(colors.display(` ======== Welcome to cp_parser_cli ======== `));
  42.       console.log(colors.info.underline.bold(` *** Round name : ${contest_name} (${online_judge}) *** `));
  43.    }
  44.    console.log('\n');
  45.    console.log(colors.info.bold(`Downloading problem : ${problem_name} ...`));
  46.    console.log(colors.info.italic(`${test_count} sample test case(s) found`));
  47.  
  48.    // console.log(colors.debug(`${final_path}`));
  49.  
  50.    // making folders and subfolders ---> online_judges/contest_name/problem_name
  51.    try {
  52.       if (!fs.existsSync(online_judge)) {
  53.          fs.mkdirSync(online_judge);
  54.       }
  55.       if (!fs.existsSync(online_judge + '/' + contest_name)) {
  56.          fs.mkdirSync(online_judge + '/' + contest_name);
  57.       }
  58.       if (!fs.existsSync(online_judge + '/' + contest_name + '/' + problem_name)) {
  59.          fs.mkdirSync(online_judge + '/' + contest_name + '/' + problem_name);
  60.       }
  61.    } catch (err) {
  62.       console.log(colors.error(`${err}`));
  63.    }
  64.  
  65.    // pconfig json object to be read by tester
  66.    var pconfig_obj = {
  67.       test_count : test_count,
  68.       time_limit : parseFloat(data.timeLimit) / 1000.0,
  69.       mem_limit : parseInt(data.memoryLimit) * 1000
  70.    };
  71.  
  72.    //convert json to string and write into the file
  73.    var pconfig_string = JSON.stringify(pconfig_obj);
  74.    fs.writeFile(final_path + '/pconfig.json', pconfig_string, 'utf8');
  75.  
  76.    //writing test cases input and output as input + 'idx' ...
  77.    for (var idx = 1; idx <= test_count; idx++) {
  78.       var input_string = data.tests[idx - 1].input;
  79.       var output_string = data.tests[idx - 1].output;
  80.  
  81.       fs.writeFile(final_path + "/input" + idx, input_string, (err) => {
  82.          if (err) console.log(colors.error(`${err}`));
  83.       });
  84.       fs.writeFile(final_path + "/output" + idx, output_string, (err) => {
  85.          if (err) console.log(colors.error(`${err}`));
  86.       });
  87.    }
  88.  
  89.  
  90.    //get the global config file to copy template file
  91.    var parser_config_file = '/home/' + user_name + '/.config/parser/parser.json';
  92.    // console.log(colors.debug(`parser_config_file : ${parser_config_file}`));
  93.    try {
  94.       //check if parser config file exists
  95.       if (!fs.existsSync(parser_config_file)) {
  96.          const cpp_template =
  97.             "#include <bits/stdc++.h>\nusing namespace std;\n\nint main() {\n\treturn 0;\n}\n";
  98.  
  99.          fs.writeFile(final_path + '/' + problem_name + '.cpp', cpp_template, (err) => {
  100.             if (err) console.log(colors.error(`${err}`));
  101.          });
  102.          console.log(colors.warn.underline(`Warning : config path doesnt exist, default file created`));
  103.       }
  104.       else { // exists so copy the template file from the given location
  105.          var parser_config_json = require(parser_config_file);
  106.          // console.log(colors.debug(`${parser_config_json.Template}`)); // this contains location of template file
  107.  
  108.          fs.copyFile(parser_config_json.Template, final_path + '/' + problem_name + '.cpp', (err) => {
  109.             if (err) throw err;
  110.             // console.log(colors.debug(`Template file copied`));
  111.          });
  112.       }
  113.    } catch (err) {
  114.       console.log(colors.error(`${err}`));
  115.    }
  116.    res.sendStatus(200);
  117. });
  118.  
  119. app.listen(port, err => {
  120.    if (err) {
  121.       console.log(colors.error(`${err}`));
  122.       process.exit(1);
  123.    }
  124.    console.log(colors.display.italic(`Listening on port ${port}\n`));
  125. });
Add Comment
Please, Sign In to add comment