def _proc_logging(self, path): text = f'Listing installed {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) suffix = path.replace('/', ':') with tempfile.NamedTemporaryFile() as _temp_file: logfile = os.path.join(self._logroot, f'{self.log}-{suffix}{self.ext}') argv = [ path, 'bundle', 'dump', '--force', f'--file={_temp_file.name}' ] print_scpt(argv, self._file, redirect=self._qflag) script(argv, self._file, shell=True, timeout=self._timeout, redirect=self._vflag) with open(_temp_file.name, 'r') as file: context = file.read() print_text(context, get_logfile(), redirect=self._vflag) with open(logfile, 'w') as file: file.writelines( filter(lambda s: s.startswith('brew'), context.strip().splitlines(True))) # pylint: disable=filter-builtin-not-iterating
def wrapper(*args, **kwargs): from macdaily.util.tools.get import get_logfile, get_boolean if get_boolean('NULL_PASSWORD'): return PASS SUDO_PASSWORD = os.getenv('SUDO_PASSWORD') if SUDO_PASSWORD is None: password = func(*args, **kwargs) else: password = SUDO_PASSWORD if not get_boolean('MACDAILY_NO_CHECK'): try: subprocess.run(['sudo', '--reset-timestamp'], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) with make_pipe(password, redirect=True) as pipe: subprocess.check_call( ['sudo', '--stdin', '--prompt=""', "true"], stdin=pipe.stdout, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except subprocess.CalledProcessError: global ERR_FLAG ERR_FLAG = False print_term( f'macdaily: {red}error{reset}: incorrect password {dim}{password!r}{reset} for ' f'{bold}{USER}{reset} ({under}{USR}{reset})', get_logfile()) raise IncorrectPassword from None return password
def wrapper(*args, **kwargs): from macdaily.util.tools.get import get_logfile if sys.stdin.isatty(): # pylint: disable=no-else-return return func(*args, **kwargs) else: # timeout interval from macdaily.util.tools.get import get_int TIMEOUT = get_int('TIMEOUT', 60) QUEUE = multiprocessing.Queue(1) kwargs['queue'] = QUEUE for _ in range(3): proc = multiprocessing.Process(target=func, args=args, kwargs=kwargs) timer = threading.Timer(TIMEOUT, function=proc.kill) timer.start() proc.start() proc.join() timer.cancel() if proc.exitcode == 0: break if proc.exitcode != 9: print_term( f'macdaily: {yellow}error{reset}: function {func.__qualname__!r} ' f'exits with exit status {proc.exitcode} on child process', get_logfile()) raise ChildExit else: print_term( f'macdaily: {red}error{reset}: function {func.__qualname__!r} ' f'retry timeout after {TIMEOUT} seconds', get_logfile()) raise TimeExpired try: return QUEUE.get(block=False) except queue.Empty: return default
def dump_config(rcpath, quiet=False, verbose=False): if not sys.stdin.isatty(): raise ConfigNotFoundError(2, 'No such file or directory', rcpath) print_info(f'Creating a config file (.dailyrc) for {USER}...', get_logfile(), redirect=quiet) dskdir = input('Name of your external hard disk []: ') CONFIG[ 4] = f'dskdir = /Volumes/{dskdir.ljust(41)} ; path where your hard disk lies' askpass = launch_askpass(quiet=quiet, verbose=verbose) confirm = launch_confirm(quiet=quiet, verbose=verbose) CONFIG[ 52] = f'askpass = {askpass.ljust(49)} ; SUDO_ASKPASS utility for Homebrew Casks' CONFIG[ 53] = f'confirm = {confirm.ljust(49)} ; confirm utility for MacDaily' with open(rcpath, 'w') as file: file.write(os.linesep.join(CONFIG)) return get_config()
def wrapper(*args, **kwargs): from macdaily.util.tools.get import get_logfile if platform.system() != 'Darwin': print_term('macdaily: error: script runs only on macOS', get_logfile()) raise UnsupportedOS def _finale(epilogue): global FUNC_FLAG if FUNC_FLAG: FUNC_FLAG = False sys.stdout.write(reset) sys.stderr.write(reset) kill(os.getpid(), signal.SIGSTOP) return epilogue def _funeral(last_words): global ERR_FLAG ERR_FLAG = False # sys.tracebacklimit = 0 sys.stdout.write(reset) sys.stderr.write(reset) kill(os.getpid(), signal.SIGKILL) print(last_words, file=sys.stderr) try: return _finale(func(*args, **kwargs)) except KeyboardInterrupt: if ERR_FLAG: _funeral(f'macdaily: {red}error{reset}: operation interrupted') raise except Exception: if ERR_FLAG: _funeral(f'macdaily: {red}error{reset}: operation failed') raise
def _proc_dependency(self, path): text = f'Querying dependencies of {self.desc[1]}' print_info(text, self._file, redirect=self._qflag) def _fetch_dependency(package, depth): if depth == 0: return dict() depth -= 1 dependencies = _data_pkgs.get(package) if dependencies is not None: return dependencies text = f'Searching dependencies of {self.desc[0]} {under}{package}{reset}' print_info(text, self._file, redirect=self._vflag) argv = [path, '-m', 'pip', 'show', package] args = ' '.join(argv) print_scpt(args, self._file, redirect=self._vflag) with open(self._file, 'a') as file: file.write(f'Script started on {date()}\n') file.write(f'command: {args!r}\n') _deps_pkgs = dict() try: proc = subprocess.check_output(argv, stderr=make_stderr(self._vflag)) except subprocess.CalledProcessError: self._fail.append(package) with contextlib.suppress(KeyError): self._var__temp_pkgs.remove(package) print_text(traceback.format_exc(), self._file, redirect=self._vflag) else: context = proc.decode() print_text(context, self._file, redirect=self._vflag) requirements = set() for line in context.strip().splitlines(): match = re.match(r'Requires: (.*)', line) if match is not None: requirements = set(match.groups()[0].split(', ')) break _list_pkgs.append(package) for item in filter(None, requirements): if item in self._var__temp_pkgs: self._var__temp_pkgs.remove(item) _deps_pkgs[item] = _fetch_dependency(item, depth) _data_pkgs.update(_deps_pkgs) finally: with open(self._file, 'a') as file: file.write(f'Script done on {date()}\n') return _deps_pkgs _list_pkgs = list() _deps_pkgs = dict() _data_pkgs = dict() _temp_pkgs = copy.copy(self._var__temp_pkgs) for package in _temp_pkgs: _deps_pkgs[package] = _fetch_dependency(package, self._depth) self._pkgs.extend(_list_pkgs) def _print_dependency_tree(package, dependencies): with tempfile.NamedTemporaryFile() as dumpfile: dumper = dictdumper.Tree(dumpfile.name, quiet=True) dumper(dependencies, name=package) with open(dumpfile.name) as file: for line in filter(None, file): print_text(line.strip(), self._file) def _print_dependency_text(package, dependencies): def _list_dependency(dependencies): _list_pkgs = list() for package, deps_pkgs in dependencies.items(): _list_pkgs.append(package) _list_pkgs.extend(_list_dependency(deps_pkgs)) return _list_pkgs _list_pkgs = list() for item in reversed(_list_dependency(dependencies)): if item in _list_pkgs: continue _list_pkgs.append(item) if not self._topological: _list_pkgs.sort() if self._qflag: if _list_pkgs: print_term(f"{package}: {' '.join(_list_pkgs)}", self._file) else: print_term(f"{package} (independent)", self._file) else: print_text(os.linesep.join(_list_pkgs), self._file) text = f'Listing dependencies of {self.desc[1]}' print_info(text, self._file, redirect=self._vflag) argv = [path, 'dependency'] if self._tree: argv.append('--tree') if self._quiet: argv.append('--quiet') if self._verbose: argv.append('--verbose') if self._topological: argv.append('--topological') if self._depth != -1: argv.append(f'--depth={self._depth}') argv.append('') if self._qflag: print_scpt(path, get_logfile()) for package in sorted(self._var__temp_pkgs): argv[-1] = package print_scpt(' '.join(argv), self._file, redirect=self._qflag) if self._tree: try: import dictdumper except ImportError: print_term( f'macdaily-dependency: {yellow}pip{reset}: {bold}DictDumper{reset} not installed, ' f"which is mandatory for using `{bold}--tree{reset}' option", self._file, redirect=self._vflag) print( f'macdaily-dependency: {red}pip{reset}: broken dependency', file=sys.stderr) raise _print_dependency_tree(package, _deps_pkgs[package]) else: _print_dependency_text(package, _deps_pkgs[package]) del self._var__temp_pkgs
def _spawn(argv=SHELL, file='typescript', password=None, yes=None, redirect=False, executable=SHELL, prefix=None, suffix=None, timeout=None, shell=False): try: import ptyng except ImportError: print_term( f"macdaily: {yellow}misc{reset}: `{bold}unbuffer{reset}' and `{bold}script{reset}'" f'not found in your {under}PATH{reset}, {bold}PTYng{reset} not installed', get_logfile(), redirect=redirect) print(f'macdaily: {red}misc{reset}: broken dependency', file=sys.stderr) raise if suffix is not None: argv = f'{_merge(argv)} {suffix}' if prefix is not None: argv = f'{prefix} {_merge(argv)}' if shell or isinstance(argv, str): argv = [executable, '-c', _merge(argv)] if password is not None: bpwd = password.encode() bdim = dim.encode() repl = rb'\1' + bdim # test = bytes() def master_read_ng(fd, replace=None): data = os.read(fd, 1024).replace(b'^D\x08\x08', b'') if replace is not None: data = data.replace(replace, b'') if password is not None: data = data.replace(bpwd, PWD.pw_passwd.encode()) data = data.replace(b'Password:'******'Password:\r\n') text = re.sub(rb'\033\[[0-9][0-9;]*m', rb'', data, flags=re.IGNORECASE) typescript.write(text) byte = bdim + re.sub( rb'(\033\[[0-9][0-9;]*m)', repl, data, flags=re.IGNORECASE) # nonlocal test # test = byte return byte if yes is None: def master_read(fd): return master_read_ng(fd) def stdin_read(fd): return os.read(fd, 1024) else: if isinstance(yes, str): yes = yes.encode() txt = re.sub(rb'[\r\n]*$', rb'', yes) old = txt + b'\r\n' exp = txt + b'\n' def master_read(fd): return master_read_ng(fd, replace=old) def stdin_read(fd): # pylint: disable=unused-argument return exp with open(file, 'ab') as typescript: returncode = ptyng.spawn(argv, master_read, stdin_read, timeout=timeout, env=os.environ) # if not test.decode().endswith(os.linesep): # sys.stdout.write(os.linesep) return returncode
def logging(argv=None): # parse args & set context redirection flags args = parse_args(argv) quiet = args.quiet verbose = (args.quiet or not args.verbose) # parse config & change environ config = parse_config(quiet, verbose) os.environ['SUDO_ASKPASS'] = config['Miscellaneous']['askpass'] # fetch current time today = datetime.datetime.today() logdate = datetime.date.strftime(today, r'%y%m%d') logtime = datetime.date.strftime(today, r'%H%M%S') # prepare command paras confirm = config['Miscellaneous']['confirm'] askpass = config['Miscellaneous']['askpass'] timeout = config['Miscellaneous']['limit'] disk_dir = config['Path']['arcdir'] brew_renew = None # record program status text_record = f'{bold}{green}|🚨|{reset} {bold}Running MacDaily version {__version__}{reset}' print_term(text_record, get_logfile(), redirect=quiet) record(get_logfile(), args, today, config, redirect=verbose) # ask for password text_askpass = f'{bold}{purple}|🔑|{reset} {bold}Your {under}sudo{reset}{bold} password may be necessary{reset}' print_term(text_askpass, get_logfile(), redirect=quiet) password = get_pass(askpass) cmd_list = list() log_list = list() file_list = list() for mode in { 'apm', 'app', 'brew', 'cask', 'gem', 'mas', 'npm', 'pip', 'tap' }: # mkdir for logs logpath = pathlib.Path( os.path.join(config['Path']['logdir'], 'logging', mode, logdate)) logpath.mkdir(parents=True, exist_ok=True) filename = os.path.join(logpath, f'{logtime}-{uuid.uuid4()!s}.log') os.environ['MACDAILY_LOGFILE'] = filename # redo program status records print_term(text_record, filename, redirect=True) record(filename, args, today, config, redirect=True) print_term(text_askpass, filename, redirect=True) # skip disabled commands if (not config['Mode'].get(mode, False)) or getattr( args, f'no_{mode}', False): text = f'macdaily-logging: {yellow}{mode}{reset}: command disabled' print_term(text, filename, redirect=verbose) continue # update logging specifications namespace = getattr(args, mode, vars(args)) # check master controlling flags if args.quiet: namespace['quiet'] = True if args.verbose: namespace['verbose'] = True if args.show_log: namespace['show_log'] = True if args.no_cleanup: namespace['no_cleanup'] = True # run command cmd_cls = globals()[f'{mode.capitalize()}Logging'] command = cmd_cls(make_namespace(namespace), filename, timeout, confirm, askpass, password, disk_dir, brew_renew) # record command cmd_list.append(command) log_list.append(filename) brew_renew = command.time if not namespace['no_cleanup']: archive = make_archive(config, f'logging/{mode}', today, zipfile=False, quiet=quiet, verbose=verbose, logfile=filename) file_list.extend(archive) if namespace.get('show_log', False): try: subprocess.check_call([ 'open', '-a', '/Applications/Utilities/Console.app', filename ]) except subprocess.CalledProcessError: print_text(traceback.format_exc(), filename, redirect=verbose) print( f'macdaily: {red}logging{reset}: cannot show log file {filename!r}', file=sys.stderr) if not args.no_cleanup: storage = make_storage(config, today, quiet=quiet, verbose=verbose, logfile=filename) file_list.extend(storage) text = f'{bold}{green}|📖|{reset} {bold}MacDaily report of logging command{reset}' print_term(text, get_logfile(), redirect=quiet) for file in log_list: print_term(text, file, redirect=True) for command in cmd_list: text = f'Recorded existing {under}{command.desc[1]}{reset}{bold} at {under}{command.sample}{reset}' print_misc(text, get_logfile(), redirect=quiet) for file in log_list: print_misc(text, file, redirect=True) if file_list: formatted_list = f'{reset}{bold}, {under}'.join(file_list) text = ( f'Archived following ancient logs: {under}{formatted_list}{reset}') print_misc(text, filename, redirect=quiet) if len(cmd_list) == 0: # pylint: disable=len-as-condition text = f'macdaily: {purple}logging{reset}: no packages recorded' print_term(text, get_logfile(), redirect=quiet) for file in log_list: print_term(text, file, redirect=True) mode_lst = [command.mode for command in cmd_list] mode_str = ', '.join(mode_lst) if mode_lst else 'none' text = ( f'{bold}{green}|🍺|{reset} {bold}MacDaily successfully performed logging process ' f'for {mode_str} package managers{reset}') print_term(text, get_logfile(), redirect=quiet) for file in log_list: print_term(text, file, redirect=True)
def make_config(quiet=False, verbose=False): if not sys.stdin.isatty(): raise OSError(5, 'Input/output error') print_wrap(f'Entering interactive command line setup procedure...') print_wrap(f'Default settings are shown as in the square brackets.') print_wrap( f'Please directly {bold}{under}ENTER{reset} if you prefer the default settings.' ) rcpath = os.path.expanduser('~/.dailyrc') try: with open(rcpath, 'w') as config_file: config_file.writelines( map(lambda s: f'{s}{os.linesep}', CONFIG[:4])) # pylint: disable=map-builtin-not-iterating print() print_wrap( f'For logging utilities, we recommend you to set up your {bold}hard disk{reset} path.' ) print_wrap( f'You may change other path preferences in configuration `{under}~/.dailyrc{reset}` later.' ) print_wrap( f'Please note that all paths must be valid under all circumstances.' ) dskdir = input('Name of your external hard disk []: ').ljust(41) config_file.write( f'dskdir = /Volumes/{dskdir} ; path where your hard disk lies\n' ) config_file.writelines( map(lambda s: f'{s}{os.linesep}', CONFIG[5:38])) # pylint: disable=map-builtin-not-iterating print() print_wrap( f'In default, we will run {bold}update{reset} and {bold}logging{reset} commands twice a day.' ) print_wrap( f'You may change daily commands preferences in configuration `{under}~/.dailyrc{reset}` later.' ) print_wrap( f'Please enter schedule as {bold}{under}HH:MM[-CMD]{reset} format, ' f'and each separates with {under}comma{reset}.') timing = input( 'Time for daily scripts [10:00-update,22:30-logging,23:00-archive]: ' ) if timing: config_file.writelines([ '\t', '\n\t'.join(map(lambda s: s.strip(), timing.split(','))), '\n' ]) else: config_file.writelines( map(lambda s: f'{s}{os.linesep}', CONFIG[38:41])) # pylint: disable=map-builtin-not-iterating config_file.writelines( map(lambda s: f'{s}{os.linesep}', CONFIG[41:52])) # pylint: disable=map-builtin-not-iterating print() print_wrap( f'For better stability, {bold}MacDaily{reset} depends on several helper programs.' ) print_wrap( 'Your password may be necessary during the launch process.') askpass = launch_askpass(quiet=quiet, verbose=verbose) confirm = launch_confirm(quiet=quiet, verbose=verbose) config_file.write( f'askpass = {askpass.ljust(49)} ; SUDO_ASKPASS utility for Homebrew Casks\n' ) config_file.write( f'confirm = {confirm.ljust(49)} ; confirm utility for MacDaily\n' ) print() print_wrap( f'Also, {bold}MacDaily{reset} supports several different environment setups.' ) print_wrap( 'You may set up these variables here, ' f'or later manually in configuration `{under}~/.dailyrc{reset}`.' ) print_wrap( f'Please enter these specifications as instructed below.') shtout = ( input('Timeout limit for shell scripts in seconds [1,000]: ') or '1000').ljust(49) config_file.write( f'limit = {shtout} ; timeout limit for shell scripts in seconds\n' ) retout = (input('Retry timeout for input prompts in seconds [60]:') or '60').ljust(49) config_file.write( f'retry = {retout} ; retry timeout for input prompts in seconds\n' ) print() print_wrap( f'Configuration for {bold}MacDaily{reset} finished. Now launching...\n' ) except BaseException: os.remove(rcpath) print(reset) raise # parse config config = parse_config(quiet, verbose) askpass = config['Miscellaneous']['askpass'] # ask for password text = f'{bold}{purple}|🔑|{reset} {bold}Your {under}sudo{reset}{bold} password may be necessary{reset}' print_term(text, get_logfile(), redirect=quiet) password = get_pass(askpass) # launch daemons path = launch_daemons(config, password, quiet, verbose) text = f'Launched helper program {under}daemons{reset}{bold} at {under}{path}{reset}' print_misc(text, get_logfile(), redirect=quiet)