-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpexrc.rs
More file actions
176 lines (156 loc) · 5.17 KB
/
pexrc.rs
File metadata and controls
176 lines (156 loc) · 5.17 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
// 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;
use pexrc::commands::{extract, info, inject, script};
use pexrc::embeds::{CLIB_BY_TARGET, PROXY_BY_TARGET};
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.
#[arg(value_name = "FILE")]
pex: PathBuf,
},
/// 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 = "FILE", required = true)]
pexes: Vec<PathBuf>,
},
/// Provide information about the supported target runtimes.
Info,
/// 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,
} => extract::to_dir(
&dest_dir,
Pex::load(&pex)?,
compression_method.into(),
compression_level,
),
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)
};
inject::inject_all(
pexes,
compression_method.into(),
compression_level,
clibs.as_ref(),
proxies.as_ref(),
)
}
Commands::Info => info::display(),
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,
)
}
}
}
}