kkpyutil 1.45.0__tar.gz → 1.46.0__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: kkpyutil
3
- Version: 1.45.0
3
+ Version: 1.46.0
4
4
  Summary: Building blocks for sysadmin and DevOps
5
5
  Home-page: https://github.com/kakyoism/kkpyutil/
6
6
  License: MIT
@@ -1255,23 +1255,85 @@ def save_winreg_record(full_key, var, value, value_type=winreg.REG_EXPAND_SZ if
1255
1255
  winreg.SetValueEx(key, var, 0, value_type, value)
1256
1256
 
1257
1257
 
1258
+ def _log_subprocess_command(cmd, cwd, logger, func_name="subprocess"):
1259
+ """Helper function to log subprocess command consistently"""
1260
+ cmd_log = f"""\
1261
+ {func_name}:
1262
+ {' '.join(cmd)}
1263
+ cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1264
+ """
1265
+ logger.info(cmd_log)
1266
+
1267
+
1268
+ def _log_subprocess_startup_error(e, cmd, logger, useexception=True):
1269
+ """Helper function to log subprocess startup errors with structured format"""
1270
+ error_type = type(e).__name__
1271
+ cmd_str = ' '.join(cmd)
1272
+
1273
+ # Create structured error message based on error type
1274
+ if isinstance(e, FileNotFoundError):
1275
+ situation = "Command not found"
1276
+ detail = [f"Command: {cmd_str}", f"Error: {error_type}"]
1277
+ advice = [
1278
+ "Check if the command is installed and available in PATH",
1279
+ "Verify the command name spelling",
1280
+ "Use absolute path if the command is in a specific location"
1281
+ ]
1282
+ elif isinstance(e, PermissionError):
1283
+ situation = "Permission denied"
1284
+ detail = [f"Command: {cmd_str}", f"Error: {error_type}"]
1285
+ advice = [
1286
+ "Check if you have permission to execute the command",
1287
+ "Try running with elevated privileges if necessary",
1288
+ "Verify file permissions on the executable"
1289
+ ]
1290
+ else:
1291
+ situation = "Subprocess failed to start"
1292
+ detail = [f"Command: {cmd_str}", f"Error: {error_type}: {str(e)}"]
1293
+ advice = [
1294
+ "Check if the command exists and is executable",
1295
+ "Verify all command arguments are valid",
1296
+ "Check system resources and environment"
1297
+ ]
1298
+
1299
+ error_msg = format_log(situation, detail=detail, advice=advice)
1300
+ logger.error(error_msg)
1301
+
1302
+ if useexception:
1303
+ raise e
1304
+ return types.SimpleNamespace(returncode=2, stdout='', stderr=safe_encode_text(str(e), encoding=LOCALE_CODEC))
1305
+
1306
+
1258
1307
  def run_cmd(cmd, cwd=None, logger=None, check=True, shell=False, verbose=False, useexception=True, env=None, hidedoswin=True):
1259
1308
  """
1260
- - Use shell==True with autotools where new shell is needed to treat the entire command option sequence as a command,
1261
- e.g., shell=True means running sh -c ./configure CFLAGS="..."
1262
- - we do not use check=False to supress exception because that'd leave app no way to tell if child-proc succeeded or not
1263
- - instead, we catch CallProcessError but avoid rethrow, and then return error code and other key diagnostics to app
1264
- - allow user to input non-str options, e.g., int, bool, etc., and auto-convert to str for subprocess
1309
+ Run a subprocess command and wait for completion.
1310
+
1311
+ Args:
1312
+ cmd: Command list (non-str items auto-converted to str)
1313
+ cwd: Working directory (default: current directory)
1314
+ logger: Logger instance (default: glogger)
1315
+ check: Whether to raise exception on non-zero exit (default: True)
1316
+ shell: Whether to use shell (default: False)
1317
+ verbose: Whether to log stdout at INFO level vs DEBUG (default: False)
1318
+ useexception: Whether to raise exceptions vs return error info (default: True)
1319
+ env: Environment variables (default: None)
1320
+ hidedoswin: Whether to hide DOS window on Windows (default: True)
1321
+
1322
+ Returns:
1323
+ subprocess.CompletedProcess on success, or SimpleNamespace with error info
1324
+
1325
+ Best practices:
1326
+ - Use useexception=False for optional commands that may fail
1327
+ - Use verbose=True to see subprocess output in logs
1328
+ - Use shell=True only when needed (e.g., shell built-ins, complex commands)
1329
+ - Use check=False with useexception=False for commands where failure is expected
1265
1330
  """
1266
1331
  cmd = [comp if isinstance(comp, str) else str(comp) for comp in cmd]
1267
1332
  logger = logger or glogger
1268
1333
  console_info = logger.info if logger and verbose else logger.debug
1269
- # show cmdline with or without exceptions
1270
- cmd_log = f"""\
1271
- {' '.join(cmd)}
1272
- cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1273
- """
1274
- logger.info(cmd_log)
1334
+
1335
+ # Log command execution
1336
+ _log_subprocess_command(cmd, cwd, logger, "run_cmd")
1275
1337
  try:
1276
1338
  if hidedoswin and PLATFORM == 'Windows':
1277
1339
  startupinfo = subprocess.STARTUPINFO()
@@ -1286,39 +1348,71 @@ cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1286
1348
  if stderr_log:
1287
1349
  logger.error(f'stderr:\n{stderr_log}')
1288
1350
  # subprocess started but failed halfway: check=True, proc returns non-zero
1289
- # won't trigger this exception when useexception=True
1290
1351
  except subprocess.CalledProcessError as e:
1291
- # generic error, grandchild_cmd error with noexception enabled
1292
- stdout_log = f'stdout:\n{safe_decode_bytes(e.stdout)}'
1293
- stderr_log = f'stderr:\n{safe_decode_bytes(e.stderr)}'
1294
- logger.info(stdout_log)
1295
- logger.error(stderr_log)
1352
+ stdout_log = safe_decode_bytes(e.stdout)
1353
+ stderr_log = safe_decode_bytes(e.stderr)
1354
+
1355
+ # Log subprocess output with clear separation
1356
+ if stdout_log:
1357
+ logger.info(f'Process stdout:\n{stdout_log}')
1358
+ if stderr_log:
1359
+ logger.error(f'Process stderr:\n{stderr_log}')
1360
+
1361
+ # Log structured error message
1362
+ situation = "Subprocess completed with non-zero exit code"
1363
+ detail = [
1364
+ f"Command: {' '.join(cmd)}",
1365
+ f"Exit code: {e.returncode}",
1366
+ f"Has stdout: {'Yes' if stdout_log else 'No'}",
1367
+ f"Has stderr: {'Yes' if stderr_log else 'No'}"
1368
+ ]
1369
+ error_msg = format_log(situation, detail=detail)
1370
+ logger.error(error_msg)
1371
+
1296
1372
  if useexception:
1297
1373
  raise e
1298
1374
  return types.SimpleNamespace(returncode=1, stdout=e.stdout, stderr=e.stderr)
1299
1375
  # subprocess fails to start
1300
1376
  except Exception as e:
1301
- # cmd missing ...FileNotFound
1302
- # PermissionError, OSError, TimeoutExpired
1303
- logger.error(e)
1304
- if useexception:
1305
- raise e
1306
- return types.SimpleNamespace(returncode=2, stdout='', stderr=safe_encode_text(str(e), encoding=LOCALE_CODEC))
1377
+ return _log_subprocess_startup_error(e, cmd, logger, useexception)
1307
1378
  return proc
1308
1379
 
1309
1380
 
1310
- def run_daemon(cmd, cwd=None, logger=None, shell=False, useexception=True, env=None, hidedoswin=True):
1381
+ def run_daemon(cmd, cwd=None, logger=None, shell=False, verbose=False, useexception=True, env=None, hidedoswin=True):
1311
1382
  """
1312
- - if returned proc is None, means
1383
+ Start a subprocess in the background (non-blocking).
1384
+
1385
+ Args:
1386
+ cmd: Command list (non-str items auto-converted to str)
1387
+ cwd: Working directory (default: current directory)
1388
+ logger: Logger instance (default: glogger)
1389
+ shell: Whether to use shell (default: False)
1390
+ verbose: Whether to log at INFO level vs DEBUG (default: False)
1391
+ useexception: Whether to raise exceptions vs return error info (default: True)
1392
+ env: Environment variables (default: None)
1393
+ hidedoswin: Whether to hide DOS window on Windows (default: True)
1394
+
1395
+ Returns:
1396
+ subprocess.Popen object on success, or SimpleNamespace with error info
1397
+
1398
+ Best practices:
1399
+ - Use for long-running processes or fire-and-forget commands
1400
+ - Call proc.communicate() or proc.wait() to get final results
1401
+ - Use verbose=True to see command execution in logs
1402
+ - Background processes won't show stdout/stderr in logs automatically
1403
+ - Use useexception=False for optional background processes
1404
+
1405
+ Note:
1406
+ Background processes capture stdout/stderr but don't log them automatically.
1407
+ Call proc.communicate() to retrieve output when the process completes.
1313
1408
  """
1314
1409
  cmd = [comp if isinstance(comp, str) else str(comp) for comp in cmd]
1315
1410
  logger = logger or glogger
1316
- logger.debug(f"""run in background:
1317
- {' '.join(cmd)}
1318
- cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1319
- """)
1320
- # fake the same proc interface
1321
- proc = None
1411
+
1412
+ # Log command execution with appropriate level
1413
+ log_func = logger.info if verbose else logger.debug
1414
+ _log_subprocess_command(cmd, cwd, logger, "run_daemon")
1415
+
1322
1416
  try:
1323
1417
  if hidedoswin and PLATFORM == 'Windows':
1324
1418
  startupinfo = subprocess.STARTUPINFO()
@@ -1326,16 +1420,14 @@ cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1326
1420
  proc = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env, startupinfo=startupinfo)
1327
1421
  else:
1328
1422
  proc = subprocess.Popen(cmd, shell=shell, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd, env=env)
1329
- # won't be able to retrieve log from background
1423
+
1424
+ # Log successful startup
1425
+ log_func(f"Background process started successfully (PID: {proc.pid})")
1426
+ return proc
1427
+
1330
1428
  # subprocess fails to start
1331
1429
  except Exception as e:
1332
- # cmd missing ...FileNotFound
1333
- # PermissionError, OSError, TimeoutExpired
1334
- logger.error(e)
1335
- if useexception:
1336
- raise e
1337
- return types.SimpleNamespace(returncode=2, stdout='', stderr=safe_encode_text(str(e), encoding=LOCALE_CODEC))
1338
- return proc
1430
+ return _log_subprocess_startup_error(e, cmd, logger, useexception)
1339
1431
 
1340
1432
 
1341
1433
  def watch_cmd(cmd, cwd=None, logger=None, shell=False, verbose=False, useexception=True, prompt=None, timeout=None, env=None, hidedoswin=True):
@@ -1348,12 +1440,9 @@ def watch_cmd(cmd, cwd=None, logger=None, shell=False, verbose=False, useexcepti
1348
1440
  output_queue.put(line)
1349
1441
  cmd = [comp if isinstance(comp, str) else str(comp) for comp in cmd]
1350
1442
  logger = logger or glogger
1351
- # show cmdline with or without exceptions
1352
- cmd_log = f"""\
1353
- {' '.join(cmd)}
1354
- cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1355
- """
1356
- logger.info(cmd_log)
1443
+
1444
+ # Log command execution
1445
+ _log_subprocess_command(cmd, cwd, logger, "watch_cmd")
1357
1446
  try:
1358
1447
  if hidedoswin and PLATFORM == 'Windows':
1359
1448
  startupinfo = subprocess.STARTUPINFO()
@@ -1397,11 +1486,7 @@ cwd: {osp.abspath(cwd) if cwd else os.getcwd()}
1397
1486
  return proc
1398
1487
  # subprocess fails to start
1399
1488
  except Exception as e:
1400
- # no need to have header, exception has it all
1401
- logger.error(e)
1402
- if useexception:
1403
- raise e
1404
- return types.SimpleNamespace(returncode=2, stdout='', stderr=safe_encode_text(str(e), encoding=LOCALE_CODEC))
1489
+ return _log_subprocess_startup_error(e, cmd, logger, useexception)
1405
1490
 
1406
1491
 
1407
1492
  def extract_call_args(file, caller, callee):
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "kkpyutil"
3
- version = "1.45.0"
3
+ version = "1.46.0"
4
4
  description = "Building blocks for sysadmin and DevOps"
5
5
  authors = ["Beinan Li <li.beinan@gmail.com>"]
6
6
  maintainers = ["Beinan Li <li.beinan@gmail.com>"]
File without changes
File without changes