-
Notifications
You must be signed in to change notification settings - Fork 63
/
prefix_paths.rs
323 lines (283 loc) · 9.3 KB
/
prefix_paths.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
use crate::{paths_json::PyFileMode, utils::sha256_from_pybytes};
use pyo3::{exceptions::PyValueError, pyclass, pymethods, types::PyBytes, Bound, PyResult, Python};
use rattler_conda_types::prefix_record::{PathType, PathsEntry, PrefixPaths};
use std::path::PathBuf;
#[pymethods]
impl PyPrefixPaths {
#[new]
pub fn new(paths_version: u64) -> Self {
Self {
inner: PrefixPaths {
paths_version,
paths: Vec::new(),
},
}
}
}
#[pymethods]
impl PyPrefixPathsEntry {
#[new]
#[allow(clippy::too_many_arguments)]
#[pyo3(signature = (relative_path, path_type, prefix_placeholder=None, file_mode=None, sha256=None, sha256_in_prefix=None, size_in_bytes=None, original_path=None))]
pub fn new(
relative_path: PathBuf,
path_type: PyPrefixPathType,
prefix_placeholder: Option<String>,
file_mode: Option<PyFileMode>,
sha256: Option<Bound<'_, PyBytes>>,
sha256_in_prefix: Option<Bound<'_, PyBytes>>,
size_in_bytes: Option<u64>,
original_path: Option<PathBuf>,
) -> PyResult<Self> {
let sha256 = sha256.map(sha256_from_pybytes).transpose()?;
let sha256_in_prefix = sha256_in_prefix.map(sha256_from_pybytes).transpose()?;
Ok(Self {
inner: PathsEntry {
relative_path,
original_path,
no_link: false,
path_type: path_type.into(),
prefix_placeholder,
file_mode: file_mode.map(Into::into),
sha256,
sha256_in_prefix,
size_in_bytes,
},
})
}
}
#[pyclass]
#[repr(transparent)]
#[derive(Clone)]
pub struct PyPrefixPaths {
pub(crate) inner: PrefixPaths,
}
impl From<PyPrefixPaths> for PrefixPaths {
fn from(value: PyPrefixPaths) -> Self {
value.inner
}
}
impl From<PrefixPaths> for PyPrefixPaths {
fn from(value: PrefixPaths) -> Self {
Self { inner: value }
}
}
/// An entry in the paths_data attribute of the PrefixRecord
/// This is similar to PathsEntry from paths_json but refers
/// to an entry for an installed package
#[pyclass]
#[repr(transparent)]
#[derive(Clone)]
pub struct PyPrefixPathsEntry {
pub(crate) inner: PathsEntry,
}
impl From<PathsEntry> for PyPrefixPathsEntry {
fn from(value: PathsEntry) -> Self {
Self { inner: value }
}
}
impl From<PyPrefixPathsEntry> for PathsEntry {
fn from(value: PyPrefixPathsEntry) -> Self {
value.inner
}
}
/// The path type of the path entry
/// This is similar to PathType from paths_json; however, it contains additional enum fields
/// since it represents a file that's installed
#[pyclass]
#[repr(transparent)]
#[derive(Clone)]
pub struct PyPrefixPathType {
pub(crate) inner: PathType,
}
impl From<PathType> for PyPrefixPathType {
fn from(value: PathType) -> Self {
Self { inner: value }
}
}
impl From<PyPrefixPathType> for PathType {
fn from(value: PyPrefixPathType) -> Self {
value.inner
}
}
#[pymethods]
impl PyPrefixPathType {
#[new]
pub fn new(path_type: String) -> PyResult<Self> {
match path_type.as_str() {
"hardlink" => Ok(Self {
inner: PathType::HardLink,
}),
"softlink" => Ok(Self {
inner: PathType::SoftLink,
}),
"directory" => Ok(Self {
inner: PathType::Directory,
}),
"pyc_file" => Ok(Self {
inner: PathType::PycFile,
}),
"windows_python_entrypoint_script" => Ok(Self {
inner: PathType::WindowsPythonEntryPointScript,
}),
"windows_python_entrypoint_exe" => Ok(Self {
inner: PathType::WindowsPythonEntryPointExe,
}),
"unix_python_entrypoint" => Ok(Self {
inner: PathType::UnixPythonEntryPoint,
}),
_ => Err(PyValueError::new_err("Invalid path type")),
}
}
/// The path should be hard linked (the default)
#[getter]
pub fn hardlink(&self) -> bool {
matches!(&self.inner, PathType::HardLink)
}
/// The path should be soft linked
#[getter]
pub fn softlink(&self) -> bool {
matches!(&self.inner, PathType::SoftLink)
}
/// This should explicitly create an empty directory
#[getter]
pub fn directory(&self) -> bool {
matches!(&self.inner, PathType::Directory)
}
/// A file compiled from Python code when a noarch package was installed
#[getter]
pub fn pyc_file(&self) -> bool {
matches!(&self.inner, PathType::PycFile)
}
/// A Windows entry point python script (a <entrypoint>-script.py Python script file)
#[getter]
pub fn windows_python_entrypoint_script(&self) -> bool {
matches!(&self.inner, PathType::WindowsPythonEntryPointScript)
}
/// A Windows Python entry point executable (a <entrypoint>.exe file)
#[getter]
pub fn windows_python_entrypoint_exe(&self) -> bool {
matches!(&self.inner, PathType::WindowsPythonEntryPointExe)
}
/// This file is a Python entry point executable for Unix (a `<entrypoint>` Python script file)
/// Entry points are created in the `bin/...` directory when installing Python noarch packages
#[getter]
pub fn unix_python_entrypoint(&self) -> bool {
matches!(&self.inner, PathType::UnixPythonEntryPoint)
}
}
#[pymethods]
impl PyPrefixPathsEntry {
/// The relative path from the root of the package
#[getter]
pub fn relative_path(&self) -> PathBuf {
self.inner.relative_path.clone()
}
#[setter]
pub fn set_relative_path(&mut self, path: PathBuf) {
self.inner.relative_path = path;
}
/// Whether this file should be linked when installing the package.
#[getter]
pub fn no_link(&self) -> bool {
self.inner.no_link
}
#[setter]
pub fn set_no_link(&mut self, no_link: bool) {
self.inner.no_link = no_link;
}
/// Determines how to include the file when installing the package
#[getter]
pub fn path_type(&self) -> PyPrefixPathType {
self.inner.path_type.into()
}
#[setter]
pub fn set_path_type(&mut self, path_type: PyPrefixPathType) {
self.inner.path_type = path_type.inner;
}
/// Optionally the placeholder prefix used in the file. If this value is `None` the prefix is not
/// present in the file.
#[getter]
pub fn prefix_placeholder(&self) -> Option<String> {
self.inner.prefix_placeholder.clone()
}
#[setter]
pub fn set_prefix_placeholder(&mut self, placeholder: Option<String>) {
self.inner.prefix_placeholder = placeholder;
}
/// If a file has a placeholder, the method by which the placeholder was replaced
#[getter]
pub fn file_mode(&self) -> Option<PyFileMode> {
if let Some(file_mode) = self.inner.file_mode {
return Some(file_mode.into());
}
None
}
#[setter]
pub fn set_file_mode(&mut self, file_mode: Option<PyFileMode>) {
self.inner.file_mode = file_mode.map(|fm| fm.inner);
}
/// A hex representation of the SHA256 hash of the contents of the file
/// If prefix_placeholder is present, this represents the hash of the file *before*
/// any placeholders were replaced
#[getter]
pub fn sha256<'a>(&self, py: Python<'a>) -> Option<Bound<'a, PyBytes>> {
self.inner.sha256.map(|sha| PyBytes::new_bound(py, &sha))
}
// #[setter]
// pub fn set_sha256(&mut self, sha256: Option<Vec<u8>>) {
// self.inner.sha256 = sha256;
// }
/// A hex representation of the SHA256 hash of the contents of the file as installed
/// This will be present only if prefix_placeholder is defined. In this case,
/// this is the hash of the file after the placeholder has been replaced.
#[getter]
pub fn sha256_in_prefix<'a>(&self, py: Python<'a>) -> Option<Bound<'a, PyBytes>> {
self.inner
.sha256_in_prefix
.map(|shla| PyBytes::new_bound(py, &shla))
}
// #[setter]
// pub fn set_sha256_in_prefix(&mut self, sha256: Option<Vec<u8>>) {
// self.inner.sha256_in_prefix = sha256;
// }
/// The size of the file in bytes
/// This entry is only present in version 1 of the paths.json file.
#[getter]
pub fn size_in_bytes(&self) -> Option<u64> {
self.inner.size_in_bytes
}
#[setter]
pub fn set_size_in_bytes(&mut self, size: Option<u64>) {
self.inner.size_in_bytes = size;
}
}
#[pymethods]
impl PyPrefixPaths {
pub fn as_str(&self) -> String {
format!("{:?}", self.inner)
}
/// The version of the file
#[getter]
pub fn paths_version(&self) -> u64 {
self.inner.paths_version
}
#[setter]
pub fn set_paths_version(&mut self, version: u64) {
self.inner.paths_version = version;
}
/// All entries included in the package.
#[getter]
pub fn paths(&self) -> Vec<PyPrefixPathsEntry> {
self.inner
.paths
.clone()
.into_iter()
.map(Into::into)
.collect()
}
#[setter]
pub fn set_paths(&mut self, paths: Vec<PyPrefixPathsEntry>) {
self.inner.paths = paths.into_iter().map(|p| p.inner).collect();
}
}