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 install(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') # mkdir for logs logpath = pathlib.Path( os.path.join(config['Path']['logdir'], 'install', logdate)) logpath.mkdir(parents=True, exist_ok=True) # prepare command paras filename = os.path.join(logpath, f'{logtime}-{uuid.uuid4()!s}.log') os.environ['MACDAILY_LOGFILE'] = filename confirm = config['Miscellaneous']['confirm'] askpass = config['Miscellaneous']['askpass'] timeout = config['Miscellaneous']['limit'] disk_dir = config['Path']['arcdir'] brew_renew = None # record program status text = f'{bold}{green}|🚨|{reset} {bold}Running MacDaily version {__version__}{reset}' print_term(text, filename, redirect=quiet) record(filename, args, today, config, redirect=verbose) # ask for password text = f'{bold}{purple}|🔑|{reset} {bold}Your {under}sudo{reset}{bold} password may be necessary{reset}' print_term(text, filename, redirect=quiet) password = get_pass(askpass) cmd_list = list() for mode in {'apm', 'brew', 'cask', 'gem', 'mas', 'npm', 'pip', 'system'}: # skip disabled commands if not config['Mode'].get(mode, False): text = f'macdaily-install: {yellow}{mode}{reset}: command disabled' print_term(text, filename, redirect=verbose) continue # skip commands with no package spec packages = getattr(args, f'{mode}_pkgs', list()) namespace = getattr(args, mode, None) if not (packages or namespace): text = f'macdaily-install: {yellow}{mode}{reset}: nothing to install' print_term(text, filename, redirect=verbose) continue # update package specifications if namespace is None: namespace = dict(vars(args), packages=list()) namespace['packages'].extend(packages) # check master controlling flags if args.yes: namespace['yes'] = True if args.quiet: namespace['quiet'] = True if args.verbose: namespace['verbose'] = True if args.no_cleanup: namespace['no_cleanup'] = True # run command cmd_cls = globals()[f'{mode.capitalize()}Install'] command = cmd_cls(make_namespace(namespace), filename, timeout, confirm, askpass, password, disk_dir, brew_renew) # record command cmd_list.append(command) brew_renew = command.time archive = None if not args.no_cleanup: archive = make_archive(config, 'install', today, quiet=quiet, verbose=verbose, logfile=filename) text = f'{bold}{green}|📖|{reset} {bold}MacDaily report of install command{reset}' print_term(text, filename, redirect=quiet) for command in cmd_list: desc = make_description(command) pkgs = f'{reset}{bold}, {green}'.join(command.packages) fail = f'{reset}{bold}, {red}'.join(command.failed) if pkgs: flag = (len(pkgs) == 1) text = f'Installed following {under}{desc(flag)}{reset}{bold}: {green}{pkgs}{reset}' print_misc(text, filename, redirect=quiet) else: text = f'No {under}{desc(False)}{reset}{bold} installed' print_misc(text, filename, redirect=quiet) if fail: flag = (len(fail) == 1) text = f'Installation of following {under}{desc(flag)}{reset}{bold} failed: {red}{fail}{reset}' print_misc(text, filename, redirect=quiet) else: verb, noun = ('s', '') if len(fail) == 1 else ('', 's') text = f'All {under}{desc(False)}{reset}{bold} installation{noun} succeed{verb}' print_misc(text, filename, redirect=verbose) if archive: formatted_list = f'{reset}{bold}, {under}'.join(archive) 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}install{reset}: no packages installed' print_term(text, filename, redirect=quiet) if args.show_log: 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}install{reset}: cannot show log file {filename!r}', file=sys.stderr) 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 install process ' f'for {mode_str} package managers{reset}') print_term(text, filename, redirect=quiet)
def postinstall(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') # mkdir for logs logpath = pathlib.Path( os.path.join(config['Path']['logdir'], 'postinstall', logdate)) logpath.mkdir(parents=True, exist_ok=True) # prepare command paras filename = os.path.join(logpath, f'{logtime}-{uuid.uuid4()!s}.log') os.environ['MACDAILY_LOGFILE'] = filename confirm = config['Miscellaneous']['confirm'] askpass = config['Miscellaneous']['askpass'] timeout = config['Miscellaneous']['limit'] disk_dir = config['Path']['arcdir'] brew_renew = None # record program status text = f'{bold}{green}|🚨|{reset} {bold}Running MacDaily version {__version__}{reset}' print_term(text, filename, redirect=quiet) record(filename, args, today, config, redirect=verbose) # ask for password text = f'{bold}{purple}|🔑|{reset} {bold}Your {under}sudo{reset}{bold} password may be necessary{reset}' print_term(text, filename, redirect=quiet) password = get_pass(askpass) # check if disabled enabled = (not config['Mode'].get('brew', False)) # run command if enabled: command = PostinstallCommand(make_namespace(args), filename, timeout, confirm, askpass, password, disk_dir, brew_renew) else: text = f'macdaily-postinstall: {yellow}brew{reset}: command disabled' print_term(text, filename, redirect=verbose) archive = None if not args.no_cleanup: archive = make_archive(config, 'postinstall', today, quiet=quiet, verbose=verbose, logfile=filename) text = f'{bold}{green}|📖|{reset} {bold}MacDaily report of postinstall command{reset}' print_term(text, filename, redirect=quiet) if enabled: desc = make_description(command) pkgs = f'{reset}{bold}, {green}'.join(command.packages) miss = f'{reset}{bold}, {yellow}'.join(command.notfound) ilst = f'{reset}{bold}, {pink}'.join(command.ignored) fail = f'{reset}{bold}, {red}'.join(command.failed) if pkgs: flag = (len(pkgs) == 1) text = f'Postinstalled following {under}{desc(flag)}{reset}{bold}: {green}{pkgs}{reset}' print_misc(text, filename, redirect=quiet) else: text = f'No {under}{desc(False)}{reset}{bold} postinstalled' print_misc(text, filename, redirect=quiet) if fail: flag = (len(fail) == 1) text = f'Postinstallation of following {under}{desc(flag)}{reset}{bold} failed: {red}{fail}{reset}' print_misc(text, filename, redirect=quiet) else: verb, noun = ('s', '') if len(fail) == 1 else ('', 's') text = f'All {under}{desc(False)}{reset}{bold} postinstallation{noun} succeed{verb}' print_misc(text, filename, redirect=verbose) if ilst: flag = (len(ilst) == 1) text = f'Ignored postinstallation of following {under}{desc(flag)}{reset}{bold}: {pink}{ilst}{reset}' print_misc(text, filename, redirect=quiet) else: text = f'No {under}{desc(False)}{reset}{bold} ignored' print_misc(text, filename, redirect=verbose) if miss: flag = (len(miss) == 1) text = f'Following {under}{desc(flag)}{reset}{bold} not found: {yellow}{miss}{reset}' print_misc(text, filename, redirect=quiet) else: text = f'Hit all {under}{desc(False)}{reset}{bold} specifications' print_misc(text, filename, redirect=verbose) if archive: formatted_list = f'{reset}{bold}, {under}'.join(archive) text = ( f'Archived following ancient logs: {under}{formatted_list}{reset}') print_misc(text, filename, redirect=quiet) if not enabled: text = f'macdaily: {purple}postinstall{reset}: no Homebrew formulae postinstalled' print_term(text, filename, redirect=quiet) if args.show_log: 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}postinstall{reset}: cannot show log file {filename!r}', file=sys.stderr) text = ( f'{bold}{green}|🍺|{reset} {bold}MacDaily successfully performed postinstall process{reset}' ) print_term(text, filename, redirect=quiet)