Skip to contentSkip to conversation

Adding support for shell arithmetics

PATCH In review Olof Johansson 2 participants 5 messages May 7, 2013 – Jun 17, 2013

lore
Share & tools
lore
#0 lore raw

cover Adding support for shell arithmetics

Hi,

A week or so I found out that shell fragments in bitbake recipes don't support shell arithmetics ($((1+1))). The reason for this is the shell lexing throwing NotImplemented exceptions. I've made some progress in adding support for shell arithmetics, but I have a hard time getting support for corner cases.

It was suprising to see that currently, only a subset of posix shell scripts is supported, even though they are sent through to be executed by the system shell. I think I understand the reason for having to do the shell lexing; knowing what functions and variables to export right? But I wonder if it's possible to do the shell lexing as a best effort, and ignore any errors.

What are the risks with this approach? Is it feasibile to do so?

This patch series is a work in progress, and does support the use cases we had issues with, however, I currently skip one unit test, as I can't get it to work. Comments and feedback much appreciated!

The branch is also pushed to github at:

https://github.com/olof/bitbake/tree/topic/shlex_shell_arith

Olof Johansson (2):
pysh: Say what kind of token isn't implemented
initial work on supporting shell arith

 bin/bitbake-selftest       |    1 +
 lib/bb/pysh/pyshlex.py     |   48 ++++++++++++++++--
 lib/bb/tests/codeparser.py |   22 ++++++++
 lib/bb/tests/pysh.py       |  119 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 186 insertions(+), 4 deletions(-)
 create mode 100644 lib/bb/tests/pysh.py
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH 1/2 pysh: Say what kind of token isn't implemented

When the shell lexer finds an unrecognized dollar token, the error message should contain what kind of token it is having problems with.

---
 lib/bb/pysh/pyshlex.py |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/bb/pysh/pyshlex.py +1 -1
index b977b5e..b301236 100644
@@ -292,7 +292,7 @@ class WordLexer:
         elif sep=='${':
             parsefunc = self._parse_parameter
         else:
-            raise NotImplementedError()
+            raise NotImplementedError(sep)
             
         pos, closed = parsefunc(buf, result, eof)
         return pos, closed
Signature · 1 line
1.7.10.4
#2 lore raw

PATCH 2/2 initial work on supporting shell arith

In reply to Olof Johansson
---
 bin/bitbake-selftest       |    1 +
 lib/bb/pysh/pyshlex.py     |   46 +++++++++++++++--
 lib/bb/tests/codeparser.py |   22 ++++++++
 lib/bb/tests/pysh.py       |  119 ++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 185 insertions(+), 3 deletions(-)
 create mode 100644 lib/bb/tests/pysh.py
bin/bitbake-selftest +1 -0
index 48a58fe..904a801 100755
@@ -29,6 +29,7 @@ tests = ["bb.tests.codeparser",
          "bb.tests.cow",
          "bb.tests.data",
          "bb.tests.fetch",
+         "bb.tests.pysh",
          "bb.tests.utils"]
 
 for t in tests:
lib/bb/pysh/pyshlex.py +43 -3

70 lines not on this page · raw message

lib/bb/tests/codeparser.py +22 -0
index 938b04b..debb933 100644
@@ -110,6 +110,28 @@ ${D}${libdir}/pkgconfig/*.pc
         self.parseExpression("foo=$(echo bar)")
         self.assertExecs(set(["echo"]))
 
+    def test_assign_arith(self):
+        self.parseExpression("python2.$((8-1))")
+        self.assertExecs(set(["python2.$((8-1))"]))
+
+    def test_assign_arith_only(self):
+        self.parseExpression("$((8-1))")
+        self.assertExecs(set(["$((8-1))"]))
+
+    @unittest.skip(
+        "FIXME: multiple shell arith expressions are not supported")
+    def test_assign_arith_dual(self):
+        self.parseExpression("$((1+1))to$((4-1))")
+        self.assertExecs(set(["$((1+1))to$((4-1))"]))
+
+    def test_assign_arith_nested1(self):
+        self.parseExpression("$((1+$((1))))")
+        self.assertExecs(set(["$((1+$((1))))"]))
+
+    def test_assign_arith_nested2(self):
+        self.parseExpression("$(( 1 + $(( 1 )) ))")
+        self.assertExecs(set(["$(( 1 + $(( 1 )) ))"]))
+
     def test_shell_unexpanded(self):
         self.setEmptyVars(["QT_BASE_NAME"])
         self.parseExpression('echo "${QT_BASE_NAME}"')
lib/bb/tests/pysh.py new file +119 -0

119 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#3 lore raw
In reply to Olof Johansson
Quoted 15 lines · Olof Johansson

Hi,

A week or so I found out that shell fragments in bitbake recipes don't support shell arithmetics ($((1+1))). The reason for this is the shell lexing throwing NotImplemented exceptions. I've made some progress in adding support for shell arithmetics, but I have a hard time getting support for corner cases.

It was suprising to see that currently, only a subset of posix shell scripts is supported, even though they are sent through to be executed by the system shell. I think I understand the reason for having to do the shell lexing; knowing what functions and variables to export right? But I wonder if it's possible to do the shell lexing as a best effort, and ignore any errors.

Quoted 6 lines

What are the risks with this approach? Is it feasibile to do so?

This patch series is a work in progress, and does support the use cases we had issues with, however, I currently skip one unit test, as I can't get it to work. Comments and feedback much appreciated!

Did anybody have any feedback on this? I have had a hard time understanding the design of the lexing. I really could use some pointers --- is there some documents somewhere that could make me understand it better? I haven't had time to work on this lately, but it would be great to get this fixed, so I figured I'll try to take some time in the near future.

/Olof

PS. Saw that RP merged the exception message patch, thanks :).

#4 lore raw
Quoted 30 lines · Olof Johansson
Quoted 15 lines · Olof Johansson

Hi,

A week or so I found out that shell fragments in bitbake recipes don't support shell arithmetics ($((1+1))). The reason for this is the shell lexing throwing NotImplemented exceptions. I've made some progress in adding support for shell arithmetics, but I have a hard time getting support for corner cases.

It was suprising to see that currently, only a subset of posix shell scripts is supported, even though they are sent through to be executed by the system shell. I think I understand the reason for having to do the shell lexing; knowing what functions and variables to export right? But I wonder if it's possible to do the shell lexing as a best effort, and ignore any errors.

Quoted 6 lines

What are the risks with this approach? Is it feasibile to do so?

This patch series is a work in progress, and does support the use cases we had issues with, however, I currently skip one unit test, as I can't get it to work. Comments and feedback much appreciated!

Did anybody have any feedback on this? I have had a hard time understanding the design of the lexing. I really could use some pointers --- is there some documents somewhere that could make me understand it better? I haven't had time to work on this lately, but it would be great to get this fixed, so I figured I'll try to take some time in the near future.

Quoted 3 lines

/Olof

PS. Saw that RP merged the exception message patch, thanks :).

That one was easy :)

I'm afraid I don't have much knowledge of the sh lexing code, Chris might have more knowledge than me...

Cheers,

Richard