pelicode-cli 0.1.0

Know where your code came from: finds code similar to published open-source packages, function by function
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
//! The `pelicode` command: crawl, index, stats, scan, extract, export and
//! languages, on top of the pelicode-core, -db, -detect and -crawl crates.

// The doc comments of the clap arguments are the text of `--help`, where
// `NAME[@VERSION]` is notation, not a link.
#![allow(rustdoc::broken_intra_doc_links)]

mod crawl;
mod git;
mod oci;
mod output;
mod sarif;
mod scan;
mod settings;
mod stats;

use std::fs::File;
use std::io::{self, BufWriter, Write};
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use clap::{Args, Parser, Subcommand, ValueEnum};
use pelicode_core::{
    Config, FileFeatures, IdentMode, Language, Level, discover, extract_all, slash_path,
};
use pelicode_db::{Database, PackageInfo};

/// Detects code similar to known open-source code at the function level.
#[derive(Parser)]
#[command(name = "pelicode", version, about)]
struct Cli {
    #[command(subcommand)]
    command: Command,
    /// Settings file [default: .pelicode.yaml, .pelicode.yml, .pelicode.json5 or .pelicode.json
    /// in the current directory or above, up to the root of the git repository].
    #[arg(long, global = true, value_name = "FILE", display_order = 900)]
    config: Option<PathBuf>,
    /// Ignore settings files.
    #[arg(long, global = true, conflicts_with = "config", display_order = 901)]
    no_config: bool,
}

#[derive(Subcommand)]
enum Command {
    /// Extracts the functions of every supported source file under the given
    /// directories and stores their features in the database.
    Index(IndexArgs),
    /// Extracts the functions of the given files or directories and prints their features.
    Extract(ExtractArgs),
    /// Prints the functions stored in the database.
    Export(ExportArgs),
    /// Reports stored functions similar to the functions of the given code.
    Scan(scan::ScanArgs),
    /// Fetches packages from a public repository (pypi, maven, go, rubygems, crates) and indexes them.
    Crawl(crawl::CrawlArgs),
    /// Shows database statistics and the hashes shared by the most packages.
    Stats(stats::StatsArgs),
    /// Removes files and functions no package version refers to any more, and shrinks the
    /// database file.
    Clean(CleanArgs),
    /// Writes a copy of the database for checking only, to be distributed: without the data
    /// only indexing needs, optionally limited to some ecosystems or to the latest versions.
    Snapshot(SnapshotArgs),
    /// Fetches a snapshot from an OCI registry into the cache, and prints its path.
    Pull(PullArgs),
    /// Lists the supported languages and their file extensions.
    Languages,
}

#[derive(Args)]
struct FeatureArgs {
    /// Functions with fewer tokens get exact hashes only (no fingerprints).
    #[arg(long, default_value_t = Config::default().min_tokens)]
    min_tokens: usize,
    /// How identifiers are abstracted.
    #[arg(long, default_value = "uniform")]
    ident_mode: IdentMode,
    /// Normalization levels for which fingerprints are computed.
    #[arg(long, value_delimiter = ',', default_value = "N1")]
    levels: Vec<Level>,
    /// k of the k-grams.
    #[arg(long, default_value_t = Config::default().kgram)]
    kgram: usize,
    /// Window size of winnowing.
    #[arg(long, default_value_t = Config::default().window)]
    window: usize,
    /// Length of MinHash signatures.
    #[arg(long, default_value_t = Config::default().minhash_size)]
    minhash_size: usize,
    /// Number of LSH bands.
    #[arg(long, default_value_t = Config::default().bands)]
    bands: usize,
}

impl FeatureArgs {
    /// The feature parameters as a validated [`Config`].
    fn config(&self, keep_tokens: bool) -> Result<Config> {
        let config = Config {
            min_tokens: self.min_tokens,
            ident_mode: self.ident_mode,
            kgram: self.kgram,
            window: self.window,
            minhash_size: self.minhash_size,
            bands: self.bands,
            fingerprint_levels: self.levels.clone(),
            keep_tokens,
        };
        config.validate()?;
        Ok(config)
    }
}

#[derive(Args)]
struct IndexArgs {
    /// Project directories (or files) to index.
    #[arg(required = true)]
    paths: Vec<PathBuf>,
    /// Database file [default: `database` of the settings file, else pelicode.db].
    #[arg(long)]
    db: Option<PathBuf>,
    /// Ecosystem the project belongs to.
    #[arg(long, default_value = "local")]
    ecosystem: String,
    /// Package name (default: the name of the first directory).
    #[arg(long)]
    name: Option<String>,
    /// Package version.
    #[arg(long, default_value = "unversioned")]
    version: String,
    /// License of the project (SPDX identifier).
    #[arg(long)]
    license: Option<String>,
    /// Repository URL of the project.
    #[arg(long)]
    repository: Option<String>,
    /// Format of the summary.
    #[arg(short, long, value_enum, default_value_t = SummaryFormat::Text)]
    format: SummaryFormat,
    /// Rebuild the per-hash package counts afterwards (see `pelicode stats`).
    #[arg(long)]
    refresh_stats: bool,
    #[command(flatten)]
    features: FeatureArgs,
}

#[derive(Args)]
struct ExtractArgs {
    /// Files or directories to extract.
    #[arg(required = true)]
    paths: Vec<PathBuf>,
    /// Output format.
    #[arg(short, long, value_enum, default_value_t = Format::Text)]
    format: Format,
    /// Output file (default: standard output).
    #[arg(short, long)]
    output: Option<PathBuf>,
    /// Include the normalized tokens (of LEVEL in text and CSV; of every level in JSON).
    #[arg(long, value_name = "LEVEL", num_args = 0..=1, default_missing_value = "N1")]
    tokens: Option<Level>,
    #[command(flatten)]
    features: FeatureArgs,
}

#[derive(Args)]
struct ExportArgs {
    /// Database file, or a snapshot as oci://REGISTRY/REPOSITORY:TAG [default: `database` of
    /// the settings file, else pelicode.db].
    #[arg(long)]
    db: Option<PathBuf>,
    /// Output format.
    #[arg(short, long, value_enum, default_value_t = Format::Text)]
    format: Format,
    /// Output file (default: standard output).
    #[arg(short, long)]
    output: Option<PathBuf>,
}

#[derive(Args)]
struct CleanArgs {
    /// Database file [default: `database` of the settings file, else pelicode.db].
    #[arg(long)]
    db: Option<PathBuf>,
    /// Only report what would be removed.
    #[arg(long)]
    dry_run: bool,
    /// Do not shrink the database file afterwards (VACUUM needs as much free disk space as the
    /// database takes).
    #[arg(long)]
    no_vacuum: bool,
    /// Format of the summary.
    #[arg(short, long, value_enum, default_value_t = SummaryFormat::Text)]
    format: SummaryFormat,
}

#[derive(Args)]
struct SnapshotArgs {
    /// Database file [default: `database` of the settings file, else pelicode.db].
    #[arg(long)]
    db: Option<PathBuf>,
    /// Snapshot file to write.
    #[arg(short, long)]
    out: PathBuf,
    /// Only these ecosystems (repeatable or comma-separated) [default: all].
    #[arg(long = "ecosystem", value_name = "ECOSYSTEM", value_delimiter = ',')]
    ecosystems: Vec<String>,
    /// Only the latest version of each package, by publication time.
    #[arg(long)]
    latest: bool,
    /// Replace the snapshot file if it exists.
    #[arg(long)]
    force: bool,
    /// Also write the annotations to give the snapshot in a registry, as an annotation file
    /// of ORAS (`oras push --annotation-file`).
    #[arg(long, value_name = "FILE")]
    annotations: Option<PathBuf>,
    /// Format of the summary.
    #[arg(short, long, value_enum, default_value_t = SummaryFormat::Text)]
    format: SummaryFormat,
}

#[derive(Args)]
struct PullArgs {
    /// The snapshot, as oci://REGISTRY/REPOSITORY:TAG or oci://REGISTRY/REPOSITORY@DIGEST.
    reference: String,
    /// Also copy the snapshot to this file.
    #[arg(short, long)]
    out: Option<PathBuf>,
    /// Only show what the registry says about the snapshot (digest, size, annotations),
    /// without downloading it.
    #[arg(long, conflicts_with = "out")]
    info: bool,
    /// Format of --info.
    #[arg(short, long, value_enum, default_value_t = SummaryFormat::Text)]
    format: SummaryFormat,
}

#[derive(Clone, Copy, ValueEnum)]
enum Format {
    Text,
    Json,
    Csv,
}

#[derive(Clone, Copy, ValueEnum)]
enum SummaryFormat {
    Text,
    Json,
}

/// Runs the command; output cut short by a closed pipe (`| head`) is not an error.
fn main() -> Result<()> {
    match run(Cli::parse()) {
        // Output piped into `head` and the like.
        Err(e) if is_broken_pipe(&e) => Ok(()),
        other => other,
    }
}

/// Whether `e` comes from writing to a closed pipe.
fn is_broken_pipe(e: &anyhow::Error) -> bool {
    e.chain().any(|cause| {
        cause
            .downcast_ref::<io::Error>()
            .is_some_and(|io| io.kind() == io::ErrorKind::BrokenPipe)
            || cause
                .downcast_ref::<csv::Error>()
                .is_some_and(|c| matches!(c.kind(), csv::ErrorKind::Io(io) if io.kind() == io::ErrorKind::BrokenPipe))
            || cause
                .downcast_ref::<serde_json::Error>()
                .is_some_and(|j| j.io_error_kind() == Some(io::ErrorKind::BrokenPipe))
    })
}

/// Dispatches to the subcommand; `crawl` and `scan` exit with status 1 on failure or match.
fn run(cli: Cli) -> Result<()> {
    let loaded = settings::load(cli.config.as_deref(), cli.no_config)?;
    let settings = loaded.as_ref();
    match cli.command {
        Command::Index(args) => index(args, settings),
        Command::Extract(args) => extract(args),
        Command::Export(args) => export(args, settings),
        Command::Stats(args) => stats::run(args, settings),
        Command::Clean(args) => clean(args, settings),
        Command::Snapshot(args) => snapshot(args, settings),
        Command::Pull(args) => pull(args),
        Command::Crawl(args) => {
            if !crawl::run(args, settings)? {
                std::process::exit(1);
            }
            Ok(())
        }
        Command::Scan(args) => {
            if scan::run(args, settings)? {
                std::process::exit(1);
            }
            Ok(())
        }
        Command::Languages => {
            for lang in Language::ALL {
                println!("{:<8} {}", lang.name(), lang.extensions().join(", "));
            }
            Ok(())
        }
    }
}

/// The databases to use: `--db`, else the `database` of the settings, else `pelicode.db`.
fn databases(db: &[PathBuf], settings: Option<&settings::Loaded>) -> Vec<PathBuf> {
    if !db.is_empty() {
        return db.to_vec();
    }
    let configured = settings
        .map(settings::Loaded::databases)
        .unwrap_or_default();
    if configured.is_empty() {
        vec![PathBuf::from("pelicode.db")]
    } else {
        configured
    }
}

/// The one database to use, for commands that take a single one.
fn database(db: &Option<PathBuf>, settings: Option<&settings::Loaded>) -> Result<PathBuf> {
    let mut all = databases(db.as_slice(), settings);
    if all.len() > 1 {
        bail!(
            "the settings file lists {} databases; give one with --db",
            all.len()
        );
    }
    Ok(all.remove(0))
}

/// Extracts every path; files that fail are reported on stderr and skipped.
/// Paths of files under a directory are relative to it; with several
/// directories they are prefixed with the directory name so that, e.g.,
/// `json/__init__.py` and `http/__init__.py` stay distinct.
fn extract_paths(paths: &[PathBuf], config: &Config) -> Result<Vec<FileFeatures>> {
    let mut results = Vec::new();
    for path in paths {
        let files = discover(path).with_context(|| format!("cannot scan {}", path.display()))?;
        let prefix = (paths.len() > 1 && path.is_dir())
            .then(|| path.canonicalize().ok()?.file_name().map(PathBuf::from))
            .flatten();
        for result in extract_all(&files, config) {
            match result {
                Ok(mut file) => {
                    if let Some(prefix) = &prefix {
                        file.path = slash_path(&prefix.join(&file.path));
                    }
                    results.push(file);
                }
                Err(e) => eprintln!("warning: {e}"),
            }
        }
    }
    Ok(results)
}

/// Size of an SQLite database with its write-ahead log, in bytes.
fn database_size(path: &Path) -> u64 {
    let size = |p: &Path| std::fs::metadata(p).map(|m| m.len()).unwrap_or(0);
    let mut wal = path.as_os_str().to_owned();
    wal.push("-wal");
    size(path) + size(Path::new(&wal))
}

/// `pelicode snapshot`: writes a copy of the database for checking only.
fn snapshot(args: SnapshotArgs, settings: Option<&settings::Loaded>) -> Result<()> {
    let db_path = database(&args.db, settings)?;
    if !db_path.exists() {
        bail!("{}: no such database", db_path.display());
    }
    if args.out.exists() {
        if !args.force {
            bail!(
                "{}: already exists (use --force to replace it)",
                args.out.display()
            );
        }
        std::fs::remove_file(&args.out)
            .with_context(|| format!("cannot remove {}", args.out.display()))?;
    }
    let db = Database::open_unchecked(&db_path)?;
    let options = pelicode_db::SnapshotOptions {
        ecosystems: args.ecosystems,
        latest_only: args.latest,
    };
    let summary = db
        .snapshot(&args.out, &options)
        .with_context(|| format!("cannot write {}", args.out.display()))?;
    match args.format {
        SummaryFormat::Text => println!(
            "{}: {} packages, {} versions, {} functions, {:.1} MB",
            args.out.display(),
            summary.packages,
            summary.versions,
            summary.functions,
            summary.bytes as f64 / 1024.0 / 1024.0
        ),
        SummaryFormat::Json => println!("{}", serde_json::to_string_pretty(&summary)?),
    }
    if let Some(path) = args.annotations {
        let file = serde_json::json!({ "$manifest": oci::annotations(&summary) });
        std::fs::write(&path, serde_json::to_string_pretty(&file)? + "\n")
            .with_context(|| format!("cannot write {}", path.display()))?;
    }
    Ok(())
}

/// `pelicode pull`: fetches a snapshot into the cache.
fn pull(args: PullArgs) -> Result<()> {
    let reference: oci::Reference = args.reference.parse()?;
    if args.info {
        let info = oci::info(&reference)?;
        match args.format {
            SummaryFormat::Text => {
                println!("{reference}");
                println!("  digest: {}", info.digest);
                println!("  size: {:.1} MB", info.size as f64 / 1024.0 / 1024.0);
                for (key, value) in &info.annotations {
                    let key = key.strip_prefix(oci::ANNOTATION_PREFIX).unwrap_or(key);
                    println!("  {key}: {value}");
                }
            }
            SummaryFormat::Json => println!("{}", serde_json::to_string_pretty(&info)?),
        }
        return Ok(());
    }
    let fetched = oci::fetch(&reference, &oci::cache_dir()?)?;
    eprintln!(
        "{} {reference} ({})",
        if fetched.downloaded {
            "fetched"
        } else {
            "up to date:"
        },
        fetched.digest
    );
    let path = match args.out {
        Some(out) => {
            std::fs::copy(&fetched.path, &out)
                .with_context(|| format!("cannot write {}", out.display()))?;
            out
        }
        None => fetched.path,
    };
    println!("{}", path.display());
    Ok(())
}

/// `pelicode clean`: removes unreferenced files and functions, then vacuums.
fn clean(args: CleanArgs, settings: Option<&settings::Loaded>) -> Result<()> {
    let db_path = database(&args.db, settings)?;
    if !db_path.exists() {
        bail!("{}: no such database", db_path.display());
    }
    let mut db = Database::open_unchecked(&db_path)?;
    let before = database_size(&db_path);
    let removed = if args.dry_run {
        db.unreferenced()?
    } else {
        let removed = db.clean()?;
        if !args.no_vacuum {
            db.vacuum()?;
        }
        removed
    };
    let after = database_size(&db_path);
    match args.format {
        SummaryFormat::Text => {
            let verb = if args.dry_run {
                "would remove"
            } else {
                "removed"
            };
            println!(
                "{verb} {} files and {} functions no longer referenced",
                removed.files, removed.functions
            );
            if !args.dry_run {
                let mb = |b: u64| b as f64 / 1024.0 / 1024.0;
                println!("database: {:.1} MB -> {:.1} MB", mb(before), mb(after));
            }
        }
        SummaryFormat::Json => {
            let value = serde_json::json!({
                "dry_run": args.dry_run,
                "removed": removed,
                "size_before": before,
                "size_after": after,
            });
            println!("{}", serde_json::to_string_pretty(&value)?);
        }
    }
    Ok(())
}

/// `pelicode index`: extracts the given paths and stores them as one package version.
fn index(args: IndexArgs, settings: Option<&settings::Loaded>) -> Result<()> {
    let config = args.features.config(false)?;
    let db_path = database(&args.db, settings)?;
    let name = match &args.name {
        Some(name) => name.clone(),
        None => default_name(&args.paths[0])?,
    };
    let files = extract_paths(&args.paths, &config)?;
    let mut db = Database::open(&db_path, &config)
        .with_context(|| format!("cannot open {}", db_path.display()))?;
    let package = PackageInfo {
        ecosystem: args.ecosystem,
        name,
        version: args.version,
        license_spdx: args.license,
        repository_url: args.repository,
        ..PackageInfo::default()
    };
    let summary = db.index(&package, &files)?;
    if args.refresh_stats {
        db.refresh_hash_stats()?;
    }
    let stats = db.stats()?;
    match args.format {
        SummaryFormat::Text => {
            println!(
                "indexed {}:{}@{} into {}",
                package.ecosystem,
                package.name,
                package.version,
                db_path.display()
            );
            println!(
                "  files: {} ({} new), functions: {} new ({} with fingerprints)",
                summary.files, summary.new_files, summary.functions, summary.indexed_functions
            );
            println!(
                "  database: {} packages, {} versions, {} files, {} functions",
                stats.packages, stats.versions, stats.source_files, stats.functions
            );
        }
        SummaryFormat::Json => {
            let value = serde_json::json!({
                "package": {
                    "ecosystem": package.ecosystem,
                    "name": package.name,
                    "version": package.version,
                },
                "summary": summary,
                "database": stats,
            });
            println!("{}", serde_json::to_string_pretty(&value)?);
        }
    }
    Ok(())
}

/// Package name from the name of `path` (or of its directory when it is a file).
fn default_name(path: &Path) -> Result<String> {
    let path = path
        .canonicalize()
        .with_context(|| format!("cannot access {}", path.display()))?;
    let dir = if path.is_file() {
        path.parent().unwrap_or(&path)
    } else {
        &path
    };
    match dir.file_name() {
        Some(name) => Ok(name.to_string_lossy().into_owned()),
        None => bail!(
            "cannot infer a package name from {}; use --name",
            path.display()
        ),
    }
}

/// `pelicode extract`: prints the features of the given paths, without a database.
fn extract(args: ExtractArgs) -> Result<()> {
    let config = args.features.config(args.tokens.is_some())?;
    let files = extract_paths(&args.paths, &config)?;
    let mut out = open_output(args.output.as_deref())?;
    match args.format {
        Format::Text => output::extract_text(&mut out, &files, args.tokens)?,
        Format::Json => output::json(&mut out, &files)?,
        Format::Csv => output::extract_csv(&mut out, &files, args.tokens)?,
    }
    out.flush()?;
    Ok(())
}

/// `pelicode export`: prints the functions stored in the database.
fn export(args: ExportArgs, settings: Option<&settings::Loaded>) -> Result<()> {
    let db_path = oci::resolve(&database(&args.db, settings)?)?;
    if !db_path.exists() {
        bail!("{}: no such database", db_path.display());
    }
    let db = Database::open_unchecked(&db_path)?;
    let rows = db.functions()?;
    let mut out = open_output(args.output.as_deref())?;
    match args.format {
        Format::Text => output::export_text(&mut out, &rows)?,
        Format::Json => output::json(&mut out, &rows)?,
        Format::Csv => output::export_csv(&mut out, &rows)?,
    }
    out.flush()?;
    Ok(())
}

/// A buffered writer to `path`, or to the standard output.
fn open_output(path: Option<&Path>) -> Result<Box<dyn Write>> {
    Ok(match path {
        Some(path) => Box::new(BufWriter::new(
            File::create(path).with_context(|| format!("cannot create {}", path.display()))?,
        )),
        None => Box::new(BufWriter::new(io::stdout().lock())),
    })
}