netbox-community / Device-Type-Library-Import

This library is intended to assist with importing device and module types into NetBox from the NetBox Community DeviceType-Library
https://github.com/netbox-community/devicetype-library
MIT License
248 stars 75 forks source link

Remove Repo init from __init__ #76

Closed p-rintz closed 1 year ago

p-rintz commented 1 year ago

Initializing the repository from init, before the path has been updated, will lead an exception when the script is not run directly from the repository.

Example exception (during the run in an ansible playbook): git.exc.InvalidGitRepositoryError: /home/ansible

I ran the script successfully directy from the folder as well as from outside with the line removed: # python3 nb-dt-import.py # /opt/Netbox-Device-Type-Library-Import/.venv/bin/python3 /opt/Netbox-Device-Type-Library-Import/nb-dt-import.py

danner26 commented 1 year ago

Hey @p-rintz good catch. I am in the middle of doing a complete overhaul and I really appreciate you catching this potential regression (might have always been there? dunno). Either way this was a great catch and in the latest release which is yet to come, the actual root cause of the issue is fixed. Now we will utilize REPO_PATH = f"{os.path.dirname(os.path.realpath(__file__))}/repo" for the repo path, which will ensure the folder is always within the same location as the script.

danner26 commented 1 year ago

In testing, it looks like this issue is exasperated more so the further away you get. Anyway I have implemented a full fix in the dev branch I have right now.. Can you give me access to your repo so I can push those changes into your PR?

That or please implement these changes. gitcmd.py Remove self.repo = Repo() on line 14

nb-dt-import.py Change base_path = './repo/device-types/' to base_path = f'{settings.REPO_PATH}/device-types/' on line 56 Change base_path = './repo/module-types/' to base_path = f'{settings.REPO_PATH}/module-types/' on line 91

settings.py Change REPO_PATH = "./repo" to REPO_PATH = f"{os.path.dirname(os.path.realpath(__file__))}/repo" on line 15

danner26 commented 1 year ago

Thanks @p-rintz! Could you please pull down and test my changes? Once you approve I will merge this :)

p-rintz commented 1 year ago

I will do so tomorrow, once I'm at work. Currently 1am here. :smile: Thanks for the quick fix.

danner26 commented 1 year ago

Awesome! Happy to help

p-rintz commented 1 year ago

LGTM

Ran through fine both from inside and outside the script-folder.