From 23e41a80a0eb9ab4cf61b6a58f231b899d1fe391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9?= Date: Tue, 24 May 2022 11:06:55 +0100 Subject: [PATCH] refactored slac package name to pyslac (#26) * refactored slac package name to pyslac as 'slac' is not available to be used in pypi * removed reference to switch pypi server * refactored the tests * removed unused env from README * updated readme * refactored a bit the README --- .github/workflows/pull-request.yml | 7 +--- CHANGELOG.md | 1 + Dockerfile | 6 +-- Makefile | 20 +++++----- README.md | 10 ++--- pyproject.toml | 2 +- {slac => pyslac}/__init__.py | 0 {slac => pyslac}/enums.py | 0 {slac => pyslac}/environment.py | 2 +- {slac => pyslac}/examples/__init__.py | 0 .../examples/cs_configuration.json | 0 {slac => pyslac}/examples/ev_slac_scapy.py | 4 +- .../examples/multiple_slac_sessions.py | 8 ++-- .../examples/single_slac_session.py | 8 ++-- {slac => pyslac}/layer_2_headers.py | 2 +- {slac => pyslac}/messages.py | 2 +- {slac => pyslac}/session.py | 18 ++++----- {slac => pyslac}/sockets/__init__.py | 0 .../sockets/async_linux_socket.py | 6 +-- {slac => pyslac}/sockets/enums.py | 0 {slac => pyslac}/utils.py | 0 tests/conftest.py | 8 ++-- tests/test_slac_messages.py | 4 +- tests/test_slac_session.py | 38 +++++++++++-------- tests/test_slac_version.py | 5 --- 25 files changed, 75 insertions(+), 76 deletions(-) rename {slac => pyslac}/__init__.py (100%) rename {slac => pyslac}/enums.py (100%) rename {slac => pyslac}/environment.py (97%) rename {slac => pyslac}/examples/__init__.py (100%) rename {slac => pyslac}/examples/cs_configuration.json (100%) rename {slac => pyslac}/examples/ev_slac_scapy.py (99%) rename {slac => pyslac}/examples/multiple_slac_sessions.py (94%) rename {slac => pyslac}/examples/single_slac_session.py (94%) rename {slac => pyslac}/layer_2_headers.py (96%) rename {slac => pyslac}/messages.py (99%) rename {slac => pyslac}/session.py (98%) rename {slac => pyslac}/sockets/__init__.py (100%) rename {slac => pyslac}/sockets/async_linux_socket.py (98%) rename {slac => pyslac}/sockets/enums.py (100%) rename {slac => pyslac}/utils.py (100%) delete mode 100644 tests/test_slac_version.py diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index b34b9d8..1587a6f 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -25,12 +25,7 @@ jobs: - name: Code Quality Check shell: bash run: | - poetry config http-basic.pypi-switch $PYPI_USER $PYPI_PASS poetry update poetry install poetry run pytest tests - poetry run black --check --diff --line-length=88 slac tests - env: - TAG_VERSION: ${{ steps.context.outputs.TAG_VERSION }} - PYPI_USER: ${{ secrets.PYPI_USER }} - PYPI_PASS: ${{ secrets.PYPI_PASS }} \ No newline at end of file + poetry run black --check --diff --line-length=88 pyslac tests diff --git a/CHANGELOG.md b/CHANGELOG.md index 867b15b..6555825 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - switched the package upload to public pypi server by @tropxy in https://github.com/SwitchEV/slac/pull/24 +- refactored the package name to pyslac by @tropxy in https://github.com/SwitchEV/pyslac/pull/26 ## [0.7.1] - 2022-04-28 diff --git a/Dockerfile b/Dockerfile index 4aed71d..99bb856 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,7 +34,7 @@ RUN poetry install --no-interaction --no-ansi # Run the tests and linting for slac (as we are not using poetry venv, we are # not running it with RUN poetry run pytest...) RUN pytest -vv --cov-config .coveragerc --cov-report term-missing --durations=3 --cov=. -RUN flake8 --config .flake8 slac tests +RUN flake8 --config .flake8 pyslac tests # Generate the wheel to be used by next stage @@ -49,10 +49,10 @@ WORKDIR /usr/src/app RUN python -m venv /venv COPY --from=build /usr/src/app/dist/ dist/ -COPY --from=build /usr/src/app/slac/examples/cs_configuration.json . +COPY --from=build /usr/src/app/pyslac/examples/cs_configuration.json . # This will install the wheel in the venv RUN /venv/bin/pip install dist/*.whl -CMD /venv/bin/python3 /venv/lib/python3.10/site-packages/slac/examples/single_slac_session.py +CMD /venv/bin/python3 /venv/lib/python3.10/site-packages/pyslac/examples/single_slac_session.py diff --git a/Makefile b/Makefile index afbc044..04b23e2 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ help: @echo " run-local runs the app locally with prod settings" @echo " run-local-sudo runs the app locally using prod settings and root privileges" @echo " poetry-update updates the dependencies in poetry.lock" - @echo " install-local installs slac into the current environment" + @echo " install-local installs pyslac into the current environment" @echo " tests run all the tests" @echo " reformat reformats the code, using Black" @echo " flake8 flakes8 the code" @@ -61,31 +61,31 @@ install-local: pip install . run-local-single: - python slac/examples/single_slac_session.py + python pyslac/examples/single_slac_session.py run-local-multiple: - python slac/examples/multiple_slac_sessions.py + python pyslac/examples/multiple_slac_sessions.py run-local-sudo-single: - sudo $(shell which python) slac/examples/single_slac_session.py + sudo $(shell which python) pyslac/examples/single_slac_session.py run-local-sudo-multiple: - sudo $(shell which python) slac/examples/multiple_slac_sessions.py + sudo $(shell which python) pyslac/examples/multiple_slac_sessions.py run-ev-slac: - sudo $(shell which python) slac/examples/ev_slac_scapy.py + sudo $(shell which python) pyslac/examples/ev_slac_scapy.py mypy: - mypy --config-file mypy.ini slac tests + mypy --config-file mypy.ini pyslac tests reformat: - isort slac tests && black --exclude --line-length=88 slac tests + isort pyslac tests && black --exclude --line-length=88 pyslac tests black: - black --exclude --check --diff --line-length=88 slac tests + black --exclude --check --diff --line-length=88 pyslac tests flake8: - flake8 --config .flake8 slac tests + flake8 --config .flake8 pyslac tests code-quality: reformat mypy black flake8 diff --git a/README.md b/README.md index 5559513..daf1e18 100644 --- a/README.md +++ b/README.md @@ -103,7 +103,7 @@ fails the attempt. That task includes calls to two stub methods `notify_matching_ongoing` and `notify_matching_failed`, which can be used by the end user to notify other services of the current SLAC matching state ("ongoing" or "failed"). This can be useful -for the use case defined in ISO 15118-3, section 7, where different strategies need +for the use case defined in ISO 15118-3, section 7, where different strategies need to be carried on, depending on if SLAC matching has started after or before EIM authentication was completed. @@ -114,7 +114,7 @@ brokers. In that case, the user would have some external service monitoring the Control Pilot state, which would send the state to the SLAC application. The Slac Handler would then -be waiting, listening for the Control Pilot state notification and on reception of it, +await, listening for the Control Pilot state notification and upon its reception, would call the `process_cp_state`, which in its turn would spawn a matching process task. @@ -154,7 +154,7 @@ The key-value pairs defined in the `.env` file directly affect the settings pres Any of those variables can also be set by exporting their value in the env: -`$ export NETWORK_INTERFACE=eth1` +`$ export LOG_LEVEL=DEBUG` @@ -183,8 +183,8 @@ To start the test, you may need root privileges and to start in the following order: ```bash -$ sudo $(shell which python) slac/examples/single_slac_session.py -$ sudo $(shell which python) slac/examples/ev_slac_scapy.py +$ sudo $(shell which python) pyslac/examples/single_slac_session.py +$ sudo $(shell which python) pyslac/examples/ev_slac_scapy.py ``` This integration test was tested under: diff --git a/pyproject.toml b/pyproject.toml index 94390eb..4d59c35 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [tool.poetry] -name = "slac" +name = "pyslac" version = "0.8.0" description = "SLAC Protocol implementation, defined in ISO15118-3" authors = ["André Duarte "] diff --git a/slac/__init__.py b/pyslac/__init__.py similarity index 100% rename from slac/__init__.py rename to pyslac/__init__.py diff --git a/slac/enums.py b/pyslac/enums.py similarity index 100% rename from slac/enums.py rename to pyslac/enums.py diff --git a/slac/environment.py b/pyslac/environment.py similarity index 97% rename from slac/environment.py rename to pyslac/environment.py index 1bf37ba..6c19f4d 100644 --- a/slac/environment.py +++ b/pyslac/environment.py @@ -5,7 +5,7 @@ import environs -from slac.enums import Timers +from pyslac.enums import Timers logger = logging.getLogger(__name__) diff --git a/slac/examples/__init__.py b/pyslac/examples/__init__.py similarity index 100% rename from slac/examples/__init__.py rename to pyslac/examples/__init__.py diff --git a/slac/examples/cs_configuration.json b/pyslac/examples/cs_configuration.json similarity index 100% rename from slac/examples/cs_configuration.json rename to pyslac/examples/cs_configuration.json diff --git a/slac/examples/ev_slac_scapy.py b/pyslac/examples/ev_slac_scapy.py similarity index 99% rename from slac/examples/ev_slac_scapy.py rename to pyslac/examples/ev_slac_scapy.py index 984e0f6..14c2f42 100644 --- a/slac/examples/ev_slac_scapy.py +++ b/pyslac/examples/ev_slac_scapy.py @@ -12,7 +12,7 @@ sendp, ) -from slac.enums import ( +from pyslac.enums import ( CM_ATTEN_CHAR, CM_ATTEN_PROFILE, CM_MNBC_SOUND, @@ -27,7 +27,7 @@ MMTYPE_RSP, SLAC_SETTLE_TIME, ) -from slac.utils import get_if_hwaddr +from pyslac.utils import get_if_hwaddr BROADCAST_ADDR = "FF:FF:FF:FF:FF:FF" ATHEROS_CHIP_MAC = "00:b0:52:00:00:01" diff --git a/slac/examples/multiple_slac_sessions.py b/pyslac/examples/multiple_slac_sessions.py similarity index 94% rename from slac/examples/multiple_slac_sessions.py rename to pyslac/examples/multiple_slac_sessions.py index 6483925..2f74baa 100644 --- a/slac/examples/multiple_slac_sessions.py +++ b/pyslac/examples/multiple_slac_sessions.py @@ -1,4 +1,4 @@ -from slac.utils import is_distro_linux +from pyslac.utils import is_distro_linux if not is_distro_linux(): raise EnvironmentError("Non-Linux systems are not supported") @@ -10,9 +10,9 @@ import os from typing import List, Optional -from slac.environment import Config -from slac.session import SlacEvseSession, SlacSessionController -from slac.utils import wait_till_finished +from pyslac.environment import Config +from pyslac.session import SlacEvseSession, SlacSessionController +from pyslac.utils import wait_till_finished logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__file__) diff --git a/slac/examples/single_slac_session.py b/pyslac/examples/single_slac_session.py similarity index 94% rename from slac/examples/single_slac_session.py rename to pyslac/examples/single_slac_session.py index f68888f..9fa0692 100644 --- a/slac/examples/single_slac_session.py +++ b/pyslac/examples/single_slac_session.py @@ -1,4 +1,4 @@ -from slac.utils import is_distro_linux +from pyslac.utils import is_distro_linux if not is_distro_linux(): raise EnvironmentError("Non-Linux systems are not supported") @@ -9,9 +9,9 @@ import os from typing import List, Optional -from slac.environment import Config -from slac.session import SlacEvseSession, SlacSessionController -from slac.utils import wait_till_finished +from pyslac.environment import Config +from pyslac.session import SlacEvseSession, SlacSessionController +from pyslac.utils import wait_till_finished logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger(__file__) diff --git a/slac/layer_2_headers.py b/pyslac/layer_2_headers.py similarity index 96% rename from slac/layer_2_headers.py rename to pyslac/layer_2_headers.py index 665a1f0..fa996b7 100644 --- a/slac/layer_2_headers.py +++ b/pyslac/layer_2_headers.py @@ -1,6 +1,6 @@ from dataclasses import dataclass -from slac.enums import ETH_TYPE_HPAV, HOMEPLUG_FMID, HOMEPLUG_FMSN, HOMEPLUG_MMV +from pyslac.enums import ETH_TYPE_HPAV, HOMEPLUG_FMID, HOMEPLUG_FMSN, HOMEPLUG_MMV @dataclass diff --git a/slac/messages.py b/pyslac/messages.py similarity index 99% rename from slac/messages.py rename to pyslac/messages.py index 9e0b9dc..ef3aaec 100644 --- a/slac/messages.py +++ b/pyslac/messages.py @@ -2,7 +2,7 @@ from dataclasses import dataclass from typing import List -from slac.enums import ( +from pyslac.enums import ( BROADCAST_ADDR, CM_SET_CCO_CAPAB, CM_SET_KEY_MY_NONCE, diff --git a/slac/session.py b/pyslac/session.py similarity index 98% rename from slac/session.py rename to pyslac/session.py index 2b29ec6..18a8697 100644 --- a/slac/session.py +++ b/pyslac/session.py @@ -6,7 +6,7 @@ from os import urandom from typing import List, Optional, Union -from slac.enums import ( +from pyslac.enums import ( ATTEN_RESULTS_TIMEOUT, CM_ATTEN_CHAR, CM_ATTEN_PROFILE, @@ -38,9 +38,9 @@ # This timeout is imported from the environment file, because it makes it # easier to use it with the dev compose file for dev and debugging reasons -from slac.environment import Config -from slac.layer_2_headers import EthernetHeader, HomePlugHeader -from slac.messages import ( +from pyslac.environment import Config +from pyslac.layer_2_headers import EthernetHeader, HomePlugHeader +from pyslac.messages import ( AtennChar, AtennCharRsp, AttenProfile, @@ -53,15 +53,15 @@ SlacParmReq, StartAtennChar, ) -from slac.sockets.async_linux_socket import ( +from pyslac.sockets.async_linux_socket import ( create_socket, readeth, send_recv_eth, sendeth, ) -from slac.utils import cancel_task, generate_nid, get_if_hwaddr -from slac.utils import half_round as hw -from slac.utils import task_callback, time_now_ms +from pyslac.utils import cancel_task, generate_nid, get_if_hwaddr +from pyslac.utils import half_round as hw +from pyslac.utils import task_callback, time_now_ms logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("slac_session") @@ -294,7 +294,7 @@ async def evse_set_key(self) -> bytes: The only secure way to remove a STA from an AVLN is to change the NMK """ logger.debug("CM_SET_KEY: Started...") - # for each new set_key message sent (or slac session), + # for each new set_key message sent (or pyslac session), # a new pair of NID (Network ID) and NMK (Network Mask) shall be # generated nmk = urandom(16) diff --git a/slac/sockets/__init__.py b/pyslac/sockets/__init__.py similarity index 100% rename from slac/sockets/__init__.py rename to pyslac/sockets/__init__.py diff --git a/slac/sockets/async_linux_socket.py b/pyslac/sockets/async_linux_socket.py similarity index 98% rename from slac/sockets/async_linux_socket.py rename to pyslac/sockets/async_linux_socket.py index 10cbf19..6d070f6 100644 --- a/slac/sockets/async_linux_socket.py +++ b/pyslac/sockets/async_linux_socket.py @@ -16,8 +16,8 @@ from struct import pack from typing import Optional -from slac.enums import BUFF_MAX_SIZE, Timers -from slac.sockets.enums import ( +from pyslac.enums import BUFF_MAX_SIZE, Timers +from pyslac.sockets.enums import ( BPF_ABS, BPF_H, BPF_JEQ, @@ -29,7 +29,7 @@ ETH_P_HPAV, SO_ATTACH_FILTER, ) -from slac.utils import time_now_ms +from pyslac.utils import time_now_ms logging.basicConfig(level=logging.DEBUG) logger = logging.getLogger("async_linux_socket") diff --git a/slac/sockets/enums.py b/pyslac/sockets/enums.py similarity index 100% rename from slac/sockets/enums.py rename to pyslac/sockets/enums.py diff --git a/slac/utils.py b/pyslac/utils.py similarity index 100% rename from slac/utils.py rename to pyslac/utils.py diff --git a/tests/conftest.py b/tests/conftest.py index b274789..d70076d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,8 +2,8 @@ import pytest -from slac.environment import Config -from slac.session import SlacEvseSession +from pyslac.environment import Config +from pyslac.session import SlacEvseSession EVSE_ID = "DE*12*122333" IFACE = "en0" @@ -21,8 +21,8 @@ def evse_mac(): @pytest.fixture def evse_slac_session(dummy_config, evse_mac): - with patch("slac.session.get_if_hwaddr", new=Mock(return_value=evse_mac)): - with patch("slac.session.create_socket", new=Mock()): + with patch("pyslac.session.get_if_hwaddr", new=Mock(return_value=evse_mac)): + with patch("pyslac.session.create_socket", new=Mock()): evse_session = SlacEvseSession(EVSE_ID, IFACE, dummy_config) evse_session.reset_socket = Mock() diff --git a/tests/test_slac_messages.py b/tests/test_slac_messages.py index e8628b8..a08e721 100644 --- a/tests/test_slac_messages.py +++ b/tests/test_slac_messages.py @@ -1,4 +1,4 @@ -from slac.enums import ( +from pyslac.enums import ( BROADCAST_ADDR, CM_SET_CCO_CAPAB, CM_SET_KEY_MY_NONCE, @@ -14,7 +14,7 @@ SLAC_RESP_TYPE, SLAC_SECURITY_TYPE, ) -from slac.messages import ( +from pyslac.messages import ( AtennChar, AtennCharRsp, AttenProfile, diff --git a/tests/test_slac_session.py b/tests/test_slac_session.py index efe0553..4721498 100644 --- a/tests/test_slac_session.py +++ b/tests/test_slac_session.py @@ -2,7 +2,7 @@ import pytest -from slac.enums import ( +from pyslac.enums import ( ATTEN_RESULTS_TIMEOUT, BROADCAST_ADDR, CM_ATTEN_CHAR, @@ -33,9 +33,9 @@ STATE_MATCHING, STATE_UNMATCHED, ) -from slac.layer_2_headers import EthernetHeader, HomePlugHeader -from slac.messages import AttenProfile # MnbcSound, -from slac.messages import ( +from pyslac.layer_2_headers import EthernetHeader, HomePlugHeader +from pyslac.messages import AttenProfile # MnbcSound, +from pyslac.messages import ( AtennChar, AtennCharRsp, MatchCnf, @@ -46,7 +46,7 @@ SlacParmReq, StartAtennChar, ) -from slac.utils import half_round as hw +from pyslac.utils import half_round as hw PEV_MAC = b"\xBB" * 6 RUN_ID = b"\xFA" * 8 @@ -92,9 +92,9 @@ async def test_set_key(evse_slac_session, dummy_config, evse_mac): # This first patch is to change the original SETTLE_TIME of 10 sec # so that during tests we dont wait so long evse_slac_session.send_frame = AsyncMock() - with patch("slac.session.SLAC_SETTLE_TIME", 0.5): - with patch("slac.session.readeth", new=AsyncMock(return_value=key_cnf_frame)): - with patch("slac.session.urandom", new=Mock(return_value=QUALCOMM_NMK)): + with patch("pyslac.session.SLAC_SETTLE_TIME", 0.5): + with patch("pyslac.session.readeth", new=AsyncMock(return_value=key_cnf_frame)): + with patch("pyslac.session.urandom", new=Mock(return_value=QUALCOMM_NMK)): data_rcvd = await evse_slac_session.evse_set_key() assert data_rcvd == key_cnf_frame @@ -128,7 +128,9 @@ async def test_slac_parm(evse_slac_session, evse_mac): slac_parm_cnf_frame = ( ether_header.pack_big() + homeplug_header.pack_big() + slac_parm_cnf.pack_big() ) - with patch("slac.session.readeth", new=AsyncMock(return_value=slac_parm_req_frame)): + with patch( + "pyslac.session.readeth", new=AsyncMock(return_value=slac_parm_req_frame) + ): evse_slac_session.send_frame = AsyncMock() await evse_slac_session.evse_slac_parm() @@ -165,7 +167,7 @@ async def test_cm_start_atten_charac(evse_slac_session): + start_atten_car.pack_big() ) with patch( - "slac.session.readeth", new=AsyncMock(return_value=start_atten_car_frame) + "pyslac.session.readeth", new=AsyncMock(return_value=start_atten_car_frame) ): # Fake that we already received these parameters before, during # SLAC Param message sequence @@ -202,7 +204,7 @@ async def test_cm_mnbc_sound(evse_slac_session, evse_mac): ) with patch( - "slac.session.readeth", new=AsyncMock(return_value=atten_profile_ind_frame) + "pyslac.session.readeth", new=AsyncMock(return_value=atten_profile_ind_frame) ): # mocking of data that is set in previous steps # The timeout is set here, because originally it would be only @@ -265,7 +267,9 @@ async def test_cm_atten_charac(evse_slac_session, evse_mac): + homeplug_header.pack_big() + atten_car_rsp.pack_big() ) - with patch("slac.session.readeth", new=AsyncMock(return_value=atten_car_rsp_frame)): + with patch( + "pyslac.session.readeth", new=AsyncMock(return_value=atten_car_rsp_frame) + ): evse_slac_session.send_frame = AsyncMock() # Fake that we already received these parameters before, during # SLAC Param message sequence @@ -292,7 +296,9 @@ async def test_cm_atten_charac(evse_slac_session, evse_mac): + homeplug_header.pack_big() + atten_car_rsp.pack_big() ) - with patch("slac.session.readeth", new=AsyncMock(return_value=atten_car_rsp_frame)): + with patch( + "pyslac.session.readeth", new=AsyncMock(return_value=atten_car_rsp_frame) + ): with pytest.raises(ValueError): await evse_slac_session.cm_atten_char() assert evse_slac_session.state == STATE_UNMATCHED @@ -304,7 +310,9 @@ async def test_cm_atten_charac(evse_slac_session, evse_mac): + homeplug_header.pack_big() + atten_car_rsp.pack_big() ) - with patch("slac.session.readeth", new=AsyncMock(return_value=atten_car_rsp_frame)): + with patch( + "pyslac.session.readeth", new=AsyncMock(return_value=atten_car_rsp_frame) + ): with pytest.raises(ValueError): await evse_slac_session.cm_atten_char() assert evse_slac_session.state == STATE_UNMATCHED @@ -327,7 +335,7 @@ async def test_slac_match(evse_slac_session, evse_mac): ) with patch( - "slac.session.readeth", new=AsyncMock(return_value=slac_match_req_frame) + "pyslac.session.readeth", new=AsyncMock(return_value=slac_match_req_frame) ): # mock of the send_frame routine evse_slac_session.send_frame = AsyncMock() diff --git a/tests/test_slac_version.py b/tests/test_slac_version.py deleted file mode 100644 index 9cecc9f..0000000 --- a/tests/test_slac_version.py +++ /dev/null @@ -1,5 +0,0 @@ -from slac import __version__ - - -def test_version(): - assert __version__ == "0.1.0"