debcargo 2.8.4

Create a Debian package from a Cargo crate.
Documentation
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
use cargo::core::manifest;
use chrono::{DateTime, Datelike};
use git2::Repository;
use regex;
use tempfile;
use textwrap::fill;
use walkdir;

use std::cmp::Ordering;
use std::collections::BTreeMap;
use std::env;
use std::fmt;
use std::fs;
use std::io::{BufRead, BufReader, Read};
use std::path::Path;

use crate::errors::Result;

const DEB_COPYRIGHT_FORMAT: &str = "\
     https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/";

macro_rules! format_para {
    ($fmt: expr, $text:expr) => {{
        for line in $text.lines() {
            let line = line.trim_end();
            if line.is_empty() {
                writeln!($fmt, " .")?;
            } else {
                writeln!($fmt, " {}", line)?;
            }
        }
        write!($fmt, "")
    }};
}

struct UpstreamInfo {
    name: String,
    contacts: Vec<String>,
    source: String,
}

#[derive(Clone)]
pub struct Files {
    files: String,
    copyright: Vec<String>,
    license: String,
    comment: String,
}

#[derive(Clone)]
struct License {
    name: String,
    text: String,
}

pub struct DebCopyright {
    format: String,
    upstream: UpstreamInfo,
    files: Vec<Files>,
    licenses: Vec<License>,
    exclusions: Vec<String>,
}

impl fmt::Display for DebCopyright {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Format: {}", self.format)?;
        write!(f, "{}", self.upstream)?;
        if !self.exclusions.is_empty() {
            write!(f, "Files-Excluded:")?;
            for excluded in &self.exclusions {
                write!(f, "\n {excluded}")?;
            }
            writeln!(f)?;
        }

        for file in &self.files {
            write!(f, "\n{file}")?;
        }

        for license in &self.licenses {
            write!(f, "\n{license}")?;
        }

        Ok(())
    }
}

impl DebCopyright {
    fn new(u: UpstreamInfo, f: &[Files], l: &[License], e: &[String]) -> DebCopyright {
        DebCopyright {
            format: DEB_COPYRIGHT_FORMAT.to_string(),
            upstream: u,
            files: f.to_vec(),
            licenses: l.to_vec(),
            exclusions: e.to_vec(),
        }
    }
}

impl fmt::Display for UpstreamInfo {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Upstream-Name: {}", self.name)?;
        write!(f, "Upstream-Contact:")?;
        if self.contacts.len() > 1 {
            writeln!(f)?;
        }
        for contact in &self.contacts {
            writeln!(f, " {contact}")?;
        }
        if !self.source.is_empty() {
            writeln!(f, "Source: {}", self.source)?;
        }

        Ok(())
    }
}

impl UpstreamInfo {
    fn new(name: String, authors: &[String], repo: &str) -> UpstreamInfo {
        assert!(!authors.is_empty());
        UpstreamInfo {
            name,
            contacts: authors.to_vec(),
            source: repo.to_string(),
        }
    }
}

impl fmt::Display for Files {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "Files: {}", self.files)?;
        write!(f, "Copyright:")?;
        if self.copyright.len() > 1 {
            writeln!(f)?;
        }
        for copyright in &self.copyright {
            writeln!(f, " {copyright}")?;
        }
        writeln!(f, "License: {}", self.license)?;
        if !self.comment.is_empty() {
            writeln!(f, "Comment:")?;
            format_para!(f, &self.comment)?;
        }
        Ok(())
    }
}

impl Files {
    pub fn new<T: ToString>(name: &str, notice: &[T], license: &str, comment: &str) -> Files {
        assert!(!notice.is_empty());
        Files {
            files: name.to_string(),
            copyright: notice.iter().map(ToString::to_string).collect(),
            license: license.to_string(),
            comment: comment.to_string(),
        }
    }

    pub fn files(&self) -> &str {
        &self.files
    }

    pub fn license(&self) -> &str {
        &self.license
    }
}

impl fmt::Display for License {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "License: {}", self.name)?;
        format_para!(f, &self.text)?;
        Ok(())
    }
}

impl License {
    fn new(name: String, text: String) -> License {
        License { name, text }
    }
}

macro_rules! default_files {
    ($file:expr, $notice:expr) => {{
        let comment = concat!(
            "FIXME (overlay): These notices are extracted from files. Please ",
            "review them before uploading to the archive."
        );
        Files::new(
            $file,
            $notice,
            "UNKNOWN-LICENSE; FIXME (overlay)",
            &fill(comment, 79),
        )
    }};
}

fn gen_files(debsrcdir: &Path) -> Result<Vec<Files>> {
    let mut copyright_notices = BTreeMap::new();

    let copyright_notice_re =
        regex::Regex::new(r"(?:[Cc]opyright|©)(?:\s|[©:,()Cc<])*\b(\d{4}\b.*)$")?;

    // Get current working directory and move inside the extracted source of
    // crate. This is necessary in order to capture correct path for files in
    // debian/copyright.
    let current_dir = env::current_dir()?;
    env::set_current_dir(debsrcdir)?;

    // Here we specifically use "." to avoid absolute paths. If we use
    // current_dir then we end up having absolute path from user home directory,
    // which again messes debian/copyright.
    // Use of . creates paths in format ./src/, so we strip the leading "./"
    for entry in walkdir::WalkDir::new(".").sort_by(|a, b| a.file_name().cmp(b.file_name())) {
        let entry = entry?;
        if entry.file_type().is_file() {
            let copyright_file = entry
                .path()
                .strip_prefix("./")?
                .to_str()
                .unwrap()
                .to_string();
            let file = fs::File::open(entry.path())?;
            let reader = BufReader::new(file);
            for line in reader.lines() {
                if let Ok(line) = line {
                    if let Some(m) = copyright_notice_re.captures(&line) {
                        let m = m.get(1).unwrap();
                        let start = m.start();
                        let end = m.end();
                        let notice = line[start..end]
                            .trim_end()
                            .trim_end_matches(". See the COPYRIGHT")
                            .to_string();
                        if !copyright_notices.contains_key(&copyright_file) {
                            copyright_notices.insert(copyright_file.clone(), vec![]);
                        }
                        copyright_notices
                            .get_mut(&copyright_file)
                            .unwrap()
                            .push(notice);
                    }
                } else {
                    break;
                }
            }
        }
    }

    // Restore back to original working directory as we can continue without
    // problems.
    env::set_current_dir(current_dir.as_path())?;

    let mut notices: Vec<Files> = Vec::new();
    for (filename, notice) in &copyright_notices {
        notices.push(default_files!(filename, notice));
    }

    Ok(notices)
}

fn get_licenses(license: &str) -> Result<Vec<License>> {
    let mut licenses = BTreeMap::new();
    let sep = regex::Regex::new(r"(?i:(\s(or|and)\s|/))")?;
    let known_licenses = vec![
        ("agpl-3.0", include_str!("licenses/AGPL-3.0")),
        ("apache-2.0", include_str!("licenses/Apache-2.0")),
        ("bsd-2-clause", include_str!("licenses/BSD-2-Clause")),
        ("bsd-3-clause", include_str!("licenses/BSD-3-Clause")),
        ("cc0-1.0", include_str!("licenses/CC0-1.0")),
        ("gpl-2.0", include_str!("licenses/GPL-2.0")),
        ("gpl-3.0", include_str!("licenses/GPL-3.0")),
        ("isc", include_str!("licenses/ISC")),
        ("lgpl-2.0", include_str!("licenses/LGPL-2.0")),
        ("lgpl-2.1", include_str!("licenses/LGPL-2.1")),
        ("lgpl-3.0", include_str!("licenses/LGPL-3.0")),
        ("mit", include_str!("licenses/MIT")),
        ("mitnfa", include_str!("licenses/MITNFA")),
        ("mpl-1.1", include_str!("licenses/MPL-1.1")),
        ("mpl-2.0", include_str!("licenses/MPL-2.0")),
        ("unlicense", include_str!("licenses/Unlicense")),
        ("zlib", include_str!("licenses/Zlib")),
    ]
    .into_iter()
    .collect::<BTreeMap<_, _>>();

    let lses: Vec<&str> = sep.split(license).filter(|s| !s.is_empty()).collect();
    for ls in lses {
        let lname = ls
            .trim()
            .to_lowercase()
            .trim_end_matches('+')
            .trim_end_matches("-or-later")
            .to_string();
        let text = match known_licenses.get(lname.as_str()) {
            Some(s) => s,
            None => {
                "FIXME (overlay): Unrecognized crate license, please find the \
                     full license text in the rest of the crate source code and \
                     copy-paste it here"
            }
        };
        licenses.insert(ls.trim().to_string(), text.to_string());
    }

    let mut lblocks: Vec<License> = Vec::new();
    if !licenses.is_empty() {
        for (l, t) in licenses {
            lblocks.push(License::new(l, t));
        }
    }

    Ok(lblocks)
}

fn copyright_fromgit(repo_url: &str) -> Result<String> {
    let tempdir = tempfile::Builder::new()
        .prefix("debcargo")
        .tempdir_in(".")?;
    let repo = Repository::clone(repo_url, tempdir.path())?;

    let mut revwalker = repo.revwalk()?;
    revwalker.push_head()?;

    // Get the latest and first commit id. This is a bit ugly
    let latest_id = revwalker.next().unwrap()?;
    let first_id = revwalker.last().unwrap()?; // revwalker ends here is consumed by last

    let first_commit = repo.find_commit(first_id)?;
    let latest_commit = repo.find_commit(latest_id)?;

    let first_year = &DateTime::from_timestamp(first_commit.time().seconds(), 0)
        .ok_or(anyhow::Error::msg("lo"))?
        .year();

    let latest_year = &DateTime::from_timestamp(latest_commit.time().seconds(), 0)
        .ok_or(anyhow::Error::msg("lo"))?
        .year();

    let notice = match first_year.cmp(latest_year) {
        Ordering::Equal => format!("{first_year}"),
        _ => format!("{first_year}-{latest_year},"),
    };

    Ok(notice)
}

#[expect(clippy::too_many_arguments)]
pub fn debian_copyright(
    srcdir: &Path,
    manifest: &manifest::Manifest,
    manifest_path: &Path,
    maintainer: &str,
    uploaders: &[&str],
    year_range: (i32, i32),
    guess_harder: bool,
    exclusions: &[String],
) -> Result<DebCopyright> {
    let meta = manifest.metadata().clone();
    let repository = meta.repository.unwrap_or_default();

    // The Authors field is optional according to
    // https://rust-lang.github.io/rfcs/3052-optional-authors-field.html
    // and crates.io publishes crates without the field already.
    let unknown_authors = vec!["FIXME (overlay) UNKNOWN-AUTHORS".to_string()];
    let authors = if meta.authors.is_empty() {
        &unknown_authors
    } else {
        &meta.authors
    };

    let upstream = UpstreamInfo::new(manifest.name().to_string(), authors, &repository);

    let mut licenses: Vec<License> = Vec::new();
    let mut crate_license: String = String::new();

    if let Some(ref license_file_name) = meta.license_file {
        let license_file = manifest_path.with_file_name(license_file_name);
        let mut text = Vec::new();
        licenses.reserve(1);
        let stext = match fs::File::open(license_file).and_then(|mut f| f.read_to_end(&mut text)) {
            Ok(_) => String::from_utf8(text)?,
            Err(e) => {
                format!("Failed to read license from {license_file_name} - {e:?}")
            }
        };
        licenses.push(License::new(
            "UNKNOWN-LICENSE; FIXME (overlay)".to_string(),
            stext,
        ));
    } else if let Some(ref license) = meta.license {
        licenses = get_licenses(license)?;
        crate_license = license
            .trim()
            .replace('/', " or ")
            .replace(" OR ", " or ")
            .replace(" AND ", " and ");
    } else {
        debcargo_bail!("Crate has no license or license_file");
    }

    let mut files = gen_files(srcdir)?;

    let (y0, y1) = year_range;
    let years = if y0 == y1 {
        format!("{y0}")
    } else {
        format!("{y0}-{y1}")
    };
    let mut deb_notice = vec![format!("{} {}", years, maintainer)];
    deb_notice.extend(uploaders.iter().map(|s| format!("{years} {s}")));
    files.push(Files::new("debian/*", &deb_notice, &crate_license, ""));

    // Insert catch all block as the first block of copyright file. Capture
    // copyright notice from git log of the upstream repository.
    let years = if guess_harder && !repository.is_empty() {
        match copyright_fromgit(&repository) {
            Ok(x) => x,
            Err(e) => {
                debcargo_warn!(
                    "Failed to generate d/copyright from git repository {}: {}\n",
                    repository,
                    e
                );
                "FIXME (overlay) UNKNOWN-YEARS".to_string()
            }
        }
    } else {
        "FIXME (overlay) UNKNOWN-YEARS".to_string()
    };
    let notice = match meta.authors.len() {
        0 => vec![format!("FIXME (overlay) UNKNOWN-AUTHORS {}", years)],
        1 => vec![format!("{} {}", years, &meta.authors[0])],
        _ => meta
            .authors
            .iter()
            .map(|s| format!("{years} {s}"))
            .collect(),
    };
    let comment = concat!(
        "FIXME (overlay): Since upstream copyright years are not available ",
        "in Cargo.toml, they were extracted from the upstream Git ",
        "repository. This may not be correct information so you should ",
        "review and fix this before uploading to the archive."
    );
    files.insert(
        0,
        Files::new("*", notice.as_slice(), &crate_license, &fill(comment, 79)),
    );

    Ok(DebCopyright::new(upstream, &files, &licenses, exclusions))
}

#[cfg(test)]
mod tests;