-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpexrc.rs
More file actions
213 lines (190 loc) · 6.51 KB
/
pexrc.rs
File metadata and controls
213 lines (190 loc) · 6.51 KB
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
// Copyright 2026 Pex project contributors.
// SPDX-License-Identifier: Apache-2.0
#![deny(clippy::all)]
use std::collections::HashSet;
use std::path::PathBuf;
use clap::{ArgAction, Parser, Subcommand, ValueEnum};
use pex::{Pex, WheelOptions};
use pexrc::commands::{extract, info, inject, repackage, script};
use pexrc::embeds::{CLIB_BY_TARGET, PROXY_BY_TARGET};
use pexrc::source;
use target::Target;
/// Pex Runtime Control.
#[derive(Parser)]
#[command(version, about, long_about = None, styles = cli::STYLES)]
struct Cli {
#[command(flatten)]
verbosity: Option<clap_verbosity_flag::Verbosity>,
#[command(flatten)]
color: colorchoice_clap::Color,
#[command(subcommand)]
command: Commands,
}
#[derive(Clone, ValueEnum)]
enum CompressionMethod {
Deflated,
Zstd,
}
impl From<CompressionMethod> for zip::CompressionMethod {
fn from(val: CompressionMethod) -> Self {
match val {
CompressionMethod::Deflated => zip::CompressionMethod::Deflated,
CompressionMethod::Zstd => zip::CompressionMethod::Zstd,
}
}
}
#[derive(Subcommand)]
enum Commands {
/// Extract Pex dependencies as zstd wheels.
Extract {
#[arg(short = 'Z', long, value_enum, default_value_t = CompressionMethod::Zstd)]
compression_method: CompressionMethod,
#[arg(long)]
compression_level: Option<i64>,
/// The directory to extract the wheels to.
#[arg(short = 'd', long)]
dest_dir: PathBuf,
/// The Pex to extract dependency wheels from. Can be a path or URL.
#[arg(value_name = "PEX")]
pex: String,
},
/// Inject a traditional PEX with the pexrc runtime.
Inject {
#[arg(short = 'Z', long, value_enum, default_value_t = CompressionMethod::Zstd)]
compression_method: CompressionMethod,
#[arg(long)]
compression_level: Option<i64>,
#[arg(long = "target")]
#[arg(action=ArgAction::Append)]
#[arg(value_parser=clap::builder::PossibleValuesParser::new(CLIB_BY_TARGET.keys()))]
targets: Vec<String>,
#[arg(value_name = "PEX", required = true)]
pexes: Vec<String>,
},
/// Provide information about the supported target runtimes.
Info,
/// Re-package a traditional whl as a zstd compressed whl.
Repackage {
#[arg(short = 'Z', long, value_enum, default_value_t = CompressionMethod::Zstd)]
compression_method: CompressionMethod,
#[arg(long)]
compression_level: Option<i64>,
/// The directory to extract the wheels to.
#[arg(short = 'd', long)]
dest_dir: PathBuf,
#[arg(value_name = "WHEEL", required = true)]
wheels: Vec<String>,
},
/// Create a Windows-style Python venv console script executable.
Script {
#[arg(long)]
#[arg(value_parser=clap::builder::PossibleValuesParser::new(PROXY_BY_TARGET.keys()))]
target: Option<String>,
#[arg(short = 'p', long, required = true)]
python: PathBuf,
#[arg(short = 'o', long)]
output_file: PathBuf,
#[arg(value_name = "SCRIPT")]
script: PathBuf,
},
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
logging::init(cli.verbosity.map(|verbosity| verbosity.log_level_filter()))?;
cli.color.write_global();
match cli.command {
Commands::Extract {
compression_method,
compression_level,
dest_dir,
pex,
} => {
let pex = source::to_path(pex, Some(&dest_dir))?;
extract::to_dir(
&dest_dir,
Pex::load(&pex)?,
&WheelOptions::new(compression_method.into(), compression_level, None),
)
}
Commands::Inject {
compression_method,
compression_level,
targets,
pexes,
} => {
let (clibs, proxies) = if !targets.is_empty() {
(
Some(
targets
.iter()
.map(|target| {
CLIB_BY_TARGET.get(target.as_str()).copied().expect(
"The allowed --target values are all keys in the CLIB_BY_TARGET \
map.",
)
})
.collect::<HashSet<_>>(),
),
Some(
targets
.iter()
.map(|target| {
PROXY_BY_TARGET.get(target.as_str()).copied().expect(
"The allowed --target values are all keys in the PROXY_BY_TARGET \
map.",
)
})
.collect::<HashSet<_>>(),
),
)
} else {
(None, None)
};
let pexes = pexes
.into_iter()
.map(|source| source::to_path(source, None))
.collect::<anyhow::Result<Vec<_>>>()?;
inject::inject_all(
pexes,
&WheelOptions::new(compression_method.into(), compression_level, None),
clibs.as_ref(),
proxies.as_ref(),
)
}
Commands::Info => info::display(),
Commands::Repackage {
compression_method,
compression_level,
dest_dir,
wheels,
} => {
let wheels = wheels
.into_iter()
.map(|source| source::to_path(source, None))
.collect::<anyhow::Result<Vec<_>>>()?;
repackage::repackage_all(
wheels,
&WheelOptions::new(compression_method.into(), compression_level, None),
&dest_dir,
)
}
Commands::Script {
target,
python,
script,
output_file,
} => {
if let Some(target) = target {
script::create(target.as_str(), &python, &script, &output_file)
} else {
let current_target = Target::current()?;
script::create(
current_target.simplified_target_triple().as_ref(),
&python,
&script,
&output_file,
)
}
}
}
}