From 3f59325cb8dae0e842d9111436a3ab431ac81bf4 Mon Sep 17 00:00:00 2001 From: dennisvang <29799340+dennisvang@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:05:13 +0200 Subject: [PATCH] explicitly warn against whitespace in app name and filename --- README.md | 4 +++- examples/client/example_app.py | 2 +- src/tufup/common.py | 10 ++++++++-- tests/test_common.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 564e748..bdec256 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,9 @@ Archive filenames and patch filenames follow the pattern `-` -where `name` is a short string that may contain alphanumeric characters, underscores, and hyphens, `version` is a version string according to the [PEP440][6] specification, and `suffix` is either `'.tar.gz'` or `'.patch'`. +where `name` is a short string that may *only* contain *alphanumeric characters*, *underscores*, and *hyphens*, `version` is a version string according to the [PEP440][6] specification, and `suffix` is either `'.tar.gz'` or `'.patch'`. + +***BEWARE***: *whitespace* is NOT allowed in the filename. Patches are typically smaller than archives, so the tufup *client* will always attempt to update using one or more patches. However, if the total amount of patch data is greater than the desired full archive file, a full update will be performed. diff --git a/examples/client/example_app.py b/examples/client/example_app.py index 0f2ab99..210f0a8 100644 --- a/examples/client/example_app.py +++ b/examples/client/example_app.py @@ -13,7 +13,7 @@ # python -m http.server -d examples/repo/content # App info -APP_NAME = 'example_app' +APP_NAME = 'example_app' # BEWARE: app name cannot contain whitespace CURRENT_VERSION = '1.0' # For this example, all files are stored in the tufup/examples/client diff --git a/src/tufup/common.py b/src/tufup/common.py index aaddad8..99fbbba 100644 --- a/src/tufup/common.py +++ b/src/tufup/common.py @@ -26,8 +26,10 @@ def __init__( is_archive: Optional[bool] = True, ): """ - Initialize either with target_path, or with name, version, archive. + + BEWARE: whitespace is not allowed in the filename, + nor in the `name` or `version` arguments """ super().__init__() if target_path is None: @@ -36,6 +38,10 @@ def __init__( ) self.target_path_str = str(target_path) # keep the original for reference self.path = pathlib.Path(target_path) + if ' ' in self.filename: + logger.critical( + f'invalid filename "{self.filename}": whitespace not allowed' + ) def __str__(self): return str(self.target_path_str) @@ -85,7 +91,7 @@ def version(self) -> Optional[Version]: version = Version(match_dict.get('version', '')) except InvalidVersion: version = None - logger.debug(f'No valid version in filename: {self.filename}') + logger.critical(f'No valid version in filename: {self.filename}') return version @property diff --git a/tests/test_common.py b/tests/test_common.py index 12d0e42..86a743d 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,3 +1,4 @@ +import logging import pathlib import bsdiff4 @@ -8,6 +9,15 @@ class TestTargetMeta(TempDirTestCase): + def test_init_whitespace(self): + for kwargs in [ + dict(target_path='w h i t e s p a c e-1.2.3.tar.gz'), + dict(name='w h i t e s p a c e'), + ]: + with self.subTest(msg=kwargs): + with self.assertLogs(level=logging.CRITICAL): + TargetMeta(**kwargs) + def test_eq_ne(self): target_meta = TargetMeta() self.assertEqual(target_meta, TargetMeta())