From patchwork Thu Aug 4 18:33:54 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Zaman X-Patchwork-Id: 9264049 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C4F106048B for ; Thu, 4 Aug 2016 18:38:09 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id AACB2283E4 for ; Thu, 4 Aug 2016 18:38:09 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 998282841F; Thu, 4 Aug 2016 18:38:09 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, T_DKIM_INVALID autolearn=no version=3.3.1 Received: from emsm-gh1-uea10.nsa.gov (smtp.nsa.gov [8.44.101.8]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 617DD283E4 for ; Thu, 4 Aug 2016 18:38:06 +0000 (UTC) Received: from unknown (HELO tarius.tycho.ncsc.mil) ([144.51.242.1]) by emsm-gh1-uea10.nsa.gov with ESMTP; 04 Aug 2016 18:38:03 +0000 Received: from prometheus.infosec.tycho.ncsc.mil (prometheus [192.168.25.40]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u74IbxDf011729; Thu, 4 Aug 2016 14:38:02 -0400 Received: from tarius.tycho.ncsc.mil (tarius.infosec.tycho.ncsc.mil [144.51.242.1]) by prometheus.infosec.tycho.ncsc.mil (8.15.2/8.15.2) with ESMTP id u74IYMXl250297 for ; Thu, 4 Aug 2016 14:34:22 -0400 Received: from goalie.tycho.ncsc.mil (goalie [144.51.242.250]) by tarius.tycho.ncsc.mil (8.14.4/8.14.4) with ESMTP id u74IYMKU011354 for ; Thu, 4 Aug 2016 14:34:24 -0400 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: A1BhAQBGiqNXdMLAVdFcHQGCeoEDfLEshWSEDCCFfQKBSEwBAQEBAQETAQoLFBeFEAIBAxIuAQE3AQ85AQYRNAEFARwZIogPAaEUgTI+MYpUhSoBAQWLRQEBAQEBBQEBAQEbCBCED4gigz4BggALgweIKoVydIophhqFYIMKZYFShw2Feo5pMIEPglmCAmEBhWcNF4EfAQEB X-IPAS-Result: A1BhAQBGiqNXdMLAVdFcHQGCeoEDfLEshWSEDCCFfQKBSEwBAQEBAQETAQoLFBeFEAIBAxIuAQE3AQ85AQYRNAEFARwZIogPAaEUgTI+MYpUhSoBAQWLRQEBAQEBBQEBAQEbCBCED4gigz4BggALgweIKoVydIophhqFYIMKZYFShw2Feo5pMIEPglmCAmEBhWcNF4EfAQEB X-IronPort-AV: E=Sophos;i="5.28,471,1464667200"; d="scan'208";a="5626382" Received: from emsm-gh1-uea10.corp.nsa.gov (HELO emsm-gh1-uea10.nsa.gov) ([10.208.41.36]) by goalie.tycho.ncsc.mil with ESMTP; 04 Aug 2016 14:35:11 -0400 Received: from mail-pf0-f194.google.com ([209.85.192.194]) by emsm-gh1-uea10.nsa.gov with ESMTP/TLS/AES128-GCM-SHA256; 04 Aug 2016 18:34:23 +0000 Received: by mail-pf0-f194.google.com with SMTP id i6so18706705pfe.0 for ; Thu, 04 Aug 2016 11:34:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=perfinion-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=bzGXahyDPoRrxSujMYD59VscVLGBfUmkddK95Bu7Qq4=; b=QdvGQw4qd541VWAW1nTUWvrBQeLH2j+cdWC0f128Rrl0pEXKGpL2GLjA6I33ILA2L+ XsesLrHgdITmFncDZ7Z1tliCjnsdpUH5zFYxvogtpTlDinlFR8IvPSr8AHXJwLJcnljR TWT0DNQEg2/HQo7yRBN9pVBaza3LhS2dN6FiAaYJGXJMgcpKGEIJIbDVnKXT1tf2ZUnr wC95ELd1IjK9phxeSRShfAy8L4qVGoid/JPrhJx19kdpfzRukDk1lVp7DkpBUF87YX3+ ojECvXIPVPopJP2xonlRQkLovxwgNq+cvD1qKGMFJgqwB/I1PYDSY79/YDM0DcTuS9WT 5FvA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=bzGXahyDPoRrxSujMYD59VscVLGBfUmkddK95Bu7Qq4=; b=HLy/l6BhAJ46bt1i5fsW0JaG+cKrtdyKSS3k6mOHoU7BsvHC2G6Je4sPzam0WpciQ1 0BNXEils9ydhordeAlXAHWBFZ4DlSKXFahU5jeB6LhGJFyXfb7uuPnfzhO45ZcrLkfPA hf6uAZPYdWC7KP8OxDlR4PB72PegMvST3/gRQlj6Z9w5VAh0dz72Mdxo+pfPUjA9F2tb qSkVrnoNKurYD2oIAOoe2OeZdcsLSWHgKqoEbLSedAC1ALXzI8oAhNQHk7dP56Kw3RB8 mbOG8GNT2jKDZbCKgHEGAfHHiIPapvn++UtOSiO6u6+gs36UjRHQoZrJz7ECF6gCrPNI xrbA== X-Gm-Message-State: AEkoouvnAKYz1MLQDcjLoplWGEXDbidbQgiTNHFHH2r8vIG/KMXm+34+fhyrqyfrySUkUQ== X-Received: by 10.98.16.75 with SMTP id y72mr128109967pfi.50.1470335661975; Thu, 04 Aug 2016 11:34:21 -0700 (PDT) Received: from localhost ([2404:e800:e600:57b:dcfa:b5a4:c9d2:f744]) by smtp.gmail.com with ESMTPSA id x66sm22040581pfb.86.2016.08.04.11.34.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Aug 2016 11:34:20 -0700 (PDT) From: Jason Zaman To: selinux@tycho.nsa.gov Subject: [PATCH 01/12] semanage: py3 support for print function and map/filter Date: Fri, 5 Aug 2016 02:33:54 +0800 Message-Id: <1470335645-24224-2-git-send-email-jason@perfinion.com> X-Mailer: git-send-email 2.7.3 In-Reply-To: <1470335645-24224-1-git-send-email-jason@perfinion.com> References: <1470335645-24224-1-git-send-email-jason@perfinion.com> X-BeenThere: selinux@tycho.nsa.gov X-Mailman-Version: 2.1.20 Precedence: list List-Id: "Security-Enhanced Linux \(SELinux\) mailing list" List-Post: List-Help: MIME-Version: 1.0 Errors-To: selinux-bounces@tycho.nsa.gov Sender: "Selinux" X-Virus-Scanned: ClamAV using ClamSMTP map and filter were turned into list comprehensions. This was done using python's 2to3 utility. Signed-off-by: Jason Zaman --- policycoreutils/semanage/seobject.py | 94 +++++++++++++++---------------- policycoreutils/semanage/test-semanage.py | 2 +- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/policycoreutils/semanage/seobject.py b/policycoreutils/semanage/seobject.py index 3b0b108..863d4a5 100644 --- a/policycoreutils/semanage/seobject.py +++ b/policycoreutils/semanage/seobject.py @@ -336,7 +336,7 @@ class moduleRecords(semanageRecords): all = self.get_all() if len(all) == 0: return - return map(lambda x: "-d %s" % x[0], filter(lambda t: t[1] == 0, all)) + return ["-d %s" % x[0] for x in [t for t in all if t[1] == 0]] def list(self, heading=1, locallist=0): all = self.get_all() @@ -344,7 +344,7 @@ class moduleRecords(semanageRecords): return if heading: - print "\n%-25s %-9s %s\n" % (_("Module Name"), _("Priority"), _("Language")) + print("\n%-25s %-9s %s\n" % (_("Module Name"), _("Priority"), _("Language"))) for t in all: if t[1] == 0: disabled = _("Disabled") @@ -352,7 +352,7 @@ class moduleRecords(semanageRecords): if locallist: continue disabled = "" - print "%-25s %-9s %-5s %s" % (t[0], t[2], t[3], disabled) + print("%-25s %-9s %-5s %s" % (t[0], t[2], t[3], disabled)) def add(self, file, priority): if not os.path.exists(file): @@ -402,7 +402,7 @@ class moduleRecords(semanageRecords): self.commit() def deleteall(self): - l = map(lambda x: x[0], filter(lambda t: t[1] == 0, self.get_all())) + l = [x[0] for x in [t for t in self.get_all() if t[1] == 0]] for m in l: self.set_enabled(m, True) @@ -439,24 +439,24 @@ class permissiveRecords(semanageRecords): return l def list(self, heading=1, locallist=0): - all = map(lambda y: y["name"], filter(lambda x: x["permissive"], sepolicy.info(sepolicy.TYPE))) + all = [y["name"] for y in [x for x in sepolicy.info(sepolicy.TYPE) if x["permissive"]]] if len(all) == 0: return if heading: - print "\n%-25s\n" % (_("Builtin Permissive Types")) + print("\n%-25s\n" % (_("Builtin Permissive Types"))) customized = self.get_all() for t in all: if t not in customized: - print t + print(t) if len(customized) == 0: return if heading: - print "\n%-25s\n" % (_("Customized Permissive Types")) + print("\n%-25s\n" % (_("Customized Permissive Types"))) for t in customized: - print t + print(t) def add(self, type): import glob @@ -564,7 +564,7 @@ class loginRecords(semanageRecords): self.begin() self.__add(name, sename, serange) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -624,7 +624,7 @@ class loginRecords(semanageRecords): self.begin() self.__modify(name, sename, serange) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -666,7 +666,7 @@ class loginRecords(semanageRecords): self.__delete(name) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -680,7 +680,7 @@ class loginRecords(semanageRecords): for u in ulist: self.__delete(semanage_seuser_get_name(u)) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -734,21 +734,21 @@ class loginRecords(semanageRecords): if is_mls_enabled == 1: if heading: - print "\n%-20s %-20s %-20s %s\n" % (_("Login Name"), _("SELinux User"), _("MLS/MCS Range"), _("Service")) + print("\n%-20s %-20s %-20s %s\n" % (_("Login Name"), _("SELinux User"), _("MLS/MCS Range"), _("Service"))) for k in keys: u = ddict[k] - print "%-20s %-20s %-20s %s" % (k, u[0], translate(u[1]), u[2]) + print("%-20s %-20s %-20s %s" % (k, u[0], translate(u[1]), u[2])) if len(lkeys): - print "\nLocal customization in %s" % self.logins_path + print("\nLocal customization in %s" % self.logins_path) for k in lkeys: u = ldict[k] - print "%-20s %-20s %-20s %s" % (k, u[0], translate(u[1]), u[2]) + print("%-20s %-20s %-20s %s" % (k, u[0], translate(u[1]), u[2])) else: if heading: - print "\n%-25s %-25s\n" % (_("Login Name"), _("SELinux User")) + print("\n%-25s %-25s\n" % (_("Login Name"), _("SELinux User"))) for k in keys: - print "%-25s %-25s" % (k, ddict[k][0]) + print("%-25s %-25s" % (k, ddict[k][0])) class seluserRecords(semanageRecords): @@ -839,7 +839,7 @@ class seluserRecords(semanageRecords): self.begin() self.__add(name, roles, selevel, serange, prefix) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -904,7 +904,7 @@ class seluserRecords(semanageRecords): self.begin() self.__modify(name, roles, selevel, serange, prefix) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -947,7 +947,7 @@ class seluserRecords(semanageRecords): self.__delete(name) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -961,7 +961,7 @@ class seluserRecords(semanageRecords): for u in ulist: self.__delete(semanage_user_get_name(u)) self.commit() - except ValueError, error: + except ValueError as error: self.mylog.commit(0) raise error @@ -1003,15 +1003,15 @@ class seluserRecords(semanageRecords): if is_mls_enabled == 1: if heading: - print "\n%-15s %-10s %-10s %-30s" % ("", _("Labeling"), _("MLS/"), _("MLS/")) - print "%-15s %-10s %-10s %-30s %s\n" % (_("SELinux User"), _("Prefix"), _("MCS Level"), _("MCS Range"), _("SELinux Roles")) + print("\n%-15s %-10s %-10s %-30s" % ("", _("Labeling"), _("MLS/"), _("MLS/"))) + print("%-15s %-10s %-10s %-30s %s\n" % (_("SELinux User"), _("Prefix"), _("MCS Level"), _("MCS Range"), _("SELinux Roles"))) for k in keys: - print "%-15s %-10s %-10s %-30s %s" % (k, ddict[k][0], translate(ddict[k][1]), translate(ddict[k][2]), ddict[k][3]) + print("%-15s %-10s %-10s %-30s %s" % (k, ddict[k][0], translate(ddict[k][1]), translate(ddict[k][2]), ddict[k][3])) else: if heading: - print "%-15s %s\n" % (_("SELinux User"), _("SELinux Roles")) + print("%-15s %s\n" % (_("SELinux User"), _("SELinux Roles"))) for k in keys: - print "%-15s %s" % (k, ddict[k][3]) + print("%-15s %s" % (k, ddict[k][3])) class portRecords(semanageRecords): @@ -1268,13 +1268,13 @@ class portRecords(semanageRecords): keys.sort() if heading: - print "%-30s %-8s %s\n" % (_("SELinux Port Type"), _("Proto"), _("Port Number")) + print("%-30s %-8s %s\n" % (_("SELinux Port Type"), _("Proto"), _("Port Number"))) for i in keys: rec = "%-30s %-8s " % i rec += "%s" % ddict[i][0] for p in ddict[i][1:]: rec += ", %s" % p - print rec + print(rec) class nodeRecords(semanageRecords): @@ -1502,16 +1502,16 @@ class nodeRecords(semanageRecords): keys.sort() if heading: - print "%-18s %-18s %-5s %-5s\n" % ("IP Address", "Netmask", "Protocol", "Context") + print("%-18s %-18s %-5s %-5s\n" % ("IP Address", "Netmask", "Protocol", "Context")) if is_mls_enabled: for k in keys: val = '' for fields in k: val = val + '\t' + str(fields) - print "%-18s %-18s %-5s %s:%s:%s:%s " % (k[0], k[1], k[2], ddict[k][0], ddict[k][1], ddict[k][2], translate(ddict[k][3], False)) + print("%-18s %-18s %-5s %s:%s:%s:%s " % (k[0], k[1], k[2], ddict[k][0], ddict[k][1], ddict[k][2], translate(ddict[k][3], False))) else: for k in keys: - print "%-18s %-18s %-5s %s:%s:%s " % (k[0], k[1], k[2], ddict[k][0], ddict[k][1], ddict[k][2]) + print("%-18s %-18s %-5s %s:%s:%s " % (k[0], k[1], k[2], ddict[k][0], ddict[k][1], ddict[k][2])) class interfaceRecords(semanageRecords): @@ -1693,13 +1693,13 @@ class interfaceRecords(semanageRecords): keys.sort() if heading: - print "%-30s %s\n" % (_("SELinux Interface"), _("Context")) + print("%-30s %s\n" % (_("SELinux Interface"), _("Context"))) if is_mls_enabled: for k in keys: - print "%-30s %s:%s:%s:%s " % (k, ddict[k][0], ddict[k][1], ddict[k][2], translate(ddict[k][3], False)) + print("%-30s %s:%s:%s:%s " % (k, ddict[k][0], ddict[k][1], ddict[k][2], translate(ddict[k][3], False))) else: for k in keys: - print "%-30s %s:%s:%s " % (k, ddict[k][0], ddict[k][1], ddict[k][2]) + print("%-30s %s:%s:%s " % (k, ddict[k][0], ddict[k][1], ddict[k][2])) class fcontextRecords(semanageRecords): @@ -2048,28 +2048,28 @@ class fcontextRecords(semanageRecords): if len(keys) != 0: keys.sort() if heading: - print "%-50s %-18s %s\n" % (_("SELinux fcontext"), _("type"), _("Context")) + print("%-50s %-18s %s\n" % (_("SELinux fcontext"), _("type"), _("Context"))) for k in keys: if fcon_dict[k]: if is_mls_enabled: - print "%-50s %-18s %s:%s:%s:%s " % (k[0], k[1], fcon_dict[k][0], fcon_dict[k][1], fcon_dict[k][2], translate(fcon_dict[k][3], False)) + print("%-50s %-18s %s:%s:%s:%s " % (k[0], k[1], fcon_dict[k][0], fcon_dict[k][1], fcon_dict[k][2], translate(fcon_dict[k][3], False))) else: - print "%-50s %-18s %s:%s:%s " % (k[0], k[1], fcon_dict[k][0], fcon_dict[k][1], fcon_dict[k][2]) + print("%-50s %-18s %s:%s:%s " % (k[0], k[1], fcon_dict[k][0], fcon_dict[k][1], fcon_dict[k][2])) else: - print "%-50s %-18s <>" % (k[0], k[1]) + print("%-50s %-18s <>" % (k[0], k[1])) if len(self.equiv_dist): if not locallist: if heading: - print _("\nSELinux Distribution fcontext Equivalence \n") + print(_("\nSELinux Distribution fcontext Equivalence \n")) for target in self.equiv_dist.keys(): - print "%s = %s" % (target, self.equiv_dist[target]) + print("%s = %s" % (target, self.equiv_dist[target])) if len(self.equiv): if heading: - print _("\nSELinux Local fcontext Equivalence \n") + print(_("\nSELinux Local fcontext Equivalence \n")) for target in self.equiv.keys(): - print "%s = %s" % (target, self.equiv[target]) + print("%s = %s" % (target, self.equiv[target])) class booleanRecords(semanageRecords): @@ -2237,7 +2237,7 @@ class booleanRecords(semanageRecords): keys = ddict.keys() for k in keys: if ddict[k]: - print "%s=%s" % (k, ddict[k][2]) + print("%s=%s" % (k, ddict[k][2])) return ddict = self.get_all(locallist) keys = ddict.keys() @@ -2245,7 +2245,7 @@ class booleanRecords(semanageRecords): return if heading: - print "%-30s %s %s %s\n" % (_("SELinux boolean"), _("State"), _("Default"), _("Description")) + print("%-30s %s %s %s\n" % (_("SELinux boolean"), _("State"), _("Default"), _("Description"))) for k in keys: if ddict[k]: - print "%-30s (%-5s,%5s) %s" % (k, on_off[selinux.security_get_boolean_active(k)], on_off[ddict[k][2]], self.get_desc(k)) + print("%-30s (%-5s,%5s) %s" % (k, on_off[selinux.security_get_boolean_active(k)], on_off[ddict[k][2]], self.get_desc(k))) diff --git a/policycoreutils/semanage/test-semanage.py b/policycoreutils/semanage/test-semanage.py index 293e1fd..10c4d8f 100644 --- a/policycoreutils/semanage/test-semanage.py +++ b/policycoreutils/semanage/test-semanage.py @@ -278,7 +278,7 @@ def gen_semanage_test_args(parser): if __name__ == "__main__": import selinux - semanage_test_list = filter(lambda x: x.startswith("test_"), dir(SemanageTests)) + semanage_test_list = [x for x in dir(SemanageTests) if x.startswith("test_")] if selinux.security_getenforce() == 1: parser = argparse.ArgumentParser(description='Semanage unit test script') gen_semanage_test_args(parser)