forked from astral-sh/python-build-standalone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
237 lines (221 loc) · 8.03 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
mod github;
mod json;
mod macho;
mod release;
mod validation;
use {
anyhow::{anyhow, Context, Result},
clap::{value_parser, Arg, ArgAction, Command},
std::{
io::Read,
path::{Path, PathBuf},
},
};
pub fn open_distribution_archive(path: &Path) -> Result<tar::Archive<impl Read>> {
let fh =
std::fs::File::open(path).with_context(|| format!("unable to open {}", path.display()))?;
let reader = std::io::BufReader::new(fh);
let dctx = zstd::stream::Decoder::new(reader)?;
Ok(tar::Archive::new(dctx))
}
fn main_impl() -> Result<()> {
let app = Command::new("Python Build")
.arg_required_else_help(true)
.version("0.1")
.author("Gregory Szorc <gregory.szorc@gmail.com>")
.about("Perform tasks related to building Python distributions");
let app = app.subcommand(
Command::new("fetch-release-distributions")
.about("Fetch builds from GitHub Actions that are release artifacts")
.arg(
Arg::new("token")
.long("token")
.action(ArgAction::Set)
.required(true)
.help("GitHub API token"),
)
.arg(
Arg::new("commit")
.long("commit")
.action(ArgAction::Set)
.help("Git commit whose artifacts to fetch"),
)
.arg(
Arg::new("dest")
.long("dest")
.required(true)
.action(ArgAction::Set)
.value_parser(value_parser!(PathBuf))
.help("Destination directory"),
)
.arg(
Arg::new("organization")
.long("org")
.action(ArgAction::Set)
.default_value("indygreg")
.help("GitHub organization"),
)
.arg(
Arg::new("repo")
.long("repo")
.action(ArgAction::Set)
.default_value("python-build-standalone")
.help("GitHub repository name"),
),
);
let app = app.subcommand(
Command::new("convert-install-only")
.about("Convert a .tar.zst archive to an install_only tar.gz archive")
.arg(
Arg::new("path")
.required(true)
.action(ArgAction::Append)
.value_parser(value_parser!(PathBuf))
.help("Path of archive to convert"),
),
);
let app = app.subcommand(
Command::new("convert-install-only-stripped")
.about("Convert an install_only .tar.gz archive to an install_only_stripped tar.gz archive")
.arg(
Arg::new("path")
.required(true)
.action(ArgAction::Append)
.value_parser(value_parser!(PathBuf))
.help("Path of archive to convert"),
),
);
let app = app.subcommand(
Command::new("upload-release-distributions")
.about("Upload release distributions to a GitHub release")
.arg(
Arg::new("token")
.long("token")
.action(ArgAction::Set)
.required(true)
.help("GitHub API token"),
)
.arg(
Arg::new("dist")
.long("dist")
.action(ArgAction::Set)
.required(true)
.value_parser(value_parser!(PathBuf))
.help("Directory with release artifacts"),
)
.arg(
Arg::new("datetime")
.long("datetime")
.action(ArgAction::Set)
.required(true)
.help("Date/time tag associated with builds"),
)
.arg(
Arg::new("dry_run")
.short('n')
.action(ArgAction::SetTrue)
.help("Dry run mode; do not actually upload"),
)
.arg(
Arg::new("tag")
.long("tag")
.action(ArgAction::Set)
.required(true)
.help("Release tag"),
)
.arg(
Arg::new("ignore_missing")
.long("ignore-missing")
.action(ArgAction::SetTrue)
.help("Continue even if there are missing artifacts"),
)
.arg(
Arg::new("organization")
.long("org")
.action(ArgAction::Set)
.default_value("indygreg")
.help("GitHub organization"),
)
.arg(
Arg::new("repo")
.long("repo")
.action(ArgAction::Set)
.default_value("python-build-standalone")
.help("GitHub repository name"),
),
);
let app = app.subcommand(
Command::new("validate-distribution")
.about("Ensure a distribution archive conforms to standards")
.arg(
Arg::new("run")
.long("run")
.action(ArgAction::SetTrue)
.help("Run the interpreter to verify behavior"),
)
.arg(
Arg::new("macos_sdks_path")
.long("macos-sdks-path")
.action(ArgAction::Set)
.help("Path to a directory containing MacOS SDKs (typically a checkout of https://github.com/phracker/MacOSX-SDKs)")
)
.arg(
Arg::new("path")
.help("Path to tar.zst file to validate")
.action(ArgAction::Append)
.value_parser(value_parser!(PathBuf))
.required(true),
),
);
let matches = app.get_matches();
match matches.subcommand() {
Some(("convert-install-only", args)) => {
for path in args.get_many::<PathBuf>("path").unwrap() {
let dest_path = release::produce_install_only(path)?;
println!("wrote {}", dest_path.display());
}
Ok(())
}
Some(("convert-install-only-stripped", args)) => {
let llvm_dir = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(release::bootstrap_llvm())?;
for path in args.get_many::<PathBuf>("path").unwrap() {
let dest_path = release::produce_install_only_stripped(path, &llvm_dir)?;
println!("wrote {}", dest_path.display());
}
Ok(())
}
Some(("fetch-release-distributions", args)) => {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(github::command_fetch_release_distributions(args))
}
Some(("upload-release-distributions", args)) => {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(github::command_upload_release_distributions(args))
}
Some(("validate-distribution", args)) => validation::command_validate_distribution(args),
_ => Err(anyhow!("invalid sub-command")),
}
}
fn main() {
let exit_code = match main_impl() {
Ok(()) => 0,
Err(err) => {
eprintln!("Error: {:?}", err);
1
}
};
std::process::exit(exit_code);
}