def register_yaml_targets(path): """ scans and registers all valid **YAML** defined targets in `path`. The name of the **YAML** file (without extension) becomes the target name :param str path: a valid directory """ global __TARGETS__ for name, yaml_file in yaml_files(path): if os.path.basename(yaml_file) == GLOBAL_CONFIG_FILE: continue debug('registering : {0}'.format(name)) __TARGETS__[name] = partial(load_target, yaml_file)
def register_target(name, target): """ register a target type to a given target name :param str name: the target name (case sensitive) :param target: the target to register :type target: :class:`cleanmymac.target.Target` """ global __TARGETS__ if issubclass(target, Target): debug('registering : {0}'.format(name)) __TARGETS__[name] = target else: error('target {0} is not of type Target, instead got: {1}'.format(name, target))
def register_target(name, target): """ register a target type to a given target name :param str name: the target name (case sensitive) :param target: the target to register :type target: :class:`cleanmymac.target.Target` """ global __TARGETS__ if issubclass(target, Target): debug('registering : {0}'.format(name)) __TARGETS__[name] = target else: error('target {0} is not of type Target, instead got: {1}'.format( name, target))
def _debug(self, msg, *arg): debug('[{0}] {1}'.format( click.style(str(self.__class__.__name__), fg='yellow'), msg))
""" global __TARGETS__ for name, target in __TARGETS__.iteritems(): yield name, target def get_targets_as_table(simple=True, fancy=False): headers = ['Name', 'Type'] def row(name, target): data = [name.upper()] t = target(None, update=False, verbose=False, strict=False) data.append(t.__class__.__name__ if simple else t.__class__) return data return tabulate( [row(name, target) for name, target in __TARGETS__.iteritems()], headers=headers, tablefmt='fancy_grid' if fancy else 'orgtbl') # register built in targets # 1 YAML based ones register_yaml_targets(BUILTINS_PATH) # register installed targets (if any) debug("looking for registered cleanup targets...") for ep in iter_entry_points(TARGET_ENTRY_POINT): debug("found: {0}".format(ep)) register_target(ep.name, ep.load())
:return: pairs of (name: target) """ global __TARGETS__ for name, target in __TARGETS__.iteritems(): yield name, target def get_targets_as_table(simple=True, fancy=False): headers = ['Name', 'Type'] def row(name, target): data = [name.upper()] t = target(None, update=False, verbose=False, strict=False) data.append(t.__class__.__name__ if simple else t.__class__) return data return tabulate([row(name, target) for name, target in __TARGETS__.iteritems()], headers=headers, tablefmt='fancy_grid' if fancy else 'orgtbl') # register built in targets # 1 YAML based ones register_yaml_targets(BUILTINS_PATH) # register installed targets (if any) debug("looking for registered cleanup targets...") for ep in iter_entry_points(TARGET_ENTRY_POINT): debug("found: {0}".format(ep)) register_target(ep.name, ep.load())
def cli(update, dry_run, quiet, pretty_print, strict, list_targets, stop_on_error, config, targets_path, targets, **kwargs): """ the main **run** method, responsible for creating the parser and executing the main logic in **cleanmymac** :param bool update: perform update of targets (if applicable) :param bool dry_run: do not execute the actions, but log the result :param bool quiet: quiet mode (no output), show a progressbar instead :param bool pretty_print: enable pretty printing with colors :param bool strict: if set enforce strict(er) rules when validating targets :param bool list_targets: list the installed targets :param bool stop_on_error: abort the execution on first error :param str config: the configuration path :param str targets_path: extra targets paths :param list targets: the targets """ disable_logger('sarge') targets = tuple([target.lower() for target in targets]) set_pretty_print(pretty_print) debug_param('update', update) debug_param('dry run', dry_run) debug_param('quiet mode', quiet) debug_param('pretty print', pretty_print) debug_param('strict mode', strict) debug_param('list available targets', list_targets) debug_param('stop on error', stop_on_error) debug_param('global config path', config) debug_param('extra targets path', targets_path) debug_param('targets', targets) debug('') all_targets = dict(iter_targets()) if is_debug(): debug("Detailed information about registered targets") debug(get_targets_as_table(simple=False, fancy=False)) if dry_run: verbose = True else: verbose = not quiet debug_param('verbose', verbose) if targets: target_names = set(targets) else: target_names = set(all_targets.keys()) if update: echo_warn( 'updating may result in an increase of the total space taken by the cleanup targets', verbose=verbose) echo_info('found {0} registered cleanup targets'.format(len(all_targets)), verbose=verbose) config = get_options(path=config) # register extra targets if any for pth in _config_targets_path(config): register_yaml_targets(pth) if targets_path and os.path.isdir(targets_path): register_yaml_targets(targets_path) if list_targets: echo_warn(get_targets_as_table(simple=True, fancy=True)) else: with progressbar(verbose, all_targets.items(), label='Processing cleanup targets:', width=40) as all_targets_bar: free_space_before = get_disk_usage('/', unit=UNIT_MB).free for name, target_initializer in all_targets_bar: echo_info(_HORIZONTAL_RULE, verbose=verbose) if name not in target_names: debug('skipping target "{0}"'.format(name)) continue echo_target('\ncleaning: {0}'.format(name.upper()), verbose=verbose) target_cfg = config[name] if name in config else None debug("got target configuration: {0}".format( pformat(target_cfg))) try: target = target_initializer(target_cfg, update=update, verbose=verbose, strict=strict) if not isinstance(target, Target): error( 'expected an instance of Target, instead got: {0}'. format(target)) continue if dry_run: echo_warn(target.describe()) else: target() except Exception, ex: error( 'could not cleanup target "{0}". Reason:\n{1}'.format( name, ex)) if stop_on_error: break if not verbose: sleep( PROGRESSBAR_ADVANCE_DELAY ) # nicer progress bar display for fast executing targets free_space_after = get_disk_usage('/', unit=UNIT_MB).free if not dry_run: echo_info('\ncleanup complete', verbose=verbose) echo_success('\nfreed {0:.3f} MB of disk space'.format( free_space_after - free_space_before), verbose=True, nl=verbose)
def cli(update, dry_run, quiet, pretty_print, strict, list_targets, stop_on_error, config, targets_path, targets, **kwargs): """ the main **run** method, responsible for creating the parser and executing the main logic in **cleanmymac** :param bool update: perform update of targets (if applicable) :param bool dry_run: do not execute the actions, but log the result :param bool quiet: quiet mode (no output), show a progressbar instead :param bool pretty_print: enable pretty printing with colors :param bool strict: if set enforce strict(er) rules when validating targets :param bool list_targets: list the installed targets :param bool stop_on_error: abort the execution on first error :param str config: the configuration path :param str targets_path: extra targets paths :param list targets: the targets """ disable_logger('sarge') targets = tuple([target.lower() for target in targets]) set_pretty_print(pretty_print) debug_param('update', update) debug_param('dry run', dry_run) debug_param('quiet mode', quiet) debug_param('pretty print', pretty_print) debug_param('strict mode', strict) debug_param('list available targets', list_targets) debug_param('stop on error', stop_on_error) debug_param('global config path', config) debug_param('extra targets path', targets_path) debug_param('targets', targets) debug('') all_targets = dict(iter_targets()) if is_debug(): debug("Detailed information about registered targets") debug(get_targets_as_table(simple=False, fancy=False)) if dry_run: verbose = True else: verbose = not quiet debug_param('verbose', verbose) if targets: target_names = set(targets) else: target_names = set(all_targets.keys()) if update: echo_warn('updating may result in an increase of the total space taken by the cleanup targets', verbose=verbose) echo_info('found {0} registered cleanup targets'.format(len(all_targets)), verbose=verbose) config = get_options(path=config) # register extra targets if any for pth in _config_targets_path(config): register_yaml_targets(pth) if targets_path and os.path.isdir(targets_path): register_yaml_targets(targets_path) if list_targets: echo_warn(get_targets_as_table(simple=True, fancy=True)) else: with progressbar(verbose, all_targets.items(), label='Processing cleanup targets:', width=40) as all_targets_bar: free_space_before = get_disk_usage('/', unit=UNIT_MB).free for name, target_initializer in all_targets_bar: echo_info(_HORIZONTAL_RULE, verbose=verbose) if name not in target_names: debug('skipping target "{0}"'.format(name)) continue echo_target('\ncleaning: {0}'.format(name.upper()), verbose=verbose) target_cfg = config[name] if name in config else None debug("got target configuration: {0}".format(pformat(target_cfg))) try: target = target_initializer(target_cfg, update=update, verbose=verbose, strict=strict) if not isinstance(target, Target): error('expected an instance of Target, instead got: {0}'.format(target)) continue if dry_run: echo_warn(target.describe()) else: target() except Exception, ex: error('could not cleanup target "{0}". Reason:\n{1}'.format(name, ex)) if stop_on_error: break if not verbose: sleep(PROGRESSBAR_ADVANCE_DELAY) # nicer progress bar display for fast executing targets free_space_after = get_disk_usage('/', unit=UNIT_MB).free if not dry_run: echo_info('\ncleanup complete', verbose=verbose) echo_success('\nfreed {0:.3f} MB of disk space'.format(free_space_after - free_space_before), verbose=True, nl=verbose)