pyinotify.py: Simplify identification of which event has occurred

Message ID 20220408144430.29768-1-pkj@axis.com
State Accepted, archived
Commit 2ab60c7be124d928d304ab1fb73f0dbff29964ae
Headers show
Series pyinotify.py: Simplify identification of which event has occurred | expand

Commit Message

Peter Kjellerstedt April 8, 2022, 2:44 p.m. UTC
Use bitwise operators to manipulate the received event mask in
_ProcessEvent.

Also minor clarification & clean up of the related comments.

Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
---
 bitbake/lib/pyinotify.py | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

Comments

Richard Purdie April 12, 2022, 8:39 a.m. UTC | #1
On Fri, 2022-04-08 at 16:44 +0200, Peter Kjellerstedt wrote:
> Use bitwise operators to manipulate the received event mask in
> _ProcessEvent.
> 
> Also minor clarification & clean up of the related comments.
> 
> Signed-off-by: Peter Kjellerstedt <peter.kjellerstedt@axis.com>
> ---
>  bitbake/lib/pyinotify.py | 15 +++++++--------
>  1 file changed, 7 insertions(+), 8 deletions(-)
> 
> diff --git a/bitbake/lib/pyinotify.py b/bitbake/lib/pyinotify.py
> index 5c9b6d0fe2..3c5dab0312 100644
> --- a/bitbake/lib/pyinotify.py
> +++ b/bitbake/lib/pyinotify.py
> @@ -595,24 +595,23 @@ class _ProcessEvent:
>          @type event: Event object
>          @return: By convention when used from the ProcessEvent class:
>                   - Returning False or None (default value) means keep on
> -                 executing next chained functors (see chain.py example).
> +                   executing next chained functors (see chain.py example).
>                   - Returning True instead means do not execute next
>                     processing functions.
>          @rtype: bool
>          @raise ProcessEventError: Event object undispatchable,
>                                    unknown event.
>          """
> -        stripped_mask = event.mask - (event.mask & IN_ISDIR)
> -        # Bitbake hack - we see event masks of 0x6, IN_MODIFY & IN_ATTRIB
> +        stripped_mask = event.mask & ~IN_ISDIR
> +        # Bitbake hack - we see event masks of 0x6, i.e., IN_MODIFY & IN_ATTRIB.

What worries me a little is the above is part of the upstream we took this from.
That is why I copied the style below too although we don't have to do that.

I'm torn. Aligning with the upstream code is good but equally it appears
unmaintained now :(. There are other inotify extensions for python but this one
seemed the best for cooker's needs at the time. If we did adopt it, there are
probably things we could strip out as we only use a subsection of the code.

Cheers,

Richard

Patch

diff --git a/bitbake/lib/pyinotify.py b/bitbake/lib/pyinotify.py
index 5c9b6d0fe2..3c5dab0312 100644
--- a/bitbake/lib/pyinotify.py
+++ b/bitbake/lib/pyinotify.py
@@ -595,24 +595,23 @@  class _ProcessEvent:
         @type event: Event object
         @return: By convention when used from the ProcessEvent class:
                  - Returning False or None (default value) means keep on
-                 executing next chained functors (see chain.py example).
+                   executing next chained functors (see chain.py example).
                  - Returning True instead means do not execute next
                    processing functions.
         @rtype: bool
         @raise ProcessEventError: Event object undispatchable,
                                   unknown event.
         """
-        stripped_mask = event.mask - (event.mask & IN_ISDIR)
-        # Bitbake hack - we see event masks of 0x6, IN_MODIFY & IN_ATTRIB
+        stripped_mask = event.mask & ~IN_ISDIR
+        # Bitbake hack - we see event masks of 0x6, i.e., IN_MODIFY & IN_ATTRIB.
         # The kernel inotify code can set more than one of the bits in the mask,
         # fsnotify_change() in linux/fsnotify.h is quite clear that IN_ATTRIB,
         # IN_MODIFY and IN_ACCESS can arrive together.
         # This breaks the code below which assume only one mask bit is ever
-        # set in an event. We don't care about attrib or access in bitbake so drop those
-        if (stripped_mask & IN_MODIFY) and (stripped_mask & IN_ATTRIB):
-            stripped_mask = stripped_mask - (stripped_mask & IN_ATTRIB)
-        if (stripped_mask & IN_MODIFY) and (stripped_mask & IN_ACCESS):
-            stripped_mask = stripped_mask - (stripped_mask & IN_ACCESS)
+        # set in an event. We don't care about attrib or access in bitbake so
+        # drop those.
+        if stripped_mask & IN_MODIFY:
+            stripped_mask &= ~(IN_ATTRIB | IN_ACCESS)
 
         maskname = EventsCodes.ALL_VALUES.get(stripped_mask)
         if maskname is None: