def is_portage_running() -> bool: """ Check if potrage command in currently running. :return: True if it is running, False otherwise """ out, _ = utils.run_cmd('pgrep -f /usr/bin/emerge') return bool(out)
def emerge(arguments: List[str], verbose: bool, build=True) -> bytes: """ Run emerge command. :param arguments: :param verbose: :param build: :return: """ if verbose: info(f"running emerge with: {' '.join(arguments)}") cmd = f"sudo /usr/bin/emerge --nospinner {' '.join(arguments)}" if build: return_code, _ = utils.run_cmd(cmd, use_system=True) return return_code output, _ = utils.run_cmd(cmd) return output
def emerge(arguments: List[str], build=True) -> Tuple[bytes, bytes]: """ Run emerge command. :param arguments: :param build: :return: """ info(f"running emerge with: {' '.join(arguments)}") cmd = f"sudo /usr/bin/emerge --nospinner {' '.join(arguments)}" if build: return_code, stderr = utils.run_cmd(cmd, use_system=True) debug( f'RC: {return_code.decode("utf-8")}, stderr: {stderr.decode("utf-8")}' ) return return_code, stderr output, stderr = utils.run_cmd(cmd) return output, stderr
def test_run_cmd_as_subprocess_ver2(): from pyerge import utils from subprocess import Popen with mock.patch.object(Popen, 'communicate') as communicate_mock: out = b'Filesystem 1K-blocks Used Available Use% Mounted on\n' \ b'/dev/sda2 126931 76647 43731 64% /boot\n' err = b'' communicate_mock.return_value = (out, err) assert utils.run_cmd(cmd='df') == (out, err) communicate_mock.assert_called_once_with()
def test_run_cmd_as_subprocess_ver1(): from pyerge import utils with mock.patch('pyerge.utils.Popen') as popen_mock: process_mock = mock.Mock() out = b'Filesystem 1K-blocks Used Available Use% Mounted on\n' \ b'/dev/sda2 126931 76647 43731 64% /boot\n' err = b'' attrs = {'communicate.return_value': (out, err)} process_mock.configure_mock(**attrs) popen_mock.return_value = process_mock assert utils.run_cmd(cmd='df') == (out, err) popen_mock.assert_called_once_with(['df'], stderr=-1, stdout=-1)
def check_upd(local_chk: bool) -> None: """ Check system updates. :param local_chk: """ utils.delete_content(TMPLOGFILE) utils.delete_content(TMERGE_LOGFILE) with open(file=TMPLOGFILE, mode='w', encoding='utf-8') as tmp, open(file=TMERGE_LOGFILE, mode='w', encoding='utf-8') as log: tmp.write(strftime('%a %b %d %H:%M:%S %Z %Y') + '\n') if not local_chk: info('Start syncing portage...') debug(f'sudo eix-sync >> {TMPLOGFILE} > {DEVNULL}') utils.run_cmd(f'sudo eix-sync >> {TMPLOGFILE} > {DEVNULL}', use_system=True) info('Checking updates...') output, error = emerge( '-pvNDu --color n --with-bdeps=y @world'.split(), build=False) debug(f'stderr: {error.decode("utf-8")}') log.write(output.decode('utf-8')) log.write(error.decode('utf-8')) info('Creating log file...') debug(f'cat {TMERGE_LOGFILE} >> {TMPLOGFILE}') utils.run_cmd(f'cat {TMERGE_LOGFILE} >> {TMPLOGFILE}', use_system=True) debug(f'cat {TMERGE_LOGFILE} | genlop -pn >> {TMPLOGFILE}') utils.run_cmd(f'cat {TMERGE_LOGFILE} | genlop -pn >> {TMPLOGFILE}', use_system=True)
def glsa_test(elements: int) -> str: """ Test system against GLSAs. :param elements: number of elements :return: string with result """ glsalist = ' '.join( _rss(regex=r'GLSA\s(\d{6}-\d{2}):\s.*', elements=elements)) out, err = utils.run_cmd(f'glsa-check -t {glsalist}') if err == b'This system is not affected by any of the listed GLSAs\n': return 'System is not affected by any of listed GLSAs' return out.decode('utf-8').strip().replace('\n', ',')
def e_eta() -> str: """Get estimated time of compilation of current package.""" output, _ = run_cmd('genlop -cn') out = output.decode('utf-8') eta = '' if search(r'Error.*no working merge found', out): eta = 'Unknown' reqex = search(r'ETA:\s(.*)\.', out) if reqex is not None: eta = reqex.group(1) print(eta) return eta
def e_raid(raid_id: str) -> str: """ Check of Raid array. :param raid_id: name i.e. md126 or md127 :return: status of RAID """ raid = 'Unknown' out, _ = run_cmd('cat /proc/mdstat') out = out.decode('utf-8') # type: ignore reqex = search(rf'{raid_id}.*\n.*(\[[U_]*\])', out) # type: ignore if reqex is not None: raid = reqex.group(1) # type: ignore return raid
def check_upd(local_chk: bool, verbose: bool) -> None: """ Check system updates. :param local_chk: :param verbose: """ utils.delete_content(tmplogfile) utils.delete_content(tmerge_logfile) tmp = open(tmplogfile, 'w') log = open(tmerge_logfile, 'w') tmp.write(strftime('%a %b %d %H:%M:%S %Z %Y') + '\n') if not local_chk: if verbose: # info('Start syncing overlays...') # utils.run_cmd(f'sudo layman -SN >> {tmplogfile} > {dev_null}', use_subproc=False) info('Start syncing portage...') if verbose > 1: debug(f'sudo eix-sync >> {tmplogfile} > {dev_null}') utils.run_cmd(f'sudo eix-sync >> {tmplogfile} > {dev_null}', use_system=True) if verbose: info('Checking updates...') output = emerge('-pvNDu --color n @world'.split(), verbose, build=False) log.write(output.decode(encoding='utf-8')) tmp.close() log.close() if verbose: info('Creating log file...') if verbose > 1: debug(f'cat {tmerge_logfile} >> {tmplogfile}') utils.run_cmd(f'cat {tmerge_logfile} >> {tmplogfile}', use_system=True) if verbose > 1: debug(f'cat {tmerge_logfile} | genlop -pn >> {tmplogfile}') utils.run_cmd(f'cat {tmerge_logfile} | genlop -pn >> {tmplogfile}', use_system=True)
def test_run_cmd_as_sysyem(): from pyerge import utils with mock.patch('pyerge.utils.system') as system_mock: system_mock.return_value = 0 assert utils.run_cmd(cmd='df', use_system=True) == (b'0', b'') system_mock.assert_called_once_with('df')
#!/usr/bin/python3.6 """Various tools to emerge and to show status for conky.""" from re import search from pyerge import utils __version__ = '0.3' if __name__ == '__main__': output, _ = utils.run_cmd('genlop -cn') if search(r'Error.*no working merge found', output.decode()): print('Unknown') exit() match = search(r'ETA:\s(.*)\.', output.decode()) if match: print(match.group(1))