-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
211 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import subprocess | ||
import pathlib | ||
import requests | ||
|
||
class CrossRepoPR: | ||
org: str | ||
repo: str | ||
pr_num: str | ||
|
||
def __init__(self, org: str, repo: str, pr_num: str) -> None: | ||
self.org = org | ||
self.repo = repo | ||
self.pr_num = pr_num | ||
|
||
def cross_repo_prs() -> list[CrossRepoPR]: | ||
return [ | ||
CrossRepoPR("swiftlang", "swift-syntax", "2859") | ||
] | ||
|
||
def run(cmd: list[str], cwd: str|None = None): | ||
print(" ".join(cmd)) | ||
subprocess.check_call(cmd, cwd=cwd) | ||
|
||
def main(): | ||
for cross_repo_pr in cross_repo_prs(): | ||
run(["git", "clone", f"https://github.com/{cross_repo_pr.org}/{cross_repo_pr.repo}.git", f"{cross_repo_pr.repo}"], cwd="..") | ||
run(["git", "fetch", "origin", f"pull/{cross_repo_pr.pr_num}/merge:pr_merge"], cwd="../swift-syntax") | ||
run(["git", "checkout", "main"], cwd="../swift-syntax") | ||
run(["git", "reset", "--hard", "pr_merge"], cwd="../swift-syntax") | ||
run(["swift", "package", "config", "set-mirror", "--package-url", "https://github.com/swiftlang/swift-syntax.git", "--mirror-url", str(pathlib.Path("../swift-syntax").resolve())]) | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import Foundation | ||
|
||
/// Provides convenience APIs for launching and gathering output from a subprocess | ||
public class ProcessRunner { | ||
private static let serialQueue = DispatchQueue(label: "\(ProcessRunner.self)") | ||
|
||
let process: Process | ||
var launched = false | ||
|
||
public init( | ||
launchPath: String, | ||
arguments: [String], | ||
workingDirectory: String?, | ||
environment: [String: String] = [:] | ||
) { | ||
process = Process() | ||
process.launchPath = launchPath | ||
process.arguments = arguments | ||
if let workingDirectory { | ||
process.currentDirectoryURL = URL(fileURLWithPath: workingDirectory) | ||
} | ||
process.environment = environment.merging(ProcessInfo.processInfo.environment) { (current, _) in current } | ||
} | ||
|
||
public func run( | ||
input: String? = nil, | ||
captureStdout: Bool = true, | ||
captureStderr: Bool = true | ||
) -> ProcessResult { | ||
let group = DispatchGroup() | ||
|
||
let inPipe = Pipe() | ||
if input != nil { | ||
process.standardInput = inPipe | ||
} | ||
|
||
var outData = Data() | ||
if captureStdout { | ||
let outPipe = Pipe() | ||
process.standardOutput = outPipe | ||
addHandler(pipe: outPipe, group: group) { outData.append($0) } | ||
} | ||
|
||
var errData = Data() | ||
if captureStderr { | ||
let errPipe = Pipe() | ||
process.standardError = errPipe | ||
addHandler(pipe: errPipe, group: group) { errData.append($0) } | ||
} | ||
|
||
ProcessRunner.serialQueue.sync { | ||
process.launch() | ||
launched = true | ||
} | ||
|
||
if let input = input { | ||
guard let data = input.data(using: .utf8) else { | ||
return ProcessResult( | ||
status: 1, | ||
stdout: Data(), | ||
stderr: "Invalid input".data(using: .utf8)! | ||
) | ||
} | ||
inPipe.fileHandleForWriting.write(data) | ||
inPipe.fileHandleForWriting.closeFile() | ||
} | ||
|
||
process.waitUntilExit() | ||
if captureStdout || captureStderr { | ||
// Make sure we've received all stdout/stderr | ||
group.wait() | ||
} | ||
|
||
return ProcessResult( | ||
status: process.terminationStatus, | ||
stdout: outData, | ||
stderr: errData | ||
) | ||
} | ||
|
||
public func terminate() { | ||
ProcessRunner.serialQueue.sync { | ||
if launched { | ||
process.terminate() | ||
} | ||
} | ||
} | ||
|
||
private func addHandler( | ||
pipe: Pipe, | ||
group: DispatchGroup, | ||
addData: @escaping (Data) -> Void | ||
) { | ||
group.enter() | ||
pipe.fileHandleForReading.readabilityHandler = { fileHandle in | ||
// Apparently using availableData can cause various issues | ||
let newData = fileHandle.readData(ofLength: Int.max) | ||
if newData.count == 0 { | ||
pipe.fileHandleForReading.readabilityHandler = nil; | ||
group.leave() | ||
} else { | ||
addData(newData) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// The exit code and output (if redirected) from a subprocess that has | ||
/// terminated | ||
public struct ProcessResult { | ||
public let status: Int32 | ||
public let stdout: Data | ||
public let stderr: Data | ||
|
||
public var stdoutStr: String? { | ||
return String(data: stdout, encoding: .utf8) | ||
} | ||
public var stderrStr: String? { | ||
return String(data: stderr, encoding: .utf8) | ||
} | ||
} | ||
|
||
func run(_ executable: String, _ arguments: String..., workingDirectory: String? = nil) { | ||
let runner = ProcessRunner( | ||
launchPath: executable, | ||
arguments: arguments, | ||
environment: [:], | ||
workingDirectory: workingDirectory | ||
) | ||
runner.run() | ||
} | ||
|
||
struct CrossRepoPR { | ||
let org: String | ||
let repo: String | ||
let prNum: String | ||
} | ||
|
||
let crossRepoPrs = [ | ||
CrossRepoPR(org: "swiftlang", repo: "swift-syntax", prNum: "2859") | ||
] | ||
|
||
for crossRepoPr in crossRepoPrs { | ||
run( | ||
"git", | ||
"clone", | ||
"https://github.com/\(crossRepoPr.org)/\(crossRepoPrs.repo).git", | ||
"\(crossRepoPr.repo)", | ||
workingDirectory: ".." | ||
) | ||
run("git", "fetch", "origin", "pull/\(crossRepoPr.prNum)/merge:pr_merge", workingDirectory: "../swift-syntax") | ||
run("git", "checkout", "main", workingDirectory: "../swift-syntax") | ||
run("git", "reset", "--hard", "pr_merge", workingDirectory: "../swift-syntax") | ||
run( | ||
"swift", | ||
"package", | ||
"config", | ||
"set-mirror", | ||
"--package-url", | ||
"https://github.com/swiftlang/swift-syntax.git", | ||
"--mirror-url", | ||
URL(fileURLWithPath: "../swift-syntax").resolveSymlinksInPath().path | ||
) | ||
} |