<commit_before>var fs = require('fs');

var _ = require('underscore');
var log = require('loglevel');
var sprintf = require('sprintf-js').sprintf;

var chalk = require('../chalk');
var core = require('../core');
var h = require('../helper');
var queue = require('../queue');

var cmd = {
  command: 'submission [keyword]',
  desc:    'retrieve earlier submission by name or index',
  builder: {
    all: {
      alias:    'a',
      type:     'boolean',
      default:  false,
      describe: 'Retrieve for all problems'
    },
    outdir: {
      alias:    'o',
      type:     'string',
      describe: 'Where to save the submissions',
      default:  '.'
    },
    extra: {
      alias:    'x',
      type:     'boolean',
      default:  false,
      describe: 'Provide extra problem details in submission file'
    }
  }
};

function onTaskDone(e, msg, problem, cb) {
  // NOTE: msg color means different purpose:
  // - red: error
  // - green: accepted, fresh download
  // - yellow: not ac-ed, fresh download
  // - white: existed already, skip download
  log.info(sprintf('[%3d] %-60s %s',
        problem.id,
        problem.name,
        (e ? chalk.red('ERROR: ' + (e.msg || e)) : msg)
        ));
  if (cb) cb(e);
}

function onTaskRun(argv, problem, cb) {
  var done = _.partial(onTaskDone, _, _, problem, cb);

  if (argv.extra) {
    // have to get problem details, e.g. problem description.
    core.getProblem(problem.id, function(e, problem) {
      if (e) return done(e);

      exportSubmission(argv, problem, done);
    });
  } else {
    exportSubmission(argv, problem, done);
  }
}

function exportSubmission(argv, problem, cb) {
  core.getSubmissions(problem, function(e, submissions) {
    if (e) return cb(e);
    if (submissions.length === 0) return cb('no submissions?');

    // find the latest accepted one
    var submission = _.find(submissions, function(x) {
      // TODO: select by lang?
      return x.status_display === 'Accepted';
    });

    // if no accepted, use the latest non-accepted one
    submission = submission || submissions[0];

    var filename = sprintf('%s/%d.%s.%s.%s%s',
        argv.outdir,
        problem.id,
        problem.key,
        submission.id,
        problem.state,
        h.langToExt(submission.lang));

    // skip the existing cached submissions
    if (fs.existsSync(filename)) {
      return cb(null, chalk.underline(filename));
    }

    core.getSubmission(submission, function(e, submission) {
      if (e) return cb(e);

      problem.code = submission.code;
      core.exportProblem(problem, filename, !argv.extra);
<commit_msg>1.Add an option -l for submission command which allows user to retrive recent submission in requried programming language, if no submissino in required language, an error will be printed. If no language is specified, program will retrive retrive most recent accepted submission. 2. change behavior: when user specify a language and there are no accepted submission, program will return the most recent non accepted submission in this language 3. bug-fix: problem state is related to the latest submission regardless which language we use. When user specified a language, the status should be related to the latest submission in this language rather than problem<commit_after>var fs = require('fs');

var _ = require('underscore');
var log = require('loglevel');
var sprintf = require('sprintf-js').sprintf;

var chalk = require('../chalk');
var core = require('../core');
var h = require('../helper');
var queue = require('../queue');

var cmd = {
  command: 'submission [keyword]',
  desc:    'retrieve earlier submission by name or index',
  builder: {
    all: {
      alias:    'a',
      type:     'boolean',
      default:  false,
      describe: 'Retrieve for all problems'
    },
    outdir: {
      alias:    'o',
      type:     'string',
      describe: 'Where to save the submissions',
      default:  '.'
    },
    extra: {
      alias:    'x',
      type:     'boolean',
      default:  false,
      describe: 'Provide extra problem details in submission file'
    },
    lang: {
      alias:    'l',
      type:     'string',
      default:  'all',
      describe: 'Programming language used for previous submission'
    }
  }
};

function onTaskDone(e, msg, problem, cb) {
  // NOTE: msg color means different purpose:
  // - red: error
  // - green: accepted, fresh download
  // - yellow: not ac-ed, fresh download
  // - white: existed already, skip download
  log.info(sprintf('[%3d] %-60s %s',
        problem.id,
        problem.name,
        (e ? chalk.red('ERROR: ' + (e.msg || e)) : msg)
        ));
  if (cb) cb(e);
}

function onTaskRun(argv, problem, cb) {
  var done = _.partial(onTaskDone, _, _, problem, cb);

  if (argv.extra) {
    // have to get problem details, e.g. problem description.
    core.getProblem(problem.id, function(e, problem) {
      if (e) return done(e);

      exportSubmission(argv, problem, done);
    });
  } else {
    exportSubmission(argv, problem, done);
  }
}

function exportSubmission(argv, problem, cb) {
  core.getSubmissions(problem, function(e, submissions) {
    if (e) return cb(e);
    if (submissions.length === 0) return cb('no submissions?');

    // get obj list contain required filetype
    var submissionInTargetType = _.filter(submissions, function(x) {
      return argv.lang === 'all' || argv.lang === x.lang;
    });
    if (submissionInTargetType.length === 0) {
      return cb("No previous submission in required language.");
    }
    var submission = _.find(submissionInTargetType, function(x) {
      return x.status_display === 'Accepted';
    });

    var submissionState = submission === undefined ? 'notac' : 'ac';

    // if no accepted, use the latest non-accepted one
    submission = submission || submissionInTargetType[0];

    var filename = sprintf('%s/%d.%s.%s.%s%s',
        argv.outdir,
        problem.id,
        problem.key,
        submission.id,
        submissionState,
        h.langToExt(submission.lang));

    // skip the existing cached submissions
    if (fs.existsSync(filename)) {
      return cb(null, chalk.underline(filename));
    }

    core.getSubmission(submission, function(e, submission) {
      if (e) return cb(e);

      problem.code = submission.code;
      core.exportProblem(problem, filename, !argv.extra);

{'fraction_non_alphanumeric': 0.13608442274373916, 'fraction_numerical': 0.002205071664829107, 'mean_word_length': 2.759621077560687, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3485235', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1890, 'n_words': 586}