<commit_before>// Copyrhs (c) 2016 Anarchy Tools Contributors.
//
// 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.
import atfoundation
import atpkg
import atpm_tools

///We populate the _applicationInfo with this value
///Basically, although via the git architecture it's pretty easy to find out what git we checked out (just inspect the commit)
///Doing that for binaries is too complicated.  We don't remember where the tarball is, we don't remember what its shasum was, etc.
///So instead, we build one of these during the binary fetch process and then use it when we write the lockfile
struct HTTPDependencyInfo {
    let channels: [HTTPDependencyChannel]
}

struct HTTPDependencyChannel {
    let lockedPayload: LockedPayload
    let channel: String
}

///The world's most terrible HTTP implementation
private func fetch(url: URL, to file: Path) throws {
    if url.schema != "https" { throw PMError.InsecurePackage }
    let curlResult = system("curl -sL -o '\(file)' '\(url)'")
    if curlResult != 0 {
        throw PMError.HTTPSTransportError(exitCode: curlResult)
    }
}

func fetchHTTPDependency(_ pkg:ExternalDependency,lock: LockedPackage?, forceUpdate: Bool = false) throws {
    print("Processing HTTP dependency...")
    let packagePath = Path("external/manifest.atpkg")
    if !FS.fileExists(path: packagePath) { try FS.createDirectory(path: packagePath) }


    let manifestPath = packagePath.appending("manifest.atpkg")

    if FS.fileExists(path: manifestPath) && !forceUpdate {
        print("Manifest already exists; won't update")
    }
    else {
        try fetch(url: pkg.url, to: manifestPath)
    }

    let parsedManifest = try Package(filepath: manifestPath, overlay: [], focusOnTask: nil)
    pkg._parsedNameFromManifest = parsedManifest.name
    guard let channels = parsedManifest.binaryChannels else { fatalError("No binary channels in manifest ")}
    //what channels should we load?
    let channelsToLoad: [String]
    if let c = pkg.channels {
        //load specified channels
        channelsToLoad = c
    }
    else  { 
        //load all available channel
        channelsToLoad = channels.map({$0.name}) 
    }
    var httpChannels : [HTTPDependencyChannel] = []
    for channel in channelsToLoad {
        //load channel
        guard let parsedChannel = channels.filter({$0.name == channel}).first else {
            fatalError("Can't find channel named \(channel) in channels")
        }
        //parse version range from build.atpkg
        guard case .Version(let versionSpecifications) = pkg.version else {
            fatalError("Only `version` supported in binary specification.  Actual: \(pkg.version)")
        }<commit_msg>Improve manifest calculation<commit_after>// Copyrhs (c) 2016 Anarchy Tools Contributors.
//
// 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.
import atfoundation
import atpkg
import atpm_tools

///We populate the _applicationInfo with this value
///Basically, although via the git architecture it's pretty easy to find out what git we checked out (just inspect the commit)
///Doing that for binaries is too complicated.  We don't remember where the tarball is, we don't remember what its shasum was, etc.
///So instead, we build one of these during the binary fetch process and then use it when we write the lockfile
struct HTTPDependencyInfo {
    let channels: [HTTPDependencyChannel]
}

struct HTTPDependencyChannel {
    let lockedPayload: LockedPayload
    let channel: String
}

///The world's most terrible HTTP implementation
private func fetch(url: URL, to file: Path) throws {
    if url.schema != "https" { throw PMError.InsecurePackage }
    let curlResult = system("curl -sL -o '\(file)' '\(url)'")
    if curlResult != 0 {
        throw PMError.HTTPSTransportError(exitCode: curlResult)
    }
}

func fetchHTTPDependency(_ pkg:ExternalDependency,lock: LockedPackage?, forceUpdate: Bool = false) throws {
    print("Processing HTTP dependency...")

    //calculate a safe path to store the manifest
    let manifestName = pkg.url.description.replacing(searchTerm: "/", replacement: "-").replacing(searchTerm: ":", replacement: "-")
    let manifestPath = Path("external/\(manifestName)")
    print("saving to \(manifestPath)")

    if FS.fileExists(path: manifestPath) && !forceUpdate {
        print("Manifest already exists; won't update")
    }
    else {
        try fetch(url: pkg.url, to: manifestPath)
    }

    let parsedManifest = try Package(filepath: manifestPath, overlay: [], focusOnTask: nil)
    pkg._parsedNameFromManifest = parsedManifest.name
    let packagePath = Path("external/\(parsedManifest.name)")
    if !FS.fileExists(path: packagePath) { try FS.createDirectory(path: packagePath) }

    guard let channels = parsedManifest.binaryChannels else { fatalError("No binary channels in manifest ")}
    //what channels should we load?
    let channelsToLoad: [String]
    if let c = pkg.channels {
        //load specified channels
        channelsToLoad = c
    }
    else  { 
        //load all available channel
        channelsToLoad = channels.map({$0.name}) 
    }
    var httpChannels : [HTTPDependencyChannel] = []
    for channel in channelsToLoad {
        //load channel
        guard let parsedChannel = channels.filter({$0.name == channel}).first else {
            fatalError("Can't find channel named \(channel) in channels")
        }
        //parse version range from build.atpkg
        guard case .Version(let versionSpecifications) = pkg.version else {
            fatalError("Only `version` supported in binary specification.  Actual: \(pkg.version)")
        }
{'fraction_non_alphanumeric': 0.09551922494701787, 'fraction_numerical': 0.003330305782621859, 'mean_word_length': 3.349572086899276, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7277021', 'n_tokens_mistral': 1774, 'n_tokens_neox': 1743, 'n_words': 679}