<commit_before>/* jshint ignore:start */
'use strict';

const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');
const fse = require('fs-extra');
const child_process = require('child_process');
const Zip = require('adm-zip');

BbPromise.promisifyAll(fse);

class ServerlessPythonRequirements {
  packVendorHelper() {
    this.serverless.cli.log('Packaging Python requirements helper...');

    return fse.copyAsync(
      path.resolve(__dirname, 'requirements.py'),
      path.join(this.serverless.config.servicePath, 'requirements.py'));
  };

  installRequirements() {
    if (!fse.existsSync(path.join(this.serverless.config.servicePath, 'requirements.txt'))) {
      return BbPromise.resolve();
    }

    this.serverless.cli.log('Packaging required Python packages...');

    return new BbPromise((resolve, reject) => {
      let cmd = 'pip';
      let options = [
        '--isolated', 'install',
        '-t', '.requirements',
        '-r', 'requirements.txt',
      ];
      if (this.custom.dockerizePip) {
        cmd = 'docker';
        options = [
          'run', '--rm',
          '-v', `${this.serverless.config.servicePath}:/var/task:z`,
          'lambci/lambda:build-python2.7', 'pip',
        ].concat(options);
      }
      const res = child_process.spawnSync(cmd, options);
      if (res.error) {
        return reject(res.error);
      }
      if (res.status != 0) {
        return reject(res.stderr);
      }
      resolve();
    });
  };

  packRequirements() {
    return this.installRequirements().then(() => {
      return new BbPromise((resolve, reject) => {
        if (this.custom.zipImport) {
          const zip = new Zip();
          zip.addLocalFolder('.requirements', '');
          zip.writeZip('.requirements.zip');
          fse.remove('.requirements', (err) => err?reject():resolve());
        } else resolve();
      });
    });
  }

  cleanup() {
    const artifacts = ['requirements.py'];
    if (this.custom.zipImport)
      artifacts.push('.requirements.zip')
    else
      artifacts.push('.requirements')

    return BbPromise.all(_.map(artifacts, (artifact) =>
      fse.removeAsync(path.join(this.serverless.config.servicePath, artifact))));;
  };

  serve() {
    const port = this.options.port || 5000;

    return new BbPromise((resolve, reject) => {
      child_process.spawnSync('python', [
        path.resolve(__dirname, 'serve.py'),
        this.serverless.config.servicePath,
        this.wsgiApp,
        port
      ], { stdio: 'inherit' });
      resolve();
    });
  };

  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options;
    this.custom = this.serverless.service.custom && this.serverless.service.custom.pythonRequirements || {};

    this.commands = {
      'requirements': {
        commands: {
          'clean': {
            usage: 'Remove .requirements and requirements.py',
            lifecycleEvents: [
              'clean',
            ],
          },
          'install': {
            usage: 'install requirements manually',
            lifecycleEvents: [
              'install',
            ],
          },
        },
      },
    };

    this.hooks = {
      'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
        .then(this.packVendorHelper)
        .then(this.packRequirements),<commit_msg>remove unused serve function from wsgi plugin<commit_after>/* jshint ignore:start */
'use strict';

const BbPromise = require('bluebird');
const _ = require('lodash');
const path = require('path');
const fse = require('fs-extra');
const child_process = require('child_process');
const Zip = require('adm-zip');

BbPromise.promisifyAll(fse);

class ServerlessPythonRequirements {
  packVendorHelper() {
    this.serverless.cli.log('Packaging Python requirements helper...');

    return fse.copyAsync(
      path.resolve(__dirname, 'requirements.py'),
      path.join(this.serverless.config.servicePath, 'requirements.py'));
  };

  installRequirements() {
    if (!fse.existsSync(path.join(this.serverless.config.servicePath, 'requirements.txt'))) {
      return BbPromise.resolve();
    }

    this.serverless.cli.log('Packaging required Python packages...');

    return new BbPromise((resolve, reject) => {
      let cmd = 'pip';
      let options = [
        '--isolated', 'install',
        '-t', '.requirements',
        '-r', 'requirements.txt',
      ];
      if (this.custom.dockerizePip) {
        cmd = 'docker';
        options = [
          'run', '--rm',
          '-v', `${this.serverless.config.servicePath}:/var/task:z`,
          'lambci/lambda:build-python2.7', 'pip',
        ].concat(options);
      }
      const res = child_process.spawnSync(cmd, options);
      if (res.error) {
        return reject(res.error);
      }
      if (res.status != 0) {
        return reject(res.stderr);
      }
      resolve();
    });
  };

  packRequirements() {
    return this.installRequirements().then(() => {
      return new BbPromise((resolve, reject) => {
        if (this.custom.zipImport) {
          const zip = new Zip();
          zip.addLocalFolder('.requirements', '');
          zip.writeZip('.requirements.zip');
          fse.remove('.requirements', (err) => err?reject():resolve());
        } else resolve();
      });
    });
  }

  cleanup() {
    const artifacts = ['requirements.py'];
    if (this.custom.zipImport)
      artifacts.push('.requirements.zip')
    else
      artifacts.push('.requirements')

    return BbPromise.all(_.map(artifacts, (artifact) =>
      fse.removeAsync(path.join(this.serverless.config.servicePath, artifact))));;
  };

  constructor(serverless, options) {
    this.serverless = serverless;
    this.options = options;
    this.custom = this.serverless.service.custom && this.serverless.service.custom.pythonRequirements || {};

    this.commands = {
      'requirements': {
        commands: {
          'clean': {
            usage: 'Remove .requirements and requirements.py',
            lifecycleEvents: [
              'clean',
            ],
          },
          'install': {
            usage: 'install requirements manually',
            lifecycleEvents: [
              'install',
            ],
          },
        },
      },
    };

    this.hooks = {
      'before:deploy:createDeploymentArtifacts': () => BbPromise.bind(this)
        .then(this.packVendorHelper)
        .then(this.packRequirements),
{'fraction_non_alphanumeric': 0.15743034055727553, 'fraction_numerical': 0.0015479876160990713, 'mean_word_length': 2.4094986807387864, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '840219', 'n_tokens_mistral': 1952, 'n_tokens_neox': 1895, 'n_words': 329}