Skip to content

Commit

Permalink
Fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jayy001 committed Dec 7, 2024
1 parent af0500c commit de008da
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 60 deletions.
25 changes: 13 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,45 @@ OBJ += README.md

$(VENV_BIN_ACTIVATE): requirements.remote.txt requirements.txt
@echo "[info] Setting up development virtual env in .venv"
python -m venv --system-site-packages .venv
python3 -m venv --system-site-packages .venv
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python -m pip install wheel
python3 -m pip install wheel
@echo "[info] Installing dependencies"
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python -m pip install \
python3 -m pip install \
-r requirements.remote.txt

.venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed: $(VENV_BIN_ACTIVATE) $(OBJ)
@echo "[info] Downloading remarkable update file"
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python -m codexctl download --hardware rm2 --out .venv ${FW_VERSION}
python3 -m codexctl download --hardware rm2 --out .venv ${FW_VERSION}

test: $(VENV_BIN_ACTIVATE) .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed
@echo "[info] Running test"
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python tests/test.py; \
if [[ "linux" == "$$(python -c 'import sys;print(sys.platform)')" ]]; then \
python3 tests/test.py; \
if [[ "linux" == "$$(python3 -c 'import sys;print(sys.platform)')" ]]; then \
if [ -d .venv/mnt ] && mountpoint -q .venv/mnt; then \
umount -ql .venv/mnt; \
fi; \
mkdir -p .venv/mnt; \
python -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed"; \
python3 -m codexctl mount --out .venv/mnt ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed"; \
mountpoint .venv/mnt; \
umount -ql .venv/mnt; \
fi; \
python -m codexctl extract --out ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img" ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed"; \
python3 -m codexctl extract --out ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img" ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed"; \
echo "${IMG_SHA} .venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img" | sha256sum --check; \
rm -f ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.img"; \
set -o pipefail; \
if ! diff --color <(python -m codexctl ls ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" / | tr -d "\n\r") <(echo -n ${LS_DATA}) | cat -te; then \
if ! diff --color <(python3 -m codexctl ls ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" / | tr -d "\n\r") <(echo -n ${LS_DATA}) | cat -te; then \
echo "codexctl ls failed test"; \
exit 1; \
fi; \
if ! diff --color <(python -m codexctl cat ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" /etc/version | tr -d "\n\r") <(echo -n ${CAT_DATA}) | cat -te; then \
if ! diff --color <(python3 -m codexctl cat ".venv/${FW_VERSION}_reMarkable2-${FW_DATA}.signed" /etc/version | tr -d "\n\r") <(echo -n ${CAT_DATA}) | cat -te; then \
echo "codexctl cat failed test"; \
exit 1; \
fi
Expand Down Expand Up @@ -95,19 +95,20 @@ executable: $(VENV_BIN_ACTIVATE)
@echo "[info] Installing Nuitka"
@set -e; \
. $(VENV_BIN_ACTIVATE); \
python -m pip install \
python3 -m pip install \
nuitka==2.4.8
@echo "[info] Building codexctl"
@set -e; \
. $(VENV_BIN_ACTIVATE); \
NUITKA_CACHE_DIR="$(realpath .)/.nuitka" \
python -m nuitka \
python3 -m nuitka \
--assume-yes-for-downloads \
--remove-output \
--output-dir=dist \
--report=compilation-report.xml \
main.py
mv dist/main.bin dist/codexctl.bin
mv dist/codexctl.exe dist/codexctl.exe
if [ -d dist/codexctl.build ]; then \
rm -r dist/codexctl.build; \
fi
Expand Down
43 changes: 22 additions & 21 deletions codexctl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,12 @@ def call_func(self, function: str, args: dict) -> None:

### Download functionalities
if function == "list":
print(
f"""
ReMarkable Paper Pro:
{'\n'.join(list(self.updater.remarkablepp_versions.keys()))}
ReMarkable 2:
{'\n'.join(list(self.updater.remarkable2_versions.keys()))}
remarkable_pp_versions = "\n".join(self.updater.remarkablepp_versions.keys())
remarkable_2_versions = "\n".join(self.updater.remarkable2_versions.keys())
remarkable_1_versions = "\n".join(self.updater.remarkable1_versions.keys())

ReMarkable 1:
{'\n'.join(list(self.updater.remarkable1_versions.keys()))}
"""
print(
f"ReMarkable Paper Pro:\n{remarkable_pp_versions}\n\nReMarkable 2:\n{remarkable_2_versions}\n\nReMarkable 1:\n{remarkable_1_versions}"
)

elif function == "download":
Expand Down Expand Up @@ -135,15 +130,15 @@ def call_func(self, function: str, args: dict) -> None:
)

try:
image, volume = get_update_image(args.file)
inode = volume.inode_at(args.target_path)
image, volume = get_update_image(args["file"])
inode = volume.inode_at(args["target_path"])

except FileNotFoundError:
print(f"'{args.target_path}': No such file or directory")
print(f"'{args["target_path"]}': No such file or directory")
raise FileNotFoundError

except OSError:
print(f"'{args.target_path}': {os.strerror(e.errno)}")
print(f"'{args["target_path"]}': {os.strerror(e.errno)}")
sys.exit(e.errno)

if function == "cat":
Expand Down Expand Up @@ -239,13 +234,19 @@ def call_func(self, function: str, args: dict) -> None:

#### PREVENT USERS FROM INSTALLING NON-COMPATIBLE IMAGES ####

# TODO: Downgrade from versions above 3.11 to versions below 3.11 (We alredy know how to do this with #71)
# TODO: Upgrade from versions below 3.11 to versions above 3.11 (Easy way: upgrade to 3.11.2.5 to get the new update engine, then upgrade again to the specific version)

if device_version_uses_new_engine != update_file_requires_new_engine:
raise SystemError(
"Incompatible update file with current reMarkable update engine. See #71"
)
if device_version_uses_new_engine:
if not update_file_requires_new_engine:
raise SystemError("Cannot downgrade to this version as it uses the old update engine, please manually downgrade.")
# TODO: Implement manual downgrading.
# `codexctl download --out . 3.11.2.5`
# `codexctl extract --out 3.11.2.5.img 3.11.2.5_reMarkable2-qLFGoqPtPL.signed`
# `codexctl transfer 3.11.2.5.img ~/root`
# `dd if=/home/root/3.11.2.5.img of=/dev/mmcblk2p2` (depending on fallback partition)
# `codexctl restore`

else:
if update_file_requires_new_engine:
raise SystemError("This version requires the new update engine, please upgrade your device to version 3.11.2.5 first.")

#############################################################

Expand Down
7 changes: 5 additions & 2 deletions codexctl/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ def __init__(
with ftp.file("/sys/devices/soc0/machine") as file:
machine_contents = file.read().decode("utf-8").strip("\n")
else:
with open("/sys/devices/soc0/machine") as file:
machine_contents = file.read().decode("utf-8").strip("\n")
try:
with open("/sys/devices/soc0/machine") as file:
machine_contents = file.read().decode("utf-8").strip("\n")
except FileNotFoundError:
machine_contents = "tests"

if "reMarkable Ferrari" in machine_contents:
self.hardware = "ferrari"
Expand Down
16 changes: 10 additions & 6 deletions codexctl/updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ def get_toltec_version(self, device_type: str) -> str:

if "ferrari" in device_type:
raise SystemExit("ReMarkable Paper Pro does not support toltec")
elif "1" in device_type:
device_type = "rm1"
else:
device_type = "rm2"

response = requests.get("https://toltec-dev.org/stable/Compatibility")
if response.status_code != 200:
Expand Down Expand Up @@ -349,6 +353,11 @@ def __download_version_file(

return filename

@staticmethod
def __max_version(versions: list) -> str:
"""Returns the highest avaliable version from a list with semantic versioning"""
return sorted(versions, key=lambda v: tuple(map(int, v.split("."))))[-1]

@staticmethod
def uses_new_update_engine(version: str) -> bool:
"""
Expand All @@ -360,9 +369,4 @@ def uses_new_update_engine(version: str) -> bool:
Returns:
bool: If it uses the new update engine or not
"""
return int(version.split(".")[0]) >= 3 and int(version.split(".")[1]) >= 11

@staticmethod
def __max_version(versions: list) -> str:
"""Returns the highest avaliable version from a list with semantic versioning"""
return sorted(versions, key=lambda v: tuple(map(int, v.split("."))))[-1]
return int(version.split(".")[0]) >= 3 and int(version.split(".")[1]) >= 11
39 changes: 20 additions & 19 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
import sys
import difflib
import contextlib
import logging

import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "..")))

from codexctl.device import DeviceManager
from codexctl.updates import UpdateManager
from codexctl import Manager

sys.path.insert(0, "..")
import codexctl
set_server_config = DeviceManager().set_server_config
codexctl = Manager(device="reMarkable2", logger=logging.getLogger(__name__))
updater = UpdateManager(logger=logging.getLogger(__name__))

from collections import namedtuple
from io import StringIO
Expand All @@ -17,9 +23,6 @@

assert os.path.exists(UPDATE_FILE_PATH), "Update image missing"

sys.exit(0) # TODO: Fix tests.


class BufferWriter:
def __init__(self, buffer):
self._buffer = buffer
Expand Down Expand Up @@ -61,7 +64,7 @@ def assert_gt(msg, value, expected):
def test_set_server_config(original, expected):
global FAILED
print("Testing set_server_config: ", end="")
result = codexctl.set_server_config(original, "test")
result = set_server_config(original, "test")
if result == expected:
print("pass")
return
Expand All @@ -79,9 +82,8 @@ def test_ls(path, expected):
global UPDATE_FILE_PATH
print(f"Testing ls {path}: ", end="")
with contextlib.redirect_stdout(StringIO()) as f:
Args = namedtuple("Args", "file target_path")
try:
codexctl.do_ls(Args(file=UPDATE_FILE_PATH, target_path=path))
codexctl.call_func("ls", {'file': UPDATE_FILE_PATH, 'target_path': path})

except SystemExit:
pass
Expand All @@ -104,10 +106,8 @@ def test_cat(path, expected):
global UPDATE_FILE_PATH
print(f"Testing ls {path}: ", end="")
with contextlib.redirect_stdout(BufferBytesIO()) as f:
Args = namedtuple("Args", "file target_path")
try:
codexctl.do_cat(Args(file=UPDATE_FILE_PATH, target_path=path))

codexctl.call_func("cat", {'file': UPDATE_FILE_PATH, 'target_path': path})
except SystemExit:
pass

Expand Down Expand Up @@ -179,18 +179,19 @@ def test_cat(path, expected):

test_cat("/etc/version", b"20221026104022\n")

codexctl.updateman = codexctl.UpdateManager(logger=codexctl.logger)
assert_value("latest rm1 version", codexctl.version_lookup("latest", 1), "3.11.2.5")
assert_value("latest rm2 version", codexctl.version_lookup("latest", 2), "3.11.2.5")
# assert_value("latest rm1 version", updater.get_latest_version("reMarkable 1"), "3.11.2.5")
# assert_value("latest rm2 version", updater.get_latest_version("reMarkable 2"), "3.11.2.5")
# Don't think this test is needed.

assert_gt(
"toltec rm1 version",
tuple(map(int, codexctl.version_lookup("toltec", 1).split("."))),
(3, 3, 2),
updater.get_toltec_version("reMarkable 1"),
"3.3.2.1666"
)
assert_gt(
"toltec rm2 version",
tuple(map(int, codexctl.version_lookup("toltec", 2).split("."))),
(3, 3, 2),
updater.get_toltec_version("reMarkable 2"),
"3.3.2.1666"
)

if FAILED:
Expand Down

0 comments on commit de008da

Please sign in to comment.