From d9cee5b14197c4934f153b4770eaa2834205faa4 Mon Sep 17 00:00:00 2001 From: Thanh Ha Date: Wed, 10 Apr 2019 16:04:37 +0800 Subject: [PATCH] Add pre-commit robot-framework tidy Use pre-commit to handle robot tidy tool rather than maintaining our own. This tool is handy because it will automatically correct the local files on "git commit" after pre-commit is installed for the first time. Install pre-commit using tox with `tox -e pre-commit`. Change-Id: I64d802b2d0754160755bce3e50272a5f28af4d72 Signed-off-by: Thanh Ha --- .pre-commit-config.yaml | 6 ++ tools/robot_check/README.markdown | 27 ------ tools/robot_check/requirements.txt | 1 - tools/robot_check/tidy.sh | 22 ----- tools/robot_check/tidytool.py | 133 ----------------------------- tox.ini | 11 +-- 6 files changed, 12 insertions(+), 188 deletions(-) create mode 100644 .pre-commit-config.yaml delete mode 100644 tools/robot_check/README.markdown delete mode 100644 tools/robot_check/requirements.txt delete mode 100755 tools/robot_check/tidy.sh delete mode 100644 tools/robot_check/tidytool.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000000..cecdca20db --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,6 @@ +--- +- repo: git://github.com/guykisel/pre-commit-robotframework-tidy + rev: 'master' + hooks: + - id: robotframework-tidy-wrapper + diff --git a/tools/robot_check/README.markdown b/tools/robot_check/README.markdown deleted file mode 100644 index 2701885a87..0000000000 --- a/tools/robot_check/README.markdown +++ /dev/null @@ -1,27 +0,0 @@ -# Robot Tidy Tool - -Tool for checking and correcting Robot Framework code formatting. - -## Installation - -The `robot.tidy` Python module ships with the Robot Framework Python package. - -We recommend using a [Virtual Environment][1] to manage Python modules. - - $ mkvirtualenv tidy - $ pip install -r requirements.txt - -## Usage - -The `tidy.sh` script is a wrapper around `robot.tidy` that checks all files in -the Integration/Test repository. - -Use the `check` argument to report problems without correcting them. - - $ ./tidy.sh check - -Use the `tidy` argument to automatically clean up all problems. - - $ ./tidy.sh tidy - -[1]: https://virtualenvwrapper.readthedocs.io/en/latest/ "Virtualenvwrapper docs" diff --git a/tools/robot_check/requirements.txt b/tools/robot_check/requirements.txt deleted file mode 100644 index b779ff3990..0000000000 --- a/tools/robot_check/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -robotframework diff --git a/tools/robot_check/tidy.sh b/tools/robot_check/tidy.sh deleted file mode 100755 index c9033db7da..0000000000 --- a/tools/robot_check/tidy.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env sh -# Convenience script to run check on all .robot files in the project. -# Run with the "quiet" argument to get rid of the non-error output. - -if test "${1-loud}" = "quiet"; then - COMMAND="quiet" -elif test "${1-loud}" = "tidy"; then - COMMAND="tidy" -else - COMMAND="check" -fi -BASE=`dirname ${0}` -cd $BASE -BASE=`pwd` -cd ../.. -DIRLIST="" -for Dir in *; do - if test -d $Dir; then - DIRLIST="$DIRLIST $Dir" - fi -done -python $BASE/tidytool.py $COMMAND $DIRLIST diff --git a/tools/robot_check/tidytool.py b/tools/robot_check/tidytool.py deleted file mode 100644 index 910e3a1ed0..0000000000 --- a/tools/robot_check/tidytool.py +++ /dev/null @@ -1,133 +0,0 @@ -import difflib -import os -import sys -import stat -import robot.tidy - - -def Error(FileSpec, Message): - global ErrorsReported - print "File", FileSpec + ":", Message - ErrorsReported = True - - -def traverse_and_process(DirList, Processor): - """Traverse the directory and process all .robot files found""" - Stack = [] - for Dir in DirList: - if Dir[-1] == "/": - Dir = Dir[:-1] - Stack.append(Dir) - while len(Stack) > 0: - Dir = Stack.pop() + "/" - try: - List = os.listdir(Dir) - except (IOError, OSError), e: - Error(Dir, "Directory not accessible: " + str(e)) - continue - for Item in List: - Spec = Dir + Item - Stat = os.stat(Spec) - if stat.S_ISDIR(Stat.st_mode): - Stack.append(Spec) - elif Item.endswith(".robot"): - Processor(Spec) - - -def check_quietly(FileSpec): - try: - Data = open(FileSpec).read() - except (IOError, OSError), e: - Error(FileSpec, "Not accessible: " + str(e)) - return - try: - Data = Data.decode("utf8") - except: - Error(FileSpec, "Has invalid UTF8 data") - return - TidyTool = robot.tidy.Tidy() - CleanedData = TidyTool.file(FileSpec) - if Data != CleanedData: - Error(FileSpec, "Found to be untidy") - - -def check(FileSpec): - Index = FileSpec.rfind("/") - FileName = FileSpec - if Index >= 0: - FileName = FileSpec[Index + 1:] - sys.stdout.write(" " + FileName + "\r") - sys.stdout.flush() - check_quietly(FileSpec) - sys.stdout.write(" " * (2 + len(FileName)) + "\r") - - -def tidy(FileSpec): - print "Processing file:", FileSpec - TidyTool = robot.tidy.Tidy() - try: - CleanedData = TidyTool.file(FileSpec) - CleanedData = CleanedData.encode("utf8") - open(FileSpec, "w").write(CleanedData) - except (IOError, OSError), e: - Error(FileSpec, "Not accessible: " + str(e)) - - -def diff(FileSpec): - TidyTool = robot.tidy.Tidy() - try: - ActualLines = open(FileSpec, 'U').readlines() - CleanedData = TidyTool.file(FileSpec) - # Unified diff wants list of lines, and split on newline creates empty line at the end. - CleanedLines = [line + '\n' for line in CleanedData.encode("utf8").split('\n')[:-1]] - DiffText = "".join(tuple(difflib.unified_diff(ActualLines, CleanedLines, n=10))) - # TODO: If the last line does not contain \n, the output is ugly. Can we fix that without causing confsion? - if DiffText: - Error(FileSpec, "Tidy requires the following diff:\n" + DiffText) - except (IOError, OSError), e: - Error(FileSpec, "Not accessible: " + str(e)) - - -# TODO: Refactor the command line argument parsing to use argparse. Since I -# wanted to just quickly make this tool to get rid of manual robot.tidy -# runs I did not have time to create polished argparse based command -# line argument parsing code. Remember also to update the convenience -# scripts. - - -def usage(): - print "Usage:\ttidytool.py k [ ...]" - print - print "where is one of these:" - print - print "check\tCheck that the Robot test data is tidy." - print "quiet\tCheck quietly that the Robot test data is tidy." - print "tidy\tTidy the Robot test data." - print - print "The program traverses the specified directories, searching for" - print "Robot test data (.robot files) and performing the specified" - print "command on them." - - -if __name__ == "__main__": - if len(sys.argv) < 2: - usage() - raise SystemExit - Command = sys.argv[1] - DirList = sys.argv[2:] - if Command == "check": - Processor = check - elif Command == "quiet": - Processor = check_quietly - elif Command == "tidy": - Processor = tidy - elif Command == "diff": - Processor = diff - else: - print "Unrecognized command:", Command - sys.exit(1) - ErrorsReported = False - traverse_and_process(DirList, Processor) - if ErrorsReported: - print "tidytool run FAILED !!!" - sys.exit(1) diff --git a/tox.ini b/tox.ini index 48e19fe1c9..1e64b83267 100644 --- a/tox.ini +++ b/tox.ini @@ -1,6 +1,6 @@ [tox] minversion = 1.6.0 -envlist = coala,pep8,tidy +envlist = coala,pep8,pre-commit # TODO: Include docs when it starts performing strict checks. skipsdist = true @@ -20,10 +20,11 @@ deps = -rdocs/requirements.txt commands = sphinx-build -W -n -b html -d {envtmpdir}/doctrees ./docs/ {toxinidir}/docs/_build/html -[testenv:tidy] -basepython = python2 -deps = robotframework -commands = python ./tools/robot_check/tidytool.py diff csit tools +[testenv:pre-commit] +deps = pre-commit +commands = + pre-commit install + pre-commit run --all-files [testenv:pep8] basepython = python2 -- 2.36.6