From 4e33aded0a644cac3a17338ad8809574179e2143 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrique=20Sebasti=C3=A3o?= Date: Thu, 29 Aug 2024 06:20:45 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=92=EF=B8=8F=20ci:=20Update=20CI=20name?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 52 +++++++++++++++++++++++++++++--------- docs/requirements.txt | 44 ++++++++++++++++---------------- netmikro/modules/base.py | 22 ++++++++++++++++ netmikro/modules/ip.py | 2 +- netmikro/modules/system.py | 8 +++--- pyproject.toml | 7 +---- requirements.txt | 15 ----------- 7 files changed, 90 insertions(+), 60 deletions(-) delete mode 100644 requirements.txt diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e168376..ee365df 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,28 +1,56 @@ -on: [ push, pull_request ] +on: [push, pull_request] name: CI jobs: - checks: - name: Checks + lint: + name: Code Quality runs-on: ubuntu-latest - strategy: - matrix: - python-version: [ 3.12 ] - check: [ ruff, blue, isort, pydocstyle, radon, mypy, bandit ] steps: - - uses: actions/checkout@v4 - - name: Set up Python ${{ matrix.python-version }} + - name: Copy files from repo + uses: actions/checkout@v4 + + - name: Install Python uses: actions/setup-python@v5 with: - python-version: ${{ matrix.python-version }} + python-version: '3.12' + - name: Install Dependencies run: | python -m pip install --upgrade pip pip install poetry poetry config virtualenvs.create false poetry install - - name: Run checks + + - name: Run Ruff + run: poetry run task lint + + - name: Run Radon + run: poetry run task radon + + - name: Run MyPy + run: poetry run task mypy + + security: + name: Code Security + runs-on: ubuntu-latest + + steps: + - name: Copy files from repo + uses: actions/checkout@v4 + + - name: Install Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + + - name: Install Dependencies run: | - task ${{ matrix.check }} + python -m pip install --upgrade pip + pip install poetry + poetry config virtualenvs.create false + poetry install + + - name: Run Bandit + run: poetry run task bandit \ No newline at end of file diff --git a/docs/requirements.txt b/docs/requirements.txt index 1c5947d..2c5f9df 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,42 +1,42 @@ -babel==2.14.0 ; python_version >= "3.11" and python_version < "4.0" -certifi==2024.2.2 ; python_version >= "3.11" and python_version < "4.0" +babel==2.16.0 ; python_version >= "3.11" and python_version < "4.0" +certifi==2024.7.4 ; python_version >= "3.11" and python_version < "4.0" charset-normalizer==3.3.2 ; python_version >= "3.11" and python_version < "4.0" click==8.1.7 ; python_version >= "3.11" and python_version < "4.0" colorama==0.4.6 ; python_version >= "3.11" and python_version < "4.0" ghp-import==2.1.0 ; python_version >= "3.11" and python_version < "4.0" gitdb==4.0.11 ; python_version >= "3.11" and python_version < "4" gitpython==3.1.43 ; python_version >= "3.11" and python_version < "4" -griffe==0.44.0 ; python_version >= "3.11" and python_version < "4.0" -idna==3.7 ; python_version >= "3.11" and python_version < "4.0" -jinja2==3.1.3 ; python_version >= "3.11" and python_version < "4.0" -markdown==3.6 ; python_version >= "3.11" and python_version < "4.0" +griffe==1.2.0 ; python_version >= "3.11" and python_version < "4.0" +idna==3.8 ; python_version >= "3.11" and python_version < "4.0" +jinja2==3.1.4 ; python_version >= "3.11" and python_version < "4.0" +markdown==3.7 ; python_version >= "3.11" and python_version < "4.0" markupsafe==2.1.5 ; python_version >= "3.11" and python_version < "4.0" mergedeep==1.3.4 ; python_version >= "3.11" and python_version < "4.0" -mkdocs-autorefs==1.0.1 ; python_version >= "3.11" and python_version < "4.0" +mkdocs-autorefs==1.1.0 ; python_version >= "3.11" and python_version < "4.0" mkdocs-get-deps==0.2.0 ; python_version >= "3.11" and python_version < "4.0" -mkdocs-git-authors-plugin==0.8.0 ; python_version >= "3.11" and python_version < "4.0" +mkdocs-git-authors-plugin==0.9.0 ; python_version >= "3.11" and python_version < "4.0" mkdocs-git-committers-plugin-2==2.3.0 ; python_version >= "3.11" and python_version < "4" -mkdocs-git-revision-date-localized-plugin==1.2.5 ; python_version >= "3.11" and python_version < "4.0" +mkdocs-git-revision-date-localized-plugin==1.2.7 ; python_version >= "3.11" and python_version < "4.0" mkdocs-macros-plugin==1.0.5 ; python_version >= "3.11" and python_version < "4.0" mkdocs-material-extensions==1.3.1 ; python_version >= "3.11" and python_version < "4.0" -mkdocs-material==9.5.20 ; python_version >= "3.11" and python_version < "4.0" +mkdocs-material==9.5.33 ; python_version >= "3.11" and python_version < "4.0" mkdocs==1.6.0 ; python_version >= "3.11" and python_version < "4.0" -mkdocstrings-python==1.10.0 ; python_version >= "3.11" and python_version < "4.0" -mkdocstrings==0.25.0 ; python_version >= "3.11" and python_version < "4.0" -packaging==24.0 ; python_version >= "3.11" and python_version < "4.0" -paginate==0.5.6 ; python_version >= "3.11" and python_version < "4.0" +mkdocstrings-python==1.10.8 ; python_version >= "3.11" and python_version < "4.0" +mkdocstrings==0.25.2 ; python_version >= "3.11" and python_version < "4.0" +packaging==24.1 ; python_version >= "3.11" and python_version < "4.0" +paginate==0.5.7 ; python_version >= "3.11" and python_version < "4.0" pathspec==0.12.1 ; python_version >= "3.11" and python_version < "4.0" -platformdirs==4.2.1 ; python_version >= "3.11" and python_version < "4.0" -pygments==2.17.2 ; python_version >= "3.11" and python_version < "4.0" -pymdown-extensions==10.8.1 ; python_version >= "3.11" and python_version < "4.0" +platformdirs==4.2.2 ; python_version >= "3.11" and python_version < "4.0" +pygments==2.18.0 ; python_version >= "3.11" and python_version < "4.0" +pymdown-extensions==10.9 ; python_version >= "3.11" and python_version < "4.0" python-dateutil==2.9.0.post0 ; python_version >= "3.11" and python_version < "4.0" pytz==2024.1 ; python_version >= "3.11" and python_version < "4.0" pyyaml-env-tag==0.1 ; python_version >= "3.11" and python_version < "4.0" -pyyaml==6.0.1 ; python_version >= "3.11" and python_version < "4.0" -regex==2024.4.28 ; python_version >= "3.11" and python_version < "4.0" -requests==2.31.0 ; python_version >= "3.11" and python_version < "4.0" +pyyaml==6.0.2 ; python_version >= "3.11" and python_version < "4.0" +regex==2024.7.24 ; python_version >= "3.11" and python_version < "4.0" +requests==2.32.3 ; python_version >= "3.11" and python_version < "4.0" six==1.16.0 ; python_version >= "3.11" and python_version < "4.0" smmap==5.0.1 ; python_version >= "3.11" and python_version < "4" termcolor==2.4.0 ; python_version >= "3.11" and python_version < "4.0" -urllib3==2.2.1 ; python_version >= "3.11" and python_version < "4.0" -watchdog==4.0.0 ; python_version >= "3.11" and python_version < "4.0" +urllib3==2.2.2 ; python_version >= "3.11" and python_version < "4.0" +watchdog==5.0.0 ; python_version >= "3.11" and python_version < "4.0" diff --git a/netmikro/modules/base.py b/netmikro/modules/base.py index 5a7bb08..9f50d58 100644 --- a/netmikro/modules/base.py +++ b/netmikro/modules/base.py @@ -45,6 +45,28 @@ def __init__( } self._connection = MikrotikRouterOsSSH(**_auth) + def _cmd(self, command: str) -> str: + """Runs a command in the router's terminal. + + Args: + command (str): Command to be executed. + + Returns: + str: Output of the command + + Examples: + >>> router._cmd('/system identity print') + 'name: Netmikro' + """ + # The `expect_string` parameter is a regex (format: [admin@mikrotik]) + # necessary in case the router's identity is changed, + # there is no ReadTimeout error due to the output format changing, + # as it includes the router's identity + return self._connection.send_command( + command_string=command, + expect_string=rf'\[{self.username}@[^]]+\]', + ) + def _get(self, command: str) -> str: """Method for returning string outputs. diff --git a/netmikro/modules/ip.py b/netmikro/modules/ip.py index 6afc313..8e50222 100644 --- a/netmikro/modules/ip.py +++ b/netmikro/modules/ip.py @@ -65,5 +65,5 @@ def ip_port_set(self, service_name: str, port: int) -> None: >>> router.ip_port_set('www', 8080) """ validate_port(port) - self.cmd(f'/ip service set {service_name} port={port}') + self._cmd(f'/ip service set {service_name} port={port}') self.service[service_name].port = port diff --git a/netmikro/modules/system.py b/netmikro/modules/system.py index 49c96e3..106d6fd 100644 --- a/netmikro/modules/system.py +++ b/netmikro/modules/system.py @@ -206,7 +206,7 @@ def history_system_get(self) -> str: U changed system note settings hick write U ip service changed hick write """ - return self.cmd('/system history print') + return self._cmd('/system history print') def identity_set(self, new_identity: str): """Sets the router's identity. @@ -218,7 +218,7 @@ def identity_set(self, new_identity: str): >>> router.identity_set('new_identity') """ new_identity.strip() - self.cmd(f'/system identity set name={new_identity}') + self._cmd(f'/system identity set name={new_identity}') self.identity = new_identity def note_set(self, note: str, show_at_login: bool = False): @@ -232,7 +232,7 @@ def note_set(self, note: str, show_at_login: bool = False): >>> router.note_set('new_note', True) """ show_at_login_command = 'yes' if show_at_login else 'no' - self.cmd( + self._cmd( f'/system note set note="{note}" show-at-login={show_at_login_command}' ) @@ -317,7 +317,7 @@ def ntp_client_set( raise InvalidNtpMode(f'Invalid mode: {mode}') vrf = vrf.lower().strip() - self.cmd( + self._cmd( f'/system ntp client set ' f'enabled={enabled_command} mode={mode} servers={servers_command} vrf={vrf}' ) diff --git a/pyproject.toml b/pyproject.toml index 65c892c..20dfc4d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,7 +39,6 @@ pytest = "^8.3.2" bandit = {extras = ["toml"], version = "^1.7.9"} radon = {extras = ["toml"], version = "^6.0.1"} mypy = "^1.11.2" -pydocstyle = "^6.3.0" ruff = "^0.6.2" [tool.poetry.group.doc.dependencies] @@ -98,15 +97,11 @@ convention = "google" mypy = "mypy -p netmikro" radon = "radon cc ./netmikro -a -na" bandit = "bandit -r ./netmikro" -pydocstyle = "pydocstyle ./netmikro --count --convention=google --add-ignore=D100,D104,D105,D107" lint = "ruff check .; ruff check . --diff" format = 'ruff format .; ruff check . --fix' -docs = "mkdocs serve" -quality = "task mypy && task radon && task pydocstyle" +doc = "mkdocs serve" badge = "coverage-badge -o docs/assets/coverage.svg -f" pre_test = "task lint" test = "pytest -s -x --cov=netmikro -vv" post_test = "coverage html" -export-requirements = "rm requirements.txt && poetry export -f requirements.txt --output requirements.txt --without-hashes" export-requirements-doc = "poetry export -f requirements.txt --output docs/requirements.txt --without-hashes --only doc" -ready = "task lint && task quality && task bandit && pytest -s -x --cov=netmikro -vv && coverage html && task export-requirements && task export-requirements-doc && task badge" diff --git a/requirements.txt b/requirements.txt deleted file mode 100644 index 9f2fd2d..0000000 --- a/requirements.txt +++ /dev/null @@ -1,15 +0,0 @@ -bcrypt==4.1.2 ; python_version >= "3.11" and python_version < "4.0" -cffi==1.16.0 ; python_version >= "3.11" and python_version < "4.0" -cryptography==42.0.5 ; python_version >= "3.11" and python_version < "4.0" -future==1.0.0 ; python_version >= "3.11" and python_version < "4.0" -netmiko==4.3.0 ; python_version >= "3.11" and python_version < "4.0" -ntc-templates==5.0.0 ; python_version >= "3.11" and python_version < "4.0" -paramiko==3.4.0 ; python_version >= "3.11" and python_version < "4.0" -pycparser==2.22 ; python_version >= "3.11" and python_version < "4.0" -pynacl==1.5.0 ; python_version >= "3.11" and python_version < "4.0" -pyserial==3.5 ; python_version >= "3.11" and python_version < "4.0" -python-dotenv==1.0.1 ; python_version >= "3.11" and python_version < "4.0" -pyyaml==6.0.1 ; python_version >= "3.11" and python_version < "4.0" -scp==0.14.5 ; python_version >= "3.11" and python_version < "4.0" -six==1.16.0 ; python_version >= "3.11" and python_version < "4.0" -textfsm==1.1.3 ; python_version >= "3.11" and python_version < "4.0"