<commit_before>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.performance.fixture

import groovy.json.JsonSlurper
import org.apache.commons.io.FileUtils
import org.gradle.internal.os.OperatingSystem

class MavenInstallationDownloader {

    private final File installsRoot

    public MavenInstallationDownloader(File installsRoot) {
        this.installsRoot = installsRoot
    }

    public MavenInstallation getMavenInstallation(String mavenVersion) {
        def home = mavenInstallDirectory(installsRoot, mavenVersion)
        if (MavenInstallation.valid(home)) {
            return new MavenInstallation(mavenVersion, home)
        }
        def preferredUrl = fetchPreferredUrl(mavenVersion)
        def binArchive = downloadBinArchive(mavenVersion, preferredUrl)
        def tempHome = extractBinArchive(mavenVersion, binArchive)
        home = moveToInstallsRoot(tempHome)
        new MavenInstallation(mavenVersion, home)
    }

    private static File mavenInstallDirectory(File parent, String mavenVersion) {
        new File(parent, "apache-maven-$mavenVersion")
    }

    private static String fetchPreferredUrl(String mavenVersion) {
        // Use ASF preferred mirror resolution
        def url = "http://www.apache.org/dyn/closer.cgi/maven/maven-3/$mavenVersion/binaries/apache-maven-$mavenVersion-bin.tar.gz?as_json=1"
        def parsed = new JsonSlurper().parse(new URL(url))
        parsed.preferred + parsed.path_info
    }

    private static File downloadBinArchive(String mavenVersion, String binArchiveUrl) {
        def target = File.createTempFile("maven-bin-$mavenVersion-", ".tar.gz")
        target.withOutputStream { out ->
            out << new URL(binArchiveUrl).openStream()
        }
        target
    }

    private static File extractBinArchive(String mavenVersion, File binArchive) {
        def target = File.createTempDir("maven-install-$mavenVersion-", "")
        def ant = new AntBuilder()
        ant.mkdir(dir: target)
        ant.untar(src: binArchive, dest: target, compression: "gzip")
        def home = mavenInstallDirectory(target, mavenVersion)
        if (!OperatingSystem.current().isWindows()) {
            ant.chmod(file: MavenInstallation.findMvnExecutable(home), perm: "+x")
        }
        home
    }

    private File moveToInstallsRoot(File home) {
        FileUtils.moveDirectoryToDirectory(home, installsRoot, true)
        new File(installsRoot, home.getName())
    }
}<commit_msg>Maven provisioning for performance tests, fix perms<commit_after>/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.gradle.performance.fixture

import groovy.json.JsonSlurper
import org.apache.commons.io.FileUtils
import org.gradle.internal.os.OperatingSystem

class MavenInstallationDownloader {

    private final File installsRoot

    public MavenInstallationDownloader(File installsRoot) {
        this.installsRoot = installsRoot
    }

    public MavenInstallation getMavenInstallation(String mavenVersion) {
        def home = mavenInstallDirectory(installsRoot, mavenVersion)
        if (MavenInstallation.valid(home)) {
            return new MavenInstallation(mavenVersion, home)
        }
        def preferredUrl = fetchPreferredUrl(mavenVersion)
        def binArchive = downloadBinArchive(mavenVersion, preferredUrl)
        def tempHome = extractBinArchive(mavenVersion, binArchive)
        home = moveToInstallsRoot(tempHome)
        new MavenInstallation(mavenVersion, home)
    }

    private static File mavenInstallDirectory(File parent, String mavenVersion) {
        new File(parent, "apache-maven-$mavenVersion")
    }

    private static String fetchPreferredUrl(String mavenVersion) {
        // Use ASF preferred mirror resolution
        def url = "http://www.apache.org/dyn/closer.cgi/maven/maven-3/$mavenVersion/binaries/apache-maven-$mavenVersion-bin.tar.gz?as_json=1"
        def parsed = new JsonSlurper().parse(new URL(url))
        parsed.preferred + parsed.path_info
    }

    private static File downloadBinArchive(String mavenVersion, String binArchiveUrl) {
        def target = File.createTempFile("maven-bin-$mavenVersion-", ".tar.gz")
        target.withOutputStream { out ->
            out << new URL(binArchiveUrl).openStream()
        }
        target
    }

    private static File extractBinArchive(String mavenVersion, File binArchive) {
        def target = File.createTempDir("maven-install-$mavenVersion-", "")
        def ant = new AntBuilder()
        ant.mkdir(dir: target)
        ant.untar(src: binArchive, dest: target, compression: "gzip")
        mavenInstallDirectory(target, mavenVersion)
    }

    private File moveToInstallsRoot(File tmpHome) {
        FileUtils.moveDirectoryToDirectory(tmpHome, installsRoot, true)
        def home = new File(installsRoot, tmpHome.getName())
        if (!OperatingSystem.current().isWindows()) {
            new AntBuilder().chmod(file: MavenInstallation.findMvnExecutable(home), perm: "+x")
        }
        home
    }
}
{'fraction_non_alphanumeric': 0.08707682291666667, 'fraction_numerical': 0.0032552083333333335, 'mean_word_length': 3.345827439886846, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5223152', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1635, 'n_words': 457}