From b926c4862fa6014bbdf962d63d14ed77f2cbdeaf Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Wed, 25 Oct 2017 11:51:41 +0100 Subject: [PATCH 1/4] Add pycodestyle syntax test. --- t/syntax/{00-lib.t => 00-pep8.t} | 0 t/syntax/02-pycodestyle.t | 36 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) rename t/syntax/{00-lib.t => 00-pep8.t} (100%) create mode 100755 t/syntax/02-pycodestyle.t diff --git a/t/syntax/00-lib.t b/t/syntax/00-pep8.t similarity index 100% rename from t/syntax/00-lib.t rename to t/syntax/00-pep8.t diff --git a/t/syntax/02-pycodestyle.t b/t/syntax/02-pycodestyle.t new file mode 100755 index 0000000000..516a51f763 --- /dev/null +++ b/t/syntax/02-pycodestyle.t @@ -0,0 +1,36 @@ +#!/bin/bash +#------------------------------------------------------------------------------- +# (C) British Crown Copyright 2012-7 Met Office. +# +# This file is part of Rose, a framework for meteorological suites. +# +# Rose 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 3 of the License, or +# (at your option) any later version. +# +# Rose 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 Rose. If not, see . +#------------------------------------------------------------------------------- +. "$(dirname "$0")/test_header" + +if ! pycodestyle --version 1>'/dev/null' 2>&1; then + skip_all '"pycodestyle" command not available' +fi + +tests 3 + +run_pass "${TEST_KEY_BASE}" \ + pycodestyle --ignore=E402,E731 \ + "${ROSE_HOME}/lib/python/isodatetime" \ + "${ROSE_HOME}/lib/python/rose" \ + "${ROSE_HOME}/lib/python/rosie" +file_cmp "${TEST_KEY_BASE}.out" "${TEST_KEY_BASE}.out" <'/dev/null' +file_cmp "${TEST_KEY_BASE}.err" "${TEST_KEY_BASE}.err" <'/dev/null' + +exit From 633dc5af3ac8db73b805d2c5f84e6c41477e102d Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Wed, 25 Oct 2017 11:52:12 +0100 Subject: [PATCH 2/4] Use pycodestyle for syntax checks in travis. --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 94c2455484..8d46cb98e8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -24,7 +24,7 @@ install: - sudo apt-get install python-pip python-dev graphviz libgraphviz-dev python-jinja2 python-sqlalchemy - pip install cherrypy Jinja2 requests simplejson sqlalchemy pyopenssl - pip install pygraphviz --install-option="--include-path=/usr/include/graphviz" --install-option="--library-path=/usr/lib/graphviz/" - - pip install pep8 + - pip install pycodestyle - sudo sh -c 'echo "deb http://opensource.wandisco.com/ubuntu `lsb_release -cs` svn19" >> /etc/apt/sources.list.d/subversion19.list' - sudo wget -q http://opensource.wandisco.com/wandisco-debian.gpg -O- | sudo apt-key add - - sudo apt-get update From cf3dbc86c40a1b956bae25780176e2751acb24c4 Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Wed, 25 Oct 2017 15:51:27 +0100 Subject: [PATCH 3/4] pep8 --- lib/python/rose/apps/rose_ana_v1.py | 2 +- lib/python/rose/config_cli.py | 2 +- lib/python/rose/gtk/dialog.py | 2 +- lib/python/rose/gtk/splash.py | 1 + lib/python/rose/meta_type.py | 2 +- lib/python/rose/metadata_check.py | 1 + lib/python/rose/metadata_gen.py | 1 + lib/python/rose/reporter.py | 9 +++++---- lib/python/rose/run.py | 8 ++++---- lib/python/rosie/browser/util.py | 2 +- lib/python/rosie/db_create.py | 1 + lib/python/rosie/ws.py | 2 +- 12 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/python/rose/apps/rose_ana_v1.py b/lib/python/rose/apps/rose_ana_v1.py index 0d38f73b3a..784d986217 100644 --- a/lib/python/rose/apps/rose_ana_v1.py +++ b/lib/python/rose/apps/rose_ana_v1.py @@ -114,7 +114,7 @@ def execute_sql_retry(self, conn, sql_args, retries=10, timeout=5.0): try: conn.execute(*sql_args) return - except: + except sqlite3.Error: time.sleep(timeout) # In the event that the retries are exceeded, re-raise the final # exception for the calling application to handle diff --git a/lib/python/rose/config_cli.py b/lib/python/rose/config_cli.py index 368f50610c..6bdf5c1b04 100644 --- a/lib/python/rose/config_cli.py +++ b/lib/python/rose/config_cli.py @@ -136,7 +136,7 @@ def main(): if opts.keys_mode: try: keys = root_node.get(args, opts.no_ignore).value.keys() - except: + except AttributeError: sys.exit(1) keys.sort() for key in keys: diff --git a/lib/python/rose/gtk/dialog.py b/lib/python/rose/gtk/dialog.py index 62b8775f5e..313163b181 100644 --- a/lib/python/rose/gtk/dialog.py +++ b/lib/python/rose/gtk/dialog.py @@ -363,7 +363,7 @@ def run_dialog(dialog_type, text, title=None, modal=True, except glib.GError: try: dialog.label.set_markup(rose.gtk.util.safe_str(text)) - except: + except Exception: dialog.label.set_text(text) else: dialog.label.set_markup(text) diff --git a/lib/python/rose/gtk/splash.py b/lib/python/rose/gtk/splash.py index e338eb72a8..6b842077a8 100755 --- a/lib/python/rose/gtk/splash.py +++ b/lib/python/rose/gtk/splash.py @@ -290,6 +290,7 @@ def _update_splash_screen(self, update_input): *update_input["args"], **update_input["kwargs"]) return False + if __name__ == "__main__": sys.path.append(os.getenv('ROSE_HOME')) splash_screen = SplashScreen(*sys.argv[1:]) diff --git a/lib/python/rose/meta_type.py b/lib/python/rose/meta_type.py index 0710813277..019c8d88b5 100644 --- a/lib/python/rose/meta_type.py +++ b/lib/python/rose/meta_type.py @@ -216,5 +216,5 @@ def meta_type_transform(value, meta_type): c = c() try: return c.transform(value) - except: + except Exception: return False diff --git a/lib/python/rose/metadata_check.py b/lib/python/rose/metadata_check.py index df0f8aaff5..4e855aebe5 100644 --- a/lib/python/rose/metadata_check.py +++ b/lib/python/rose/metadata_check.py @@ -385,5 +385,6 @@ def main(): sys.exit(1) reporter(rose.macro.MacroFinishNothingEvent(), level=reporter.V) + if __name__ == "__main__": main() diff --git a/lib/python/rose/metadata_gen.py b/lib/python/rose/metadata_gen.py index 3ec75a86ae..fcd6d4557a 100644 --- a/lib/python/rose/metadata_gen.py +++ b/lib/python/rose/metadata_gen.py @@ -163,5 +163,6 @@ def main(): dest_file = os.path.join(dest, rose.META_CONFIG_NAME) rose.config.dump(metadata_config, dest_file) + if __name__ == "__main__": main() diff --git a/lib/python/rose/reporter.py b/lib/python/rose/reporter.py index 2e992874e2..e9d343bb3a 100644 --- a/lib/python/rose/reporter.py +++ b/lib/python/rose/reporter.py @@ -214,6 +214,7 @@ class ReporterContext(object): """ TTY_COLOUR_ERR = "\033[1;31m" + TTY_COLOUR_NORM = "\033[0m" def __init__(self, kind=None, @@ -262,10 +263,10 @@ def write(self, message): def _tty_colour_err(self, s): try: if self.handle.isatty(): - return self.TTY_COLOUR_ERR + s + "\033[0m" - except: - pass - return s + return "%s%s%s" % (self.TTY_COLOUR_ERR, s, + self.TTY_COLOUR_NORM) + except AttributeError: + return s class ReporterContextQueue(ReporterContext): diff --git a/lib/python/rose/run.py b/lib/python/rose/run.py index 2f0d9ce855..367a4b1126 100644 --- a/lib/python/rose/run.py +++ b/lib/python/rose/run.py @@ -202,8 +202,8 @@ def run(self, opts, args): finally: # Close handle on specific log file try: - self.event_handler.contexts.get(uuid).handle.close() - except: + self.event_handler.contexts[uuid].handle.close() + except (KeyError, IOError, AttributeError): pass # Remove work files for work_file in work_files: @@ -212,12 +212,12 @@ def run(self, opts, args): os.unlink(work_file) elif os.path.isdir(work_file): shutil.rmtree(work_file) - except: + except OSError: pass # Change back to original working directory try: os.chdir(cwd) - except: + except OSError: pass # Reset os.environ os.environ = {} diff --git a/lib/python/rosie/browser/util.py b/lib/python/rosie/browser/util.py index c2655f79f9..088313b857 100644 --- a/lib/python/rosie/browser/util.py +++ b/lib/python/rosie/browser/util.py @@ -844,7 +844,7 @@ def set_show_controls(self): try: if self.adv_control_menuitem.get_active() != self.adv_controls_on: self.adv_control_menuitem.set_active(self.adv_controls_on) - except: + except Exception: pass for child in self.filter_table.get_children(): if isinstance(child, rosie.browser.util.BracketWidget): diff --git a/lib/python/rosie/db_create.py b/lib/python/rosie/db_create.py index 4afd6f5b2d..df54103476 100644 --- a/lib/python/rosie/db_create.py +++ b/lib/python/rosie/db_create.py @@ -216,5 +216,6 @@ def main(): repos_path = conf.get_value(["rosie-db", "repos." + prefix]) init(db_url, repos_path) + if __name__ == "__main__": main() diff --git a/lib/python/rosie/ws.py b/lib/python/rosie/ws.py index 2a9154a988..30665eef9c 100644 --- a/lib/python/rosie/ws.py +++ b/lib/python/rosie/ws.py @@ -109,7 +109,7 @@ def index(self, *_): """Provide the index page.""" try: return self._render() - except: + except (KeyError, AttributeError, jinja2.exceptions.TemplateError): import traceback traceback.print_exc() From daaab22f10aeba81c2d0e901b12554ba1a699531 Mon Sep 17 00:00:00 2001 From: Oliver Sanders Date: Thu, 26 Oct 2017 13:21:10 +0000 Subject: [PATCH 4/4] . --- lib/python/rose/reporter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/python/rose/reporter.py b/lib/python/rose/reporter.py index e9d343bb3a..b26c74f89e 100644 --- a/lib/python/rose/reporter.py +++ b/lib/python/rose/reporter.py @@ -266,7 +266,8 @@ def _tty_colour_err(self, s): return "%s%s%s" % (self.TTY_COLOUR_ERR, s, self.TTY_COLOUR_NORM) except AttributeError: - return s + pass + return s class ReporterContextQueue(ReporterContext):