<commit_before>import { CliTrackerController } from 'azk/cli/cli_tracker_controller';
import { Helpers } from 'azk/cli/helpers';
import { _, config, lazy_require } from 'azk';
import { defer, asyncUnsubscribe } from 'azk/utils/promises';
import { subscribe } from 'azk/utils/postal';

var lazy = lazy_require({
  Manifest: ['azk/manifest'],
  docker  : ['azk/docker', 'default'],
});

class Shell extends CliTrackerController {
  index() {
    var { options } = this.normalized_params;
    var args = this.normalized_params.arguments;

    var _subscription = subscribe('docker.pull.status', (data) => {
      Helpers.newPullProgressBar(this.ui)(data);
    });

    return asyncUnsubscribe(this, _subscription, function* () {
      var dir = this.cwd;
      var manifest, system;

      yield Helpers.requireAgent(this.ui);

      if (options.image) {
        // Arbitrary image
        manifest = lazy.Manifest.makeFake(dir, options.image);
        system   = manifest.systemDefault;
      } else {
        manifest = new lazy.Manifest(dir, true);
        Helpers.manifestValidate(this.ui, manifest);

        system   = manifest.systemDefault;
        if (args.system) {
          system = manifest.system(args.system, true);
        }
      }

      // Use or not tty?
      var shell_args = args['shell-args'];
      var tty_default = options.tty || (_.isEmpty(options.command) && !_.isEmpty(shell_args));
      var tty = (options['no-tty']) ? (options.tty || false) : tty_default;

      var stdin = this.ui.stdin();
      stdin.custom_pipe = () => { };

      var docker_options = {
        interactive: tty,
        pull   : this.ui.stdout(),
        stdout : this.ui.stdout(),
        stderr : this.ui.stderr(),
        stdin  : stdin,
        workdir: options.cwd || null,
      };

      // Support extra envs, ports and mount volumes
      docker_options.envs       = this._parse_option(options.env  , /.+=.+/, '=', 'invalid_env');
      docker_options.shell_term = process.env.TERM;
      docker_options.mounts     = this._parse_option(options.mount, /.+:.+:?.*/, ':', 'invalid_mount', 1, (opts) => {
        return { type: (opts[2] ? opts[1] : 'path'), value: (opts[2] ? opts[2] : opts[0]) };
      });

      var cmd = [options.shell];
      if (!_.isEmpty(options.command)) {
        cmd.push("-c", options.command);
      } else if (!_.isEmpty(shell_args)) {
        cmd = cmd.concat(shell_args);
      }
      cmd = _.compact(cmd);

      // Remove container before run
      var is_remove = !options['no-remove'] ? config("docker:remove_container") : !options['no-remove'];
      docker_options = _.merge(docker_options, {
        build_force    : options.rebuild || false,
        provision_force: (options.rebuild ? true : options.reprovision) || false,
        remove         : is_remove,
        command        : cmd,
      });

      var result = defer((resolver, reject) => {
        var escape = (key, container, next) => {
          if (key === ".") {
            process.nextTick(() => {
              lazy.docker.getContainer(container).stop({ t: 5000 }).catch(reject);
            });
            return true;
          } else if (key === "?") {
            this.ok("coming soon...");
            process.nextTick(() => next());
            return true;
          }
          return false;
        };
<commit_msg>[Cli] Fixing `azk shell` logic<commit_after>import { CliTrackerController } from 'azk/cli/cli_tracker_controller';
import { Helpers } from 'azk/cli/helpers';
import { _, config, lazy_require } from 'azk';
import { defer, asyncUnsubscribe } from 'azk/utils/promises';
import { subscribe } from 'azk/utils/postal';

var lazy = lazy_require({
  Manifest: ['azk/manifest'],
  docker  : ['azk/docker', 'default'],
});

class Shell extends CliTrackerController {
  index() {
    var { options } = this.normalized_params;
    var args = this.normalized_params.arguments;

    var _subscription = subscribe('docker.pull.status', (data) => {
      Helpers.newPullProgressBar(this.ui)(data);
    });

    return asyncUnsubscribe(this, _subscription, function* () {
      var dir = this.cwd;
      var manifest, system;

      yield Helpers.requireAgent(this.ui);

      if (options.image) {
        // Arbitrary image
        manifest = lazy.Manifest.makeFake(dir, options.image);
        system   = manifest.systemDefault;
      } else {
        manifest = new lazy.Manifest(dir, true);
        Helpers.manifestValidate(this.ui, manifest);

        system   = manifest.systemDefault;
        if (args.system) {
          system = manifest.system(args.system, true);
        }
      }

      // Use or not tty?
      var shell_args = args['shell-args'];
      var tty_default = options.tty || (_.isEmpty(options.command) && _.isEmpty(shell_args));
      var tty = (options['no-tty']) ? (options.tty || false) : tty_default;

      var stdin = this.ui.stdin();
      stdin.custom_pipe = () => { };

      var docker_options = {
        interactive: tty,
        pull   : this.ui.stdout(),
        stdout : this.ui.stdout(),
        stderr : this.ui.stderr(),
        stdin  : stdin,
        workdir: options.cwd || null,
      };

      // Support extra envs, ports and mount volumes
      docker_options.envs       = this._parse_option(options.env  , /.+=.+/, '=', 'invalid_env');
      docker_options.shell_term = process.env.TERM;
      docker_options.mounts     = this._parse_option(options.mount, /.+:.+:?.*/, ':', 'invalid_mount', 1, (opts) => {
        return { type: (opts[2] ? opts[1] : 'path'), value: (opts[2] ? opts[2] : opts[0]) };
      });

      var cmd = [options.shell];
      if (!_.isEmpty(options.command)) {
        cmd.push("-c", options.command);
      } else if (!_.isEmpty(shell_args)) {
        cmd = [shell_args];
      }

      // Remove container before run
      var is_remove = !options['no-remove'] ? config("docker:remove_container") : !options['no-remove'];
      docker_options = _.merge(docker_options, {
        build_force    : options.rebuild || false,
        provision_force: (options.rebuild ? true : options.reprovision) || false,
        remove         : is_remove,
        command        : cmd,
      });

      var result = defer((resolver, reject) => {
        var escape = (key, container, next) => {
          if (key === ".") {
            process.nextTick(() => {
              lazy.docker.getContainer(container).stop({ t: 5000 }).catch(reject);
            });
            return true;
          } else if (key === "?") {
            this.ok("coming soon...");
            process.nextTick(() => next());
            return true;
          }
          return false;
        };

{'fraction_non_alphanumeric': 0.15415791053737615, 'fraction_numerical': 0.003002101471029721, 'mean_word_length': 2.3149253731343284, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, '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': '1631082', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2004, 'n_words': 422}