diff options
author | Chandan Singh <csingh43@bloomberg.net> | 2018-12-30 21:03:57 +0000 |
---|---|---|
committer | Tristan Van Berkom <tristan.van.berkom@gmail.com> | 2018-12-31 19:54:25 +0000 |
commit | 87568074008e0b22d5d543312f3dd3278ca49bed (patch) | |
tree | 57aa713b6a734bc03b33a9d846c0e0f9d122c284 /buildstream | |
parent | 8a020a5ae0063957ffccc7594fd754485953c3a3 (diff) | |
download | buildstream-87568074008e0b22d5d543312f3dd3278ca49bed.tar.gz |
_loader/loader.py: Add warnings about invalid characters in filename
See https://mail.gnome.org/archives/buildstream-list/2018-December/msg00061.html
for some related discussion.
Diffstat (limited to 'buildstream')
-rw-r--r-- | buildstream/_loader/loader.py | 37 | ||||
-rw-r--r-- | buildstream/types.py | 6 |
2 files changed, 43 insertions, 0 deletions
diff --git a/buildstream/_loader/loader.py b/buildstream/_loader/loader.py index 8dda0468e..16e880388 100644 --- a/buildstream/_loader/loader.py +++ b/buildstream/_loader/loader.py @@ -683,14 +683,51 @@ class Loader(): # A dict that maps warning types to the matching elements. invalid_elements = { CoreWarnings.BAD_ELEMENT_SUFFIX: [], + CoreWarnings.BAD_CHARACTERS_IN_NAME: [], } for filename in elements: if not filename.endswith(".bst"): invalid_elements[CoreWarnings.BAD_ELEMENT_SUFFIX].append(filename) + if not self._valid_chars_name(filename): + invalid_elements[CoreWarnings.BAD_CHARACTERS_IN_NAME].append(filename) if invalid_elements[CoreWarnings.BAD_ELEMENT_SUFFIX]: self._warn("Target elements '{}' do not have expected file extension `.bst` " "Improperly named elements will not be discoverable by commands" .format(invalid_elements[CoreWarnings.BAD_ELEMENT_SUFFIX]), warning_token=CoreWarnings.BAD_ELEMENT_SUFFIX) + if invalid_elements[CoreWarnings.BAD_CHARACTERS_IN_NAME]: + self._warn("Target elements '{}' have invalid characerts in their name." + .format(invalid_elements[CoreWarnings.BAD_CHARACTERS_IN_NAME]), + warning_token=CoreWarnings.BAD_CHARACTERS_IN_NAME) + + # Check if given filename containers valid characters. + # + # Args: + # name (str): Name of the file + # + # Returns: + # (bool): True if all characters are valid, False otherwise. + # + def _valid_chars_name(self, name): + for char in name: + char_val = ord(char) + + # 0-31 are control chars, 127 is DEL, and >127 means non-ASCII + if char_val <= 31 or char_val >= 127: + return False + + # Disallow characters that are invalid on Windows. The list can be + # found at https://docs.microsoft.com/en-us/windows/desktop/FileIO/naming-a-file + # + # Note that although : (colon) is not allowed, we do not raise + # warnings because of that, since we use it as a separator for + # junctioned elements. + # + # We also do not raise warnings on slashes since they are used as + # path separators. + if char in r'<>"|?*': + return False + + return True diff --git a/buildstream/types.py b/buildstream/types.py index 5e6265b4e..23d78b08c 100644 --- a/buildstream/types.py +++ b/buildstream/types.py @@ -105,6 +105,12 @@ class CoreWarnings(): is referenced either on the command line or by another element """ + BAD_CHARACTERS_IN_NAME = "bad-characters-in-name" + """ + This warning will be produces when filename for a target contains invalid + characters in its name. + """ + # _KeyStrength(): # |