Compare commits

..

19 Commits

Author SHA1 Message Date
59162ba45f support escaped arrays and functions 2024-09-21 00:50:39 +03:00
b3208ce263 small improovements 2024-09-20 23:22:52 +03:00
ede9653588 add moroe tests 2024-09-20 23:22:52 +03:00
c73c1b085c docs update 2024-09-20 23:22:52 +03:00
e98b7ff4ab allow packages without package function 2024-09-20 23:22:52 +03:00
e553d96d33 expand bash 2024-09-20 23:22:52 +03:00
59af64c303 handle quoted control sequences correctly 2024-09-20 23:22:52 +03:00
3b964345b1 tests update 2024-09-20 23:22:52 +03:00
01d57c47a8 docs update 2024-09-20 23:22:52 +03:00
20e11cd7f4 add support of array expansion 2024-09-20 23:22:52 +03:00
36a53c4262 udpate tests 2024-09-20 23:22:52 +03:00
42b6637d63 never raise keyerror instead return empty string 2024-09-20 23:22:52 +03:00
67d05932cd docs and recipes updatte 2024-09-20 23:22:52 +03:00
405af4da5b try to improve parser 2024-09-20 23:22:52 +03:00
4938f8ffe7 simplify typed get 2024-09-20 23:22:52 +03:00
e53f744f9a completely remove makepkg calls 2024-09-20 23:22:52 +03:00
abc8df8ef3 pkgbuild parser impl 2024-09-20 23:22:52 +03:00
12a3aa8573 generate filenames without using makepkg 2024-09-20 23:22:52 +03:00
393104f9fa type: ignore too-many-positional-arguments 2024-09-20 23:22:33 +03:00
3 changed files with 27 additions and 11 deletions

View File

@ -448,6 +448,7 @@ disable=raw-checker-failed,
too-many-arguments, too-many-arguments,
duplicate-code, duplicate-code,
cyclic-import, cyclic-import,
too-many-positional-arguments,
# Enable the message, report, category or checker with the given id(s). You can # Enable the message, report, category or checker with the given id(s). You can
# either give multiple identifier separated by comma (,) or put this option # either give multiple identifier separated by comma (,) or put this option

View File

@ -165,26 +165,35 @@ class PkgbuildParser(shlex.shlex):
return result return result
def _is_quoted(self) -> bool: def _is_escaped(self) -> bool:
""" """
check if the last element was quoted. ``shlex.shlex`` parser doesn't provide information about was the token check if the last element was quoted. ``shlex.shlex`` parser doesn't provide information about was the token
quoted or not, thus there is no difference between "'#'" (diez in quotes) and "#" (diez without quotes). This quoted or not, thus there is no difference between "'#'" (diez in quotes) and "#" (diez without quotes). This
method simply rolls back to the last non-space character and check if it is a quotation mark method simply rolls back to the last non-space character and check if it is a quotation mark
Returns: Returns:
bool: ``True`` if the previous element of the stream is a quote and ``False`` otherwise bool: ``True`` if the previous element of the stream is a quote or escaped and ``False`` otherwise
""" """
current_position = self._io.tell() current_position = self._io.tell()
last_char = None last_char = penultimate_char = None
for index in range(current_position - 1, -1, -1): for index in range(current_position - 1, -1, -1):
self._io.seek(index) self._io.seek(index)
last_char = self._io.read(1) last_char = self._io.read(1)
if not last_char.isspace(): if last_char.isspace():
continue
if index >= 0:
self._io.seek(index - 1)
penultimate_char = self._io.read(1)
break break
self._io.seek(current_position) # reset position of the stream self._io.seek(current_position) # reset position of the stream
return last_char is not None and last_char in self.quotes is_quoted = last_char is not None and last_char in self.quotes
is_escaped = penultimate_char is not None and penultimate_char in self.escape
return is_quoted or is_escaped
def _parse_array(self) -> list[str]: def _parse_array(self) -> list[str]:
""" """
@ -200,7 +209,7 @@ class PkgbuildParser(shlex.shlex):
def extract() -> Generator[str, None, None]: def extract() -> Generator[str, None, None]:
while token := self.get_token(): while token := self.get_token():
match token: match token:
case _ if self._is_quoted(): case _ if self._is_escaped():
pass pass
case PkgbuildToken.ArrayEnds: case PkgbuildToken.ArrayEnds:
break break
@ -229,9 +238,9 @@ class PkgbuildParser(shlex.shlex):
# find start and end positions # find start and end positions
start_position = end_position = -1 start_position = end_position = -1
counter = 0 # simple processing of the inner "{" and "}" counter = 0 # simple processing of the inner "{" and "}"
while token := self.get_token(): for token in self:
match token: match token:
case _ if self._is_quoted(): case _ if self._is_escaped():
continue continue
case PkgbuildToken.FunctionStarts: case PkgbuildToken.FunctionStarts:
if counter == 0: if counter == 0:

View File

@ -68,7 +68,7 @@ def test_parse_array_comment() -> None:
])] ])]
def test_parse_array_quotes() -> None: def test_parse_array_escaped() -> None:
""" """
must correctly process quoted brackets must correctly process quoted brackets
""" """
@ -81,6 +81,9 @@ def test_parse_array_quotes() -> None:
parser = PkgbuildParser(StringIO("""var=(first ')' second)""")) parser = PkgbuildParser(StringIO("""var=(first ')' second)"""))
assert list(parser.parse()) == [PkgbuildPatch("var", ["first", ")", "second"])] assert list(parser.parse()) == [PkgbuildPatch("var", ["first", ")", "second"])]
parser = PkgbuildParser(StringIO("""var=(first \\) second)"""))
assert list(parser.parse()) == [PkgbuildPatch("var", ["first", ")", "second"])]
def test_parse_array_exception() -> None: def test_parse_array_exception() -> None:
""" """
@ -123,7 +126,7 @@ def test_parse_function_inner_shell() -> None:
assert list(parser.parse()) == [PkgbuildPatch("var()", "{ { echo hello world } }")] assert list(parser.parse()) == [PkgbuildPatch("var()", "{ { echo hello world } }")]
def test_parse_function_quotes() -> None: def test_parse_function_escaped() -> None:
""" """
must parse function with bracket in quotes must parse function with bracket in quotes
""" """
@ -142,6 +145,9 @@ def test_parse_function_quotes() -> None:
parser = PkgbuildParser(StringIO("""var ( ) { echo hello world '}' } """)) parser = PkgbuildParser(StringIO("""var ( ) { echo hello world '}' } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo hello world '}' }""")] assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo hello world '}' }""")]
parser = PkgbuildParser(StringIO("""var ( ) { echo hello world \\} } """))
assert list(parser.parse()) == [PkgbuildPatch("var()", """{ echo hello world \\} }""")]
def test_parse_function_exception() -> None: def test_parse_function_exception() -> None:
""" """