comply 0.1.1

REUSE Specification compliance tool and library: lint, annotate, and fix SPDX license headers
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
// SPDX-FileCopyrightText: COMPLY contributors
//
// SPDX-License-Identifier: MIT OR Apache-2.0

use std::path::Path;

use super::FileType;
use crate::config;

/// Check if the file extension is a known programming language source file.
fn is_source_extension(ext: &str) -> bool {
    matches!(
        ext,
        "rs" | "c"
            | "cpp"
            | "cc"
            | "cxx"
            | "h"
            | "hpp"
            | "hxx"
            | "java"
            | "kt"
            | "kts"
            | "py"
            | "pyx"
            | "pxd"
            | "pxi"
            | "js"
            | "jsx"
            | "ts"
            | "tsx"
            | "go"
            | "rb"
            | "sh"
            | "bash"
            | "zsh"
            | "fish"
            | "swift"
            | "scala"
            | "sc"
            | "zig"
            | "dart"
            | "cs"
            | "fs"
            | "fsx"
            | "fsi"
            | "erl"
            | "hrl"
            | "ex"
            | "exs"
            | "hs"
            | "lhs"
            | "lua"
            | "pl"
            | "pm"
            | "lisp"
            | "clj"
            | "cljs"
            | "cljc"
            | "pas"
            | "pp"
            | "r"
            | "rkt"
            | "ml"
            | "mli"
            | "v"
            | "vh"
            | "sv"
            | "vhd"
            | "vhdl"
    )
}

/// Check if the file name is a well-known source file without extension.
fn is_well_known_source_name(name: &str) -> bool {
    matches!(
        name.to_ascii_lowercase().as_str(),
        "makefile"
            | "dockerfile"
            | "gemfile"
            | "rakefile"
            | "cargo.lock"
            | "build.gradle"
            | "settings.gradle"
            | "gradle.properties"
            | "pom.xml"
    )
}

/// Check if the file extension is a binary format.
fn is_binary_extension(ext: &str) -> bool {
    matches!(
        ext,
        "png"
            | "jpg"
            | "jpeg"
            | "gif"
            | "ico"
            | "svg"
            | "pdf"
            | "zip"
            | "tar"
            | "gz"
            | "bz2"
            | "xz"
            | "o"
            | "so"
            | "dll"
            | "dylib"
            | "exe"
            | "bin"
            | "class"
            | "jar"
            | "ttf"
            | "otf"
            | "woff"
            | "woff2"
            | "icns"
    )
}

/// Check if the path contains ignored directory components.
fn has_ignored_component(path: &Path) -> bool {
    path.components()
        .any(|c| crate::scope::IGNORED_DIRS.contains(&c.as_os_str().to_string_lossy().as_ref()))
}

/// Classify a file for REUSE analysis.
///
/// REUSE covers every file in the project, so the default is [`FileType::Source`]
/// and only three things fall outside it: paths under a VCS or build directory,
/// the license texts themselves, and formats with no comment syntax -- which are
/// [`FileType::Binary`], meaning their licensing belongs in a `.license` companion.
pub(super) fn classify(path: &Path, source_patterns: &[String]) -> FileType {
    let name = path
        .file_name()
        .map_or_else(String::new, |n| n.to_string_lossy().to_string());

    if has_ignored_component(path) {
        return FileType::Ignored;
    }

    // An explicit `source-patterns` entry in REUSE.toml outranks every default
    // below, so a project can force a file to be treated as commentable source.
    let rel_path = path
        .strip_prefix(std::env::current_dir().unwrap_or_default())
        .unwrap_or(path)
        .to_string_lossy()
        .replace('\\', "/");
    if source_patterns
        .iter()
        .any(|p| config::glob_match(p, &rel_path))
    {
        return FileType::Source;
    }

    let ext = name
        .split('.')
        .next_back()
        .unwrap_or("")
        .to_ascii_lowercase();

    if is_source_extension(&ext) || is_well_known_source_name(&name) {
        return FileType::Source;
    }

    if is_binary_extension(&ext) || is_uncommentable_text_extension(&ext) {
        return FileType::Binary;
    }

    if is_license_text_name(&name) {
        return FileType::Ignored;
    }

    FileType::Source
}

/// Text formats that carry no comment syntax comply can emit, so their
/// licensing has to live in a `.license` companion rather than a header.
fn is_uncommentable_text_extension(ext: &str) -> bool {
    matches!(ext, "json")
}

/// The project's own license texts, which carry no licensing of their own.
///
/// Matches `LICENSE`, `LICENCE`, `COPYING` and suffixed variants such as
/// `LICENSE-APACHE` or `COPYING.LESSER`, while leaving names that merely
/// mention licensing (`licensing.md`, `LICENSES.md`) covered.
fn is_license_text_name(name: &str) -> bool {
    let stem = name.split('.').next().unwrap_or(name).to_ascii_lowercase();
    matches!(stem.as_str(), "license" | "licence" | "copying")
        || stem.starts_with("license-")
        || stem.starts_with("licence-")
}

/// Check if a path should be ignored for REUSE analysis.
#[must_use]
pub fn is_ignored(path: &Path) -> bool {
    path.components()
        .any(|c| crate::scope::is_pruned_dir(c.as_os_str().to_string_lossy().as_ref()))
}

// REUSE-IgnoreStart
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn classify_rust_source() {
        let p = Path::new("src/lib.rs");
        assert_eq!(classify(p, &[]), FileType::Source);
    }

    #[test]
    fn a_custom_source_pattern_overrides_the_default_classification() {
        let p = Path::new("assets/logo.png");

        assert_eq!(
            classify(p, &[]),
            FileType::Binary,
            "a png needs a companion by default"
        );
        assert_eq!(
            classify(p, &["**/*.png".to_string()]),
            FileType::Source,
            "source-patterns from REUSE.toml force a file to be treated as source"
        );
        assert_eq!(
            classify(p, &["**/*.other".to_string()]),
            FileType::Binary,
            "a pattern that does not match changes nothing"
        );
    }

    #[test]
    fn anything_under_a_vcs_or_build_directory_is_ignored() {
        for p in ["target/debug/build.rs", "node_modules/pkg/index.js"] {
            assert_eq!(
                classify(Path::new(p), &[]),
                FileType::Ignored,
                "{p} carries no licensing obligation"
            );
        }
    }

    #[test]
    fn classify_python_source() {
        let p = Path::new("main.py");
        assert_eq!(classify(p, &[]), FileType::Source);
    }

    #[test]
    fn classify_png_binary() {
        let p = Path::new("image.png");
        assert_eq!(classify(p, &[]), FileType::Binary);
    }

    #[test]
    fn classify_jpg_binary() {
        let p = Path::new("photo.jpg");
        assert_eq!(classify(p, &[]), FileType::Binary);
    }

    /// REUSE has no by-extension exemption: documentation and configuration
    /// are Covered Files and need licensing information like anything else.
    /// Expectations here match the observed behaviour of `reuse` 6.2.0.
    #[test]
    fn documentation_and_configuration_are_covered() {
        for p in [
            "README.md",
            "CHANGELOG.md",
            "notes.txt",
            "Cargo.toml",
            "config.yaml",
            "settings.ini",
            "schema.sql",
            "page.html",
            ".gitignore",
            "Cargo.lock",
            "SECURITY",
            "NOTICE",
            "AUTHORS",
        ] {
            assert_eq!(classify(Path::new(p), &[]), FileType::Source, "{p}");
        }
    }

    #[test]
    fn formats_with_no_comment_syntax_need_a_companion() {
        assert_eq!(
            classify(Path::new("data.json"), &[]),
            FileType::Binary,
            "JSON cannot carry a comment, so licensing goes in a .license file"
        );
    }

    #[test]
    fn stylesheets_and_rst_carry_their_own_header() {
        for p in [
            "style.css",
            "theme.scss",
            "vars.sass",
            "mixins.less",
            "guide.rst",
        ] {
            assert_eq!(
                classify(Path::new(p), &[]),
                FileType::Source,
                "{p} has a comment syntax comply can write"
            );
        }
    }

    #[test]
    fn the_licence_texts_themselves_are_not_covered() {
        for p in [
            "LICENSE",
            "LICENSE-APACHE",
            "LICENSE-MIT",
            "LICENSE.txt",
            "LICENCE",
            "COPYING",
            "COPYING.LESSER",
        ] {
            assert_eq!(classify(Path::new(p), &[]), FileType::Ignored, "{p}");
        }
    }

    #[test]
    fn a_name_merely_mentioning_licensing_is_still_covered() {
        for p in ["licensing.md", "LICENSES.md", "copying-guide.txt"] {
            assert_eq!(classify(Path::new(p), &[]), FileType::Source, "{p}");
        }
    }

    #[test]
    fn classify_makefile() {
        let p = Path::new("Makefile");
        assert_eq!(classify(p, &[]), FileType::Source);
    }

    #[test]
    fn classify_dockerfile() {
        let p = Path::new("Dockerfile");
        assert_eq!(classify(p, &[]), FileType::Source);
    }

    #[test]
    fn classify_unknown_extension_is_covered() {
        let p = Path::new("somefile.xyz");
        assert_eq!(
            classify(p, &[]),
            FileType::Source,
            "an unrecognised file is still a Covered File"
        );
    }

    #[test]
    fn classify_so_binary() {
        let p = Path::new("libfoo.so");
        assert_eq!(classify(p, &[]), FileType::Binary);
    }

    #[test]
    fn classify_dll_binary() {
        let p = Path::new("libfoo.dll");
        assert_eq!(classify(p, &[]), FileType::Binary);
    }

    #[test]
    fn is_ignored_git_dir() {
        let p = Path::new("/project/.git/config");
        assert!(is_ignored(p));
    }

    #[test]
    fn is_ignored_node_modules() {
        let p = Path::new("/project/node_modules/foo/index.js");
        assert!(is_ignored(p));
    }

    #[test]
    fn is_ignored_target_dir() {
        let p = Path::new("/project/target/debug/foo");
        assert!(is_ignored(p));
    }

    #[test]
    fn is_not_ignored_src_dir() {
        let p = Path::new("/project/src/lib.rs");
        assert!(!is_ignored(p));
    }

    #[test]
    fn is_ignored_licenses_dir() {
        let p = Path::new("/project/LICENSES/MIT.txt");
        assert!(is_ignored(p));
    }

    #[test]
    fn is_ignored_reuse_dir() {
        let p = Path::new("/project/.reuse/dep5");
        assert!(is_ignored(p));
    }
}
// REUSE-IgnoreEnd