|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +import re |
| 6 | +import sys |
| 7 | +from typing import List, Tuple |
| 8 | + |
| 9 | + |
| 10 | +def main() -> None: |
| 11 | + """ |
| 12 | + Process video recording configuration based on session capabilities. |
| 13 | +
|
| 14 | + Args: |
| 15 | + sys.argv[1]: SESSION_ID |
| 16 | + sys.argv[2]: SESSION_CAPABILITIES (JSON string) |
| 17 | +
|
| 18 | + Outputs: |
| 19 | + Space-separated values: RECORD_VIDEO TEST_NAME |
| 20 | + """ |
| 21 | + # Define parameters |
| 22 | + session_id = sys.argv[1] if len(sys.argv) > 1 else "" |
| 23 | + session_capabilities = sys.argv[2] if len(sys.argv) > 2 else "" |
| 24 | + |
| 25 | + # Environment variables with defaults |
| 26 | + video_cap_name = os.environ.get("VIDEO_CAP_NAME", "se:recordVideo") |
| 27 | + test_name_cap = os.environ.get("TEST_NAME_CAP", "se:name") |
| 28 | + video_name_cap = os.environ.get("VIDEO_NAME_CAP", "se:videoName") |
| 29 | + video_file_name_trim = os.environ.get("SE_VIDEO_FILE_NAME_TRIM_REGEX", "[:alnum:]-_") |
| 30 | + video_file_name_suffix = os.environ.get("SE_VIDEO_FILE_NAME_SUFFIX", "true") |
| 31 | + |
| 32 | + # Initialize variables |
| 33 | + record_video = None |
| 34 | + test_name = None |
| 35 | + video_name = None |
| 36 | + |
| 37 | + # Extract values from session capabilities if provided |
| 38 | + if session_capabilities: |
| 39 | + try: |
| 40 | + capabilities = json.loads(session_capabilities) |
| 41 | + record_video = capabilities.get(video_cap_name) |
| 42 | + test_name = capabilities.get(test_name_cap) |
| 43 | + video_name = capabilities.get(video_name_cap) |
| 44 | + except (json.JSONDecodeError, AttributeError): |
| 45 | + # If JSON parsing fails, continue with None values |
| 46 | + pass |
| 47 | + |
| 48 | + # Check if enabling to record video |
| 49 | + if (isinstance(record_video, str) and record_video.lower() == "false") or record_video is False: |
| 50 | + record_video = "false" |
| 51 | + else: |
| 52 | + record_video = "true" |
| 53 | + |
| 54 | + # Check if video file name is set via capabilities |
| 55 | + if video_name and video_name != "null": |
| 56 | + test_name = video_name |
| 57 | + elif test_name and test_name != "null": |
| 58 | + test_name = test_name |
| 59 | + else: |
| 60 | + test_name = "" |
| 61 | + |
| 62 | + # Check if append session ID to the video file name suffix |
| 63 | + if not test_name: |
| 64 | + test_name = session_id |
| 65 | + elif video_file_name_suffix.lower() == "true": |
| 66 | + test_name = f"{test_name}_{session_id}" |
| 67 | + |
| 68 | + # Normalize the video file name |
| 69 | + test_name = normalize_filename(test_name, video_file_name_trim) |
| 70 | + |
| 71 | + # Output the values for other scripts consuming |
| 72 | + print(f"{record_video} {test_name}") |
| 73 | + |
| 74 | + |
| 75 | +def normalize_filename(filename: str, trim_pattern: str) -> str: |
| 76 | + """ |
| 77 | + Normalize the filename by replacing spaces with underscores, |
| 78 | + keeping only allowed characters, and truncating to 251 characters. |
| 79 | +
|
| 80 | + Args: |
| 81 | + filename: The original filename |
| 82 | + trim_pattern: Pattern defining allowed characters (e.g., "[:alnum:]-_") |
| 83 | +
|
| 84 | + Returns: |
| 85 | + Normalized filename |
| 86 | + """ |
| 87 | + if not filename: |
| 88 | + return "" |
| 89 | + |
| 90 | + # Replace spaces with underscores |
| 91 | + normalized = filename.replace(" ", "_") |
| 92 | + |
| 93 | + # Convert trim pattern to regex |
| 94 | + # Handle character classes like [:alnum:] |
| 95 | + posix_classes = { |
| 96 | + "[:alnum:]": "a-zA-Z0-9", |
| 97 | + "[:alpha:]": "a-zA-Z", |
| 98 | + "[:digit:]": "0-9", |
| 99 | + "[:space:]": " \t\n\r\f\v" |
| 100 | + } |
| 101 | + |
| 102 | + allowed_chars = trim_pattern |
| 103 | + for posix_class, replacement in posix_classes.items(): |
| 104 | + if posix_class in allowed_chars: |
| 105 | + allowed_chars = allowed_chars.replace(posix_class, replacement) |
| 106 | + |
| 107 | + pattern = f"[^{re.escape(allowed_chars)}]" |
| 108 | + |
| 109 | + # Remove disallowed characters |
| 110 | + normalized = re.sub(pattern, "", normalized) |
| 111 | + |
| 112 | + # Truncate to 251 characters |
| 113 | + return normalized[:251] |
| 114 | + |
| 115 | + |
| 116 | +if __name__ == "__main__": |
| 117 | + main() |
0 commit comments