forked from prefix-dev/rattler-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packaging.rs
303 lines (241 loc) · 10.2 KB
/
packaging.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
//! This module contains the functions to package a conda package from a given output.
use fs_err as fs;
use fs_err::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use itertools::Itertools;
use rattler_conda_types::package::PathsJson;
use rattler_conda_types::package::{ArchiveType, PackageFile};
use rattler_package_streaming::write::{
write_conda_package, write_tar_bz2_package, CompressionLevel,
};
mod file_finder;
mod file_mapper;
mod metadata;
pub use file_finder::{Files, TempFiles};
pub use metadata::create_prefix_placeholder;
use crate::metadata::Output;
use crate::package_test::write_test_files;
use crate::{post_process, tool_configuration};
#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
pub enum PackagingError {
#[error("Serde error: {0}")]
SerdeError(#[from] serde_yaml::Error),
#[error("Failed to build glob from pattern")]
GlobError(#[from] globset::Error),
#[error("Build String is not yet set")]
BuildStringNotSet,
#[error("Dependencies are not yet finalized / resolved")]
DependenciesNotFinalized,
#[error("Could not open or create, or write to file")]
IoError(#[from] std::io::Error),
#[error("Could not strip a prefix from a Path")]
StripPrefixError(#[from] std::path::StripPrefixError),
#[error("Could not serialize JSON: {0}")]
SerializationError(#[from] serde_json::Error),
#[error("Could not run walkdir: {0}")]
WalkDirError(#[from] walkdir::Error),
#[error("Failed to parse version {0}")]
VersionParseError(#[from] rattler_conda_types::ParseVersionError),
#[error("Relink error: {0}")]
RelinkError(#[from] crate::post_process::relink::RelinkError),
#[error(transparent)]
SourceError(#[from] crate::source::SourceError),
#[error("could not create python entry point: {0}")]
CannotCreateEntryPoint(String),
#[error("linking check error: {0}")]
LinkingCheckError(#[from] crate::post_process::checks::LinkingCheckError),
#[error("Failed to compile Python bytecode: {0}")]
PythonCompileError(String),
#[error("Failed to find content type for file: {0:?}")]
ContentTypeNotFound(PathBuf),
}
/// This function copies the license files to the info/licenses folder.
fn copy_license_files(
output: &Output,
tmp_dir_path: &Path,
) -> Result<Option<Vec<PathBuf>>, PackagingError> {
if output.recipe.about().license_file.is_empty() {
Ok(None)
} else {
let license_globs = output.recipe.about().license_file.clone();
let licenses_folder = tmp_dir_path.join("info/licenses/");
fs::create_dir_all(&licenses_folder)?;
let copy_dir = crate::source::copy_dir::CopyDir::new(
&output.build_configuration.directories.recipe_dir,
&licenses_folder,
)
.with_parse_globs(license_globs.iter().map(AsRef::as_ref))
.use_gitignore(false)
.run()?;
let copied_files_recipe_dir = copy_dir.copied_paths();
let any_include_matched_recipe_dir = copy_dir.any_include_glob_matched();
let copy_dir = crate::source::copy_dir::CopyDir::new(
&output.build_configuration.directories.work_dir,
&licenses_folder,
)
.with_parse_globs(license_globs.iter().map(AsRef::as_ref))
.use_gitignore(false)
.run()?;
let copied_files_work_dir = copy_dir.copied_paths();
let any_include_matched_work_dir = copy_dir.any_include_glob_matched();
let copied_files = copied_files_recipe_dir
.iter()
.chain(copied_files_work_dir)
.map(PathBuf::from)
.collect::<Vec<PathBuf>>();
if !any_include_matched_work_dir && !any_include_matched_recipe_dir {
tracing::warn!("No include glob matched for copying license files");
}
if copied_files.is_empty() {
tracing::warn!("No license files were copied");
}
Ok(Some(copied_files))
}
}
fn write_recipe_folder(
output: &Output,
tmp_dir_path: &Path,
) -> Result<Vec<PathBuf>, PackagingError> {
let recipe_folder = tmp_dir_path.join("info/recipe/");
let recipe_dir = &output.build_configuration.directories.recipe_dir;
let copy_result = crate::source::copy_dir::CopyDir::new(recipe_dir, &recipe_folder).run()?;
let mut files = Vec::from(copy_result.copied_paths());
// write the variant config to the appropriate file
let variant_config_file = recipe_folder.join("variant_config.yaml");
let mut variant_config = File::create(&variant_config_file)?;
variant_config
.write_all(serde_yaml::to_string(&output.build_configuration.variant)?.as_bytes())?;
files.push(variant_config_file);
// TODO(recipe): define how we want to render it exactly!
let rendered_recipe_file = recipe_folder.join("rendered_recipe.yaml");
let mut rendered_recipe = File::create(&rendered_recipe_file)?;
rendered_recipe.write_all(serde_yaml::to_string(&output)?.as_bytes())?;
files.push(rendered_recipe_file);
Ok(files)
}
struct ProgressBar {
progress_bar: indicatif::ProgressBar,
}
impl rattler_package_streaming::write::ProgressBar for ProgressBar {
fn set_progress(&mut self, progress: u64, message: &str) {
self.progress_bar.set_position(progress);
self.progress_bar.set_message(message.to_string());
}
fn set_total(&mut self, total: u64) {
self.progress_bar.set_length(total);
}
}
/// Given an output and a set of new files, create a conda package.
/// This function will copy all the files to a temporary directory and then
/// create a conda package from that. Note that the output needs to have its
/// dependencies finalized before calling this function.
///
/// The `local_channel_dir` is the path to the local channel / output directory.
pub fn package_conda(
output: &Output,
tool_configuration: &tool_configuration::Configuration,
files: &Files,
) -> Result<(PathBuf, PathsJson), PackagingError> {
let local_channel_dir = &output.build_configuration.directories.output_dir;
let packaging_settings = &output.build_configuration.packaging_settings;
if output.finalized_dependencies.is_none() {
return Err(PackagingError::DependenciesNotFinalized);
}
let mut tmp = files.to_temp_folder(output)?;
tracing::info!("Copying done!");
post_process::relink::relink(&tmp, output)?;
tmp.add_files(post_process::python::python(output, &tmp)?);
tracing::info!("Post-processing done!");
let info_folder = tmp.temp_dir.path().join("info");
tracing::info!("Writing metadata for package");
tmp.add_files(output.write_metadata(&tmp)?);
// TODO move things below also to metadata.rs
if let Some(license_files) = copy_license_files(output, tmp.temp_dir.path())? {
tmp.add_files(license_files);
}
if output.build_configuration.store_recipe {
let recipe_files = write_recipe_folder(output, tmp.temp_dir.path())?;
tmp.add_files(recipe_files);
}
let test_files = write_test_files(output, tmp.temp_dir.path())?;
tmp.add_files(test_files);
// create any entry points or link.json for noarch packages
if output.recipe.build().noarch().is_python() {
let link_json = File::create(info_folder.join("link.json"))?;
serde_json::to_writer_pretty(link_json, &output.link_json()?)?;
tmp.add_files(vec![info_folder.join("link.json")]);
}
// print sorted files
tracing::info!("\nFiles in package:\n");
tmp.files
.iter()
.map(|x| x.strip_prefix(tmp.temp_dir.path()))
.collect::<Result<Vec<_>, _>>()?
.iter()
.sorted()
.for_each(|f| tracing::info!(" - {}", f.to_string_lossy()));
let output_folder =
local_channel_dir.join(output.build_configuration.target_platform.to_string());
tracing::info!("Creating target folder {:?}", output_folder);
fs::create_dir_all(&output_folder)?;
let identifier = output
.identifier()
.ok_or(PackagingError::BuildStringNotSet)?;
let out_path = output_folder.join(format!(
"{}{}",
identifier,
packaging_settings.archive_type.extension()
));
let file = File::create(&out_path)?;
tracing::info!("Compressing archive...");
let progress_bar = tool_configuration.fancy_log_handler.add_progress_bar(
indicatif::ProgressBar::new(0)
.with_prefix("Compressing ")
.with_style(tool_configuration.fancy_log_handler.default_bytes_style()),
);
match packaging_settings.archive_type {
ArchiveType::TarBz2 => {
write_tar_bz2_package(
file,
tmp.temp_dir.path(),
&tmp.files.iter().cloned().collect::<Vec<_>>(),
CompressionLevel::Numeric(packaging_settings.compression_level),
Some(&output.build_configuration.timestamp),
Some(Box::new(ProgressBar { progress_bar })),
)?;
}
ArchiveType::Conda => {
write_conda_package(
file,
tmp.temp_dir.path(),
&tmp.files.iter().cloned().collect::<Vec<_>>(),
CompressionLevel::Numeric(packaging_settings.compression_level),
packaging_settings.compression_threads,
&identifier,
Some(&output.build_configuration.timestamp),
Some(Box::new(ProgressBar { progress_bar })),
)?;
}
}
tracing::info!("Archive written to {:?}", out_path);
let paths_json = PathsJson::from_path(info_folder.join("paths.json"))?;
Ok((out_path, paths_json))
}
impl Output {
/// Create a conda package from any new files in the host prefix. Note: the previous stages should have been
/// completed before calling this function.
pub async fn create_package(
&self,
tool_configuration: &tool_configuration::Configuration,
) -> Result<(PathBuf, PathsJson), PackagingError> {
let span = tracing::info_span!("Packaging new files");
let _enter = span.enter();
let files_after = Files::from_prefix(
&self.build_configuration.directories.host_prefix,
self.recipe.build().always_include_files(),
)?;
package_conda(self, tool_configuration, &files_after)
}
}