Skip to content

Commit

Permalink
Fix for multiple extension entry bug (+ api change) in 'build' subcmd (
Browse files Browse the repository at this point in the history
…#170)

In the prior commit there was a bug which prevented multiple extensions
from being entered via the `build` subcmd (it would always take the last
one entered).

The below code
```python
build_sp.add_argument("-n", "--ext-name", nargs=True, help="name of extension")
build_sp.add_argument("-p", "--ext-path", nargs=True, help="path of extension")
```
really should have been 
```python
build_sp.add_argument("-n", "--ext-name", nargs='+', help="name of extension")
build_sp.add_argument("-p", "--ext-path", nargs='+', help="path of extension")
```

But it was found that this would force the user to enter names and files
separately:

```bash
python -m pydust build \
	--ext-name 'fib._lib' 'fib._hello' \
	--ext-path 'fib/fib.zig' 'fib/hello.zig'
```

While this is not terrible, this PR proposes the following new api for
entering extensions from the commandline instead:

```python
build_sp.add_argument("-e", "--extensions", nargs='+', help="space separated list of extension '<name>=<path>' entries")
```
which would allow the entry of multiple extensions as follows:

```bash
python -m pydust build --extensions 'fib._lib=fib/fib.zig' 'fib._hello=fib/hello.zig'
# or
python -m pydust build -e 'fib._lib=fib/fib.zig' 'fib._hello=fib/hello.zig'
```
  • Loading branch information
shakfu committed Oct 3, 2023
1 parent f902d87 commit 35eddc4
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pydust/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@
build_sp.add_argument("-b", "--build-zig", default="build.zig", help="build.zig file")
build_sp.add_argument("-m", "--self-managed", default=False, action="store_true", help="self-managed mode")
build_sp.add_argument("-a", "--limited-api", default=True, action="store_true", help="use limited python c-api")
build_sp.add_argument("-n", "--ext-name", nargs=True, help="name of extension")
build_sp.add_argument("-p", "--ext-path", nargs=True, help="path of extension")

build_sp.add_argument("-e", "--extensions", nargs='+', help="space separated list of extension '<name>=<path>' entries")

def main():
args = parser.parse_args()
Expand All @@ -43,11 +41,12 @@ def main():
build(args)

def build(args):
"""Given a list of (name, path) pairs, compiles zig-based python extensions"""
names, paths = args.ext_name, args.ext_path
assert (names and paths) and (len(names) == len(paths)), "requires at least one pair of --ext-name and --ext-path"
"""Given a list of '<name>=<path>' entries, compiles corresponding zig-based python extensions"""
assert args.extensions and all('=' in ext for ext in args.extensions),\
"requires at least one --extensions '<name>=<path>'"
ext_items = [tuple(ext.split('=')) for ext in args.extensions]
_extensions = []
for name, path in zip(names, paths):
for name, path in ext_items:
_extensions.append(
config.ExtModule(name=name, root=path, limited_api=args.limited_api)
)
Expand Down

0 comments on commit 35eddc4

Please sign in to comment.