<commit_before>require "capistrano-jdk-installer/version"
require 'capistrano/transfer'
require 'json'
require 'logger'
require 'mechanize'
require 'net/sftp'
require 'uri'

module Capistrano
  module JDKInstaller
    def download_archive(uri, archive)
      if FileTest.exist?(archive)
        logger.info("Found downloaded archive: #{archive}")
      else
        if java_release_license_title != java_license_title or !java_accept_license
          abort("You must accept JDK license before downloading.")
        end

        logger.info("Download archive from #{uri}.")
        unless dry_run
          run_locally("mkdir -p #{File.dirname(archive)}")
          page = java_mechanize_agent.get(uri)
          1.upto(16) { # to avoid infinity loop...
            if page.uri.host == "login.oracle.com" # login.oracle.com doesn't return proper Content-Type
              page = Mechanize::Page.new(page.uri, page.response, page.body, page.code, java_mechanize_agent) if page.is_a?(Mechanize::File)
              form = page.form_with
              form["ssousername"] = java_oracle_username
              form["password"] = <PASSWORD>
              page = java_mechanize_agent.submit(form)
            else
              page.save(archive)
              logger.info("Wrote #{page.body.size} bytes to #{archive}.")
              break
            end
          }
        end
      end
    end

    def upload_archive(from, to, options={}, &block)
      mode = options.delete(:mode)
      execute_on_servers(options) { |servers|
        targets = servers.map { |server| sessions[server] }
        if dry_run
          logger.debug "transfering: #{[:up, from, to, targets, options.merge(:logger => logger).inspect ] * ', '}"
        else
          stat = File.stat(from)
          checksum_cmd = fetch(:java_checksum_cmd, 'md5sum')
          checksum = run_locally("( cd #{File.dirname(from)} && #{checksum_cmd} #{File.basename(from)} )").strip
          targets = targets.reject { |ssh|
            begin
              sftp = Net::SFTP::Session.new(ssh).connect!
              remote_stat = sftp.stat!(to)
              if stat.size == remote_stat.size
                remote_checksum = ssh.exec!("( cd #{File.dirname(to)} && #{checksum_cmd} #{File.basename(to)} )").strip
                checksum == remote_checksum # skip upload if file size and checksum are same between local and remote
              else
                false
              end
            rescue Net::SFTP::StatusException
              false # upload again if the remote file is absent
            end
          }<commit_msg>create enclosing directory if missing<commit_after>require "capistrano-jdk-installer/version"
require 'capistrano/transfer'
require 'json'
require 'logger'
require 'mechanize'
require 'net/sftp'
require 'uri'

module Capistrano
  module JDKInstaller
    def download_archive(uri, archive)
      if FileTest.exist?(archive)
        logger.info("Found downloaded archive: #{archive}")
      else
        if java_release_license_title != java_license_title or !java_accept_license
          abort("You must accept JDK license before downloading.")
        end

        logger.info("Download archive from #{uri}.")
        unless dry_run
          run_locally("mkdir -p #{File.dirname(archive)}")
          page = java_mechanize_agent.get(uri)
          1.upto(16) { # to avoid infinity loop...
            if page.uri.host == "login.oracle.com" # login.oracle.com doesn't return proper Content-Type
              page = Mechanize::Page.new(page.uri, page.response, page.body, page.code, java_mechanize_agent) if page.is_a?(Mechanize::File)
              form = page.form_with
              form["ssousername"] = java_oracle_username
              form["password"] = <PASSWORD>
              page = java_mechanize_agent.submit(form)
            else
              page.save(archive)
              logger.info("Wrote #{page.body.size} bytes to #{archive}.")
              break
            end
          }
        end
      end
    end

    def upload_archive(from, to, options={}, &block)
      mode = options.delete(:mode)
      run("mkdir -p #{File.dirname(to)}")
      execute_on_servers(options) { |servers|
        targets = servers.map { |server| sessions[server] }
        if dry_run
          logger.debug "transfering: #{[:up, from, to, targets, options.merge(:logger => logger).inspect ] * ', '}"
        else
          stat = File.stat(from)
          checksum_cmd = fetch(:java_checksum_cmd, 'md5sum')
          checksum = run_locally("( cd #{File.dirname(from)} && #{checksum_cmd} #{File.basename(from)} )").strip
          targets = targets.reject { |ssh|
            begin
              sftp = Net::SFTP::Session.new(ssh).connect!
              remote_stat = sftp.stat!(to)
              if stat.size == remote_stat.size
                remote_checksum = ssh.exec!("( cd #{File.dirname(to)} && #{checksum_cmd} #{File.basename(to)} )").strip
                checksum == remote_checksum # skip upload if file size and checksum are same between local and remote
              else
                false
              end
            rescue Net::SFTP::StatusException
              false # upload again if the remote file is absent
            end
          }
{'fraction_non_alphanumeric': 0.11412837067983289, 'fraction_numerical': 0.0015191796429927839, 'mean_word_length': 2.203771289537713, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5193275', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1481, 'n_words': 361}