python-utility-scripts 2.0.5__tar.gz → 2.0.6__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.
Files changed (18) hide show
  1. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/PKG-INFO +1 -1
  2. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/jira_utils/jira_information.py +37 -5
  3. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/unused_code/unused_code.py +125 -119
  4. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/utils.py +3 -2
  5. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/pyproject.toml +1 -1
  6. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/.gitignore +0 -0
  7. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/LICENSE +0 -0
  8. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/README.md +0 -0
  9. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/__init__.py +0 -0
  10. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/jira_utils/README.md +0 -0
  11. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/jira_utils/__init__.py +0 -0
  12. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/polarion/README.md +0 -0
  13. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/polarion/__init__.py +0 -0
  14. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/polarion/polarion_set_automated.py +0 -0
  15. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/polarion/polarion_utils.py +0 -0
  16. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/polarion/polarion_verify_tc_requirements.py +0 -0
  17. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/unused_code/README.md +0 -0
  18. {python_utility_scripts-2.0.5 → python_utility_scripts-2.0.6}/apps/unused_code/__init__.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: python-utility-scripts
3
- Version: 2.0.5
3
+ Version: 2.0.6
4
4
  Summary: Python utility scripts
5
5
  Project-URL: Homepage, https://github.com/RedHatQE/python-utility-scripts
6
6
  Project-URL: Repository, https://github.com/RedHatQE/python-utility-scripts
@@ -122,7 +122,7 @@ def get_jira_information(
122
122
  jira_fix_versions = ",".join([jira_fix_version.name for jira_fix_version in jira_fix_versions])
123
123
  current_target_versions = re.findall(re_compile, jira_fix_versions)
124
124
 
125
- if any([version in jira_target_versions for version in current_target_versions]):
125
+ if any(version in jira_target_versions for version in current_target_versions):
126
126
  return file_name, jira_error_string
127
127
 
128
128
  else:
@@ -146,16 +146,24 @@ def process_jira_command_line_config_file(
146
146
  version_string_not_targeted_jiras: str,
147
147
  target_versions: list[str],
148
148
  skip_projects: list[str],
149
+ user: str,
150
+ cloud: bool | None,
149
151
  ) -> dict[str, Any]:
150
152
  # Process all the arguments passed from command line or config file or environment variable
151
153
  config_dict = get_util_config(util_name="pyutils-jira", config_file_path=config_file_path)
152
154
  url = url or config_dict.get("url", "")
153
155
  token = token or config_dict.get("token", "")
156
+ user = user or config_dict.get("user", "")
157
+ cloud = cloud if cloud is not None else config_dict.get("cloud", False)
154
158
 
155
159
  if not (url and token):
156
160
  LOGGER.error("Jira url and token are required.")
157
161
  sys.exit(1)
158
162
 
163
+ if cloud and not user:
164
+ LOGGER.error("Jira user is required for cloud Jira.")
165
+ sys.exit(1)
166
+
159
167
  return {
160
168
  "url": url,
161
169
  "token": token,
@@ -166,6 +174,8 @@ def process_jira_command_line_config_file(
166
174
  ),
167
175
  "target_versions": target_versions or config_dict.get("target_versions", []),
168
176
  "skip_project_ids": skip_projects or config_dict.get("skip_project_ids", []),
177
+ "user": user,
178
+ "cloud": cloud,
169
179
  }
170
180
 
171
181
 
@@ -197,6 +207,12 @@ def process_jira_command_line_config_file(
197
207
  type=click.STRING,
198
208
  default=os.getenv("JIRA_TOKEN"),
199
209
  )
210
+ @click.option(
211
+ "--user",
212
+ help="Provide the Jira user email (required for cloud Jira).",
213
+ type=click.STRING,
214
+ default=os.getenv("JIRA_USER"),
215
+ )
200
216
  @click.option(
201
217
  "--issue-pattern",
202
218
  help="Provide the regex for Jira ids",
@@ -219,16 +235,24 @@ def process_jira_command_line_config_file(
219
235
  default="vfuture",
220
236
  )
221
237
  @click.option("--verbose", default=False, is_flag=True)
238
+ @click.option(
239
+ "--cloud",
240
+ help="Use cloud Jira authentication (basic_auth with user email and API token).",
241
+ is_flag=True,
242
+ default=None,
243
+ )
222
244
  def get_jira_mismatch(
223
245
  config_file_path: str,
224
246
  target_versions: list[str],
225
247
  url: str,
226
248
  token: str,
249
+ user: str,
227
250
  skip_projects: list[str],
228
251
  resolved_statuses: list[str],
229
252
  issue_pattern: str,
230
253
  version_string_not_targeted_jiras: str,
231
254
  verbose: bool,
255
+ cloud: bool | None,
232
256
  ) -> None:
233
257
  LOGGER.setLevel(logging.DEBUG if verbose else logging.INFO)
234
258
  if not (config_file_path or (token and url)):
@@ -245,12 +269,20 @@ def get_jira_mismatch(
245
269
  skip_projects=skip_projects,
246
270
  version_string_not_targeted_jiras=version_string_not_targeted_jiras,
247
271
  target_versions=target_versions,
272
+ user=user,
273
+ cloud=cloud,
248
274
  )
249
275
 
250
- jira_obj = JIRA(
251
- token_auth=jira_config_dict["token"],
252
- options={"server": jira_config_dict["url"]},
253
- )
276
+ if jira_config_dict["cloud"]:
277
+ jira_obj = JIRA(
278
+ server=jira_config_dict["url"],
279
+ basic_auth=(jira_config_dict["user"], jira_config_dict["token"]),
280
+ )
281
+ else:
282
+ jira_obj = JIRA(
283
+ token_auth=jira_config_dict["token"],
284
+ options={"server": jira_config_dict["url"]},
285
+ )
254
286
  jira_error: dict[str, str] = {}
255
287
 
256
288
  if jira_id_dict := get_jiras_from_python_files(
@@ -6,10 +6,11 @@ import os
6
6
  import re
7
7
  import subprocess
8
8
  import sys
9
+ from collections.abc import Callable, Iterable
9
10
  from concurrent.futures import Future, ThreadPoolExecutor, as_completed
10
11
  from functools import lru_cache
11
12
  from pathlib import Path
12
- from typing import Any, Callable, Iterable
13
+ from typing import Any
13
14
 
14
15
  import click
15
16
  from ast_comments import parse
@@ -46,9 +47,9 @@ def _detect_supported_grep_flag() -> str:
46
47
  result = subprocess.run(probe_cmd, check=False, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
47
48
  if result.returncode in (0, 1):
48
49
  return flag
49
- except Exception:
50
+ except (OSError, subprocess.SubprocessError):
50
51
  # Try next candidate
51
- pass
52
+ LOGGER.debug(f"git grep flag {flag!r} probe failed, trying next candidate")
52
53
 
53
54
  raise RuntimeError(
54
55
  "git grep does not support '-P' (PCRE) or '-G' (basic regex) on this platform. "
@@ -62,11 +63,15 @@ def is_fixture_autouse(func: ast.FunctionDef) -> bool:
62
63
  if not hasattr(deco, "func"):
63
64
  continue
64
65
 
65
- if getattr(deco.func, "attr", None) and getattr(deco.func, "value", None):
66
- if deco.func.attr == "fixture" and deco.func.value.id == "pytest":
67
- for _key in deco.keywords:
68
- if _key.arg == "autouse":
69
- return True
66
+ if (
67
+ getattr(deco.func, "attr", None)
68
+ and getattr(deco.func, "value", None)
69
+ and deco.func.attr == "fixture"
70
+ and deco.func.value.id == "pytest"
71
+ ):
72
+ for _key in deco.keywords:
73
+ if _key.arg == "autouse":
74
+ return True
70
75
  return False
71
76
 
72
77
 
@@ -80,18 +85,25 @@ def is_pytest_fixture(func: ast.FunctionDef) -> bool:
80
85
  # Case 1: @pytest.fixture(...)
81
86
  if hasattr(decorator, "func"):
82
87
  # e.g. @pytest.fixture(...)
83
- if getattr(decorator.func, "attr", None) and getattr(decorator.func, "value", None):
84
- if decorator.func.attr == "fixture" and getattr(decorator.func.value, "id", None) == "pytest":
85
- return True
88
+ if (
89
+ getattr(decorator.func, "attr", None)
90
+ and getattr(decorator.func, "value", None)
91
+ and decorator.func.attr == "fixture"
92
+ and getattr(decorator.func.value, "id", None) == "pytest"
93
+ ):
94
+ return True
86
95
  # e.g. from pytest import fixture; @fixture(...)
87
96
  if isinstance(decorator.func, ast.Name) and decorator.func.id == "fixture":
88
97
  return True
89
98
  # Case 2: @pytest.fixture (no parentheses)
90
99
  else:
91
100
  # e.g. @pytest.fixture
92
- if getattr(decorator, "attr", None) == "fixture" and getattr(decorator, "value", None):
93
- if getattr(decorator.value, "id", None) == "pytest":
94
- return True
101
+ if (
102
+ getattr(decorator, "attr", None) == "fixture"
103
+ and getattr(decorator, "value", None)
104
+ and getattr(decorator.value, "id", None) == "pytest"
105
+ ):
106
+ return True
95
107
  # e.g. from pytest import fixture; @fixture
96
108
  if isinstance(decorator, ast.Name) and decorator.id == "fixture":
97
109
  return True
@@ -149,17 +161,14 @@ def _build_keyword_unpacking_pattern(function_name: str) -> str:
149
161
 
150
162
  def _is_pytest_mark_usefixtures_call(call_node: ast.Call) -> bool:
151
163
  """Check if an AST Call node represents pytest.mark.usefixtures(...)."""
152
- if isinstance(call_node.func, ast.Attribute):
153
- # Handle pytest.mark.usefixtures
154
- if (
155
- call_node.func.attr == "usefixtures"
156
- and isinstance(call_node.func.value, ast.Attribute)
157
- and call_node.func.value.attr == "mark"
158
- and isinstance(call_node.func.value.value, ast.Name)
159
- and call_node.func.value.value.id == "pytest"
160
- ):
161
- return True
162
- return False
164
+ return (
165
+ isinstance(call_node.func, ast.Attribute)
166
+ and call_node.func.attr == "usefixtures"
167
+ and isinstance(call_node.func.value, ast.Attribute)
168
+ and call_node.func.value.attr == "mark"
169
+ and isinstance(call_node.func.value.value, ast.Name)
170
+ and call_node.func.value.value.id == "pytest"
171
+ )
163
172
 
164
173
 
165
174
  def _check_fixturenames_insert_pattern(fixture_name: str, file_path: str) -> bool:
@@ -173,27 +182,27 @@ def _check_fixturenames_insert_pattern(fixture_name: str, file_path: str) -> boo
173
182
  content = f.read()
174
183
 
175
184
  tree = parse(source=content)
176
-
177
- # Look for method calls like item.fixturenames.insert(...)
178
- for node in ast.walk(tree):
179
- if isinstance(node, ast.Call):
180
- # Check for attribute calls like item.fixturenames.insert
181
- if (
182
- isinstance(node.func, ast.Attribute)
183
- and node.func.attr == "insert"
184
- and isinstance(node.func.value, ast.Attribute)
185
- and node.func.value.attr == "fixturenames"
186
- ):
187
- # Check the arguments for our fixture name
188
- for arg in node.args:
189
- if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
190
- if arg.value == fixture_name:
191
- return True
192
-
193
- return False
194
185
  except (FileNotFoundError, SyntaxError, ValueError):
195
186
  return False
196
187
 
188
+ # Look for method calls like item.fixturenames.insert(...)
189
+ for node in ast.walk(tree):
190
+ if not isinstance(node, ast.Call):
191
+ continue
192
+ # Check for attribute calls like item.fixturenames.insert
193
+ if (
194
+ isinstance(node.func, ast.Attribute)
195
+ and node.func.attr == "insert"
196
+ and isinstance(node.func.value, ast.Attribute)
197
+ and node.func.value.attr == "fixturenames"
198
+ ):
199
+ # Check the arguments for our fixture name
200
+ for arg in node.args:
201
+ if isinstance(arg, ast.Constant) and isinstance(arg.value, str) and arg.value == fixture_name:
202
+ return True
203
+
204
+ return False
205
+
197
206
 
198
207
  def _check_getfixturevalue_pattern(fixture_name: str, file_path: str) -> bool:
199
208
  """Check if a fixture is used in request.getfixturevalue() pattern.
@@ -207,31 +216,31 @@ def _check_getfixturevalue_pattern(fixture_name: str, file_path: str) -> bool:
207
216
  content = f.read()
208
217
 
209
218
  tree = parse(source=content)
219
+ except (FileNotFoundError, SyntaxError, ValueError):
220
+ return False
210
221
 
211
- # Look for method calls like request.getfixturevalue(...)
212
- for node in ast.walk(tree):
213
- if isinstance(node, ast.Call):
214
- # Check for attribute calls like request.getfixturevalue
215
- if isinstance(node.func, ast.Attribute) and node.func.attr == "getfixturevalue":
216
- # Check positional arguments
217
- for arg in node.args:
218
- if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
219
- if arg.value == fixture_name:
220
- return True
222
+ # Look for method calls like request.getfixturevalue(...)
223
+ for node in ast.walk(tree):
224
+ if not isinstance(node, ast.Call):
225
+ continue
226
+ # Check for attribute calls like request.getfixturevalue
227
+ if isinstance(node.func, ast.Attribute) and node.func.attr == "getfixturevalue":
228
+ # Check positional arguments
229
+ for arg in node.args:
230
+ if isinstance(arg, ast.Constant) and isinstance(arg.value, str) and arg.value == fixture_name:
231
+ return True
221
232
 
222
- # Check keyword arguments (argname="fixture_name")
223
- for keyword in node.keywords:
224
- if keyword.arg == "argname":
225
- if (
226
- isinstance(keyword.value, ast.Constant)
227
- and isinstance(keyword.value.value, str)
228
- and keyword.value.value == fixture_name
229
- ):
230
- return True
233
+ # Check keyword arguments (argname="fixture_name")
234
+ for keyword in node.keywords:
235
+ if (
236
+ keyword.arg == "argname"
237
+ and isinstance(keyword.value, ast.Constant)
238
+ and isinstance(keyword.value.value, str)
239
+ and keyword.value.value == fixture_name
240
+ ):
241
+ return True
231
242
 
232
- return False
233
- except (FileNotFoundError, SyntaxError, ValueError):
234
- return False
243
+ return False
235
244
 
236
245
 
237
246
  def _is_usefixtures_context(file_path: str, line_number: str, fixture_name: str) -> bool:
@@ -246,55 +255,59 @@ def _is_usefixtures_context(file_path: str, line_number: str, fixture_name: str)
246
255
  # Parse the AST of the file
247
256
  tree = parse(source=content)
248
257
  target_line = int(line_number)
258
+ except (FileNotFoundError, SyntaxError, ValueError):
259
+ return False
249
260
 
250
- # Look for pytest.mark.usefixtures calls that span the target line
251
- for node in ast.walk(tree):
252
- if isinstance(node, ast.Assign):
253
- # Check if the value is a call to pytest.mark.usefixtures
254
- if isinstance(node.value, ast.Call):
255
- if _is_pytest_mark_usefixtures_call(node.value):
261
+ # Look for pytest.mark.usefixtures calls that span the target line
262
+ for node in ast.walk(tree):
263
+ if isinstance(node, ast.Assign):
264
+ # Check if the value is a call to pytest.mark.usefixtures
265
+ if isinstance(node.value, ast.Call) and _is_pytest_mark_usefixtures_call(node.value):
266
+ # Check if the target line is within this call's range
267
+ call_start = node.value.lineno
268
+ call_end = node.value.end_lineno or call_start
269
+
270
+ if call_start <= target_line <= call_end:
271
+ # Check if any of the arguments contain our fixture name
272
+ for arg in node.value.args:
273
+ if isinstance(arg, ast.Constant) and isinstance(arg.value, str) and arg.value == fixture_name:
274
+ return True
275
+
276
+ # Also check if the value is a list containing pytest.mark.usefixtures calls
277
+ elif isinstance(node.value, ast.List):
278
+ for element in node.value.elts:
279
+ if isinstance(element, ast.Call) and _is_pytest_mark_usefixtures_call(element):
256
280
  # Check if the target line is within this call's range
257
- call_start = node.value.lineno
258
- call_end = node.value.end_lineno or call_start
281
+ call_start = element.lineno
282
+ call_end = element.end_lineno or call_start
259
283
 
260
284
  if call_start <= target_line <= call_end:
261
285
  # Check if any of the arguments contain our fixture name
262
- for arg in node.value.args:
263
- if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
264
- if arg.value == fixture_name:
265
- return True
266
-
267
- # Also check if the value is a list containing pytest.mark.usefixtures calls
268
- elif isinstance(node.value, ast.List):
269
- for element in node.value.elts:
270
- if isinstance(element, ast.Call) and _is_pytest_mark_usefixtures_call(element):
271
- # Check if the target line is within this call's range
272
- call_start = element.lineno
273
- call_end = element.end_lineno or call_start
274
-
275
- if call_start <= target_line <= call_end:
276
- # Check if any of the arguments contain our fixture name
277
- for arg in element.args:
278
- if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
279
- if arg.value == fixture_name:
280
- return True
281
-
282
- # Also check decorators
283
- elif isinstance(node, (ast.FunctionDef, ast.ClassDef)):
284
- for decorator in node.decorator_list:
285
- if isinstance(decorator, ast.Call) and _is_pytest_mark_usefixtures_call(decorator):
286
- call_start = decorator.lineno
287
- call_end = decorator.end_lineno or call_start
288
-
289
- if call_start <= target_line <= call_end:
290
- for arg in decorator.args:
291
- if isinstance(arg, ast.Constant) and isinstance(arg.value, str):
292
- if arg.value == fixture_name:
293
- return True
286
+ for arg in element.args:
287
+ if (
288
+ isinstance(arg, ast.Constant)
289
+ and isinstance(arg.value, str)
290
+ and arg.value == fixture_name
291
+ ):
292
+ return True
293
+
294
+ # Also check decorators
295
+ elif isinstance(node, (ast.FunctionDef, ast.ClassDef)):
296
+ for decorator in node.decorator_list:
297
+ if isinstance(decorator, ast.Call) and _is_pytest_mark_usefixtures_call(decorator):
298
+ call_start = decorator.lineno
299
+ call_end = decorator.end_lineno or call_start
300
+
301
+ if call_start <= target_line <= call_end:
302
+ for arg in decorator.args:
303
+ if (
304
+ isinstance(arg, ast.Constant)
305
+ and isinstance(arg.value, str)
306
+ and arg.value == fixture_name
307
+ ):
308
+ return True
294
309
 
295
- return False
296
- except (FileNotFoundError, SyntaxError, ValueError):
297
- return False
310
+ return False
298
311
 
299
312
 
300
313
  def _is_documentation_pattern(line: str, function_name: str) -> bool:
@@ -351,11 +364,7 @@ def _is_documentation_pattern(line: str, function_name: str) -> bool:
351
364
  # Pattern 4: Check for common docstring patterns
352
365
  # Lines that start with common documentation patterns
353
366
  doc_starters = ['"""', "'''", "# ", "## ", "### ", "-", "•"]
354
- if any(stripped_line.startswith(starter) for starter in doc_starters):
355
- if f"{function_name}(" in stripped_line:
356
- return True
357
-
358
- return False
367
+ return any(stripped_line.startswith(starter) for starter in doc_starters) and f"{function_name}(" in stripped_line
359
368
 
360
369
 
361
370
  def _find_git_root(file_path: str) -> str:
@@ -436,10 +445,7 @@ def is_ignore_function_list(ignore_prefix_list: list[str], function: ast.Functio
436
445
  ignore_function_lists = [
437
446
  function.name for ignore_prefix in ignore_prefix_list if function.name.startswith(ignore_prefix)
438
447
  ]
439
- if ignore_function_lists:
440
- return True
441
-
442
- return False
448
+ return bool(ignore_function_lists)
443
449
 
444
450
 
445
451
  def _resolve_absolute_path(git_grep_path: str, reference_file: str) -> str:
@@ -493,7 +499,7 @@ def process_file(py_file: str, func_ignore_prefix: list[str], file_ignore_list:
493
499
 
494
500
  # Ignore commented lines (full line or inline)
495
501
  code_part = _line.split("#", 1)[0]
496
- if code_part.startswith("import") or code_part.startswith("from"):
502
+ if code_part.startswith(("import", "from")):
497
503
  continue
498
504
 
499
505
  if func.name not in code_part:
@@ -663,7 +669,7 @@ def get_unused_functions(
663
669
  try:
664
670
  if unused_func := future.result():
665
671
  unused_functions.append(unused_func)
666
- except Exception as exc:
672
+ except Exception as exc: # noqa: BLE001
667
673
  processing_errors.append(f"{jobs[future]}: {exc}")
668
674
 
669
675
  if processing_errors:
@@ -2,7 +2,8 @@ from __future__ import annotations
2
2
 
3
3
  import json
4
4
  import os
5
- from typing import Any, Dict, Iterable, Optional
5
+ from collections.abc import Iterable
6
+ from typing import Any
6
7
 
7
8
  import click
8
9
  import yaml
@@ -11,7 +12,7 @@ from simple_logger.logger import get_logger
11
12
  LOGGER = get_logger(name=__name__)
12
13
 
13
14
 
14
- def get_util_config(util_name: str, config_file_path: Optional[str] = None) -> Dict[str, Any]:
15
+ def get_util_config(util_name: str, config_file_path: str | None = None) -> dict[str, Any]:
15
16
  if config_file_path and os.path.exists(config_file_path):
16
17
  with open(config_file_path) as _file:
17
18
  return yaml.safe_load(_file).get(util_name, {})
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "python-utility-scripts"
3
- version = "2.0.5"
3
+ version = "2.0.6"
4
4
  description = "Python utility scripts"
5
5
  requires-python = ">=3.10"
6
6
  readme = "README.md"