def _get_time_of_day_greeting(): os_name = _get_os_name() user_name = _get_user_first_name() greeting = _get_greeting(user_name) assert os_name, app_settings.assertion( 'unable to retrieve the operating system version details') return f'{greeting} Welcome to {os_name}'
def _get_last_login(): output = _run_external_command('last -1') assert output, app_settings.assertion( 'system command "last" did not return anything') for line in output.splitlines(): line = line.strip() parts = line.split() #print(f'last -1 line: {line}, {len(parts)} parts') # if the list of parts is not 9-10 items long, now linux last is 11 items long specifying current process name #'jasoni pts/1 192.168.1.130 vi Sat Oct 24 01:52 still logged in' #'jasoni', 'pts/1', '192.168.1.130', 'vi', 'Sat', 'Oct', '24', '01:52', 'still', 'logged', 'in' #0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 #-11 , -10 , -9 , -8 , -7 , -6 , -5 , -4 , -3 , -2 , -1 if not (9 <= len(parts) <= 11): continue user = parts[0] terminal = parts[1] host = None if len(parts) >= 10: host = parts[2] login_time = _convert_yearless_timestamp(' '.join(parts[-7:-3])) login_time_str = _convert_date_time(login_time) if line.endswith('still logged in'): logout_time_str = 'still logged in' else: match = re.match('^(?P<hour>\d\d):(?P<minute>\d\d)$', parts[-2]) assert match, app_settings.assertion( 'the last command did not return the duration of the last login' ) hour = int(match.group('hour')) minute = int(match.group('minute')) logout_time_str = _convert_date_time( _convert_time_duration(login_time, hour, minute)) last_login = [] if host: last_login.append(f'{user} logged into {terminal} from {host}') else: last_login.append(f'{user} logged into {terminal}') last_login.append(f'Log in: {login_time_str}') last_login.append(f'Log out: {logout_time_str}') return last_login return []
def _get_time_of_day(): hour = _get_now().hour if hour >= NIGHT_HOUR or hour < MORNING_HOUR: return NIGHT_LABEL elif hour >= EVENING_HOUR: return EVENING_LABEL elif hour >= AFTERNOON_HOUR: return AFTERNOON_LABEL else: assert hour >= MORNING_HOUR and hour < AFTERNOON_HOUR, app_settings.assertion( f'hour "{hour}" is out of the 0-23 range') return MORNING_LABEL
def _get_root_partition_usage(): partitions = [ partition for partition in psutil.disk_partitions() if partition.mountpoint == '/' ] assert len(partitions) == 1, app_settings.assertion( f'found {len(partitions)} partitions with the "/" mount point') partition = partitions[0] usage = psutil.disk_usage(partition.mountpoint) total_amount = bytes2human(usage.total) used_percent = f'{usage.percent / 100.0:.1%}' return f'{used_percent} of {total_amount}'
def _get_macosx_name(): output = _run_external_command('sw_vers') assert output, app_settings.assertion( 'sw_vers command did not return any information') version = build = None for line in output.splitlines(): if (match := re.match('^ProductVersion:\s+(?P<product_version>.*)$', line)): version = match.group('product_version') if (match := re.match('^BuildVersion:\s+(?P<build_version>.*)$', line)): build = match.group('build_version')
def _get_linux_available_mail(): cmd = f'pam_tally --file /var/mail/{os.getlogin()} --user {os.getlogin()}' output = _run_external_command(cmd) if not output: app_settings.verbose( f'pam_tally did not return any info -- possible /var/mail/{os.getlogin()} doesn\'t exist' ) return 0 match = re.match( fr'^User\s+{os.getlogin()}\s*\(\d+\)\s*has\s*(?P<mail>\d+)$', output.strip()) assert match, app_settings.assertion( 'definition of pam_tally output has changed') return int(match.group('mail'))
def _get_time_of_day_emoji(): label = _get_time_of_day() assert label in TIME_OF_DAY, app_settings.assertion( f'unknown label "{label}" found for time of day') return TIME_OF_DAY[label]
return MacOsVersionNames().get_version(self._version) def _get_macosx_name(): output = _run_external_command('sw_vers') assert output, app_settings.assertion( 'sw_vers command did not return any information') version = build = None for line in output.splitlines(): if (match := re.match('^ProductVersion:\s+(?P<product_version>.*)$', line)): version = match.group('product_version') if (match := re.match('^BuildVersion:\s+(?P<build_version>.*)$', line)): build = match.group('build_version') assert version and build, app_settings.assertion( 'sw_vers is no longer giving the ProductVersion and BuildVersion') utsname = platform.uname() version_details = f'({utsname.system} {utsname.release} {utsname.machine})' return f'{MacOsVersionName(version)} {version}.{build} {version_details}' def _get_linux_name(): name = version = None with open('/etc/os-release') as release_info: text = release_info.read() for line in text.splitlines(): match = re.match(r'^PRETTY_NAME="(?P<name>[^\"]+)"$', line.strip()) if match: name = match.group('name') match = re.match(r'^UBUNTU_CODENAME=(?P<ver>[^\s]+)$', line.strip()) if match: