diff options
author | Benjamin Schubert <ben.c.schubert@gmail.com> | 2019-06-04 10:23:48 +0100 |
---|---|---|
committer | bst-marge-bot <marge-bot@buildstream.build> | 2019-06-05 15:15:23 +0000 |
commit | b2b740167cfb5f4359aecbf114deb5ff1af73b04 (patch) | |
tree | 299d3644228df9dc1ef104e13eee46543b485380 /src | |
parent | 246571606853aef6da856d778f783b14339a2cea (diff) | |
download | buildstream-b2b740167cfb5f4359aecbf114deb5ff1af73b04.tar.gz |
_loader/loader: cythonize valid_chars_name
- Create a new _loader/utils.pyx cython module for functions cythonized
in the loader module.
- Move valid_chars_name from loader to utils and cythonize.
This function is called extensively, and easy to extract
Diffstat (limited to 'src')
-rw-r--r-- | src/buildstream/_loader/_loader.pyx | 52 | ||||
-rw-r--r-- | src/buildstream/_loader/loader.py | 33 |
2 files changed, 54 insertions, 31 deletions
diff --git a/src/buildstream/_loader/_loader.pyx b/src/buildstream/_loader/_loader.pyx new file mode 100644 index 000000000..258a34ab7 --- /dev/null +++ b/src/buildstream/_loader/_loader.pyx @@ -0,0 +1,52 @@ +# +# Copyright (C) 2019 Bloomberg L.P. +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library. If not, see <http://www.gnu.org/licenses/>. +# +# Authors: +# Benjamin Schubert <bschubert@bloomberg.net> +# + + +# 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(str name): + cdef int char_value + cdef int forbidden_char + + for char_value in name: + # 0-31 are control chars, 127 is DEL, and >127 means non-ASCII + if char_value <= 31 or char_value >= 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. + for forbidden_char in '<>"|?*': + if char_value == forbidden_char: + return False + + return True diff --git a/src/buildstream/_loader/loader.py b/src/buildstream/_loader/loader.py index 752883d96..5435e0d41 100644 --- a/src/buildstream/_loader/loader.py +++ b/src/buildstream/_loader/loader.py @@ -27,6 +27,7 @@ from ..element import Element from .._profile import Topics, PROFILER from .._includes import Includes +from ._loader import valid_chars_name from .types import Symbol from .loadelement import LoadElement, _extract_depends_from_node from .metaelement import MetaElement @@ -759,7 +760,7 @@ class Loader(): for filename in elements: if not filename.endswith(".bst"): invalid_elements[CoreWarnings.BAD_ELEMENT_SUFFIX].append(filename) - if not self._valid_chars_name(filename): + if not valid_chars_name(filename): invalid_elements[CoreWarnings.BAD_CHARACTERS_IN_NAME].append(filename) if invalid_elements[CoreWarnings.BAD_ELEMENT_SUFFIX]: @@ -771,33 +772,3 @@ class Loader(): 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 |