Utilities¶
This section documents the utility modules provided by sts-libs.
Command Line¶
sts.utils.cmdline
¶
Command execution module.
This module provides functionality for command execution: - Command running - Command argument handling - Command validation
CommandArgs
¶
Bases: TypedDict
Command arguments type.
This type represents command arguments that can be passed to format_args. The values can be strings, numbers, booleans, lists, or None.
Source code in sts_libs/src/sts/utils/cmdline.py
30 31 32 33 34 35 36 37 38 |
|
exists(cmd)
¶
Check if command exists in PATH.
This is a direct passthrough to testinfra's exists().
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd
|
str
|
Command to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if command exists in PATH |
Example
assert exists('ls')
Source code in sts_libs/src/sts/utils/cmdline.py
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
|
format_arg(key, value)
¶
Format command argument.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
str
|
Argument name |
required |
value
|
ArgValue
|
Argument value (str, int, float, bool, list, or None) |
required |
Returns:
Type | Description |
---|---|
str
|
Formatted argument string |
Example
format_arg('size', '1G')
'--size=1G'
format_arg('quiet', True)
'--quiet'
format_arg('count', 5)
'--count=5'
Source code in sts_libs/src/sts/utils/cmdline.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
format_args(**kwargs)
¶
Format command arguments.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
**kwargs
|
ArgValue
|
Command arguments (str, int, float, bool, list, or None) |
{}
|
Returns:
Type | Description |
---|---|
str
|
Formatted arguments string |
Example
format_args(size='1G', quiet=True, count=5)
'--size=1G --quiet --count=5'
Source code in sts_libs/src/sts/utils/cmdline.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
|
run(cmd, msg=None)
¶
Run command and return result.
This is a thin wrapper around testinfra's run() that adds logging.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmd
|
str
|
Command to execute |
required |
msg
|
str | None
|
Optional message to log before execution |
None
|
Returns:
Type | Description |
---|---|
CommandResult
|
CommandResult from testinfra |
Example
result = run('ls -l')
assert result.succeeded
print(result.stdout)
Source code in sts_libs/src/sts/utils/cmdline.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
|
System Management¶
sts.utils.system
¶
System utilities.
This module provides functionality for system operations: - System information - System logs - System state - Service management
LogFormat
¶
Bases: Enum
Log format options.
Source code in sts_libs/src/sts/utils/system.py
33 34 35 36 37 38 |
|
LogOptions
dataclass
¶
Log retrieval options.
This class provides options for log retrieval: - Format options - Filtering options - Time range options
Parameters:
Name | Type | Description | Default |
---|---|---|---|
format
|
LogFormat
|
Log format (optional, defaults to DEFAULT) |
DEFAULT
|
length
|
int | None
|
Number of lines (optional) |
None
|
since
|
str | None
|
Since timestamp (optional) |
None
|
grep
|
str | None
|
Filter pattern (optional) |
None
|
options
|
list[str]
|
Additional options (optional) |
list()
|
Example
opts = LogOptions() # Uses defaults
opts = LogOptions(format=LogFormat.KERNEL) # Custom format
Source code in sts_libs/src/sts/utils/system.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
|
SystemInfo
dataclass
¶
System information.
This class provides functionality for system information: - Operating system details - Hardware information - System state
Properties
hostname: System hostname kernel: Kernel version arch: System architecture distribution: Distribution name release: Distribution release codename: Distribution codename
Example
info = SystemInfo() # Discovers values when needed
print(info.hostname) # Discovers hostname on first access
'localhost'
Source code in sts_libs/src/sts/utils/system.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
arch: str | None
property
¶
Get system architecture.
Returns:
Type | Description |
---|---|
str | None
|
System architecture or None if not found |
Example
info.arch
'x86_64'
codename: str | None
property
¶
Get distribution codename.
Returns:
Type | Description |
---|---|
str | None
|
Distribution codename or None if not found |
Example
info.codename
'thirty eight'
distribution: str | None
property
¶
Get distribution name.
Returns:
Type | Description |
---|---|
str | None
|
Distribution name or None if not found |
Example
info.distribution
'fedora'
hostname: str | None
property
¶
Get system hostname.
Returns:
Type | Description |
---|---|
str | None
|
System hostname or None if not found |
Example
info.hostname
'localhost'
in_container: bool
property
¶
Check if running in container.
Returns:
Type | Description |
---|---|
bool
|
True if in container, False otherwise |
Example
info.in_container
False
is_debug: bool
property
¶
Check if running debug kernel.
Returns:
Type | Description |
---|---|
bool
|
True if debug kernel, False otherwise |
Example
info.is_debug
False
kernel: str | None
property
¶
Get kernel version.
Returns:
Type | Description |
---|---|
str | None
|
Kernel version or None if not found |
Example
info.kernel
'5.4.0-1.el8'
release: str | None
property
¶
Get distribution release.
Returns:
Type | Description |
---|---|
str | None
|
Distribution release or None if not found |
Example
info.release
'38'
get_current()
classmethod
¶
Get current system information.
Returns:
Type | Description |
---|---|
SystemInfo
|
System information |
Example
info = SystemInfo.get_current()
info.kernel
'5.4.0-1.el8'
Source code in sts_libs/src/sts/utils/system.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
|
log_all()
¶
Log all system information.
Example
info = SystemInfo()
info.log_all()
INFO: Hostname: localhost
INFO: Kernel: 5.4.0-1.el8
INFO: Architecture: x86_64
INFO: Distribution: fedora
INFO: Release: 38
INFO: Codename: thirty eight
Source code in sts_libs/src/sts/utils/system.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 |
|
SystemManager
¶
System manager functionality.
This class provides functionality for system operations: - System information - System logs - System state - Service management
Example
sm = SystemManager()
sm.get_logs(LogOptions(format=LogFormat.KERNEL))
'Jan 1 00:00:00 kernel: ...'
Source code in sts_libs/src/sts/utils/system.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
|
__init__()
¶
Initialize system manager.
Source code in sts_libs/src/sts/utils/system.py
310 311 312 313 |
|
clear_logs()
¶
Clear system logs.
Example
sm = SystemManager()
sm.clear_logs()
Source code in sts_libs/src/sts/utils/system.py
417 418 419 420 421 422 423 424 425 426 |
|
generate_sosreport(skip_plugins=None, plugin_timeout=300)
¶
Generate sosreport.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
skip_plugins
|
str | None
|
Plugins to skip |
None
|
plugin_timeout
|
int
|
Plugin timeout in seconds |
300
|
Returns:
Type | Description |
---|---|
str | None
|
Path to sosreport or None if error |
Example
sm = SystemManager()
sm.generate_sosreport(skip_plugins='kdump,networking')
'/tmp/sosreport-localhost-123456-2021-01-01-abcdef.tar.xz'
Source code in sts_libs/src/sts/utils/system.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 |
|
get_logs(options=None)
¶
Get system logs.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
options
|
LogOptions | None
|
Log options |
None
|
Returns:
Type | Description |
---|---|
str | None
|
Log output or None if error |
Example
sm = SystemManager()
sm.get_logs(LogOptions(format=LogFormat.KERNEL))
'Jan 1 00:00:00 kernel: ...'
Source code in sts_libs/src/sts/utils/system.py
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
get_timestamp(timezone_='local')
¶
Get current timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timezone_
|
Literal['utc', 'local']
|
Timezone to use |
'local'
|
Returns:
Type | Description |
---|---|
str
|
Timestamp string (YYYYMMDDhhmmss) |
Example
sm = SystemManager()
sm.get_timestamp()
'20210101000000'
Source code in sts_libs/src/sts/utils/system.py
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 |
|
is_service_enabled(service)
¶
Check if service is enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if service is enabled, False otherwise |
Example
sm = SystemManager()
sm.is_service_enabled('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 |
|
is_service_running(service)
¶
Check if service is running.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if service is running, False otherwise |
Example
sm = SystemManager()
sm.is_service_running('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 |
|
service_disable(service)
¶
Disable service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
sm = SystemManager()
sm.service_disable('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
|
service_enable(service)
¶
Enable service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
sm = SystemManager()
sm.service_enable('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 |
|
service_restart(service)
¶
Restart service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
sm = SystemManager()
sm.service_restart('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
|
service_start(service)
¶
Start service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
sm = SystemManager()
sm.service_start('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 |
|
service_stop(service)
¶
Stop service.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
sm = SystemManager()
sm.service_stop('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 |
|
test_service(service)
¶
Test service operations.
This method tests: - Enable/disable cycle - Start/stop cycle - Restart operation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
service
|
str
|
Service name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if all tests pass, False otherwise |
Example
sm = SystemManager()
sm.test_service('sshd')
True
Source code in sts_libs/src/sts/utils/system.py
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 |
|
Package Management¶
sts.utils.packages
¶
Package management.
This module provides functionality for managing system packages: - Package installation - Package information - Package version management - Repository management
Dnf
¶
DNF package manager functionality.
This class provides functionality for managing system packages: - Package installation - Package removal - Repository management
Example
pm = Dnf()
pm.install('bash')
True
Source code in sts_libs/src/sts/utils/packages.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
__init__()
¶
Initialize package manager.
Source code in sts_libs/src/sts/utils/packages.py
103 104 105 |
|
add_repo(config)
¶
Add repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config
|
RepoConfig
|
Repository configuration |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
config = RepoConfig(
name='epel',
baseurl='https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/',
)
pm.add_repo(config)
True
Source code in sts_libs/src/sts/utils/packages.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
|
download_repo(url, name=None, *, overwrite=True)
¶
Download repository file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
Repository file URL |
required |
name
|
str | None
|
Repository name (optional, derived from URL) |
None
|
overwrite
|
bool
|
Overwrite existing file (optional) |
True
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
pm.download_repo('https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm')
True
Source code in sts_libs/src/sts/utils/packages.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
|
install(package)
¶
Install package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package
|
str
|
Package name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
pm.install('bash')
True
Source code in sts_libs/src/sts/utils/packages.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
remove(package)
¶
Remove package.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
package
|
str
|
Package name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
pm.remove('bash')
True
Source code in sts_libs/src/sts/utils/packages.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|
remove_repo(name)
¶
Remove repository.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Repository name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
pm.remove_repo('epel')
True
Source code in sts_libs/src/sts/utils/packages.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
repo_enabled(name)
¶
Check if repository is enabled.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Repository name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if repository exists and is enabled |
Example
pm.repo_enabled('epel')
True
Source code in sts_libs/src/sts/utils/packages.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
|
repo_exists(name)
¶
Check if repository exists.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Repository name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if repository exists |
Example
pm.repo_exists('epel')
True
Source code in sts_libs/src/sts/utils/packages.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
RepoConfig
dataclass
¶
Repository configuration.
This class provides configuration for repository management: - Repository metadata - Repository options - Repository URLs
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Repository name |
required |
baseurl
|
str | None
|
Repository URL (optional) |
None
|
metalink
|
str | None
|
Repository metalink (optional) |
None
|
enabled
|
bool
|
Enable repository |
True
|
gpgcheck
|
bool
|
Enable GPG check |
False
|
skip_if_unavailable
|
bool
|
Skip if unavailable |
True
|
Example
config = RepoConfig(
name='epel',
baseurl='https://dl.fedoraproject.org/pub/epel/8/Everything/x86_64/',
)
Source code in sts_libs/src/sts/utils/packages.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
to_config()
¶
Convert to repository configuration.
Returns:
Type | Description |
---|---|
dict[str, str]
|
Repository configuration dictionary |
Example
config = RepoConfig(name='epel', baseurl='https://example.com')
config.to_config()
{'name': 'epel', 'baseurl': 'https://example.com', 'enabled': '1', ...}
Source code in sts_libs/src/sts/utils/packages.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
|
ensure_installed(*packages)
¶
Ensure packages are installed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*packages
|
str
|
Package names to install |
()
|
Returns:
Type | Description |
---|---|
bool
|
True if all packages are installed, False otherwise |
Example
ensure_installed('lsscsi', 'curl')
True
ensure_installed('nonexistent')
False
Source code in sts_libs/src/sts/utils/packages.py
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
Module Management¶
sts.utils.modules
¶
Kernel module management.
This module provides functionality for managing kernel modules: - Module loading/unloading - Module information - Module dependencies
ModuleInfo
dataclass
¶
Kernel module information.
This class provides functionality for managing module information: - Module metadata - Module parameters - Module dependencies
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str | None
|
Module name (optional, discovers first loaded module) |
None
|
size
|
int | None
|
Module size (optional, discovered from module) |
None
|
used_by
|
list[str]
|
List of modules using this module (optional, discovered from module) |
list()
|
state
|
str | None
|
Module state (optional, discovered from module) |
None
|
parameters
|
dict[str, Any]
|
Module parameters (optional, discovered from module) |
dict()
|
Example
info = ModuleInfo() # Discovers first loaded module
info = ModuleInfo(name='dm_mod') # Discovers other values
Source code in sts_libs/src/sts/utils/modules.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
exists: bool
property
¶
Check if module exists.
Returns:
Type | Description |
---|---|
bool
|
True if exists, False otherwise |
Example
info = ModuleInfo(name='dm_mod')
info.exists
True
loaded: bool
property
¶
Check if module is loaded.
Returns:
Type | Description |
---|---|
bool
|
True if loaded, False otherwise |
Example
info = ModuleInfo(name='dm_mod')
info.loaded
True
__post_init__()
¶
Initialize module information.
Discovers module information based on provided parameters.
Source code in sts_libs/src/sts/utils/modules.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
from_name(name)
classmethod
¶
Get module information by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Module name |
required |
Returns:
Type | Description |
---|---|
ModuleInfo | None
|
Module information or None if not found |
Example
info = ModuleInfo.from_name('dm_mod')
info.used_by
['dm_mirror', 'dm_log']
Source code in sts_libs/src/sts/utils/modules.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
|
load(parameters=None)
¶
Load module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
parameters
|
str | None
|
Module parameters |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
info = ModuleInfo(name='dm_mod')
info.load()
True
Source code in sts_libs/src/sts/utils/modules.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
unload()
¶
Unload module.
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Raises:
Type | Description |
---|---|
ModuleInUseError
|
If module is in use |
RuntimeError
|
If module cannot be unloaded |
Example
info = ModuleInfo(name='dm_mod')
info.unload()
True
Source code in sts_libs/src/sts/utils/modules.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
unload_with_dependencies()
¶
Unload module and its dependencies.
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Raises:
Type | Description |
---|---|
ModuleInUseError
|
If module is in use |
RuntimeError
|
If module cannot be unloaded |
Example
info = ModuleInfo(name='dm_mod')
info.unload_with_dependencies()
True
Source code in sts_libs/src/sts/utils/modules.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
|
ModuleManager
¶
Module manager functionality.
This class provides functionality for managing kernel modules: - Module loading/unloading - Module information - Module dependencies
Example
mm = ModuleManager()
mm.load('dm_mod')
True
Source code in sts_libs/src/sts/utils/modules.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
|
__init__()
¶
Initialize module manager.
Source code in sts_libs/src/sts/utils/modules.py
252 253 254 255 |
|
get_all()
¶
Get list of all loaded modules.
Returns:
Type | Description |
---|---|
list[ModuleInfo]
|
List of module information |
Example
mm = ModuleManager()
mm.get_all()
[ModuleInfo(name='dm_mod', ...), ModuleInfo(name='ext4', ...)]
Source code in sts_libs/src/sts/utils/modules.py
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
get_parameters(name)
¶
Get module parameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Module name |
required |
Returns:
Type | Description |
---|---|
dict[str, str]
|
Dictionary of parameter names and values |
Example
mm = ModuleManager()
mm.get_parameters('dm_mod')
{'major': '253'}
Source code in sts_libs/src/sts/utils/modules.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
load(name, parameters=None)
¶
Load module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Module name |
required |
parameters
|
str | None
|
Module parameters |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
mm = ModuleManager()
mm.load('dm_mod')
True
Source code in sts_libs/src/sts/utils/modules.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
unload(name)
¶
Unload module.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Module name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Raises:
Type | Description |
---|---|
ModuleInUseError
|
If module is in use |
RuntimeError
|
If module cannot be unloaded |
Example
mm = ModuleManager()
mm.unload('dm_mod')
True
Source code in sts_libs/src/sts/utils/modules.py
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
|
unload_with_dependencies(name)
¶
Unload module and its dependencies.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Module name |
required |
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Raises:
Type | Description |
---|---|
ModuleInUseError
|
If module is in use |
RuntimeError
|
If module cannot be unloaded |
Example
mm = ModuleManager()
mm.unload_with_dependencies('dm_mod')
True
Source code in sts_libs/src/sts/utils/modules.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
|
Size Handling¶
sts.utils.size
¶
Size conversion utilities.
This module provides functionality for converting between human-readable sizes and bytes: - Human to bytes conversion (e.g., '1KiB' -> 1024) - Bytes to human conversion (e.g., 1024 -> '1KiB') - Size validation
Size
dataclass
¶
Size representation.
This class provides functionality for size operations: - Size parsing - Unit conversion - String representation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
float
|
Size value (optional, defaults to 0) |
0.0
|
unit
|
Unit
|
Size unit (optional, defaults to bytes) |
B
|
Example
size = Size() # Zero bytes
size = Size(1024) # 1024 bytes
size = Size(1.0, Unit.KIB) # Custom value and unit
size = Size.from_string('1KiB') # From string
Source code in sts_libs/src/sts/utils/size.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
__post_init__()
¶
Initialize size.
Converts value to appropriate unit if needed.
Source code in sts_libs/src/sts/utils/size.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
|
__str__()
¶
Return human-readable string.
Returns:
Type | Description |
---|---|
str
|
Size string (e.g., '1KiB') |
Example
str(Size(1.0, Unit.KIB))
'1KiB'
Source code in sts_libs/src/sts/utils/size.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
from_bytes(bytes_)
classmethod
¶
Convert bytes to human-readable size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bytes_
|
int
|
Size in bytes |
required |
Returns:
Type | Description |
---|---|
Size
|
Size instance |
Example
Size.from_bytes(1024)
Size(value=1.0, unit=Unit.KIB)
Source code in sts_libs/src/sts/utils/size.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
from_string(size)
classmethod
¶
Parse size from string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Size string (e.g., '1KiB') |
required |
Returns:
Type | Description |
---|---|
Size | None
|
Size instance or None if invalid |
Example
Size.from_string('1KiB')
Size(value=1.0, unit=Unit.KIB)
Source code in sts_libs/src/sts/utils/size.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
|
to_bytes()
¶
Convert to bytes.
Returns:
Type | Description |
---|---|
int
|
Size in bytes |
Example
Size(1.0, Unit.KIB).to_bytes()
1024
Source code in sts_libs/src/sts/utils/size.py
145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
Unit
¶
Bases: str
, Enum
Size units.
Source code in sts_libs/src/sts/utils/size.py
31 32 33 34 35 36 37 38 39 40 41 42 |
|
size_bytes_2_size_human(bytes_)
¶
Convert bytes to human-readable size.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bytes_
|
int | str | None
|
Size in bytes |
required |
Returns:
Type | Description |
---|---|
str | None
|
Human-readable size or None if invalid |
Example
size_bytes_2_size_human(1024)
'1KiB'
Source code in sts_libs/src/sts/utils/size.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
|
size_human_2_size_bytes(size)
¶
Convert human-readable size to bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Size string (e.g., '1KiB') |
required |
Returns:
Type | Description |
---|---|
int | None
|
Size in bytes or None if invalid |
Example
size_human_2_size_bytes('1KiB')
1024
Source code in sts_libs/src/sts/utils/size.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
size_human_check(size)
¶
Check if size string is valid.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
str
|
Size string to check |
required |
Returns:
Type | Description |
---|---|
bool
|
True if valid, False otherwise |
Example
size_human_check('1KiB')
True
Source code in sts_libs/src/sts/utils/size.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
|
String Utilities¶
sts.utils.string_extras
¶
String manipulation utilities.
This module provides utilities for string manipulation: - String conversion - String formatting - String validation
none_to_empty(value)
¶
Convert None to empty string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value
|
str | None
|
Value to convert |
required |
Returns:
Type | Description |
---|---|
str
|
Empty string if value is None, otherwise value |
Example
none_to_empty(None)
''
none_to_empty('test')
'test'
Source code in sts_libs/src/sts/utils/string_extras.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
rand_string(length=8, chars=None)
¶
Generate random string.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
length
|
int
|
Length of string to generate |
8
|
chars
|
str | None
|
Characters to use for generation (default: ascii_lowercase + digits) |
None
|
Returns:
Type | Description |
---|---|
str
|
Random string of specified length |
Example
rand_string()
'a1b2c3d4'
rand_string(4)
'w9x8'
rand_string(4, 'ABC123')
'B1CA'
Source code in sts_libs/src/sts/utils/string_extras.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
File Operations¶
sts.utils.files
¶
File operations module.
This module provides functionality for file system operations: - Directory validation - File counting - Path operations - Mount management - Filesystem operations
DirAccessError
¶
Bases: DirectoryError
Directory cannot be accessed.
Source code in sts_libs/src/sts/utils/files.py
47 48 |
|
DirNotFoundError
¶
Bases: DirectoryError
Directory does not exist.
Source code in sts_libs/src/sts/utils/files.py
39 40 |
|
DirTypeError
¶
Bases: DirectoryError
Path exists but is not a directory.
Source code in sts_libs/src/sts/utils/files.py
43 44 |
|
Directory
dataclass
¶
Directory representation.
Provides functionality for directory operations including: - Existence checking - File counting - Path resolution
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
Path
|
Directory path (optional, defaults to current directory) |
cwd()
|
create
|
bool
|
Create directory if it doesn't exist (optional) |
False
|
mode
|
int
|
Directory creation mode (optional) |
493
|
Example
dir = Directory() # Uses current directory
dir = Directory('/tmp/test') # Uses specific path
dir = Directory('/tmp/test', create=True) # Creates if needed
Source code in sts_libs/src/sts/utils/files.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
exists: bool
property
¶
Check if directory exists and is a directory.
__post_init__()
¶
Initialize directory.
Creates directory if needed.
Source code in sts_libs/src/sts/utils/files.py
80 81 82 83 84 85 86 87 88 89 90 |
|
count_files()
¶
Count number of files in directory.
Returns:
Type | Description |
---|---|
int
|
Number of files in directory (excluding directories) |
Raises:
Type | Description |
---|---|
DirNotFoundError
|
If directory does not exist |
DirTypeError
|
If path exists but is not a directory |
DirAccessError
|
If directory cannot be accessed |
Example
Directory('/etc').count_files()
42
Source code in sts_libs/src/sts/utils/files.py
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
iter_files()
¶
Iterate over files in directory.
Yields:
Type | Description |
---|---|
Path
|
Path objects for each file |
Raises:
Type | Description |
---|---|
DirAccessError
|
If directory cannot be accessed |
Source code in sts_libs/src/sts/utils/files.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
remove_dir()
¶
Remove directory and all its contents using shutil.rmtree.
Raises:
Type | Description |
---|---|
DirNotFoundError
|
If directory does not exist |
DirTypeError
|
If path exists but is not a directory |
DirAccessError
|
If directory cannot be accessed |
Example
Directory(Path('/tmp/test')).remove_dir()
Source code in sts_libs/src/sts/utils/files.py
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
remove_file(file)
staticmethod
¶
Remove file safely.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
Path
|
File to remove |
required |
Source code in sts_libs/src/sts/utils/files.py
165 166 167 168 169 170 171 172 173 174 175 |
|
rm_files_containing(pattern, *, invert=False)
¶
Delete files containing (or not containing) specific pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str
|
Pattern to match in file contents |
required |
invert
|
bool
|
Delete files NOT containing pattern |
False
|
Raises:
Type | Description |
---|---|
DirNotFoundError
|
If directory does not exist |
DirTypeError
|
If path exists but is not a directory |
DirAccessError
|
If directory cannot be accessed |
Example
Directory('/tmp').rm_files_containing('error') # Remove files containing 'error'
Directory('/tmp').rm_files_containing('error', invert=True) # Remove files NOT containing 'error'
Source code in sts_libs/src/sts/utils/files.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
|
should_remove_file_with_pattern(file, pattern)
staticmethod
¶
Check if file should be removed because it contains pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
Path
|
File to check |
required |
pattern
|
str
|
Pattern to match in file contents |
required |
Returns:
Type | Description |
---|---|
bool
|
True if file contains pattern and should be removed |
Source code in sts_libs/src/sts/utils/files.py
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
should_remove_file_without_pattern(file, pattern)
staticmethod
¶
Check if file should be removed because it does not contain pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
Path
|
File to check |
required |
pattern
|
str
|
Pattern to match in file contents |
required |
Returns:
Type | Description |
---|---|
bool
|
True if file does not contain pattern and should be removed |
Source code in sts_libs/src/sts/utils/files.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
|
validate()
¶
Validate directory exists and is accessible.
Raises:
Type | Description |
---|---|
DirNotFoundError
|
If directory does not exist |
DirTypeError
|
If path exists but is not a directory |
Source code in sts_libs/src/sts/utils/files.py
97 98 99 100 101 102 103 104 105 106 107 |
|
DirectoryError
¶
Bases: STSError
Base class for directory-related errors.
Source code in sts_libs/src/sts/utils/files.py
35 36 |
|
count_files(directory=None)
¶
Count number of files in directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str | Path | None
|
Path to directory to count files in (optional) |
None
|
Returns:
Type | Description |
---|---|
int
|
Number of files in directory (excluding directories) |
Raises:
Type | Description |
---|---|
DirNotFoundError
|
If directory does not exist |
DirTypeError
|
If path exists but is not a directory |
DirAccessError
|
If directory cannot be accessed |
Example
count_files() # Count files in current directory
count_files('/etc') # Count files in specific directory
Source code in sts_libs/src/sts/utils/files.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
|
get_free_space(path=None)
¶
Get free space in bytes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | Path | None
|
Path to check free space for (optional) |
None
|
Returns:
Type | Description |
---|---|
int | None
|
Free space in bytes or None if error |
Example
get_free_space() # Check current directory
get_free_space('/mnt') # Check specific path
Source code in sts_libs/src/sts/utils/files.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
|
is_mounted(device=None, mountpoint=None)
¶
Check if device or mountpoint is mounted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str | None
|
Device to check (optional) |
None
|
mountpoint
|
str | None
|
Mountpoint to check (optional) |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if mounted, False otherwise |
Example
is_mounted(device='/dev/sda1')
True
is_mounted(mountpoint='/mnt')
False
Source code in sts_libs/src/sts/utils/files.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
|
mkfs(device=None, fs_type=None, *, force=False)
¶
Create filesystem on device.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str | None
|
Device to create filesystem on (optional) |
None
|
fs_type
|
str | None
|
Filesystem type (optional) |
None
|
force
|
bool
|
Force creation even if filesystem exists (optional) |
False
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
mkfs('/dev/sda1', 'ext4') # Create ext4 filesystem
mkfs('/dev/sda1', 'ext4', force=True) # Force creation
Source code in sts_libs/src/sts/utils/files.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 |
|
mount(device=None, mountpoint=None, fs_type=None, options=None)
¶
Mount device at mountpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str | None
|
Device to mount (optional) |
None
|
mountpoint
|
str | None
|
Mountpoint to mount at (optional) |
None
|
fs_type
|
str | None
|
Filesystem type (optional) |
None
|
options
|
str | None
|
Mount options (optional) |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
mount('/dev/sda1', '/mnt') # Basic mount
mount('/dev/sda1', '/mnt', 'ext4', 'ro') # Mount with options
Source code in sts_libs/src/sts/utils/files.py
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
rm_files_containing(directory=None, pattern='', *, invert=False)
¶
Delete files containing (or not containing) specific pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
directory
|
str | Path | None
|
Directory to search in (optional) |
None
|
pattern
|
str
|
Pattern to match in file contents (optional) |
''
|
invert
|
bool
|
Delete files NOT containing pattern (optional) |
False
|
Raises:
Type | Description |
---|---|
DirNotFoundError
|
If directory does not exist |
DirTypeError
|
If path exists but is not a directory |
DirAccessError
|
If directory cannot be accessed |
Example
rm_files_containing() # Remove all files in current directory
rm_files_containing('/tmp', 'error') # Remove files containing 'error'
rm_files_containing('/tmp', 'error', invert=True) # Remove files NOT containing 'error'
Source code in sts_libs/src/sts/utils/files.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
|
umount(device=None, mountpoint=None)
¶
Unmount device or mountpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
device
|
str | None
|
Device to unmount (optional) |
None
|
mountpoint
|
str | None
|
Mountpoint to unmount (optional) |
None
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
umount('/dev/sda1') # Unmount device
umount(mountpoint='/mnt') # Unmount mountpoint
Source code in sts_libs/src/sts/utils/files.py
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|
Process Management¶
sts.utils.processes
¶
Process management.
This module provides functionality for managing system processes: - Process information - Process control - Process monitoring
ProcessInfo
dataclass
¶
Process information.
This class provides functionality for managing process information: - Process metadata - Process status - Process control
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int | None
|
Process ID (optional, discovered from name) |
None
|
name
|
str | None
|
Process name (optional, discovered from pid) |
None
|
status
|
str | None
|
Process status (optional, discovered from process) |
None
|
cmdline
|
str | None
|
Process command line (optional, discovered from process) |
None
|
Example
info = ProcessInfo() # Discovers first available process
info = ProcessInfo(pid=1234) # Discovers other values
info = ProcessInfo(name='sleep') # Discovers matching process
Source code in sts_libs/src/sts/utils/processes.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
exists: bool
property
¶
Check if process exists.
Returns:
Type | Description |
---|---|
bool
|
True if exists, False otherwise |
Example
info = ProcessInfo(pid=1234)
info.exists
True
running: bool
property
¶
Check if process is running.
Returns:
Type | Description |
---|---|
bool
|
True if running, False otherwise |
Example
info = ProcessInfo(pid=1234)
info.running
True
__post_init__()
¶
Initialize process information.
Discovers process information based on provided parameters.
Source code in sts_libs/src/sts/utils/processes.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
from_name(name)
classmethod
¶
Get process information by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Process name |
required |
Returns:
Type | Description |
---|---|
ProcessInfo | None
|
Process information or None if not found |
Example
info = ProcessInfo.from_name('sleep')
info.pid
1234
Source code in sts_libs/src/sts/utils/processes.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
from_pid(pid)
classmethod
¶
Get process information by PID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pid
|
int
|
Process ID |
required |
Returns:
Type | Description |
---|---|
ProcessInfo | None
|
Process information or None if not found |
Example
info = ProcessInfo.from_pid(1234)
info.status
'running'
Source code in sts_libs/src/sts/utils/processes.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
|
kill(timeout=1.0)
¶
Kill process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
timeout
|
float
|
Timeout in seconds between SIGTERM and SIGKILL |
1.0
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
info = ProcessInfo(pid=1234)
info.kill()
True
Source code in sts_libs/src/sts/utils/processes.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
ProcessManager
¶
Process manager functionality.
This class provides functionality for managing system processes: - Process control - Process monitoring - Process information
Example
pm = ProcessManager()
pm.kill_all('sleep')
True
Source code in sts_libs/src/sts/utils/processes.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
__init__()
¶
Initialize process manager.
Source code in sts_libs/src/sts/utils/processes.py
264 265 266 |
|
get_all()
¶
Get list of all processes.
Returns:
Type | Description |
---|---|
list[ProcessInfo]
|
List of process information |
Example
pm = ProcessManager()
pm.get_all()
[ProcessInfo(pid=1, ...), ProcessInfo(pid=2, ...)]
Source code in sts_libs/src/sts/utils/processes.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
|
get_by_name(name)
¶
Get processes by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Process name |
required |
Returns:
Type | Description |
---|---|
list[ProcessInfo]
|
List of matching processes |
Example
pm = ProcessManager()
pm.get_by_name('sleep')
[ProcessInfo(pid=1234, name='sleep', ...)]
Source code in sts_libs/src/sts/utils/processes.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
kill_all(name, timeout=1.0)
¶
Kill all processes by name.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Process name |
required |
timeout
|
float
|
Timeout in seconds between SIGTERM and SIGKILL |
1.0
|
Returns:
Type | Description |
---|---|
bool
|
True if successful, False otherwise |
Example
pm = ProcessManager()
pm.kill_all('sleep')
True
Source code in sts_libs/src/sts/utils/processes.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 |
|
System Checks¶
sts.utils.syscheck
¶
System state checker.
This module provides functionality to check system state: - Kernel state (tainted, dumps) - System logs (dmesg, messages) - Crash dumps (kdump, abrt)
abrt_check()
¶
Check if abrtd found any issues.
Returns:
Type | Description |
---|---|
bool
|
True if no errors found, False otherwise |
Example
abrt_check()
True
Source code in sts_libs/src/sts/utils/syscheck.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
|
check_all()
¶
Check for errors on the system.
Returns:
Type | Description |
---|---|
bool
|
True if no errors found, False otherwise |
Example
check_all()
True
Source code in sts_libs/src/sts/utils/syscheck.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
dmesg_check()
¶
Check for errors in dmesg.
Returns:
Type | Description |
---|---|
bool
|
True if no errors found, False otherwise |
Example
dmesg_check()
True
Source code in sts_libs/src/sts/utils/syscheck.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 |
|
kdump_check()
¶
Check for kdump crash files.
Returns:
Type | Description |
---|---|
bool
|
True if no crashes found, False otherwise |
Example
kdump_check()
True
Source code in sts_libs/src/sts/utils/syscheck.py
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
|
kernel_check()
¶
Check if kernel is tainted.
Returns:
Type | Description |
---|---|
bool
|
True if kernel is not tainted, False otherwise |
Example
kernel_check()
True
Source code in sts_libs/src/sts/utils/syscheck.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
messages_dump_check()
¶
Check for kernel dumps in system messages.
Returns:
Type | Description |
---|---|
bool
|
True if no dumps found, False otherwise |
Example
messages_dump_check()
True
Source code in sts_libs/src/sts/utils/syscheck.py
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
|
Host Information¶
sts.utils.host
¶
Host management utilities.
This module provides functionality for managing the test host: - Host initialization - Package installation - Command execution
host_init()
¶
Initialize testinfra host with local backend.
Returns:
Type | Description |
---|---|
Host
|
Host instance configured for local backend |
Example
host = host_init()
host.exists('rpm')
True
Source code in sts_libs/src/sts/utils/host.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
|
Error Handling¶
sts.utils.errors
¶
Storage Test Suite error classes.
This module provides a hierarchy of error classes used across the project:
Base Classes: - STSError: Base class for all STS exceptions - DeviceError: Base class for device-related errors - ModuleError: Base class for kernel module errors - PackageError: Base class for package-related errors - SysError: Base class for system-related errors
Device Errors: - DeviceNotFoundError: Device does not exist - DeviceTypeError: Device is not of expected type
Module Errors: - ModuleLoadError: Failed to load kernel module - ModuleUnloadError: Failed to unload kernel module - ModuleInUseError: Module cannot be unloaded because it is in use
Package Errors: - PackageNotFoundError: Package does not exist - PackageInstallError: Failed to install package
System Errors: - SysNotSupportedError: Operation not supported on this system
DeviceError
¶
Bases: STSError
Base class for device-related errors.
Source code in sts_libs/src/sts/utils/errors.py
38 39 |
|
DeviceNotFoundError
¶
Bases: DeviceError
Device does not exist.
Source code in sts_libs/src/sts/utils/errors.py
42 43 |
|
DeviceTypeError
¶
Bases: DeviceError
Device is not of expected type.
Source code in sts_libs/src/sts/utils/errors.py
46 47 |
|
ModuleError
¶
Bases: STSError
Base class for kernel module errors.
Source code in sts_libs/src/sts/utils/errors.py
50 51 |
|
ModuleInUseError
¶
Bases: ModuleError
Module cannot be unloaded because it is in use.
Source code in sts_libs/src/sts/utils/errors.py
62 63 |
|
ModuleLoadError
¶
Bases: ModuleError
Failed to load kernel module.
Source code in sts_libs/src/sts/utils/errors.py
54 55 |
|
ModuleUnloadError
¶
Bases: ModuleError
Failed to unload kernel module.
Source code in sts_libs/src/sts/utils/errors.py
58 59 |
|
PackageError
¶
Bases: STSError
Base class for package-related errors.
Source code in sts_libs/src/sts/utils/errors.py
66 67 |
|
PackageInstallError
¶
Bases: PackageError
Failed to install package.
Source code in sts_libs/src/sts/utils/errors.py
74 75 |
|
PackageNotFoundError
¶
Bases: PackageError
Package does not exist.
Source code in sts_libs/src/sts/utils/errors.py
70 71 |
|
STSError
¶
Bases: Exception
Base class for all STS exceptions.
Source code in sts_libs/src/sts/utils/errors.py
34 35 |
|
TMT Integration¶
sts.utils.tmt
¶
Test Management Tool utilities.
This module provides functionality for working with tmt: - Test result management - Log gathering - Duration calculation - Custom result submission
For more information about tmt, see: https://tmt.readthedocs.io/en/stable/spec/tests.html#result https://tmt.readthedocs.io/en/stable/spec/plans.html#spec-plans-results
CustomResults
¶
Bases: TypedDict
Custom test results type.
Attributes:
Name | Type | Description |
---|---|---|
name |
str
|
Result name (e.g. "/step-1" or "/setup/iscsi/target") |
result |
TmtResult
|
Test result |
note |
str | None
|
Additional notes |
log |
list[str] | None
|
Paths to log files |
serialnumber |
int | None
|
Serial number in test sequence |
guest |
GuestType | None
|
Guest information |
duration |
str | None
|
Test duration |
ids |
dict[str, str] | None
|
Additional identifiers |
Source code in sts_libs/src/sts/utils/tmt.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
GuestType
¶
Bases: TypedDict
Guest information type.
Attributes:
Name | Type | Description |
---|---|---|
name |
str | None
|
Guest name |
role |
str | None
|
Guest role |
Source code in sts_libs/src/sts/utils/tmt.py
105 106 107 108 109 110 111 112 113 114 |
|
Results
¶
TMT test results management.
This class provides functionality for managing TMT test results: - Adding test results - Managing logs - Calculating durations - Submitting results
Example
results = Results()
results.add(name='setup', result='pass')
results.add(name='test', result='pass', log=['test.log'])
results.submit()
Source code in sts_libs/src/sts/utils/tmt.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
__init__()
¶
Initialize results manager.
Source code in sts_libs/src/sts/utils/tmt.py
181 182 183 184 |
|
add(name='/', result='pass', note=None, log=None, errors=None)
¶
Add test result.
When TMT plan is set to 'result: custom', use this followed by submit() to create the necessary result.json. Use multiple times when test has distinctive steps (parts).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
Result name (e.g. '/setup/something' or 'setup') |
'/'
|
result
|
TmtResult
|
Test result |
'pass'
|
note
|
str | None
|
Additional notes |
None
|
log
|
list[str] | None
|
Paths to log files (relative to TMT_TEST_DATA) |
None
|
errors
|
list[str] | None
|
Error messages (sets result to 'fail' if present) |
None
|
Example
results = Results()
results.add(
name='setup',
result='pass',
log=['setup.log'],
)
Source code in sts_libs/src/sts/utils/tmt.py
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
|
submit()
¶
Submit test results.
Creates results.json file in TMT_TEST_DATA directory.
Example
results = Results()
results.add(name='test', result='pass')
results.submit() # Creates results.json
Source code in sts_libs/src/sts/utils/tmt.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
calculate_duration(start, end)
¶
Calculate duration between timestamps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start
|
float
|
Start timestamp |
required |
end
|
float
|
End timestamp |
required |
Returns:
Type | Description |
---|---|
str
|
Duration in hh:mm:ss format |
Example
calculate_duration(1677721600.0, 1677725200.0)
'01:00:00'
Source code in sts_libs/src/sts/utils/tmt.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
gather_logs_from_dir(logs_path, name)
¶
Gather logs from directory into a tarfile.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
logs_path
|
str
|
Path to directory containing logs |
required |
name
|
str | None
|
Name for the tarfile (optional) |
required |
Returns:
Type | Description |
---|---|
Path | None
|
Path to created tarfile or None if directory doesn't exist |
Example
gather_logs_from_dir('/var/log/messages', 'system_logs')
Path('/var/tmp/uuid/system_logs.tar')
Source code in sts_libs/src/sts/utils/tmt.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
remove_nones(cr)
¶
Remove None values from custom results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cr
|
CustomResults
|
Custom results dictionary |
required |
Returns:
Type | Description |
---|---|
dict[str, Any]
|
Dictionary with None values removed |
Example
remove_nones({'name': 'test', 'note': None, 'result': 'pass'})
{'name': 'test', 'result': 'pass'}
Source code in sts_libs/src/sts/utils/tmt.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
|
timestamp()
¶
Get current timestamp.
Returns:
Type | Description |
---|---|
float
|
Current time in seconds since epoch |
Example
timestamp()
1677721600.0
Source code in sts_libs/src/sts/utils/tmt.py
70 71 72 73 74 75 76 77 78 79 80 81 82 |
|