Skip to content

Commit

Permalink
Experimental/swig (#193)
Browse files Browse the repository at this point in the history
* swig

- adding basic swig

* working snmpwalk

- okay this is a working copy of snmpwalk :). Happy test cases so far.

* Remove main from name

- removed main from the function name. Trying to make it more "pythonic".

* Update README.md

- yay it runs :), why right more code when we can reused what has already been done in the net-snmp/apps code space. Obviously, it isn't very pythonic at the moment, but thats okay for now.

* ReOrg

- moving stuff into sub folders
- pulling in other apps from net-snmp/apps

* Interface .i files

- adding more inferface i files. one for each c file.

* Rename

- starting to rename items in src and headers plus interface files.

* Single Interface File

- making a single entry for all the modules.

* Compile & Readme

- updating the readme to compile
- made one interface to pull in the other modules

* Tweaks

- changing where we dump the py file
- updating the readme

* Return string vector

- attempt at having snmpwalk return a string vector instead of just printing to stdout.

* Compile

 - getting closer to compilation.

* Python Package

- attempt to convert into a python package via setup.py

* MacOS

- this compile on mac os

* Compiles On MacOS

- this compiles on macos with clang, but not linux with gcc.

* Py Module

- success, this now works and I can use it inside a python3 terminal

* snmpwalk

- snmpwalk has been vectorized.

* Docs

- updating readme with new steps
- cleaning up the interface file for snmpwalk.

* Update snmpwalk.cpp

- no more go to out

* snmpget

- working on snmpget now :)

* Patches

- making a dir for patches
- doc the cmd that was used
- this will be the starting point of pulling in upstream changes and applying the patches on it.

* Patch for snmpget

- making patch for snmpget.
- last few tweaks to the snmpget source code.

* snmpbulkwalk

- done, with snmpbulkwak

* Create snmpbulkwalk.patch

- made the snmpbulkwalk patch file

* Black

- ran black

* snmpbulkget

- snmpbulkget updated to match rest of interface to return vectors.

* Opts

- optimizing the c++ wrapper for speed.
- reading more swig docs.

* autodoc

- adding support for python3 type hints
- removing/fixing swig alerts

* Fix

- fixing print of retval

* add_first_arg

-  using the cpp add_first_arg helper to always add the program name to the first argv[0] element. This reduces down what the user has to pass in via higher level languages like python3

* V2.0.0a1

- reworked the directory to have only one copy of "ezsnmp". I removed the old one in favor of the new swig one. As it stands this version is not backwards compatible API wise with the old V1.X.X versions.

* Update README.md

- I need to move these too the README.rst file.

* Workflows

- updating workflows to run on new dev branch.

* Rename Fixes

- fixing some things that missed in the rename from ezsnmp_swig to ezsnmp.

* outdir

- updated the outdir in the readme file.

* packaging

- attempting to fix the python module packaging.

* Update __init__.py

- let's leave __init__ empty for now.

* swig Version 4.2.1

- use Version 4.2.1

* Update __init__.py

- shorten namespace imports

* Docs

- saving some notes
- updating the readme

* Readme

- a few tweaks prep for first alpha release.
  • Loading branch information
carlkidcrypto authored Sep 18, 2024
1 parent c8a6790 commit e5a6134
Show file tree
Hide file tree
Showing 41 changed files with 12,821 additions and 5,931 deletions.
Binary file added .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Black

on:
push:
branches: [ main ]
branches: [ dev/v2.0.0 ]

pull_request:
branches: [ main ]
branches: [ dev/v2.0.0 ]

jobs:
black:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build_and_publish_to_test_pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: TestPyPI Distributions
concurrency: build_and_publish_to_test_pypi
on:
push:
branches: [main]
branches: [ dev/v2.0.0 ]

jobs:
check-source-changes:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/integration_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Integration Tests

on:
push:
branches: [ main ]
branches: [ dev/v2.0.0 ]

pull_request:
branches: [ main ]
branches: [ dev/v2.0.0 ]

jobs:
check-source-changes:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/sphinx_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: "Pull Request Sphinx Docs Check"

on:
push:
branches: [ main ]
branches: [ dev/v2.0.0 ]

pull_request:
branches: [ main ]
branches: [ dev/v2.0.0 ]

jobs:
check-source-changes:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ name: Tests

on:
push:
branches: [ main ]
branches: [ dev/v2.0.0 ]

pull_request:
branches: [ main ]
branches: [ dev/v2.0.0 ]

jobs:
check-source-changes:
Expand Down
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ dev/
*/*.log
*.log
wheelhouse/**
.DS_Store
*.o
**.DS_Store**
**venv**
11 changes: 6 additions & 5 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ them with the following on Linux:
sudo systemctl start snmpd;
rm -drf build/ dist/ ezsnmp.egg-info;
python3 -m pip install -r requirements.txt;
python3 setup.py build && python3 -m pip install -e . && python3 -m pytest .;
python3 -m pip install . && python3 -m pytest tests/.;
# Bottom one for debug. Replace the top one with it if needed.
# python3 setup.py build && python3 -m pip install -e . && gdb -ex run -ex bt -ex quit --args python3 -m pytest .;
# python3 -m pip install . && gdb -ex run -ex bt -ex quit --args python3 -m pytest .;
# Bottom one for valgrind. Replace the top one with it if needed.
# python3 setup.py build && python3 -m pip install -e . && valgrind --tool=memcheck --leak-check=full --show-leak-kinds=definite,indirect,possible python3 -m pytest .
# python3 -m pip install . && valgrind --tool=memcheck --leak-check=full --show-leak-kinds=definite,indirect,possible python3 -m pytest .
# Bottom one for valgrind using helgrind. Replace the top one with it if needed.
# python3 setup.py build && python3 -m pip install -e . && valgrind --tool=helgrind --free-is-write=yes python3 -m pytest .
# python3 -m pip install . && valgrind --tool=helgrind --free-is-write=yes python3 -m pytest .
On MacOS
Expand All @@ -183,7 +183,8 @@ On MacOS
sudo launchctl unload /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist;
sudo launchctl load -w /System/Library/LaunchDaemons/org.net-snmp.snmpd.plist;
rm -drf build/ dist/ ezsnmp.egg-info;
python3 setup.py build && python3 -m pip install -e . && python3 -m pytest .;
python3 -m pip install -r requirements.txt;
python3 -m pip install . && python3 -m pytest tests/.;
Running cibuildwheels
Expand Down
49 changes: 49 additions & 0 deletions ezsnmp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Making the swig interface files

One look for the netsnmp app file under <https://github.com/net-snmp/net-snmp/tree/5e691a85bcd95a42872933515698309e57832cfc/apps>

Two copy the c file over, for example `snmpwalk.c`. Then rename to change the extension to `.cpp`.

Three make a header file for it `snmpwalk.h` and extract methods/functions from the source code.

Four run the command below to generate the wrap file.

```bash
swig -c++ -python -builtin -outdir ezsnmp/. -o ezsnmp/src/ezsnmp_wrap.cpp ezsnmp/interface/ezsnmp.i
```

* `-c++` to force generation of a `.cpp` file
* `-python` to build a python module
* `-builtin` to build with native python data types. [Python_builtin_types](https://swig.org/Doc4.0/Python.html#Python_builtin_types)

Five run

```python3
clear && rm -drf build ezsnmp.egg-info && python3 -m pip install .
```

Six run it in python3

```bash
python3
>>> import ezsnmp
>>> args = ["-v" , "3", "-u", "secondary_sha_aes", "-a", "SHA", "-A", "auth_second", "-x", "AES", "-X" ,"priv_second", "-l", "authPriv", "localhost:11161"]
>>> retval = ezsnmp.snmpwalk(args)
>>> print(retval)
```

## Making the patch files

Within the patches directory run the following command.

```bash
diff -Naurw ~/Downloads/net-snmp-master/apps/snmpwalk.c ../src/snmpwalk.cpp > snmpwalk.patch
```

consider the following names for the api.
`snmp` is redundant in the name since the module `ezsnmp` already has it in its' name.
snmpwalk --> ezsnmp.walk
snmpbulkwalk --> ezsnmp.bulk_walk
snmpget --> ezsnmp.get
snmpbulkget --> ezsnmp.bulk_get
etc...
23 changes: 1 addition & 22 deletions ezsnmp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1 @@
#!/usr/bin/env python3

from .ez import ( # noqa
snmp_get,
snmp_set,
snmp_set_multiple,
snmp_get_next,
snmp_get_bulk,
snmp_walk,
snmp_bulkwalk,
)
from .exceptions import ( # noqa
EzSNMPError,
EzSNMPConnectionError,
EzSNMPTimeoutError,
EzSNMPUnknownObjectIDError,
EzSNMPNoSuchObjectError,
EzSNMPNoSuchInstanceError,
EzSNMPUndeterminedTypeError,
)
from .session import Session # noqa
from .variables import SNMPVariable # noqa
from .ezsnmp import snmpbulkget, snmpget, snmpbulkwalk, snmpwalk
61 changes: 0 additions & 61 deletions ezsnmp/exceptions.py

This file was deleted.

Loading

0 comments on commit e5a6134

Please sign in to comment.