From b4a3193c20a40ddbdcabeb304bc34cda3d7dba77 Mon Sep 17 00:00:00 2001 From: kairoaraujo Date: Wed, 17 Feb 2016 21:07:16 -0200 Subject: [PATCH] Hacking: check for deprecated os.popen() Add hacking check for deprecated library function os.popen(). This hacking prevents new os.popen() in the code. Closes-Bug: 1529836 Change-Id: I09ad101861b790d2f9bec45757b8c921e68b696e --- HACKING.rst | 1 + nova/hacking/checks.py | 15 +++++++++++++++ nova/tests/unit/test_hacking.py | 18 ++++++++++++++++++ 3 files changed, 34 insertions(+) diff --git a/HACKING.rst b/HACKING.rst index 397cf5224562..4fab4678274e 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -58,6 +58,7 @@ Nova Specific Commandments - [N345] Python 3: do not use dict.iterkeys. - [N346] Python 3: do not use dict.itervalues. - [N347] Provide enough help text for config options +- [N348] Deprecated library function os.popen() Creating Unit Tests ------------------- diff --git a/nova/hacking/checks.py b/nova/hacking/checks.py index 6a0ab2fbdc5d..e09f4610c5a7 100644 --- a/nova/hacking/checks.py +++ b/nova/hacking/checks.py @@ -683,6 +683,20 @@ def cfg_help_with_enough_text(logical_line, tokens): yield(0, msg) +def no_os_popen(logical_line): + """Disallow 'os.popen(' + + Deprecated library function os.popen() Replace it using subprocess + https://bugs.launchpad.net/tempest/+bug/1529836 + + N348 + """ + + if 'os.popen(' in logical_line: + yield(0, 'N348 Deprecated library function os.popen(). ' + 'Replace it using subprocess module. ') + + def factory(register): register(import_no_db_in_virt) register(no_db_session_in_public_api) @@ -717,3 +731,4 @@ def factory(register): register(check_python3_no_iterkeys) register(check_python3_no_itervalues) register(cfg_help_with_enough_text) + register(no_os_popen) diff --git a/nova/tests/unit/test_hacking.py b/nova/tests/unit/test_hacking.py index 48eff888e7f1..e8dc2cf26c32 100644 --- a/nova/tests/unit/test_hacking.py +++ b/nova/tests/unit/test_hacking.py @@ -732,3 +732,21 @@ class HackingTestCase(test.NoDBTestCase): \"\"\") """ self._assert_has_no_errors(code9, checks.cfg_help_with_enough_text) + + def test_no_os_popen(self): + code = """ + import os + + foobar_cmd = "foobar -get -beer" + answer = os.popen(foobar_cmd).read() + + if answer == nok": + try: + os.popen(os.popen('foobar -beer -please')).read() + + except ValueError: + go_home() + """ + errors = [(4, 0, 'N348'), (8, 8, 'N348')] + self._assert_has_errors(code, checks.no_os_popen, + expected_errors=errors)