[PATCH 42 of 54] CPE: Add a generic 'parse' method

Wald Commits scm-commit at wald.intevation.org
Wed Jan 7 10:56:59 CET 2015


# HG changeset patch
# User Benoît Allard <benoit.allard at greenbone.net>
# Date 1419861509 -3600
# Node ID bb1dd2a55643f02ed5d51474d839a23ee2902e3e
# Parent  1d9b2b06067eb9bd9c400e2028f33f3c56dc6772
CPE: Add a generic 'parse' method

diff -r 1d9b2b06067e -r bb1dd2a55643 CHANGES
--- a/CHANGES	Mon Dec 29 14:30:39 2014 +0100
+++ b/CHANGES	Mon Dec 29 14:58:29 2014 +0100
@@ -1,7 +1,15 @@
+FarolLuz next
+=============
+
+Main changes since FarolLuz 1.0:
+--------------------------------
+* Add parsing for CPE.
+
+
 FarolLuz 1.0 (2014-11-05)
 =========================
 
-THis is the first public release of FarolLuz. FarolLuz is a set of library /
+This is the first public release of FarolLuz. FarolLuz is a set of library /
 utilities to manipulate Security Advisories. It is part of Farol, the Security
 Advisory Management Platform.
 
diff -r 1d9b2b06067e -r bb1dd2a55643 farolluz/parsers/cpe.py
--- a/farolluz/parsers/cpe.py	Mon Dec 29 14:30:39 2014 +0100
+++ b/farolluz/parsers/cpe.py	Mon Dec 29 14:58:29 2014 +0100
@@ -1,3 +1,27 @@
+# -*- coding: utf-8 -*-
+# Description:
+# Methods for parsing CPEs
+#
+# Authors:
+# Benoît Allard <benoit.allard at greenbone.net>
+#
+# Copyright:
+# Copyright (C) 2014 Greenbone Networks GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+
 """\
 a cpe class to ease the creation of a producttree based on cpe
 
@@ -257,3 +281,13 @@
                 self.target_hw = v
             elif idx == 12:
                 self.other = v
+
+def parse(s):
+    cpe = CPE()
+    if s[:5] == 'cpe:/':
+        cpe.unbind_URI(s)
+    elif s[:8] == 'cpe:2.3:':
+        cpe.unbind_fs(s)
+    else:
+        raise ValueError(s)
+    return cpe
diff -r 1d9b2b06067e -r bb1dd2a55643 tests/testCPE.py
--- a/tests/testCPE.py	Mon Dec 29 14:30:39 2014 +0100
+++ b/tests/testCPE.py	Mon Dec 29 14:58:29 2014 +0100
@@ -1,6 +1,30 @@
+# -*- coding: utf-8 -*-
+# Description:
+# Tests for the CPE parsing methods
+#
+# Authors:
+# Benoît Allard <benoit.allard at greenbone.net>
+#
+# Copyright:
+# Copyright (C) 2014 Greenbone Networks GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+
 import unittest
 
-from farolluz.parsers.cpe import CPE, CPEAttribute
+from farolluz.parsers.cpe import CPE, CPEAttribute, parse
 
 class testbindToURI(unittest.TestCase):
     
@@ -210,3 +234,16 @@
         self.assertEqual(cpe.target_hw.value, "80gb")
         self.assertTrue(cpe.other.any)
         self.assertTrue(cpe.language.any)
+
+class testParse(unittest.TestCase):
+
+    def testURI(self):
+        cpe = parse('cpe:/a:fogproject:fog:0.31')
+        self.assertEqual(cpe.part.value, 'a')
+
+    def testFS(self):
+        cpe = parse('cpe:2.3:a:tenable:web_ui:2.3.3:*:*:*:*:*:*')
+        self.assertEqual(cpe.vendor.value, 'tenable')
+
+    def testGarbage(self):
+        self.assertRaises(ValueError, parse, 'garbage')


More information about the Farol-commits mailing list