Skip to contentSkip to conversation

Fix some issues found by flake8

PATCH In review Nicolas Iooss 2 participants 16 messages Aug 12, 2018 – Aug 15, 2018 SELinux security module

lore
Share & tools
lore
#0 lore raw

v2 cover Fix some issues found by flake8

Here is the second revision of this patchset. The changes were:

* I removed the buggy code instead of commenting it, in python/sepolgen.
* I fixed a typo in the description of a patch.

Nicolas Iooss (13):
libselinux: fix flake8 warnings in SWIG-generated code python/sepolgen: do not import twice the modules python/sepolgen: return NotImplemented instead of raising it python/sepolicy: drop unused CheckPolicyType python/sepolicy: use lowercase variable name python/sepolgen: fix refpolicy parsing of "permissive" python/sepolgen: silence linter warning about has_key python/sepolgen: remove buggy code
python/sepolgen: use self when accessing members in FilesystemUse python/sepolicy: fix "procotol" misspelling python/sepolicy: use variables which exist in the gui.py python/sepolicy: do not import sepolicy.generate.DAEMON twice python/sepolicy: do not import types

 libselinux/src/selinuxswig_python.i        |  5 +++--
 python/sepolgen/src/sepolgen/interfaces.py |  7 +------
 python/sepolgen/src/sepolgen/refparser.py  |  4 ++--
 python/sepolgen/src/sepolgen/refpolicy.py  |  6 +++---
 python/sepolgen/src/sepolgen/util.py       |  6 +-----
 python/sepolicy/sepolicy.py                | 12 +-----------
 python/sepolicy/sepolicy/generate.py       |  1 -
 python/sepolicy/sepolicy/gui.py            | 12 ++++++------
 8 files changed, 17 insertions(+), 36 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH v2 1/13 libselinux: fix flake8 warnings in SWIG-generated code

flake8, a Python linter, complains:

libselinux/src/selinux.py:100:1: F401 'errno' imported but unused libselinux/src/selinux.py:100:1: F401 'stat' imported but unused libselinux/src/selinux.py:100:14: E401 multiple imports on one line libselinux/src/selinux.py:130:16: E111 indentation is not a multiple of four libselinux/src/selinux.py:130:45: E231 missing whitespace after ','

---
 libselinux/src/selinuxswig_python.i | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
libselinux/src/selinuxswig_python.i +3 -2
index be17ceffe01f..2af7c7e0c038 100644
@@ -13,7 +13,8 @@
 
 %pythoncode %{
 
-import shutil, os, errno, stat
+import shutil
+import os
 
 DISABLED = -1
 PERMISSIVE = 0
@@ -43,7 +44,7 @@ def chcon(path, context, recursive=False):
     if recursive:
         for root, dirs, files in os.walk(path):
             for name in files + dirs:
-               lsetfilecon(os.path.join(root,name), context)
+                lsetfilecon(os.path.join(root, name), context)
 
 def copytree(src, dest):
     """ An SELinux-friendly shutil.copytree method """
Signature · 1 line
2.18.0
#2 lore raw

PATCH v2 2/13 python/sepolgen: do not import twice the modules

In reply to Nicolas Iooss

util.py starts by importing "locale" and "sys", so there is no need to import these modules again in the functions.

---
 python/sepolgen/src/sepolgen/util.py | 4 ----
 1 file changed, 4 deletions(-)
python/sepolgen/src/sepolgen/util.py +0 -4
index 1fca9717c05a..5d38bce29827 100644
@@ -90,7 +90,6 @@ def first(s, sorted=False):
             return x
 
 def encode_input(text):
-    import locale
     """Encode given text via preferred system encoding"""
     # locale will often find out the correct encoding
     encoding = locale.getpreferredencoding()
@@ -104,7 +103,6 @@ def encode_input(text):
     return encoded_text
 
 def decode_input(text):
-    import locale
     """Decode given text via preferred system encoding"""
     # locale will often find out the correct encoding
     encoding = locale.getpreferredencoding()
@@ -172,11 +170,9 @@ def cmp(first, second):
     return (first > second) - (second > first)
 
 if __name__ == "__main__":
-    import sys
     import time
     p = ConsoleProgressBar(sys.stdout, steps=999)
     p.start("computing pi")
     for i in range(999):
         p.step()
         time.sleep(0.001)
-
Signature · 1 line
2.18.0
#3 lore raw

PATCH v2 3/13 python/sepolgen: return NotImplemented instead of raising it

In reply to Nicolas Iooss

sepolgen uses "return NotImplemented" (in access.py and matching.py) in order to make Python's sorting function use an other call to compare objects. For this to work, "NotImplemented" needs to be returned, not raised like _compare's default implementation does.

This issue has been found using flake8. This Python linter reported:

python/sepolgen/src/sepolgen/util.py:128:9: F901 'raise NotImplemented' should be 'raise NotImplementedError'

---
 python/sepolgen/src/sepolgen/util.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/util.py +1 -1
index 5d38bce29827..f5b66d0370a2 100644
@@ -123,7 +123,7 @@ class Comparison():
     _compare function within your class."""
 
     def _compare(self, other, method):
-        raise NotImplemented
+        return NotImplemented
 
     def __eq__(self, other):
         return self._compare(other, lambda a, b: a == b)
Signature · 1 line
2.18.0
#4 lore raw

PATCH v2 4/13 python/sepolicy: drop unused CheckPolicyType

In reply to Nicolas Iooss

CheckPolicyType class is not used in sepolicy and has been written from an buggy copy-paste: "newval.append(v)" does not make any sense in CheckPolicyType.__call__, as reported by flake8:

python/sepolicy/sepolicy.py:190:13: F821 undefined name 'newval' python/sepolicy/sepolicy.py:190:27: F821 undefined name 'v'

---
 python/sepolicy/sepolicy.py | 10 ----------
 1 file changed, 10 deletions(-)
python/sepolicy/sepolicy.py +0 -10
index 141f64ecd15f..24631b51ee37 100755
@@ -181,16 +181,6 @@ class LoadPolicy(argparse.Action):
         setattr(namespace, self.dest, values)
 
 
-class CheckPolicyType(argparse.Action):
-
-    def __call__(self, parser, namespace, values, option_string=None):
-        from sepolicy.generate import get_poltype_desc, poltype
-        if values not in poltype.keys():
-            raise ValueError("%s invalid SELinux policy type\n%s" % (values, get_poltype_desc()))
-            newval.append(v)
-        setattr(namespace, self.dest, values)
-
-
 class CheckUser(argparse.Action):
 
     def __call__(self, parser, namespace, value, option_string=None):
Signature · 1 line
2.18.0
#5 lore raw

PATCH v2 5/13 python/sepolicy: use lowercase variable name

In reply to Nicolas Iooss

SELinuxGui.files_initial_data_insert() uses both "seLinux_label" and "selinux_label", which leads to using an undefined variable, as reported by flake8:

python/sepolicy/sepolicy/gui.py:1191:42: F821 undefined name 'selinux_label'

---
 python/sepolicy/sepolicy/gui.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
python/sepolicy/sepolicy/gui.py +2 -2
index 537d5168a536..16f24a0ab2f9 100644
@@ -1175,7 +1175,7 @@ class SELinuxGui():
                         continue
                 self.files_initial_data_insert(self.writable_files_liststore, path, write, file_class)
 
-    def files_initial_data_insert(self, liststore, path, seLinux_label, file_class):
+    def files_initial_data_insert(self, liststore, path, selinux_label, file_class):
         iter = liststore.append(None)
         if path is None:
             path = _("MISSING FILE PATH")
@@ -1191,7 +1191,7 @@ class SELinuxGui():
                 file_class = self.markup(selinux_label)
                 file_class = self.markup(file_class)
         liststore.set_value(iter, 0, path)
-        liststore.set_value(iter, 1, seLinux_label)
+        liststore.set_value(iter, 1, selinux_label)
         liststore.set_value(iter, 2, file_class)
         liststore.set_value(iter, 7, modify)
 
Signature · 1 line
2.18.0
#6 lore raw

PATCH v2 6/13 python/sepolgen: fix refpolicy parsing of "permissive"

In reply to Nicolas Iooss

p_permissive() uses an undefined variable t, which is buggy. In order to ignore permissive statements, the function only needs to "pass".

flake8 reported the following error:

python/sepolgen/src/sepolgen/refparser.py:789:5: F821 undefined name 't'

---
 python/sepolgen/src/sepolgen/refparser.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refparser.py +1 -1
index 2cef8e85c679..3415aff9b48f 100644
@@ -786,7 +786,7 @@ def p_role_allow(p):
 
 def p_permissive(p):
     'permissive : PERMISSIVE names SEMI'
-    t.skip(1)
+    pass
 
 def p_avrule_def(p):
     '''avrule_def : ALLOW names names COLON names names SEMI
Signature · 1 line
2.18.0
#7 lore raw

PATCH v2 7/13 python/sepolgen: silence linter warning about has_key

In reply to Nicolas Iooss

flake8 warns about using has_key():

python/sepolgen/src/sepolgen/refparser.py:315:15: W601 .has_key() is deprecated, use 'in'

However "spt.has_key(id)" uses function SupportMacros.has_key(), which is not the same as the "in" operator. Silence this warning by using "# noqa".

---
 python/sepolgen/src/sepolgen/refparser.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolgen/src/sepolgen/refparser.py +1 -1
index 3415aff9b48f..97e2342c4693 100644
@@ -312,7 +312,7 @@ def collect(stmts, parent, val=None):
 
 def expand(ids, s):
     for id in ids:
-        if spt.has_key(id):
+        if spt.has_key(id):  # noqa
             s.update(spt.by_name(id))
         else:
             s.add(id)
Signature · 1 line
2.18.0
#8 lore raw

PATCH v2 8/13 python/sepolgen: remove buggy code

In reply to Nicolas Iooss

av_extract_params() may call __param_insert() with only 2 parameters instead of 4, which has no chance to work fine. Moreover it uses "PERM", which is undefined. As nobody complained about this code, it seems to be dead, so remove it.

This issue has been found using flake8. This Python linter reported:

python/sepolgen/src/sepolgen/interfaces.py:158:37: F821 undefined name 'PERM'

While at it, fix a typo in a comment.

---
 python/sepolgen/src/sepolgen/interfaces.py | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
python/sepolgen/src/sepolgen/interfaces.py +1 -6
index 48ae4f27a414..f4d3e5c5d6e0 100644
@@ -112,7 +112,7 @@ def av_extract_params(av, params):
     entries in the dict, but if an unresolvable conflict is
     found it is reported to the caller.
 
-    The goal here is to figure out how interface paramaters are
+    The goal here is to figure out how interface parameters are
     actually used in the interface - e.g., that $1 is a domain used as
     a SRC_TYPE. In general an interface will look like this:
 
@@ -153,11 +153,6 @@ def av_extract_params(av, params):
         if __param_insert(av.obj_class, refpolicy.OBJ_CLASS, av, params) == 1:
             ret = 1
 
-    for perm in av.perms:
-        if access.is_idparam(perm):
-            if __param_insert(perm, PERM) == 1:
-                ret = 1
-
     return ret
 
 def role_extract_params(role, params):
Signature · 1 line
2.18.0
#9 lore raw

PATCH v2 9/13 python/sepolgen: use self when accessing members in FilesystemUse

In reply to Nicolas Iooss

This silences the following flake8 errors:

python/sepolgen/src/sepolgen/refpolicy.py:758:25: F821 undefined name 'XATTR' python/sepolgen/src/sepolgen/refpolicy.py:760:27: F821 undefined name 'TRANS' python/sepolgen/src/sepolgen/refpolicy.py:762:27: F821 undefined name 'TASK'

---
 python/sepolgen/src/sepolgen/refpolicy.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolgen/src/sepolgen/refpolicy.py +3 -3
index c30a8c70f9ca..e3dd33a21935 100644
@@ -755,11 +755,11 @@ class FilesystemUse(Leaf):
 
     def to_string(self):
         s = ""
-        if self.type == XATTR:
+        if self.type == self.XATTR:
             s = "fs_use_xattr "
-        elif self.type == TRANS:
+        elif self.type == self.TRANS:
             s = "fs_use_trans "
-        elif self.type == TASK:
+        elif self.type == self.TASK:
             s = "fs_use_task "
 
         return "%s %s %s;" % (s, self.filesystem, str(self.context))
Signature · 1 line
2.18.0
#10 lore raw

PATCH v2 10/13 python/sepolicy: fix "procotol" misspelling

In reply to Nicolas Iooss

procotol -> protocol

This issue has been found using flake8. This Python linter reported:

    python/sepolicy/sepolicy/gui.py:2525:132: F821 undefined name 'procotol'
---
 python/sepolicy/sepolicy/gui.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolicy/sepolicy/gui.py +1 -1
index 16f24a0ab2f9..6933f6a142ed 100644
@@ -2522,7 +2522,7 @@ class SELinuxGui():
                     if self.cur_dict[k][(port, protocol)]["action"] == "-d":
                         update_buffer += "port -d -p %s %s\n" % (protocol, port)
                     else:
-                        update_buffer += "port %s -t %s -p %s %s\n" % (self.cur_dict[k][f]["action"], self.cur_dict[k][f]["type"], procotol, port)
+                        update_buffer += "port %s -t %s -p %s %s\n" % (self.cur_dict[k][f]["action"], self.cur_dict[k][f]["type"], protocol, port)
 
         return update_buffer
 
Signature · 1 line
2.18.0
#11 lore raw

PATCH v2 11/13 python/sepolicy: use variables which exist in the gui.py

In reply to Nicolas Iooss

Some function used variables which were not defined when populating self.cur_dict with an operation. Fix this, even though the old values do not seem to be used.

This issue has been found using flake8. This Python linter reported:

    python/sepolicy/sepolicy/gui.py:2020:101: F821 undefined name 'oldsetype'
    python/sepolicy/sepolicy/gui.py:2020:122: F821 undefined name 'oldmls'
    python/sepolicy/sepolicy/gui.py:2020:142: F821 undefined name 'oldclass'
    python/sepolicy/sepolicy/gui.py:2050:133: F821 undefined name 'oldmls'
---
 python/sepolicy/sepolicy/gui.py | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
python/sepolicy/sepolicy/gui.py +3 -3
index 6933f6a142ed..9d413652f2ed 100644
@@ -2015,9 +2015,9 @@ class SELinuxGui():
         if self.modify:
             iter = self.get_selected_iter()
             oldpath = self.unmark(self.liststore.get_value(iter, 0))
-            setype = self.unmark(self.liststore.set_value(iter, 1))
+            oldsetype = self.unmark(self.liststore.set_value(iter, 1))
             oldtclass = self.liststore.get_value(iter, 2)
-            self.cur_dict["fcontext"][(path, tclass)] = {"action": "-m", "type": setype, "oldtype": oldsetype, "oldmls": oldmls, "oldclass": oldclass}
+            self.cur_dict["fcontext"][(path, tclass)] = {"action": "-m", "type": setype, "oldtype": oldsetype, "oldpath": oldpath, "oldclass": oldtclass}
         else:
             iter = self.liststore.append(None)
             self.cur_dict["fcontext"][(path, tclass)] = {"action": "-a", "type": setype}
@@ -2047,7 +2047,7 @@ class SELinuxGui():
             oldports = self.unmark(self.liststore.get_value(iter, 0))
             oldprotocol = self.unmark(self.liststore.get_value(iter, 1))
             oldsetype = self.unmark(self.liststore.set_value(iter, 2))
-            self.cur_dict["port"][(ports, protocol)] = {"action": "-m", "type": setype, "mls": mls, "oldtype": oldsetype, "oldmls": oldmls, "oldprotocol": oldprotocol, "oldports": oldports}
+            self.cur_dict["port"][(ports, protocol)] = {"action": "-m", "type": setype, "mls": mls, "oldtype": oldsetype, "oldprotocol": oldprotocol, "oldports": oldports}
         else:
             iter = self.liststore.append(None)
             self.cur_dict["port"][(ports, protocol)] = {"action": "-a", "type": setype, "mls": mls}
Signature · 1 line
2.18.0
#12 lore raw

PATCH v2 12/13 python/sepolicy: do not import sepolicy.generate.DAEMON twice

In reply to Nicolas Iooss

This issue has been found using flake8. This Python linter reported:

python/sepolicy/sepolicy.py:582:5: F811 redefinition of unused 'DAEMON' from line 582

---
 python/sepolicy/sepolicy.py | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
python/sepolicy/sepolicy.py +1 -1
index 24631b51ee37..a000c1addd99 100755
@@ -579,7 +579,7 @@ def gen_interface_args(parser):
 
 
 def gen_generate_args(parser):
-    from sepolicy.generate import DAEMON, get_poltype_desc, poltype, DAEMON, DBUS, INETD, CGI, SANDBOX, USER, EUSER, TUSER, XUSER, LUSER, AUSER, RUSER, NEWTYPE
+    from sepolicy.generate import get_poltype_desc, poltype, DAEMON, DBUS, INETD, CGI, SANDBOX, USER, EUSER, TUSER, XUSER, LUSER, AUSER, RUSER, NEWTYPE
 
     generate_usage = generate_custom_usage(usage, usage_dict)
 
Signature · 1 line
2.18.0
#13 lore raw

PATCH v2 13/13 python/sepolicy: do not import types

In reply to Nicolas Iooss

This module is not used and its name conflicts with variable "types".

This issue has been found using flake8. This Python linter reported:

python/sepolicy/sepolicy/generate.py:657:5: F811 redefinition of unused 'types' from line 31

---
 python/sepolicy/sepolicy/generate.py | 1 -
 1 file changed, 1 deletion(-)
python/sepolicy/sepolicy/generate.py +0 -1
index f814e278663e..37ddfc7a2d63 100644
@@ -28,7 +28,6 @@ import re
 import sepolicy
 from sepolicy import get_all_types, get_all_attributes, get_all_roles
 import time
-import types
 import platform
 
 from .templates import executable
Signature · 1 line
2.18.0
#14 lore raw
In reply to Nicolas Iooss
Quoted 6 lines · Nicolas Iooss

Here is the second revision of this patchset. The changes were:

* I removed the buggy code instead of commenting it, in python/sepolgen.
* I fixed a typo in the description of a patch.

Ack on the patchset from me.

Quoted 36 lines

Nicolas Iooss (13):
libselinux: fix flake8 warnings in SWIG-generated code python/sepolgen: do not import twice the modules python/sepolgen: return NotImplemented instead of raising it python/sepolicy: drop unused CheckPolicyType python/sepolicy: use lowercase variable name python/sepolgen: fix refpolicy parsing of "permissive" python/sepolgen: silence linter warning about has_key python/sepolgen: remove buggy code
python/sepolgen: use self when accessing members in FilesystemUse python/sepolicy: fix "procotol" misspelling python/sepolicy: use variables which exist in the gui.py python/sepolicy: do not import sepolicy.generate.DAEMON twice python/sepolicy: do not import types

 libselinux/src/selinuxswig_python.i        |  5 +++--
 python/sepolgen/src/sepolgen/interfaces.py |  7 +------
 python/sepolgen/src/sepolgen/refparser.py  |  4 ++--
 python/sepolgen/src/sepolgen/refpolicy.py  |  6 +++---
 python/sepolgen/src/sepolgen/util.py       |  6 +-----
 python/sepolicy/sepolicy.py                | 12 +-----------
 python/sepolicy/sepolicy/generate.py       |  1 -
 python/sepolicy/sepolicy/gui.py            | 12 ++++++------
 8 files changed, 17 insertions(+), 36 deletions(-)

--
2.18.0

_______________________________________________
Selinux mailing list
[email protected]
To unsubscribe, send email to [email protected]. To get help, send an email containing "help" to [email protected].

#15 lore raw
Quoted 9 lines · William Roberts
Quoted 6 lines · Nicolas Iooss

Here is the second revision of this patchset. The changes were:

* I removed the buggy code instead of commenting it, in python/sepolgen.
* I fixed a typo in the description of a patch.

Ack on the patchset from me.

Thanks. I applied it.

Nicolas