<commit_before>let async = require('../../utils/async_extend.js');
const shelljs = require('shelljs');
const path = require('path');

class Vyper {

  constructor(embark, options) {
    this.logger = embark.logger;
    this.events = embark.events;
    this.contractDirectories = options.contractDirectories;

    embark.registerCompiler(".py", this.compile_vyper.bind(this));
  }

  compile_vyper(contractFiles, cb) {
    let self = this;
    if (!contractFiles || !contractFiles.length) {
      return cb();
    }
    self.logger.info("compiling Vyper contracts...");
    const compiled_object = {};
    async.each(contractFiles,
      function (file, fileCb) {
        const className = path.basename(file.filename).split('.')[0];
        compiled_object[className] = {};
        async.parallel([
          function getByteCode(paraCb) {
            shelljs.exec(`vyper ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
              if (stderr) {
                return paraCb(stderr);
              }
              if (code !== 0) {
                return paraCb(`Vyper exited with error code ${code}`);
              }
              if (!stdout) {
                return paraCb('Execution returned no bytecode');
              }
              const byteCode = stdout.replace(/\n/g, '');
              compiled_object[className].runtimeBytecode = byteCode;
              compiled_object[className].realRuntimeBytecode = byteCode;
              compiled_object[className].code = byteCode;
              paraCb();
            });
          },
          function getABI(paraCb) {
            shelljs.exec(`vyper -f json ${file.filename}`, {silent: true}, (code, stdout, stderr) => {
              if (stderr) {
                return paraCb(stderr);
              }
              if (code !== 0) {
                return paraCb(`Vyper exited with error code ${code}`);
              }
              if (!stdout) {
                return paraCb('Execution returned no ABI');
              }
              let ABI = [];
              try {
                ABI = JSON.parse(stdout.replace(/\n/g, ''));
              } catch (e) {
                return paraCb('ABI is not valid JSON');
              }
              compiled_object[className].abiDefinition = ABI;
              paraCb();
            });
          }
        ], fileCb);
      },
      function (err) {
        cb(err, compiled_object);
      });
  }

}

module.exports = Vyper;<commit_msg>move vyper contract compile to static util function<commit_after>let async = require('../../utils/async_extend.js');
const shelljs = require('shelljs');
const path = require('path');

class Vyper {

  constructor(embark, options) {
    this.logger = embark.logger;
    this.events = embark.events;
    this.contractDirectories = options.contractDirectories;

    embark.registerCompiler(".py", this.compile_vyper.bind(this));
  }

  static compileVyperContract(filename, compileABI, callback) {
    const params = compileABI ?  '-f json ' : '';
    shelljs.exec(`vyper ${params}${filename}`, {silent: true}, (code, stdout, stderr) => {
      if (stderr) {
        return callback(stderr);
      }
      if (code !== 0) {
        return callback(`Vyper exited with error code ${code}`);
      }
      if (!stdout) {
        return callback('Execution returned no result');
      }
      callback(null, stdout.replace(/\n/g, ''));
    });
  }

  compile_vyper(contractFiles, cb) {
    let self = this;
    if (!contractFiles || !contractFiles.length) {
      return cb();
    }
    self.logger.info("compiling Vyper contracts...");
    const compiled_object = {};
    async.each(contractFiles,
      function (file, fileCb) {
        const className = path.basename(file.filename).split('.')[0];
        compiled_object[className] = {};
        async.parallel([
          function getByteCode(paraCb) {
            Vyper.compileVyperContract(file.filename, false, (err, byteCode) => {
              if (err) {
                return paraCb(err);
              }
              compiled_object[className].runtimeBytecode = byteCode;
              compiled_object[className].realRuntimeBytecode = byteCode;
              compiled_object[className].code = byteCode;
              paraCb();
            });
          },
          function getABI(paraCb) {
            Vyper.compileVyperContract(file.filename, true, (err, ABIString) => {
              if (err) {
                return paraCb(err);
              }
              let ABI = [];
              try {
                ABI = JSON.parse(ABIString);
              } catch (e) {
                return paraCb('ABI is not valid JSON');
              }
              compiled_object[className].abiDefinition = ABI;
              paraCb();
            });
          }
        ], fileCb);
      },
      function (err) {
        cb(err, compiled_object);
      });
  }
}

module.exports = Vyper;
{'fraction_non_alphanumeric': 0.13287140819237825, 'fraction_numerical': 0.0010189525168127166, 'mean_word_length': 1.8419224088013897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2853641', 'n_tokens_mistral': 1396, 'n_tokens_neox': 1362, 'n_words': 280}