<commit_before>const fs = require("fs");
const path = require("path");
const OS = require("os");
const BlockchainUtils = require("@truffle/blockchain-utils");
const Provider = require("@truffle/provider");
const async = require("async");
const { Web3Shim } = require("@truffle/interface-adapter");

const Networks = {
  deployed: function(options, callback) {
    let files;
    try {
      files = fs.readdirSync(options.contracts_build_directory);
    } catch (error) {
      // We can't read the directory. Act like we found nothing.
      files = [];
    }

    const promises = [];

    files.forEach(function(file) {
      promises.push(
        new Promise(function(accept, reject) {
          fs.readFile(
            path.join(options.contracts_build_directory, file),
            "utf8",
            function(err, body) {
              if (err) return reject(err);

              try {
                body = JSON.parse(body);
              } catch (e) {
                return reject(e);
              }

              accept(body);
            }
          );
        })
      );
    });

    Promise.all(promises)
      .then(function(binaries) {
        const ids_to_names = {};
        const networks = {};

        // binaries.map(function(b) {return b.contract_name + ": " + JSON.stringify(b.networks, null, 2)}).forEach(function(b) {
        //   console.log(b);
        // });

        Object.keys(options.networks).forEach(function(network_name) {
          const network = options.networks[network_name];
          const network_id = network.network_id;

          if (network_id == null) return;

          ids_to_names[network_id] = network_name;
          networks[network_name] = {};
        });

        binaries.forEach(function(json) {
          Object.keys(json.networks).forEach(function(network_id) {
            const network_name = ids_to_names[network_id] || network_id;

            if (networks[network_name] == null) {
              networks[network_name] = {};
            }

            const address = json.networks[network_id].address;

            if (address == null) return;

            networks[network_name][json.contractName] = address;
          });
        });

        callback(null, networks);
      })
      .catch(callback);
  },

  display: function(config, callback) {
    this.deployed(config, function(err, networks) {
      if (err) return callback(err);

      let network_names = Object.keys(networks).sort();

      const star_networks = network_names.filter(network_name => {
        return (
          config.networks[network_name] != null &&
          config.networks[network_name].network_id === "*"
        );
      });

      // Remove * networks from network names.
      network_names = network_names.filter(network_name => {
        return star_networks.indexOf(network_name) < 0;
      });

      const unknown_networks = network_names.filter(network_name => {
        const configured_networks = Object.keys(config.networks);
        let found = false;
        for (let i = 0; i < configured_networks.length; i++) {
          const configured_network_name = configured_networks[i];
          if (network_name === configured_network_name) {
            found = true;
            break;
          }
        }

        return !found;
      });

      // Only display this warning if:
      //
      //   At least one network is configured with the wildcard ('*') network id
      //   There's a least one network deployed to
      //   And one of those networks deployed to is unknown (i.e., unconfigured).
      if (
        star_networks.length > 0 &&
        network_names.length > 0 &&
        unknown_networks.length > 0
      ) {
        config.logger.log(
          OS.EOL +
            "The following networks are configured to match any network id ('*'):" +
            OS.EOL
        );

        star_networks.forEach(network_name => {
          config.logger.log("    " + network_name);
        });

        config.logger.log(
          OS.EOL +
            "Closely inspect the deployed networks below, and use `truffle networks --clean` to remove any networks that don't match your configuration. You should not use the wildcard configuration ('*') for staging and production networks for which you intend to deploy your application."
        );
      }

      network_names.forEach(function(network_name) {
        config.logger.log("");

        let output = Object.keys(networks[network_name])
          .sort()
          .map(contract_name => {
            const address = networks[network_name][contract_name];
            return contract_name + ": " + address;
          });

        if (output.length === 0) {
          output = ["No contracts deployed."];
        }

        let message = "Network: ";

        const is_id = config.networks[network_name] == null;

        if (is_id) {
          message += "UNKNOWN (id: " + network_name + ")";
        } else {
          message +=
            network_name +
            " (id: " +
            config.networks[network_name].network_id +
            ")";
        }

        config.logger.log(message);
        config.logger.log("  " + output.join("\n  "));
      });

      if (network_names.length === 0) {
        config.logger.log(
          OS.EOL + "Contracts have not been deployed to any network."
        );
      }

      config.logger.log("");

      callback();
    });
  },

  clean: function(config, callback) {
    fs.readdir(config.contracts_build_directory, function(err, files) {
      if (err) return callback(err);

      const configured_networks = Object.keys(config.networks);
      const promises = [];<commit_msg>Update some variable naming syntax<commit_after>const fs = require("fs");
const path = require("path");
const OS = require("os");
const BlockchainUtils = require("@truffle/blockchain-utils");
const Provider = require("@truffle/provider");
const async = require("async");
const { Web3Shim } = require("@truffle/interface-adapter");

const Networks = {
  deployed: function(options, callback) {
    let files;
    try {
      files = fs.readdirSync(options.contracts_build_directory);
    } catch (error) {
      // We can't read the directory. Act like we found nothing.
      files = [];
    }

    const promises = [];

    files.forEach(function(file) {
      promises.push(
        new Promise(function(accept, reject) {
          fs.readFile(
            path.join(options.contracts_build_directory, file),
            "utf8",
            function(err, body) {
              if (err) return reject(err);

              try {
                body = JSON.parse(body);
              } catch (e) {
                return reject(e);
              }

              accept(body);
            }
          );
        })
      );
    });

    Promise.all(promises)
      .then(function(binaries) {
        const ids_to_names = {};
        const networks = {};

        // binaries.map(function(b) {return b.contract_name + ": " + JSON.stringify(b.networks, null, 2)}).forEach(function(b) {
        //   console.log(b);
        // });

        Object.keys(options.networks).forEach(networkName => {
          const network = options.networks[networkName];
          const networkId = network.network_id;

          if (networkId == null) return;

          ids_to_names[networkId] = networkName;
          networks[networkName] = {};
        });

        binaries.forEach(function(json) {
          Object.keys(json.networks).forEach(networkId => {
            const network_name = ids_to_names[networkId] || networkId;

            if (networks[network_name] == null) {
              networks[network_name] = {};
            }

            const address = json.networks[networkId].address;

            if (address == null) return;

            networks[network_name][json.contractName] = address;
          });
        });

        callback(null, networks);
      })
      .catch(callback);
  },

  display: function(config, callback) {
    this.deployed(config, function(err, networks) {
      if (err) return callback(err);

      let networkNames = Object.keys(networks).sort();

      const starNetworks = networkNames.filter(networkName => {
        return (
          config.networks[networkName] != null &&
          config.networks[networkName].network_id === "*"
        );
      });

      // Remove * networks from network names.
      networkNames = networkNames.filter(networkName => {
        return starNetworks.indexOf(networkName) < 0;
      });

      const unknownNetworks = networkNames.filter(networkName => {
        const configuredNetworks = Object.keys(config.networks);
        let found = false;
        for (let i = 0; i < configuredNetworks.length; i++) {
          const configured_network_name = configuredNetworks[i];
          if (networkName === configured_network_name) {
            found = true;
            break;
          }
        }

        return !found;
      });

      // Only display this warning if:
      //
      //   At least one network is configured with the wildcard ('*') network id
      //   There's a least one network deployed to
      //   And one of those networks deployed to is unknown (i.e., unconfigured).
      if (
        starNetworks.length > 0 &&
        networkNames.length > 0 &&
        unknownNetworks.length > 0
      ) {
        config.logger.log(
          OS.EOL +
            "The following networks are configured to match any network id ('*'):" +
            OS.EOL
        );

        starNetworks.forEach(network_name => {
          config.logger.log("    " + network_name);
        });

        config.logger.log(
          OS.EOL +
            "Closely inspect the deployed networks below, and use `truffle networks --clean` to remove any networks that don't match your configuration. You should not use the wildcard configuration ('*') for staging and production networks for which you intend to deploy your application."
        );
      }

      networkNames.forEach(function(network_name) {
        config.logger.log("");

        let output = Object.keys(networks[network_name])
          .sort()
          .map(contract_name => {
            const address = networks[network_name][contract_name];
            return contract_name + ": " + address;
          });

        if (output.length === 0) {
          output = ["No contracts deployed."];
        }

        let message = "Network: ";

        const is_id = config.networks[network_name] == null;

        if (is_id) {
          message += "UNKNOWN (id: " + network_name + ")";
        } else {
          message +=
            network_name +
            " (id: " +
            config.networks[network_name].network_id +
            ")";
        }

        config.logger.log(message);
        config.logger.log("  " + output.join("\n  "));
      });

      if (networkNames.length === 0) {
        config.logger.log(
          OS.EOL + "Contracts have not been deployed to any network."
        );
      }

      config.logger.log("");

      callback();
    });
  },

  clean: function(config, callback) {
    fs.readdir(config.contracts_build_directory, function(err, files) {
      if (err) return callback(err);

      const configured_networks = Object.keys(config.networks);
      const promises = [];
{'fraction_non_alphanumeric': 0.1234328094649479, 'fraction_numerical': 0.0017658484901995409, 'mean_word_length': 1.9768725361366624, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 21, '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': '1980456', 'n_tokens_mistral': 3157, 'n_tokens_neox': 3069, 'n_words': 694}