<commit_before>import { _, fs, t, path, isBlank } from 'azk';
import { async, defer, lazy_require } from 'azk';
import { ManifestError, NoInternetConnection, LostInternetConnection } from 'azk/utils/errors';
import { net } from 'azk/utils';
import Utils from 'azk/utils';
import { default as tracker } from 'azk/utils/tracker';

var AVAILABLE_PROVIDERS = ["docker", "dockerfile"];
var default_tag      = "latest";

var lazy = lazy_require({
  DImage: ['azk/docker', 'Image'],
  docker: ['azk/docker', 'default'],
  Syncronizer: ['docker-registry-downloader'],
});

export class Image {
  constructor(options) {
    this.system = options.system || { image_name_suggest: null };

    // Extract provider information
    // 2. i.e.: { docker: 'azukiapp/azktcl:0.0.2' }
    // 3. i.e.: { provider: 'dockerfile', repository: 'azukiapp/azktcl' }
    this.provider = this._parse_provider(options);

    if (options.hasOwnProperty(this.provider)) {
      if (this.provider === 'docker') {
        this.name = options[this.provider];
      } else if (this.provider === 'dockerfile') {
        this.path = options[this.provider];
      }
    } else {
      if (this.provider === 'dockerfile') {
        this.path = options.path;
      }
      this.repository = options.repository;
      this.tag        = this.tag || options.tag || default_tag;
    }

    if (_.isEmpty(this.name)) {
      this.name = `${this.system.image_name_suggest}:${this.tag}`;
    }
  }

  check() {
    return defer((_resolve, _reject, notify) => {
      notify({ type: "action", context: "image", action: "check_image" });
      return lazy.docker.findImage(this.name);
    });
  }

  pull(options, stdout) {
    return async(this, function* (notify) {
      // split docker namespace and docker repository
      var namespace   = '';
      var repository  = '';
      var splited = this.repository.split('\/');
      if (splited.length === 2) {
        namespace   = splited[0];
        repository  = splited[1];
      } else {
        namespace   = 'library';
        repository  = this.repository;
      }

      // check if exists local image
      this.repository = namespace + '/' + repository;
      var image = yield this.check();

      // check official docker image without "library/" namespace
      if (isBlank(image) && namespace === 'library') {
        this.repository = repository;
        image = yield this.check();
      }

      // download from registry
      if (isBlank(image) || options.build_force) {
        this.repository = namespace + '/' + repository;
        notify({ type: "action", context: "image", action: "pull_image", data: this });

        var registry_result;
        var output;

        var currentOnline = yield net.isOnlineCheck();
        if ( !currentOnline ) {
          throw new NoInternetConnection();
        }

        // get size and layers count
        try {
          registry_result = yield this.getDownloadInfo(
            lazy.docker.modem,
            namespace,
            repository,
            this.tag);

          output = _.isObject(stdout) && stdout;
          // docker pull
          image = yield lazy.docker.pull(this.repository, this.tag, output, registry_result);
        } catch (err) {
          output = (err || err).toString();
          throw new LostInternetConnection('  ' + output);
        }

        yield this._track('pull');
      }
      return this.check();
    });
  }

  getDownloadInfo(dockerode_modem, namespace, repository, repo_tag) {
    return async(this, function* (notify) {

      var docker_socket   = { dockerode_modem: dockerode_modem };
      var request_options = {
        timeout: 10000,
        maxAttempts: 3,
        retryDelay: 500
      };

      var syncronizer = new lazy.Syncronizer(docker_socket, request_options);
      var tag = repo_tag;<commit_msg>[fix_registry_downloader] Fixing the error in logic conversion to string.<commit_after>import { _, fs, t, path, isBlank } from 'azk';
import { async, defer, lazy_require } from 'azk';
import { ManifestError, NoInternetConnection, LostInternetConnection } from 'azk/utils/errors';
import { net } from 'azk/utils';
import Utils from 'azk/utils';
import { default as tracker } from 'azk/utils/tracker';

var AVAILABLE_PROVIDERS = ["docker", "dockerfile"];
var default_tag      = "latest";

var lazy = lazy_require({
  DImage: ['azk/docker', 'Image'],
  docker: ['azk/docker', 'default'],
  Syncronizer: ['docker-registry-downloader'],
});

export class Image {
  constructor(options) {
    this.system = options.system || { image_name_suggest: null };

    // Extract provider information
    // 2. i.e.: { docker: 'azukiapp/azktcl:0.0.2' }
    // 3. i.e.: { provider: 'dockerfile', repository: 'azukiapp/azktcl' }
    this.provider = this._parse_provider(options);

    if (options.hasOwnProperty(this.provider)) {
      if (this.provider === 'docker') {
        this.name = options[this.provider];
      } else if (this.provider === 'dockerfile') {
        this.path = options[this.provider];
      }
    } else {
      if (this.provider === 'dockerfile') {
        this.path = options.path;
      }
      this.repository = options.repository;
      this.tag        = this.tag || options.tag || default_tag;
    }

    if (_.isEmpty(this.name)) {
      this.name = `${this.system.image_name_suggest}:${this.tag}`;
    }
  }

  check() {
    return defer((_resolve, _reject, notify) => {
      notify({ type: "action", context: "image", action: "check_image" });
      return lazy.docker.findImage(this.name);
    });
  }

  pull(options, stdout) {
    return async(this, function* (notify) {
      // split docker namespace and docker repository
      var namespace   = '';
      var repository  = '';
      var splited = this.repository.split('\/');
      if (splited.length === 2) {
        namespace   = splited[0];
        repository  = splited[1];
      } else {
        namespace   = 'library';
        repository  = this.repository;
      }

      // check if exists local image
      this.repository = namespace + '/' + repository;
      var image = yield this.check();

      // check official docker image without "library/" namespace
      if (isBlank(image) && namespace === 'library') {
        this.repository = repository;
        image = yield this.check();
      }

      // download from registry
      if (isBlank(image) || options.build_force) {
        this.repository = namespace + '/' + repository;
        notify({ type: "action", context: "image", action: "pull_image", data: this });

        var registry_result;
        var output;

        var currentOnline = yield net.isOnlineCheck();
        if ( !currentOnline ) {
          throw new NoInternetConnection();
        }

        // get size and layers count
        try {
          registry_result = yield this.getDownloadInfo(
            lazy.docker.modem,
            namespace,
            repository,
            this.tag);

          output = _.isObject(stdout) && stdout;
          // docker pull
          image = yield lazy.docker.pull(this.repository, this.tag, output, registry_result);
        } catch (err) {
          output = (err || '').toString();
          throw new LostInternetConnection('  ' + output);
        }

        yield this._track('pull');
      }
      return this.check();
    });
  }

  getDownloadInfo(dockerode_modem, namespace, repository, repo_tag) {
    return async(this, function* (notify) {

      var docker_socket   = { dockerode_modem: dockerode_modem };
      var request_options = {
        timeout: 10000,
        maxAttempts: 3,
        retryDelay: 500
      };

      var syncronizer = new lazy.Syncronizer(docker_socket, request_options);
      var tag = repo_tag;
{'fraction_non_alphanumeric': 0.1362048894062864, 'fraction_numerical': 0.004397878670288449, 'mean_word_length': 2.4333925399644762, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5749840', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2230, 'n_words': 551}