diff --git a/.gitignore b/.gitignore index 14ad4e7..ff047d0 100644 --- a/.gitignore +++ b/.gitignore @@ -38,6 +38,8 @@ htmlcov/ .coverage .coverage.* .cache + +.pytest_cache nosetests.xml coverage.xml *,cover diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..341517a --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,19 @@ +repos: +- repo: https://github.com/ambv/black + rev: 18.4a4 + hooks: + - id: black + args: [--safe, --quiet] + language_version: python3.6 +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v1.2.3 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: debug-statements + - id: flake8 +- repo: https://github.com/asottile/pyupgrade + rev: v1.2.0 + hooks: + - id: pyupgrade \ No newline at end of file diff --git a/cfme_jenkins_dumper.py b/cfme_jenkins_dumper.py deleted file mode 100755 index 24e05c7..0000000 --- a/cfme_jenkins_dumper.py +++ /dev/null @@ -1,53 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -Polarion dumper for CFME Jenkins -""" - -from __future__ import unicode_literals, absolute_import, print_function - -import sys - -from dump2polarion.dumper_cli import main - - -def get_testrun_id(version_file): - """Gets testrun id out of the appliance_version file.""" - with open(version_file) as input_file: - version = input_file.read().strip() - version = version.split('-')[0].split('_')[0] - build_base = version.replace('.', '_') - try: - zval = int(build_base.split('_')[3]) - except (IndexError, ValueError): - # not in expected format - return - if zval < 10: - pad_build = build_base[-1].zfill(2) - return build_base[:-1] + pad_build - return build_base - - -def tweak_args(): - """Tweaks args for polarion dumper.""" - new_argv = sys.argv[1:] - - for index, arg in enumerate(new_argv): - if arg in ('-t', '--testrun-id'): - break - else: - index = None - - testrun_id = get_testrun_id(new_argv[index + 1]) - if not testrun_id: - print("Cannot find testrun id.", file=sys.stderr) - sys.exit(1) - - # replace `-t appliance_version` with `-t testrun_id` - new_argv[index + 1] = testrun_id - - return new_argv - - -if __name__ == '__main__': - sys.exit(main(tweak_args())) diff --git a/csv2sqlite.py b/csv2sqlite.py deleted file mode 100755 index 7eb1eac..0000000 --- a/csv2sqlite.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -csv2sqlite CLI -""" - -import sys - -from dump2polarion.csv2sqlite_cli import main - - -if __name__ == '__main__': - sys.exit(main()) diff --git a/dump2polarion/csv2sqlite_cli.py b/dump2polarion/csv2sqlite_cli.py index 383a17a..62dd7ec 100644 --- a/dump2polarion/csv2sqlite_cli.py +++ b/dump2polarion/csv2sqlite_cli.py @@ -15,6 +15,16 @@ from dump2polarion import csvtools, utils from dump2polarion.exceptions import Dump2PolarionException +REQUIRED_KEYS = ( + "verdict", + "last_status", + "exported", + "time", + "comment", + "stdout", + "stderr", + "user1", +) # pylint: disable=invalid-name logger = logging.getLogger(__name__) @@ -35,33 +45,33 @@ def get_args(args=None): def dump2sqlite(records, output_file): """Dumps tests results to database.""" results_keys = list(records.results[0].keys()) - keys_len = len(results_keys) - for key in ( - 'verdict', 'last_status', 'exported', 'time', 'comment', 'stdout', 'stderr', 'user1'): + pad_data = [] + + for key in REQUIRED_KEYS: if key not in results_keys: results_keys.append(key) + pad_data.append("") - conn = sqlite3.connect(os.path.expanduser(output_file), detect_types=sqlite3.PARSE_DECLTYPES) + conn = sqlite3.connect( + os.path.expanduser(output_file), + detect_types=sqlite3.PARSE_DECLTYPES) # in each row there needs to be data for every column - pad_data = ['' for __ in range(len(results_keys) - keys_len)] # last column is current time - now = datetime.datetime.utcnow() - - def _extend_row(row): - if pad_data: - row.extend(pad_data) - row.append(now) - return row + pad_data.append(datetime.datetime.utcnow()) - to_db = [_extend_row(list(row.values())) for row in records.results] + to_db = [list(row.values()) + pad_data for row in records.results] cur = conn.cursor() cur.execute( "CREATE TABLE testcases ({},sqltime TIMESTAMP)".format( - ','.join(['{} TEXT'.format(key) for key in results_keys]))) - cur.executemany("INSERT INTO testcases VALUES ({},?)".format( - ','.join(['?' for __ in results_keys])), to_db) + ','.join('{} TEXT'.format(key) for key in results_keys))) + cur.executemany( + "INSERT INTO testcases VALUES ({},?)".format( + ",".join(["?"] * len(results_keys)) + ), + to_db, + ) if records.testrun: cur.execute("CREATE TABLE testrun (testrun TEXT)") @@ -101,7 +111,7 @@ def main(args=None): dump2sqlite(records, args.output_file) # pylint: disable=broad-except except Exception as err: - logger.fatal(err) + logger.exception(err) return 1 return 0 diff --git a/dump2polarion/csv_unicode.py b/dump2polarion/csv_unicode.py index 353ed8e..8661af5 100644 --- a/dump2polarion/csv_unicode.py +++ b/dump2polarion/csv_unicode.py @@ -34,7 +34,7 @@ def __init__(self, f, dialect=csv.excel, encoding='utf-8', **kwds): def next(self): row = self.reader.next() - return [unicode(s, 'utf-8') for s in row] + return [s.decode('utf-8') for s in row] def __iter__(self): return self diff --git a/polarion_dumper.py b/polarion_dumper.py deleted file mode 100755 index bb8ddfb..0000000 --- a/polarion_dumper.py +++ /dev/null @@ -1,13 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -""" -Polarion dumper CLI -""" - -import sys - -from dump2polarion.dumper_cli import main - - -if __name__ == '__main__': - sys.exit(main()) diff --git a/setup.py b/setup.py index 806b1c7..8e79106 100644 --- a/setup.py +++ b/setup.py @@ -13,7 +13,10 @@ author_email='mkourim@redhat.com', license='GPL', packages=find_packages(exclude=('tests',)), - scripts=['csv2sqlite.py', 'polarion_dumper.py'], + entry_points={'console_scripts': [ + 'csv2sqlite.py = dump2polarion.csv2sqlite_cli:main', + 'polarion_dumper.py = dump2polarion.dumper_cli:main' + ]}, setup_requires=['setuptools_scm'], install_requires=['requests', 'pyyaml'], keywords=['polarion', 'testing'],