Skip to content

Commit

Permalink
Write out changes instead of merging with input
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Jan 14, 2024
1 parent e054bd2 commit f9fad9d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
61 changes: 44 additions & 17 deletions Sources/ValidatorCore/Commands/CheckRedirects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ extension Validator {
@Option(name: .long, help: "start processing URLs from <offset>")
var offset: Int = 0

@Option(name: .shortAndLong, help: "save changes to output file")
var output: String?
@Option(name: .shortAndLong, help: "output file for added packages")
var outputAdded: String?

@Option(name: .shortAndLong, help: "output file for removed packages")
var outputRemoved: String?

@Flag(name: .long, help: "check redirects of canonical package list")
var usePackageList = false
Expand Down Expand Up @@ -71,10 +74,34 @@ extension Validator {

static var normalizedPackageURLs = NormalizedPackageURLs(inputURLs: [])

enum Change: Equatable {
case add(PackageURL)
case keep(PackageURL)
case remove(PackageURL)

var added: PackageURL? {
switch self {
case let .add(url):
return url
case .keep, .remove:
return nil
}
}

var removed: PackageURL? {
switch self {
case let .remove(url):
return url
case .add, .keep:
return nil
}
}
}

static func process(redirect: Redirect,
verbose: Bool,
index: Int,
packageURL: PackageURL) async throws -> PackageURL? {
packageURL: PackageURL) async throws -> Change {
if verbose || index % 50 == 0 {
print("package \(index) ...")
fflush(stdout)
Expand All @@ -84,29 +111,29 @@ extension Validator {
if verbose {
print(" \(packageURL.absoluteString)")
}
return packageURL
return .keep(packageURL)
case let .error(error):
print("ERROR: \(packageURL.absoluteString) \(error) (ignored, keeping package)")
// don't skip packages that have unrecognised errors
return packageURL
return .keep(packageURL)
case .notFound:
print("package \(index) ...")
print("NOT FOUND: \(packageURL.absoluteString) (deleting package)")
return nil
return .remove(packageURL)
case .rateLimited:
fatalError("rate limited - should have been retried at a lower level")
case .redirected(let url):
if await normalizedPackageURLs.insert(url).inserted {
print("ADD \(packageURL) -> \(url) (new)")
return url
return .add(url)
} else {
print("DELETE \(packageURL) -> \(url) (exists)")
return nil
return .remove(packageURL)
}
case .unauthorized:
print("package \(index) ...")
print("UNAUTHORIZED: \(packageURL.absoluteString) (deleting package)")
return nil
return .remove(packageURL)
}
}

Expand Down Expand Up @@ -139,7 +166,7 @@ extension Validator {

let semaphore = Semaphore(maximum: concurrency ?? 1)

let updated = try await withThrowingTaskGroup(of: PackageURL?.self) { group in
let changes = try await withThrowingTaskGroup(of: Change.self) { group in
for (index, packageURL) in inputURLs[offset...]
.prefix(prefix)
.chunk(index: chunk, of: numberOfChunks)
Expand Down Expand Up @@ -168,16 +195,16 @@ extension Validator {
return res
}
}
return try await group
.compactMap { $0 }
.reduce(into: [], { res, next in res.append(next) })
.sorted(by: { $0.lowercased() < $1.lowercased() })
return try await group.reduce(into: [], { res, next in res.append(next) })
}

if let path = outputAdded {
try Current.fileManager.saveList(changes.compactMap(\.added), path: path)
}

if let path = output {
try Current.fileManager.saveList(updated, path: path)
if let path = outputRemoved {
try Current.fileManager.saveList(changes.compactMap(\.removed), path: path)
}
}
}
}

4 changes: 2 additions & 2 deletions Tests/ValidatorTests/CheckRedirectTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ final class CheckRedirectTests: XCTestCase {
verbose: true,
index: 0,
packageURL: .p1)
XCTAssertEqual(res, .p2)
XCTAssertEqual(res, .add(.p2))
}

func test_process_existing_redirect() async throws {
Expand All @@ -40,7 +40,7 @@ final class CheckRedirectTests: XCTestCase {
verbose: true,
index: 0,
packageURL: .p1)
XCTAssertEqual(res, nil)
XCTAssertEqual(res, .remove(.p1))
}

}
Expand Down

0 comments on commit f9fad9d

Please sign in to comment.