Skip to content

Commit

Permalink
Convert to build as a module using hatchling
Browse files Browse the repository at this point in the history
Converts the project to be structured so that it can be built into
wheels using `python3 -m build`. The build system chosen is hatchling,
because it is very simple to use.

Note that now instead of using ./main.py to invoke the script, the
module installs a command called `spec-parser` that is used instead
  • Loading branch information
JPEWdev committed Feb 29, 2024
1 parent ca9541f commit 3e3a737
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 21 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
Automagically process the model of the SPDXv3 specification to validate input or to generate stuff.


## Installation

```
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
. ./venv/bin/activate
# Install module and dependencies in editable mode
pip install -e .
```

## Usage

```
python3 ./main.py -h
usage: main.py [-h] [-d] [-f] [-n] [-q] [-v] [-V] input_dir [output_dir]
$ spec-parser -h
usage: spec-parser [-h] [-d] [-f] [-n] [-q] [-v] [-V] input_dir [output_dir]
Generate documentation from an SPDX 3.0 model
Expand All @@ -30,15 +43,12 @@ Note that not all flags are functional yet.
### Checking input

```
python3 main.py -n some/where/.../model
spec-parser -n some/where/.../model
```

Note that no dependencies are needed.

### Generate output
```
python3 -m pip install -r requirements.txt
python3 main.py some/where/.../model some/where/else/.../output_dir
spec-parser some/where/.../model some/where/else/.../output_dir
```


Expand Down
41 changes: 41 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[project]
name = "spec-parser"
description = "Parse SPDX Spec Markdown Files"
dynamic = ["version"]
dependencies = [
"Jinja2 == 3.1.2",
"jsonpickle == 3.0.2",
"rdflib == 7.0.0",
]
required-python = ">= 3.8"
authors = [
{name = "Joshua Watt", email = "[email protected]"},
]
readme = "README.md"
classifiers = [
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Build Tools",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
]

[project.urls]
Homepage = "https://github.com/spdx/spec-parser"
Repository = "https://github.com/spdx/spec-parser.git"
Issues = "https://github.com/spdx/spec-parser/issues"

[project.scripts]
spec-parser = "spec_parser.main:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.version]
path = "src/spec_parser/__init__.py"
3 changes: 0 additions & 3 deletions requirements.txt

This file was deleted.

File renamed without changes.
12 changes: 12 additions & 0 deletions src/spec_parser/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /usr/bin/env python3
#
# Copyright (c) 2024 Joshua Watt
#
# SPDX-License-Identifier: MIT

import sys

from .main import main

if __name__ == "__main__":
sys.exit(main())
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions main.py → src/spec_parser/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

# SPDX-License-Identifier: Apache-2.0

from spec_parser import Model
from runparams import RunParams
from . import Model
from .runparams import RunParams

if __name__ == "__main__":
def main():
cfg = RunParams()

m = Model(cfg.input_dir)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 9 additions & 8 deletions runparams.py → src/spec_parser/runparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ def __init__(self):
self._ts = datetime.now(timezone.utc)
self.process_args()


@property
def autogen_header(self):
return f"Automatically generated by spec-parser v{self.parser_version} on {self._ts.isoformat()}"

@property
def input_dir(self):
return self.args.input_dir

@property
def output_dir(self):
return self.args.output_dir

@property
def opt_debug(self):
return self.args.debug
Expand All @@ -36,7 +36,7 @@ def opt_force(self):
@property
def opt_nooutput(self):
return self.args.nooutput

@property
def opt_quiet(self):
return self.args.quiet
Expand All @@ -47,8 +47,9 @@ def opt_verbose(self):

@property
def parser_version(self):
return sys.modules['spec_parser'].__version__

import spec_parser
return spec_parser.__version__

@property
def all_as_dict(self):
return { k: getattr(self, k) for k in (
Expand All @@ -66,7 +67,7 @@ def all_as_dict(self):
# - separate output dirs for mkdocs / RDF /JSON-LD / ...
# - maybe flags whether something might not be generated?
# - etc.

def process_args(self, args=sys.argv[1:]):
parser = argparse.ArgumentParser(description="Generate documentation from an SPDX 3.0 model")
parser.add_argument("input_dir", help="Directory containing the input specification files")
Expand All @@ -76,7 +77,7 @@ def process_args(self, args=sys.argv[1:]):
parser.add_argument("-n", "--nooutput", action="store_true", help="Do not generate anything, only check input")
parser.add_argument("-q", "--quiet", action="store_true", help="Print no output")
parser.add_argument("-v", "--verbose", action="store_true", help="Print verbose output")
parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {RunParams.parser_version}")
parser.add_argument("-V", "--version", action="version", version=f"%(prog)s {self.parser_version}")
self.args = parser.parse_args(args)

if self.opt_nooutput:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 3e3a737

Please sign in to comment.