Skip to content

Commit 4472e65

Browse files
authored
docs: mismatch cli utility (#4348)
Signed-off-by: Meet Soni <[email protected]>
1 parent 74edc7e commit 4472e65

File tree

3 files changed

+143
-1
lines changed

3 files changed

+143
-1
lines changed

.github/actions/spelling/allow.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ api
1616
apk
1717
apparmor
1818
ares
19+
argparse
1920
Args
2021
asn
2122
asottile
@@ -702,6 +703,7 @@ URI
702703
uri
703704
URIs
704705
url
706+
URLs
705707
urlopen
706708
usecase
707709
username

doc/mismatch_cli.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Mismatch Database Management Tool Documentation
2+
3+
## Overview
4+
5+
The Mismatch Database Management Tool is a command-line utility designed to facilitate the management and querying of a mismatch database. This tool provides two main functionalities:
6+
7+
1. **Lookup**: Retrieve vendor information for a given Package URL (PURL) from the mismatch database.
8+
2. **Loader**: Set up or refresh the mismatch database using data from a specified directory.
9+
10+
## Prerequisites
11+
12+
Ensure the following Python modules are installed:
13+
14+
- `sqlite3`: For SQLite database operations.
15+
- `argparse`: For command-line argument parsing.
16+
- `cve_bin_tool.mismatch_loader`: To execute the mismatch loader functionality.
17+
18+
## Directory Structure
19+
20+
The script operates within a specific directory structure as shown below:
21+
22+
```
23+
project_root/
24+
25+
├── data/ # Directory containing the mismatch relations
26+
27+
└── mismatch/ # Directory containing the script
28+
└── cli.py # The main script file
29+
```
30+
31+
## Data Directory
32+
33+
The `data/` directory is a key component of the Mismatch Database Management Tool. It houses the structured data files that are used to populate or refresh the mismatch database.
34+
35+
### Directory Structure
36+
37+
The structure of the `data/` directory is organized as follows:
38+
39+
```
40+
data/
41+
└── namespace/
42+
└── product/
43+
└── mismatch_relations.yml
44+
```
45+
46+
### Explanation of Contents
47+
48+
- **namespace/**: This directory represents a logical grouping, such as a software ecosystem, project, or domain.
49+
50+
- **product/**: Each `namespace/` contains one or more `product/` directories. Each directory corresponds to a specific product within that namespace.
51+
52+
- **mismatch_relations.yml**: This YAML file inside each `product/` directory contains the data related to mismatches. It typically includes:
53+
54+
- **purls**: A list of package URLs (PURLs) associated with the product. For example:
55+
```yaml
56+
purls:
57+
- pkg:pypi/zstandard
58+
```
59+
60+
- **invalid_vendors**: A list of vendors that are considered invalid or mismatched for the given PURLs. For example:
61+
```yaml
62+
invalid_vendors:
63+
- facebook
64+
```
65+
66+
## Usage
67+
68+
The script can be executed using different subcommands, each serving a distinct purpose.
69+
70+
### 1. Lookup Command
71+
72+
The `lookup` command allows you to search for vendor information based on a PURL in the mismatch database.
73+
74+
**Syntax:**
75+
```bash
76+
python -m mismatch.cli lookup <purl> [--database <db_file>]
77+
```
78+
79+
**Parameters:**
80+
- `<purl>`: (Required) The Package URL to search within the mismatch database.
81+
- `--database`: (Optional) Path to the SQLite database file. Defaults to the pre-configured location if not provided.
82+
83+
**Example:**
84+
```bash
85+
python -m mismatch.cli lookup pkg:namespace/product --database /path/to/mismatch.db
86+
```
87+
88+
### 2. Loader Command
89+
90+
The `loader` command initializes or updates the mismatch database using data from a specified directory.
91+
92+
**Syntax:**
93+
```bash
94+
python -m mismatch.cli loader [--dir <data_dir>] [--database <db_file>]
95+
```
96+
97+
**Parameters:**
98+
- `--dir`: (Optional) Directory containing the data files to be loaded. Defaults to the \`data/\` directory in the project root.
99+
- `--database`: (Optional) Path to the SQLite database file. Defaults to the pre-configured location if not provided.
100+
101+
**Example:**
102+
```bash
103+
python -m mismatch.cli loader --dir /path/to/data --database /path/to/mismatch.db
104+
```
105+
106+
### 3. Default Behavior
107+
108+
If the script is executed without any subcommands, it defaults to running the `loader` command using the default directory and database file:
109+
110+
**Syntax:**
111+
```bash
112+
python -m mismatch.cli
113+
```
114+
115+
This command will trigger the `loader` functionality with default parameters.
116+
117+
## Example Commands
118+
119+
Here are some sample commands that demonstrate how to use the script:
120+
121+
1. **Look Up Vendor Information for a PURL**:
122+
```bash
123+
python -m mismatch.cli lookup pkg:namespace/product
124+
```
125+
126+
2. **Refresh the Database Using Default Settings**:
127+
```bash
128+
python -m mismatch.cli
129+
```
130+
131+
3. **Specify Custom Data Directory and Database File for Loader**:
132+
```bash
133+
python -m mismatch.cli loader --dir /custom/data/dir --database /custom/db/file.db
134+
```
135+
136+
## Conclusion
137+
138+
The Mismatch Database Management Tool is a robust utility designed to streamline the management and querying of a mismatch database.

mismatch/cli.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import argparse
22
import os
33
import sqlite3
4+
from pathlib import Path
45

5-
from cve_bin_tool.cvedb import DBNAME, DISK_LOCATION_DEFAULT
66
from cve_bin_tool.mismatch_loader import run_mismatch_loader
77

8+
DBNAME = "cve.db"
9+
DISK_LOCATION_DEFAULT = Path("~").expanduser() / ".cache" / "cve-bin-tool"
810
parent_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
911
data_dir = os.path.join(parent_dir, "data")
1012
dbpath = DISK_LOCATION_DEFAULT / DBNAME

0 commit comments

Comments
 (0)