-
Notifications
You must be signed in to change notification settings - Fork 63
CM-67459: Enrich session context payload #477
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RoniCycode
wants to merge
5
commits into
main
Choose a base branch
from
CM-67459-enrich-session-context-payload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+159
−24
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import getpass | ||
| import platform | ||
| import re | ||
| import socket | ||
| import subprocess | ||
| from typing import Optional | ||
|
|
||
| from cycode.logger import get_logger | ||
|
|
||
| logger = get_logger('HOST INFO') | ||
|
|
||
| _SUBPROCESS_TIMEOUT_SEC = 5 | ||
|
|
||
| _PLATFORM_NAMES = {'Darwin': 'macOS', 'Windows': 'Windows', 'Linux': 'Linux'} | ||
|
|
||
|
|
||
| def _run(command: list, timeout: int = _SUBPROCESS_TIMEOUT_SEC) -> Optional[str]: | ||
| """Run a command and return its stripped stdout. Never raises; returns None on any error.""" | ||
| try: | ||
| result = subprocess.run(command, capture_output=True, text=True, timeout=timeout) # noqa: S603 | ||
| return result.stdout.strip() or None | ||
| except Exception as e: | ||
| logger.debug('Failed to run command %s', command, exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def _read_text_file(path: str) -> Optional[str]: | ||
| """Read and strip a text file. Never raises; returns None if it can't be read.""" | ||
| try: | ||
| with open(path) as text_file: | ||
| return text_file.read().strip() or None | ||
| except OSError: | ||
| return None | ||
|
|
||
|
|
||
| def get_hostname() -> Optional[str]: | ||
| try: | ||
| return socket.gethostname() or None | ||
| except Exception as e: | ||
| logger.debug('Failed to resolve hostname', exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def get_platform_name() -> Optional[str]: | ||
| try: | ||
| system = platform.system() | ||
| return _PLATFORM_NAMES.get(system, system or None) | ||
| except Exception as e: | ||
| logger.debug('Failed to resolve platform name', exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def get_os_version() -> Optional[str]: | ||
| try: | ||
| system = platform.system() | ||
| if system == 'Darwin': | ||
| return platform.mac_ver()[0] or None | ||
| if system == 'Windows': | ||
| return platform.win32_ver()[1] or platform.version() or None | ||
| if system == 'Linux': | ||
| return _get_linux_os_version() | ||
| return platform.release() or None | ||
| except Exception as e: | ||
| logger.debug('Failed to resolve OS version', exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def _get_linux_os_version() -> Optional[str]: | ||
| freedesktop_os_release = getattr(platform, 'freedesktop_os_release', None) # Python 3.10+ | ||
| if freedesktop_os_release is not None: | ||
| try: | ||
| version_id = freedesktop_os_release().get('VERSION_ID') | ||
| if version_id: | ||
| return version_id | ||
| except OSError: | ||
| pass | ||
|
|
||
| os_release = _read_text_file('/etc/os-release') # Python 3.9 fallback: parse manually | ||
| if os_release: | ||
| for line in os_release.splitlines(): | ||
| if line.startswith('VERSION_ID='): | ||
| return line.split('=', 1)[1].strip().strip('"') or None | ||
|
|
||
| return platform.release() or None | ||
|
|
||
|
|
||
| def get_last_login_user() -> Optional[str]: | ||
| try: | ||
| return getpass.getuser() or None | ||
| except Exception as e: | ||
| logger.debug('Failed to resolve last login user', exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def get_serial_number() -> Optional[str]: | ||
| try: | ||
| system = platform.system() | ||
| if system == 'Darwin': | ||
| return _get_macos_serial_number() | ||
| if system == 'Windows': | ||
| return _get_windows_serial_number() | ||
| except Exception as e: | ||
| logger.debug('Failed to resolve serial number', exc_info=e) | ||
| return None | ||
|
|
||
|
|
||
| def _get_macos_serial_number() -> Optional[str]: | ||
| output = _run(['ioreg', '-c', 'IOPlatformExpertDevice', '-d', '2']) | ||
| if not output: | ||
| return None | ||
| match = re.search(r'"IOPlatformSerialNumber"\s*=\s*"([^"]+)"', output) | ||
| return match.group(1) if match else None | ||
|
|
||
|
|
||
| def _get_windows_serial_number() -> Optional[str]: | ||
| import pythoncom # from pywin32 | ||
| import win32com.client # from pywin32 | ||
|
|
||
| pythoncom.CoInitialize() | ||
| try: | ||
| wmi_service = win32com.client.GetObject('winmgmts:') | ||
| for bios in wmi_service.InstancesOf('Win32_BIOS'): | ||
| serial = bios.SerialNumber | ||
| return serial.strip() if serial else None | ||
| finally: | ||
| pythoncom.CoUninitialize() | ||
| return None | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did you check this is not returning root from hooks?