license-trace 0.1.3

A recursive license tracer, obligations evaluator, and OSS compliance analyzer across dependency graphs
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
use anyhow::{Context, Result};
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::path::Path;
use std::process::Command;

use crate::model::graph::DependencyGraph;
use crate::model::package::{DependencyScope, DependencyType, PackageId, PackageInfo};

pub struct CargoResolver;

impl CargoResolver {
    /// Cargo プロジェクトの依存関係とライセンスを解決
    pub fn resolve_project(project_dir: &Path) -> Result<DependencyGraph> {
        let cargo_toml_path = project_dir.join("Cargo.toml");
        if !cargo_toml_path.exists() {
            anyhow::bail!("No Cargo.toml found at '{}'", project_dir.display());
        }

        // 1. まず cargo metadata コマンドの実行を試行 (最も正確)
        if let Ok(graph) = Self::resolve_via_cargo_metadata(project_dir) {
            return Ok(graph);
        }

        // 2. フォールバック: Cargo.toml + Cargo.lock 手動解析
        Self::resolve_via_files(project_dir)
    }

    /// `cargo metadata --format-version 1` による正確な依存関係・ライセンス解析
    fn resolve_via_cargo_metadata(project_dir: &Path) -> Result<DependencyGraph> {
        let output = Command::new("cargo")
            .arg("metadata")
            .arg("--format-version")
            .arg("1")
            .current_dir(project_dir)
            .output()
            .context("Failed to execute cargo metadata")?;

        if !output.status.success() {
            anyhow::bail!(
                "cargo metadata exited with non-zero status: {}",
                String::from_utf8_lossy(&output.stderr)
            );
        }

        let meta: Value = serde_json::from_slice(&output.stdout)
            .context("Failed to parse cargo metadata JSON")?;

        let packages = meta
            .get("packages")
            .and_then(|v| v.as_array())
            .context("No packages in metadata")?;

        let resolve = meta
            .get("resolve")
            .and_then(|v| v.as_object())
            .context("No resolve in metadata")?;

        let root_resolve_id = resolve.get("root").and_then(|v| v.as_str());

        // パッケージ情報マップ (id -> PackageInfo)
        let mut pkg_map: HashMap<String, (PackageInfo, String, String)> = HashMap::new();
        let mut root_pkg_id: Option<PackageId> = None;
        let mut root_full_id: Option<String> = None;

        for pkg in packages {
            let full_id = pkg
                .get("id")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string();
            let name = pkg
                .get("name")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string();
            let version = pkg
                .get("version")
                .and_then(|v| v.as_str())
                .unwrap_or_default()
                .to_string();
            let manifest_path = pkg.get("manifest_path").and_then(|v| v.as_str());

            let mut license = pkg
                .get("license")
                .and_then(|v| v.as_str())
                .unwrap_or("")
                .trim()
                .to_string();
            let mut license_text: Option<String> = None;

            if let Some(m_path) = manifest_path {
                let crate_dir = Path::new(m_path).parent();
                if let Some(dir) = crate_dir {
                    // 1. 実ファイルの全文を読み取る
                    license_text = load_crate_license_text(dir);

                    // 2. license フィールドが空の場合のみファイルから特定
                    if license.is_empty() || license.eq_ignore_ascii_case("unknown") {
                        if let Some(detected) = detect_crate_license_files(dir) {
                            license = detected;
                        }
                    }
                }
            }

            if license.is_empty() {
                license = "UNKNOWN".to_string();
            }

            let is_root = root_resolve_id == Some(&full_id)
                || (root_resolve_id.is_none() && root_pkg_id.is_none());

            let pkg_id = PackageId::new(&name, &version);
            let dep_type = if is_root {
                DependencyType::Direct
            } else {
                DependencyType::Transitive
            };
            let mut info = PackageInfo::new(
                pkg_id.clone(),
                &license,
                dep_type,
                DependencyScope::Production,
            );

            info.description = pkg
                .get("description")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());
            info.repository = pkg
                .get("repository")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string());
            info.manifest_path = manifest_path.map(|s| s.to_string());
            info.license_text = license_text;

            if is_root {
                root_pkg_id = Some(pkg_id);
                root_full_id = Some(full_id.clone());
            }

            pkg_map.insert(full_id, (info, name, version));
        }

        let root_pkg = if let Some(r_id) = &root_full_id {
            pkg_map
                .get(r_id)
                .map(|(p, _, _)| p.clone())
                .context("Root package not found")?
        } else {
            anyhow::bail!("Could not identify root package");
        };

        let mut graph = DependencyGraph::new(root_pkg);

        // 全パッケージを事前にグラフへ登録
        for (info, _, _) in pkg_map.values() {
            graph.get_or_add_node(info.clone());
        }

        // ノード解決グラフの nodes 配列からエッジを追加
        if let Some(nodes) = resolve.get("nodes").and_then(|v| v.as_array()) {
            for node in nodes {
                let parent_full_id = node.get("id").and_then(|v| v.as_str()).unwrap_or_default();
                let parent_pkg_id = if let Some((p, _, _)) = pkg_map.get(parent_full_id) {
                    p.id.clone()
                } else {
                    continue;
                };

                // deps 配列から依存先を取得
                if let Some(deps) = node.get("deps").and_then(|v| v.as_array()) {
                    for dep in deps {
                        let dep_full_id =
                            dep.get("pkg").and_then(|v| v.as_str()).unwrap_or_default();
                        if let Some((dep_info, _, _)) = pkg_map.get(dep_full_id) {
                            let mut dep_info_cloned = dep_info.clone();
                            if parent_pkg_id == graph.root_id {
                                dep_info_cloned.dep_type = DependencyType::Direct;
                            }
                            graph.add_dependency(&parent_pkg_id, dep_info_cloned);
                        }
                    }
                }
            }
        }

        Ok(graph)
    }

    /// ファイル直接読み込みフォールバック
    fn resolve_via_files(project_dir: &Path) -> Result<DependencyGraph> {
        let cargo_toml_path = project_dir.join("Cargo.toml");
        let toml_content = fs::read_to_string(&cargo_toml_path)?;
        let toml_val: toml_simple::SimpleToml = toml_simple::parse(&toml_content);

        let root_name = toml_val
            .get("package.name")
            .unwrap_or_else(|| "my-crate".to_string());
        let root_ver = toml_val
            .get("package.version")
            .unwrap_or_else(|| "0.1.0".to_string());
        let root_lic = toml_val
            .get("package.license")
            .unwrap_or_else(|| "UNKNOWN".to_string());

        let root_pkg = PackageInfo::new(
            PackageId::new(&root_name, &root_ver),
            &root_lic,
            DependencyType::Direct,
            DependencyScope::Production,
        );

        let mut graph = DependencyGraph::new(root_pkg);

        let cargo_lock_path = project_dir.join("Cargo.lock");
        if cargo_lock_path.exists() {
            let lock_content = fs::read_to_string(&cargo_lock_path)?;
            Self::parse_cargo_lock_fallback(&lock_content, &mut graph, &root_name)?;
        }

        Ok(graph)
    }

    fn parse_cargo_lock_fallback(
        lock_content: &str,
        graph: &mut DependencyGraph,
        root_name: &str,
    ) -> Result<()> {
        let packages = toml_simple::parse_cargo_lock_packages(lock_content);

        // 各パッケージを追加(推測ではなく UNKNOWN を付与し安全にレビュー要求)
        for pkg_entry in &packages {
            if pkg_entry.name == root_name {
                continue;
            }

            let info = PackageInfo::new(
                PackageId::new(&pkg_entry.name, &pkg_entry.version),
                "UNKNOWN",
                DependencyType::Transitive,
                DependencyScope::Production,
            );
            graph.get_or_add_node(info);
        }

        // 依存関係エッジを結ぶ
        for pkg_entry in &packages {
            let parent_id = PackageId::new(&pkg_entry.name, &pkg_entry.version);
            for dep_str in &pkg_entry.dependencies {
                let dep_name = dep_str.split_whitespace().next().unwrap_or(dep_str);
                if let Some(dep_pkg) = graph
                    .all_packages()
                    .into_iter()
                    .find(|p| p.id.name == dep_name)
                    .cloned()
                {
                    if pkg_entry.name == root_name {
                        graph.add_dependency(&graph.root_id.clone(), dep_pkg);
                    } else {
                        graph.add_dependency(&parent_id, dep_pkg);
                    }
                }
            }
        }

        Ok(())
    }
}

/// クレートのディレクトリから実際のライセンスファイルの中身を読み込む
fn load_crate_license_text(crate_dir: &Path) -> Option<String> {
    let mut texts = Vec::new();

    let candidate_files = [
        "LICENSE",
        "LICENSE.txt",
        "LICENSE.md",
        "LICENCE",
        "LICENCE.txt",
        "LICENCE.md",
        "LICENSE-MIT",
        "LICENSE-MIT.txt",
        "LICENSE-MIT.md",
        "LICENSE-APACHE",
        "LICENSE-APACHE.txt",
        "LICENSE-APACHE.md",
        "LICENSE-BOOST",
        "LICENSE-BSL",
        "COPYING",
        "COPYING.txt",
        "COPYING.md",
        "UNLICENSE",
        "NOTICE",
        "NOTICE.txt",
        "NOTICE.md",
        "ThirdPartyNotices",
        "ThirdPartyNotices.txt",
        "ThirdPartyNotices.md",
        "THIRD-PARTY-LICENSES",
        "THIRD-PARTY-LICENSES.txt",
        "THIRD-PARTY-LICENSES.md",
        "THIRD-PARTY-NOTICES",
        "THIRD-PARTY-NOTICES.txt",
        "THIRD-PARTY-NOTICES.md",
        "THIRDPARTY",
        "THIRDPARTY.txt",
        "THIRDPARTY.md",
    ];

    for fname in &candidate_files {
        let p = crate_dir.join(fname);
        if p.is_file() {
            if let Ok(content) = fs::read_to_string(&p) {
                let trimmed = content.trim();
                if !trimmed.is_empty() {
                    texts.push(format!("--- File: {} ---\n{}", fname, trimmed));
                }
            }
        }
    }

    if texts.is_empty() {
        None
    } else {
        Some(texts.join("\n\n"))
    }
}

/// クレートのディレクトリからライセンスファイルを検索して特定
fn detect_crate_license_files(crate_dir: &Path) -> Option<String> {
    let has_mit = crate_dir.join("LICENSE-MIT").exists()
        || crate_dir.join("LICENSE-MIT.md").exists()
        || crate_dir.join("LICENSE-MIT.txt").exists();
    let has_apache = crate_dir.join("LICENSE-APACHE").exists()
        || crate_dir.join("LICENSE-APACHE.md").exists()
        || crate_dir.join("LICENSE-APACHE.txt").exists();

    if has_mit && has_apache {
        return Some("MIT OR Apache-2.0".to_string());
    }
    if has_mit {
        return Some("MIT".to_string());
    }
    if has_apache {
        return Some("Apache-2.0".to_string());
    }

    let general_licenses = [
        "LICENSE",
        "LICENSE.txt",
        "LICENSE.md",
        "LICENCE",
        "COPYING",
        "UNLICENSE",
    ];
    for fname in &general_licenses {
        let p = crate_dir.join(fname);
        if p.exists() {
            if let Ok(content) = fs::read_to_string(&p) {
                let upper = content.to_uppercase();
                if upper.contains("MIT LICENSE")
                    || upper.contains("PERMISSION IS HEREBY GRANTED, FREE OF CHARGE")
                {
                    return Some("MIT".to_string());
                } else if upper.contains("APACHE LICENSE") && upper.contains("VERSION 2.0") {
                    return Some("Apache-2.0".to_string());
                } else if upper.contains("BSD 3-CLAUSE")
                    || upper.contains("REDISTRIBUTION AND USE IN SOURCE AND BINARY")
                {
                    return Some("BSD-3-Clause".to_string());
                } else if upper.contains("BSD 2-CLAUSE") {
                    return Some("BSD-2-Clause".to_string());
                } else if upper.contains("ISC LICENSE") {
                    return Some("ISC".to_string());
                } else if upper.contains("MOZILLA PUBLIC LICENSE") {
                    return Some("MPL-2.0".to_string());
                } else if upper.contains("PUBLIC DOMAIN") || upper.contains("UNLICENSE") {
                    return Some("Unlicense".to_string());
                } else if upper.contains("BOOST SOFTWARE LICENSE") {
                    return Some("BSL-1.0".to_string());
                }
            }
        }
    }

    // Cargo.toml の中身から license を再度パース
    let cargo_toml_path = crate_dir.join("Cargo.toml");
    if let Ok(content) = fs::read_to_string(&cargo_toml_path) {
        for line in content.lines() {
            let trimmed = line.trim();
            if let Some(rest) = trimmed.strip_prefix("license") {
                let rest_trimmed = rest.trim();
                if let Some(val) = rest_trimmed.strip_prefix('=') {
                    let lic = val.trim().trim_matches('"').trim_matches('\'').trim();
                    if !lic.is_empty() && !lic.eq_ignore_ascii_case("unknown") {
                        return Some(lic.to_string());
                    }
                }
            }
        }
    }

    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_inspect_cargo_metadata() {
        let output = Command::new("cargo")
            .args(["metadata", "--format-version", "1"])
            .output()
            .unwrap();
        let meta: Value = serde_json::from_slice(&output.stdout).unwrap();
        let packages = meta.get("packages").and_then(|v| v.as_array()).unwrap();
        for p in packages {
            let name = p.get("name").and_then(|v| v.as_str()).unwrap_or_default();
            if name == "clap" || name == "comfy-table" || name == "futures" || name == "mio" {
                println!(
                    "CRATE: {} => license: {:?}, license_file: {:?}, manifest_path: {:?}",
                    name,
                    p.get("license"),
                    p.get("license_file"),
                    p.get("manifest_path")
                );
            }
        }
    }
}

mod toml_simple {
    use std::collections::HashMap;

    pub struct SimpleToml {
        values: HashMap<String, String>,
    }

    impl SimpleToml {
        pub fn get(&self, key: &str) -> Option<String> {
            self.values.get(key).cloned()
        }
    }

    pub fn parse(content: &str) -> SimpleToml {
        let mut values = HashMap::new();
        let mut current_section = "".to_string();

        for line in content.lines() {
            let line = line.trim();
            if line.starts_with('[') && line.ends_with(']') {
                current_section = line[1..line.len() - 1].trim().to_string();
                continue;
            }
            if let Some((k, v)) = line.split_once('=') {
                let k = k.trim();
                let v = v.trim().trim_matches('"').trim_matches('\'').trim();
                let full_key = if current_section.is_empty() {
                    k.to_string()
                } else {
                    format!("{}.{}", current_section, k)
                };
                values.insert(full_key, v.to_string());
            }
        }

        SimpleToml { values }
    }

    #[derive(Debug)]
    pub struct CargoLockPackage {
        pub name: String,
        pub version: String,
        pub dependencies: Vec<String>,
    }

    pub fn parse_cargo_lock_packages(content: &str) -> Vec<CargoLockPackage> {
        let mut packages = Vec::new();
        let mut in_package = false;
        let mut in_dependencies = false;

        let mut curr_name = String::new();
        let mut curr_ver = String::new();
        let mut curr_deps = Vec::new();

        for line in content.lines() {
            let trimmed = line.trim();
            if trimmed == "[[package]]" {
                if in_package && !curr_name.is_empty() {
                    packages.push(CargoLockPackage {
                        name: curr_name.clone(),
                        version: curr_ver.clone(),
                        dependencies: curr_deps.clone(),
                    });
                }
                in_package = true;
                in_dependencies = false;
                curr_name.clear();
                curr_ver.clear();
                curr_deps.clear();
                continue;
            }

            if !in_package {
                continue;
            }

            if in_dependencies {
                if trimmed.starts_with(']') {
                    in_dependencies = false;
                } else if trimmed.starts_with('"') {
                    let dep = trimmed
                        .trim_matches(',')
                        .trim_matches('"')
                        .trim()
                        .to_string();
                    curr_deps.push(dep);
                }
                continue;
            }

            if let Some((k, v)) = trimmed.split_once('=') {
                let k = k.trim();
                let v = v.trim();
                if k == "name" {
                    curr_name = v.trim_matches('"').to_string();
                } else if k == "version" {
                    curr_ver = v.trim_matches('"').to_string();
                } else if k == "dependencies" {
                    if v.starts_with('[') && v.ends_with(']') {
                        let inner = &v[1..v.len() - 1].trim();
                        if !inner.is_empty() {
                            for item in inner.split(',') {
                                let dep = item.trim().trim_matches('"').trim().to_string();
                                if !dep.is_empty() {
                                    curr_deps.push(dep);
                                }
                            }
                        }
                        in_dependencies = false;
                    } else if v.starts_with('[') {
                        in_dependencies = true;
                    }
                }
            }
        }

        if in_package && !curr_name.is_empty() {
            packages.push(CargoLockPackage {
                name: curr_name,
                version: curr_ver,
                dependencies: curr_deps,
            });
        }

        packages
    }
}