python-utility-scripts 0.0.9__tar.gz → 0.0.11__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-utility-scripts
3
- Version: 0.0.9
3
+ Version: 0.0.11
4
4
  Summary: Python utility scripts
5
5
  Home-page: https://github.com/RedHatQE/python-utility-scripts
6
6
  License: Apache-2.0
@@ -15,7 +15,9 @@ Classifier: Programming Language :: Python :: 3.9
15
15
  Classifier: Programming Language :: Python :: 3.10
16
16
  Classifier: Programming Language :: Python :: 3.11
17
17
  Classifier: Programming Language :: Python :: 3.12
18
+ Requires-Dist: pyhelper-utils (>=0.0.3,<0.0.4)
18
19
  Requires-Dist: pylero (>=0.0.8,<0.0.9)
20
+ Requires-Dist: pytest-mock (>=3.14.0,<4.0.0)
19
21
  Requires-Dist: python-simple-logger (>=1.0.8,<2.0.0)
20
22
  Requires-Dist: pyyaml
21
23
  Project-URL: Documentation, https://github.com/RedHatQE/python-utility-scripts/blob/main/README.md
@@ -24,7 +26,8 @@ Description-Content-Type: text/markdown
24
26
 
25
27
  # python-utility-scripts
26
28
  Repository for various python utility scripts
27
- * [pyappsutils-unusedcode](https://github.com/RedHatQE/python-utility-scripts/blob/main/README.md)
29
+ * [pyutils-unusedcode](https://github.com/RedHatQE/python-utility-scripts/blob/main/apps/unused_code/README.md)
30
+ * [pyutils-polarion-verify-tc-requirements](https://github.com/RedHatQE/python-utility-scripts/blob/main/apps/polarion/README.md)
28
31
 
29
32
  ## Installation
30
33
 
@@ -1,6 +1,7 @@
1
1
  # python-utility-scripts
2
2
  Repository for various python utility scripts
3
- * [pyappsutils-unusedcode](https://github.com/RedHatQE/python-utility-scripts/blob/main/README.md)
3
+ * [pyutils-unusedcode](https://github.com/RedHatQE/python-utility-scripts/blob/main/apps/unused_code/README.md)
4
+ * [pyutils-polarion-verify-tc-requirements](https://github.com/RedHatQE/python-utility-scripts/blob/main/apps/polarion/README.md)
4
5
 
5
6
  ## Installation
6
7
 
@@ -0,0 +1,32 @@
1
+ # scripts to interact with Polarion instance
2
+
3
+ ## pyutils-polarion-verify-tc-requirements
4
+ Utility to check if test cases in a pytest repository has associated Polarion requirements.
5
+
6
+ ## Requirements
7
+ This script uses [pylero](https://github.com/RedHatQE/pylero) and expects .pylero config file to be present in current directory or user's home directory.
8
+
9
+ ## Usage
10
+ ```bash
11
+ pyutils-polarion-verify-tc-requirements --help
12
+ pyutils-polarion-verify-tc-requirements --project_id <project_id>
13
+ ```
14
+
15
+ ## Config file
16
+ To specify polation project id for polarion scripts, it can be added to the config file:
17
+ `~/.config/python-utility-scripts/config.yaml`
18
+
19
+
20
+ ### Example:
21
+
22
+ ```yaml
23
+ pyutils-polarion-verify-tc-requirements :
24
+ project_id: "<project_id>"
25
+ ```
26
+ This would run the polarion requirement check against Polarion project <project_id>
27
+
28
+ To run from CLI with `--project-id`
29
+
30
+ ```bash
31
+ pyutils-polarion-verify-tc-requirements --project-id 'my_project_id'
32
+ ```
@@ -0,0 +1,49 @@
1
+ from simple_logger.logger import get_logger
2
+ import shlex
3
+ import subprocess
4
+ from pylero.exceptions import PyleroLibException
5
+
6
+
7
+ LOGGER = get_logger(name=__name__)
8
+
9
+
10
+ def git_diff():
11
+ data = subprocess.check_output(shlex.split("git diff HEAD^-1"))
12
+ data = data.decode("utf-8")
13
+ return data
14
+
15
+
16
+ def git_diff_lines():
17
+ diff = {}
18
+ for line in git_diff().splitlines():
19
+ LOGGER.debug(line)
20
+ if line.startswith("+"):
21
+ diff.setdefault("added", []).append(line)
22
+
23
+ return diff
24
+
25
+
26
+ def validate_polarion_requirements(
27
+ polarion_test_ids,
28
+ polarion_project_id,
29
+ ):
30
+ from pylero.work_item import TestCase, Requirement
31
+
32
+ tests_with_missing_requirements = []
33
+
34
+ for _id in polarion_test_ids:
35
+ has_req = False
36
+ LOGGER.debug(f"Checking if {_id} verifies any requirement")
37
+ tc = TestCase(project_id=polarion_project_id, work_item_id=_id)
38
+ for link in tc.linked_work_items:
39
+ try:
40
+ Requirement(project_id=polarion_project_id, work_item_id=link.work_item_id)
41
+ has_req = True
42
+ break
43
+ except PyleroLibException:
44
+ continue
45
+
46
+ if not has_req:
47
+ LOGGER.error(f"{_id}: Is missing requirement")
48
+ tests_with_missing_requirements.append(_id)
49
+ return tests_with_missing_requirements
@@ -0,0 +1,52 @@
1
+ import re
2
+ import logging
3
+ from simple_logger.logger import get_logger
4
+ import os
5
+ import click
6
+ from apps.polarion.polarion_utils import (
7
+ git_diff_lines,
8
+ validate_polarion_requirements,
9
+ )
10
+ from apps.utils import get_util_config
11
+
12
+ LOGGER = get_logger(name="polarion-verify-tc-requirements")
13
+
14
+
15
+ @click.command()
16
+ @click.option(
17
+ "--config-file-path",
18
+ help="Provide absolute path to the config file. Any CLI option(s) would override YAML file",
19
+ type=click.Path(),
20
+ default=os.path.expanduser("~/.config/python-utility-scripts/config.yaml"),
21
+ )
22
+ @click.option("--project-id", "-p", help="Provide the polarion project id")
23
+ @click.option("--verbosity", default=False, is_flag=True)
24
+ def has_verify(config_file_path, project_id, verbosity):
25
+ if verbosity:
26
+ LOGGER.setLevel(logging.DEBUG)
27
+
28
+ polarion_project_id = project_id or get_util_config(
29
+ util_name="pyutils-polarion-verify-tc-requirements",
30
+ config_file_path=config_file_path,
31
+ ).get("project_id")
32
+
33
+ if not polarion_project_id:
34
+ LOGGER.error("Polarion project id must be passed via config file or command line")
35
+ raise click.Abort()
36
+
37
+ if added_ids := re.findall(
38
+ rf"pytest.mark.polarion.*({polarion_project_id}-[0-9]+)",
39
+ "\n".join(git_diff_lines().get("added", [])),
40
+ re.MULTILINE | re.IGNORECASE,
41
+ ):
42
+ LOGGER.debug(f"Checking following ids: {added_ids}")
43
+ if tests_with_missing_requirements := validate_polarion_requirements(
44
+ polarion_test_ids=added_ids,
45
+ polarion_project_id=polarion_project_id,
46
+ ):
47
+ click.echo(f"TestCases with missing requirement: {tests_with_missing_requirements}")
48
+ raise click.Abort()
49
+
50
+
51
+ if __name__ == "__main__":
52
+ has_verify()
@@ -1,11 +1,11 @@
1
- # pyappsutils-unusedcode
1
+ # pyutils-unusedcode
2
2
  Helper to identify unused code in a pytest repository. It should be run from inside the test repository using this tool.
3
3
 
4
4
  ## Usage
5
5
 
6
6
  ```bash
7
- pyappsutils-unusedcode
8
- pyappsutils-unusedcode --help
7
+ pyutils-unusedcode
8
+ pyutils-unusedcode --help
9
9
  ```
10
10
 
11
11
  ## Config file
@@ -15,7 +15,7 @@ To skip unused code check on specific files or functions of a repository, a conf
15
15
  ### Example:
16
16
 
17
17
  ```yaml
18
- pyappsutils-unusedcode:
18
+ pyutils-unusedcode:
19
19
  exclude_files:
20
20
  - "my_exclude_file.py"
21
21
  exclude_function_prefix:
@@ -26,11 +26,11 @@ This would exclude any functions with prefix my_exclude_function_prefix and file
26
26
  To run from CLI with `--exclude-function-prefixes`
27
27
 
28
28
  ```bash
29
- pyappsutils-unusedcode --exclude-function-prefixes 'my_exclude_function1,my_exclude_function2'
29
+ pyutils-unusedcode --exclude-function-prefixes 'my_exclude_function1,my_exclude_function2'
30
30
  ```
31
31
 
32
32
  To run from CLI with `--exclude-files`
33
33
 
34
34
  ```bash
35
- pyappsutils-unusedcode --exclude-files 'my_exclude_file1.py,my_exclude_file2.py'
35
+ pyutils-unusedcode --exclude-files 'my_exclude_file1.py,my_exclude_file2.py'
36
36
  ```
@@ -1,11 +1,11 @@
1
1
  import ast
2
+ import logging
2
3
  import os
3
4
  import subprocess
4
5
  import click
5
- import yaml
6
6
  from simple_logger.logger import get_logger
7
7
 
8
- from apps.utils import all_python_files, ListParamType
8
+ from apps.utils import all_python_files, ListParamType, get_util_config
9
9
 
10
10
  LOGGER = get_logger(name=__name__)
11
11
 
@@ -42,19 +42,12 @@ def _iter_functions(tree):
42
42
  yield elm
43
43
 
44
44
 
45
- def read_config_file(config_file_path):
46
- if os.path.exists(config_file_path):
47
- with open(config_file_path) as _file:
48
- return yaml.safe_load(_file)
49
- return {}
50
-
51
-
52
45
  def is_ignore_function_list(ignore_prefix_list, function):
53
46
  ignore_function_lists = [
54
47
  function.name for ignore_prefix in ignore_prefix_list if function.name.startswith(ignore_prefix)
55
48
  ]
56
49
  if ignore_function_lists:
57
- LOGGER.info(f"Following functions are getting skipped: {ignore_function_lists}")
50
+ LOGGER.debug(f"Following functions are getting skipped: {ignore_function_lists}")
58
51
  return True
59
52
 
60
53
 
@@ -75,10 +68,12 @@ def is_ignore_function_list(ignore_prefix_list, function):
75
68
  help="Provide a comma-separated string or list of function prefixes to exclude",
76
69
  type=ListParamType(),
77
70
  )
78
- def get_unused_functions(config_file_path, exclude_files, exclude_function_prefixes):
71
+ @click.option("--verbosity", default=False, is_flag=True)
72
+ def get_unused_functions(config_file_path, exclude_files, exclude_function_prefixes, verbosity):
73
+ if verbosity:
74
+ LOGGER.setLevel(logging.DEBUG)
79
75
  _unused_functions = []
80
- config_yaml = read_config_file(config_file_path=config_file_path)
81
- unused_code_config = config_yaml.get("pyappsutils-unusedcode", {})
76
+ unused_code_config = get_util_config(util_name="pyutils-unusedcode", config_file_path=config_file_path)
82
77
  func_ignore_prefix = exclude_function_prefixes or unused_code_config.get("exclude_function_prefix", [])
83
78
  file_ignore_list = exclude_files or unused_code_config.get("exclude_files", [])
84
79
  for py_file in all_python_files():
@@ -1,4 +1,6 @@
1
1
  import os
2
+
3
+ import yaml
2
4
  from simple_logger.logger import get_logger
3
5
  import json
4
6
  import click
@@ -6,6 +8,13 @@ import click
6
8
  LOGGER = get_logger(name=__name__)
7
9
 
8
10
 
11
+ def get_util_config(util_name, config_file_path):
12
+ if os.path.exists(config_file_path):
13
+ with open(config_file_path) as _file:
14
+ return yaml.safe_load(_file).get(util_name, {})
15
+ return {}
16
+
17
+
9
18
  # Reference: https://gist.github.com/welel/9cf860dd3f4d3e09f9b4305878b3a04e
10
19
  class ListParamType(click.ParamType):
11
20
  """Represents the list type of a CLI parameter.
@@ -1,3 +1,14 @@
1
+ [tool.coverage.run]
2
+ omit = ["tests/*"]
3
+
4
+ [tool.coverage.report]
5
+ fail_under = 60
6
+ skip_empty = true
7
+
8
+ [tool.coverage.html]
9
+ directory = ".tests_coverage"
10
+ show_contexts = true
11
+
1
12
  [tool.ruff]
2
13
  preview = true
3
14
  line-length = 120
@@ -9,7 +20,7 @@ exclude = [".git", ".venv", ".mypy_cache", ".tox", "__pycache__"]
9
20
 
10
21
  [tool.poetry]
11
22
  name = "python-utility-scripts"
12
- version = "0.0.9"
23
+ version = "0.0.11"
13
24
  description = "Python utility scripts"
14
25
  authors = ["dbasunag <dbasunag@redhat.com>"]
15
26
  license = "Apache License 2.0"
@@ -25,7 +36,8 @@ classifiers = [
25
36
  packages = [{ include = "apps" }]
26
37
 
27
38
  [tool.poetry.scripts]
28
- pyappsutils-unusedcode = "apps.unused_code.unused_code:get_unused_functions"
39
+ pyutils-unusedcode = "apps.unused_code.unused_code:get_unused_functions"
40
+ pyutils-polarion-verify-tc-requirements = "apps.polarion.polarion_verify_tc_requirements:has_verify"
29
41
 
30
42
 
31
43
  [tool.poetry.dependencies]
@@ -33,11 +45,14 @@ python = "^3.8"
33
45
  python-simple-logger = "^1.0.8"
34
46
  pylero = "^0.0.8"
35
47
  pyyaml = "*"
48
+ pyhelper-utils = "^0.0.3"
49
+ pytest-mock = "^3.14.0"
36
50
 
37
51
  [tool.poetry.group.dev.dependencies]
38
52
  ipdb = "^0.13.13"
39
53
  ipython = "*"
40
54
 
55
+
41
56
  [tool.poetry-dynamic-versioning]
42
57
  enable = false
43
58
  pattern = "((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)"
@@ -45,8 +60,13 @@ pattern = "((?P<epoch>\\d+)!)?(?P<base>\\d+(\\.\\d+)*)"
45
60
 
46
61
 
47
62
 
63
+
64
+
65
+
66
+
48
67
  [tool.poetry.group.test.dependencies]
49
68
  pytest = "^8.0.0"
69
+ pytest-cov = "^4.1.0"
50
70
 
51
71
  [tool.poetry-dynamic-versioning.substitution]
52
72
  files = ["VERSION"]