PK 6O'pwily/__init__.py""" Wily. A Python application for tracking, reporting on timing and complexity in tests and applications. """ import colorlog import datetime __version__ = "1.12.4" _handler = colorlog.StreamHandler() _handler.setFormatter(colorlog.ColoredFormatter("%(log_color)s%(message)s")) logger = colorlog.getLogger(__name__) logger.addHandler(_handler) """ Max number of characters of the Git commit to print """ MAX_MESSAGE_WIDTH = 50 def format_date(timestamp): """Reusable timestamp -> date.""" return datetime.date.fromtimestamp(timestamp).isoformat() def format_datetime(timestamp): """Reusable timestamp -> datetime.""" return datetime.datetime.fromtimestamp(timestamp).isoformat() def format_revision(sha): """Return a shorter git sha.""" return sha[:7] PKg6O0%%wily/__main__.py# -*- coding: UTF-8 -*- """Main command line.""" import click from pathlib import Path from wily import logger, __version__ from wily.archivers import resolve_archiver from wily.cache import exists, get_default_metrics from wily.config import DEFAULT_CONFIG_PATH, DEFAULT_GRID_STYLE from wily.config import load as load_config from wily.decorators import add_version from wily.helper.custom_enums import ReportFormat from wily.operators import resolve_operators @click.group() @click.version_option( __version__, "-V", "--version", message="\U0001F98A %(prog)s, version %(version)s" ) @click.option( "--debug/--no-debug", default=False, help="Print debug information, used for development", ) @click.option( "--config", default=DEFAULT_CONFIG_PATH, help="Path to configuration file, defaults to wily.cfg", ) @click.option( "-p", "--path", type=click.Path(resolve_path=True), default=".", help="Root path to the project folder to scan", ) @click.option( "-c", "--cache", type=click.Path(resolve_path=True), help="Override the default cache path (defaults to $HOME/.wily/HASH)", ) @click.pass_context @add_version def cli(ctx, debug, config, path, cache): """ \U0001F98A Inspect and search through the complexity of your source code. To get started, run setup: $ wily setup To reindex any changes in your source code: $ wily build Then explore basic metrics with: $ wily report You can also graph specific metrics in a browser with: $ wily graph """ ctx.ensure_object(dict) ctx.obj["DEBUG"] = debug if debug: logger.setLevel("DEBUG") else: logger.setLevel("INFO") ctx.obj["CONFIG"] = load_config(config) if path: logger.debug(f"Fixing path to {path}") ctx.obj["CONFIG"].path = path if cache: logger.debug(f"Fixing cache to {cache}") ctx.obj["CONFIG"].cache_path = cache logger.debug(f"Loaded configuration from {config}") @cli.command() @click.option( "-n", "--max-revisions", default=None, type=click.INT, help="The maximum number of historical commits to archive", ) @click.argument("targets", type=click.Path(resolve_path=True), nargs=-1, required=False) @click.option( "-o", "--operators", type=click.STRING, help="List of operators, separated by commas", ) @click.option( "-a", "--archiver", type=click.STRING, default="git", help="Archiver to use, defaults to git if git repo, else filesystem", ) @click.pass_context def build(ctx, max_revisions, targets, operators, archiver): """Build the wily cache.""" config = ctx.obj["CONFIG"] from wily.commands.build import build if max_revisions: logger.debug(f"Fixing revisions to {max_revisions}") config.max_revisions = max_revisions if operators: logger.debug(f"Fixing operators to {operators}") config.operators = operators.strip().split(",") if archiver: logger.debug(f"Fixing archiver to {archiver}") config.archiver = archiver if targets: logger.debug(f"Fixing targets to {targets}") config.targets = targets build( config=config, archiver=resolve_archiver(config.archiver), operators=resolve_operators(config.operators), ) logger.info( "Completed building wily history, run `wily report ` or `wily index` to see more." ) @cli.command() @click.pass_context @click.option("--message/--no-message", default=False, help="Include revision message") def index(ctx, message): """Show the history archive in the .wily/ folder.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) from wily.commands.index import index index(config=config, include_message=message) @cli.command() @click.argument("file", type=click.Path(resolve_path=False)) @click.argument("metrics", nargs=-1, required=False) @click.option("-n", "--number", help="Number of items to show", type=click.INT) @click.option("--message/--no-message", default=False, help="Include revision message") @click.option( "-f", "--format", default=ReportFormat.CONSOLE.name, help="Specify report format (console or html)", type=click.Choice(ReportFormat.get_all()), ) @click.option( "--console-format", default=DEFAULT_GRID_STYLE, help="Style for the console grid, see Tabulate Documentation for a list of styles.", ) @click.option( "-o", "--output", help="Output report to specified HTML path, e.g. reports/out.html" ) @click.pass_context def report(ctx, file, metrics, number, message, format, console_format, output): """Show metrics for a given file.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) if not metrics: metrics = get_default_metrics(config) logger.info(f"Using default metrics {metrics}") new_output = Path().cwd() if output: new_output = new_output / Path(output) else: new_output = new_output / "wily_report" / "index.html" from wily.commands.report import report logger.debug(f"Running report on {file} for metric {metrics}") logger.debug(f"Output format is {format}") report( config=config, path=file, metrics=metrics, n=number, output=new_output, include_message=message, format=ReportFormat[format], console_format=console_format, ) @cli.command() @click.argument("files", type=click.Path(resolve_path=False), nargs=-1, required=True) @click.option( "--metrics", default=None, help="comma-seperated list of metrics, see list-metrics for choices", ) @click.option( "--all/--changes-only", default=False, help="Show all files, instead of changes only", ) @click.option( "--detail/--no-detail", default=True, help="Show function/class level metrics where available", ) @click.pass_context def diff(ctx, files, metrics, all, detail): """Show the differences in metrics for each file.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) if not metrics: metrics = get_default_metrics(config) logger.info(f"Using default metrics {metrics}") else: metrics = metrics.split(",") logger.info(f"Using specified metrics {metrics}") from wily.commands.diff import diff logger.debug(f"Running diff on {files} for metric {metrics}") diff( config=config, files=files, metrics=metrics, changes_only=not all, detail=detail ) @cli.command() @click.argument("path", type=click.Path(resolve_path=False)) @click.argument("metrics", nargs=-2, required=True) @click.option( "-o", "--output", help="Output report to specified HTML path, e.g. reports/out.html" ) @click.option("-x", "--x-axis", help="Metric to use on x-axis, defaults to history.") @click.option( "-a/-c", "--changes/--all", default=True, help="All commits or changes only" ) @click.pass_context def graph(ctx, path, metrics, output, x_axis, changes): """ Graph a specific metric for a given file, if a path is given, all files within path will be graphed. Some common examples: Graph all .py files within src/ for the raw.loc metric $ wily graph src/ raw.loc Graph test.py against raw.loc and cyclomatic.complexity metrics $ wily graph src/test.py raw.loc cyclomatic.complexity Graph test.py against raw.loc and raw.sloc on the x-axis $ wily graph src/test.py raw.loc --x-axis raw.sloc """ config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) from wily.commands.graph import graph logger.debug(f"Running report on {path} for metrics {metrics}") graph( config=config, path=path, metrics=metrics, output=output, x_axis=x_axis, changes=changes, ) @cli.command() @click.option("-y/-p", "--yes/--prompt", default=False, help="Skip prompt") @click.pass_context def clean(ctx, yes): """Clear the .wily/ folder.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) if not yes: p = input("Are you sure you want to delete wily cache? [y/N]") if p.lower() != "y": exit(0) from wily.cache import clean clean(config) @cli.command("list-metrics") @click.pass_context def list_metrics(ctx): """List the available metrics.""" config = ctx.obj["CONFIG"] if not exists(config): handle_no_cache(ctx) from wily.commands.list_metrics import list_metrics list_metrics() @cli.command("setup") @click.pass_context def setup(ctx): """Run a guided setup to build the wily cache.""" handle_no_cache(ctx) def handle_no_cache(context): """Handle lack-of-cache error, prompt user for index process.""" logger.error( f"Could not locate wily cache, the cache is required to provide insights." ) p = input("Do you want to run setup and index your project now? [y/N]") if p.lower() != "y": exit(1) else: revisions = input("How many previous git revisions do you want to index? : ") revisions = int(revisions) path = input("Path to your source files; comma-separated for multiple: ") paths = path.split(",") context.invoke(build, max_revisions=revisions, targets=paths, operators=None) if __name__ == "__main__": cli() # pragma: no cover PK6O@>UU wily/cache.py""" A module for working with the .wily/ cache directory. This API is not intended to be public and should not be consumed directly. The API in this module is for archivers and commands to work with the local cache """ import json import os.path import pathlib import shutil from wily import logger, __version__ from wily.archivers import ALL_ARCHIVERS from wily.operators import resolve_operator def exists(config): """ Check whether the .wily/ directory exists. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :return: Whether the .wily directory exists :rtype: ``boolean`` """ exists = ( pathlib.Path(config.cache_path).exists() and pathlib.Path(config.cache_path).is_dir() ) if not exists: return False index_path = pathlib.Path(config.cache_path) / "index.json" if index_path.exists(): with open(index_path, "r") as out: index = json.load(out) if index["version"] != __version__: # TODO: Inspect the versions properly. logger.warning( "Wily cache is old, you may incur errors until you rebuild the cache." ) else: logger.warning( "Wily cache was not versioned, you may incur errors until you rebuild the cache." ) create_index(config) return True def create_index(config): """Create the root index.""" filename = pathlib.Path(config.cache_path) / "index.json" index = {"version": __version__} with open(filename, "w") as out: out.write(json.dumps(index, indent=2)) def create(config): """ Create a wily cache. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :return: The path to the cache :rtype: ``str`` """ if exists(config): logger.debug("Wily cache exists, skipping") return config.cache_path logger.debug(f"Creating wily cache {config.cache_path}") pathlib.Path(config.cache_path).mkdir(parents=True, exist_ok=True) create_index(config) return config.cache_path def clean(config): """ Delete a wily cache. :param config: The configuration :type config: :class:`wily.config.WilyConfig` """ if not exists(config): logger.debug("Wily cache does not exist, skipping") return shutil.rmtree(config.cache_path) logger.debug("Deleted wily cache") def store(config, archiver, revision, stats): """ Store a revision record within an archiver folder. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :param archiver: The name of the archiver type (e.g. 'git') :type archiver: ``str`` :param revision: The revision ID :type revision: ``str`` :param stats: The collected data :type stats: ``dict`` :return: The absolute path to the created file :rtype: ``str`` :rtype: `pathlib.Path` """ root = pathlib.Path(config.cache_path) / archiver.name if not root.exists(): logger.debug("Creating wily cache") root.mkdir() # fix absolute path references. if config.path != ".": for operator, operator_data in list(stats["operator_data"].items()): if operator_data: new_operator_data = operator_data.copy() for k, v in list(operator_data.items()): new_key = os.path.relpath(str(k), str(config.path)) del new_operator_data[k] new_operator_data[new_key] = v del stats["operator_data"][operator] stats["operator_data"][operator] = new_operator_data logger.debug(f"Creating {revision.key} output") filename = root / (revision.key + ".json") if filename.exists(): raise RuntimeError(f"File {filename} already exists, index may be corrupt.") with open(filename, "w") as out: out.write(json.dumps(stats, indent=2)) return filename def store_archiver_index(config, archiver, index): """ Store an archiver's index record for faster search. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :param archiver: The name of the archiver type (e.g. 'git') :type archiver: ``str`` :param index: The archiver index record :type index: ``dict`` :rtype: `pathlib.Path` """ root = pathlib.Path(config.cache_path) / archiver.name if not root.exists(): root.mkdir() logger.debug("Created archiver directory") index = sorted(index, key=lambda k: k["date"], reverse=True) filename = root / "index.json" with open(filename, "w") as out: out.write(json.dumps(index, indent=2)) logger.debug(f"Created index output") return filename def list_archivers(config): """ List the names of archivers with data. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :return: A list of archiver names :rtype: ``list`` of ``str`` """ root = pathlib.Path(config.cache_path) result = [] for name in ALL_ARCHIVERS.keys(): if (root / name).exists(): result.append(name) return result def get_default_metrics(config): """ Get the default metrics for a configuration. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :return: Return the list of default metrics in this index :rtype: ``list`` of ``str`` """ archivers = list_archivers(config) default_metrics = [] for archiver in archivers: index = get_archiver_index(config, archiver) if len(index) == 0: logger.warning("No records found in the index, no metrics available") return [] operators = index[0]["operators"] for operator in operators: o = resolve_operator(operator) if o.cls.default_metric_index is not None: metric = o.cls.metrics[o.cls.default_metric_index] default_metrics.append("{0}.{1}".format(o.cls.name, metric.name)) return default_metrics def has_archiver_index(config, archiver): """ Check if this archiver has an index file. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :param archiver: The name of the archiver type (e.g. 'git') :type archiver: ``str`` :return: the exist :rtype: ``bool`` """ root = pathlib.Path(config.cache_path) / archiver / "index.json" return root.exists() def get_archiver_index(config, archiver): """ Get the contents of the archiver index file. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :param archiver: The name of the archiver type (e.g. 'git') :type archiver: ``str`` :return: The index data :rtype: ``dict`` """ root = pathlib.Path(config.cache_path) / archiver with (root / "index.json").open("r") as index_f: index = json.load(index_f) return index def get(config, archiver, revision): """ Get the data for a given revision. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :param archiver: The name of the archiver type (e.g. 'git') :type archiver: ``str`` :param revision: The revision ID :type revision: ``str`` :return: The data record for that revision :rtype: ``dict`` """ root = pathlib.Path(config.cache_path) / archiver # TODO : string escaping!!! with (root / f"{revision}.json").open("r") as rev_f: index = json.load(rev_f) return index PK6OYwily/config.py""" Configuration of wily. TODO : Handle operator settings. Maybe a section for each operator and then pass kwargs to operators? TODO : Better utilise default values and factory in @dataclass to replace DEFAULT_CONFIG and replace the logic in load() to set default values. """ from functools import lru_cache import configparser import logging import pathlib import hashlib from dataclasses import dataclass, field from typing import Any, List import wily.operators as operators from wily.archivers import ARCHIVER_GIT logger = logging.getLogger(__name__) @lru_cache(maxsize=128) def generate_cache_path(path): """ Generate a reusable path to cache results. Will use the --path of the target and hash into a 9-character directory within the HOME folder. :return: The cache path :rtype: ``str`` """ logger.debug(f"Generating cache for {path}") sha = hashlib.sha1(str(path).encode()).hexdigest()[:9] HOME = pathlib.Path.home() cache_path = str(HOME / ".wily" / sha) logger.debug(f"Cache path is {cache_path}") return cache_path @dataclass class WilyConfig(object): """ Wily configuration. A data class to reflect the configurable options within Wily. """ operators: List archiver: Any path: str max_revisions: int targets: List[str] = None checkout_options: dict = field(default_factory=dict) def __post_init__(self): """Clone targets as a list of path.""" if self.targets is None or "": self.targets = [self.path] self._cache_path = None @property def cache_path(self): """Path to the cache.""" if not self._cache_path: self._cache_path = generate_cache_path(pathlib.Path(self.path).absolute()) return self._cache_path @cache_path.setter def cache_path(self, value): """Override the cache path.""" logger.debug(f"Setting custom cache path to {value}") self._cache_path = value # Default values for Wily """ The default operators """ DEFAULT_OPERATORS = { operators.OPERATOR_RAW.name, operators.OPERATOR_MAINTAINABILITY.name, operators.OPERATOR_CYCLOMATIC.name, operators.OPERATOR_HALSTEAD.name, } """ The name of the default archiver """ DEFAULT_ARCHIVER = ARCHIVER_GIT.name """ The default configuration file name """ DEFAULT_CONFIG_PATH = "wily.cfg" """ The default section name in the config """ DEFAULT_CONFIG_SECTION = "wily" """ The default maximum number of revisions to archiver """ DEFAULT_MAX_REVISIONS = 50 DEFAULT_PATH = "." """ The default configuration for Wily (if no config file exists) """ DEFAULT_CONFIG = WilyConfig( operators=DEFAULT_OPERATORS, archiver=DEFAULT_ARCHIVER, path=DEFAULT_PATH, max_revisions=DEFAULT_MAX_REVISIONS, ) """ Default table style in console. See tabulate docs for more. """ DEFAULT_GRID_STYLE = "fancy_grid" def load(config_path=DEFAULT_CONFIG_PATH): """ Load config file and set values to defaults where no present. :return: The configuration ``WilyConfig`` :rtype: :class:`wily.config.WilyConfig` """ if not pathlib.Path(config_path).exists(): logger.debug(f"Could not locate {config_path}, using default config.") return DEFAULT_CONFIG config = configparser.ConfigParser(default_section=DEFAULT_CONFIG_SECTION) config.read(config_path) operators = config.get( section=DEFAULT_CONFIG_SECTION, option="operators", fallback=DEFAULT_OPERATORS ) archiver = config.get( section=DEFAULT_CONFIG_SECTION, option="archiver", fallback=DEFAULT_ARCHIVER ) path = config.get(section=DEFAULT_CONFIG_SECTION, option="path", fallback=".") max_revisions = int( config.get( section=DEFAULT_CONFIG_SECTION, option="max_revisions", fallback=DEFAULT_MAX_REVISIONS, ) ) return WilyConfig( operators=operators, archiver=archiver, path=path, max_revisions=max_revisions ) PK~MGwily/decorators.py""" A module including decorators for wily. This API is not intended to be public and should not be consumed directly. """ from wily import __version__ def add_version(f): """ Add the version of wily to the help heading. :param f: function to decorate :return: decorated function """ doc = f.__doc__ f.__doc__ = "Version: " + __version__ + "\n\n" + doc return f PK6O / wily/state.py""" For managing the state of the wily process. Contains a lazy revision, index and process state model. """ from collections import OrderedDict from dataclasses import dataclass, asdict from typing import List import wily.cache as cache from wily import logger from wily.archivers import Revision, resolve_archiver from wily.operators import get_metric @dataclass class IndexedRevision(object): """Union of revision and the operators executed.""" revision: Revision operators: List _data = None @staticmethod def fromdict(d): """Instantiate from a dictionary.""" rev = Revision( key=d["key"], author_name=d["author_name"], author_email=d["author_email"], date=d["date"], message=d["message"], ) operators = d["operators"] return IndexedRevision(revision=rev, operators=operators) def asdict(self): """Convert to dictionary.""" d = asdict(self.revision) d["operators"] = self.operators return d def get(self, config, archiver, operator, path, key): """ Get the metric data for this indexed revision. :param config: The wily config. :type config: :class:`wily.config.WilyConfig` :param archiver: The archiver. :type archiver: :class:`wily.archivers.Archiver` :param operator: The operator to find :type operator: ``str`` :param path: The path to find :type path: ``str`` :param key: The metric key :type key: ``str`` """ if not self._data: self._data = cache.get( config=config, archiver=archiver, revision=self.revision.key )["operator_data"] logger.debug(f"Fetching metric {path} - {key} for operator {operator}") return get_metric(self._data, operator, path, key) def store(self, config, archiver, stats): """ Store the stats for this indexed revision. :param config: The wily config. :type config: :class:`wily.config.WilyConfig` :param archiver: The archiver. :type archiver: :class:`wily.archivers.Archiver` :param stats: The data :type stats: ``dict`` """ self._data = stats return cache.store(config, archiver, self.revision, stats) class Index(object): """The index of the wily cache.""" operators = None def __init__(self, config, archiver): """ Instantiate a new index. :param config: The wily config. :type config: :class:`wily.config.WilyConfig` :param archiver: The archiver. :type archiver: :class:`wily.archivers.Archiver` """ self.config = config self.archiver = archiver self.data = ( cache.get_archiver_index(config, archiver.name) if cache.has_archiver_index(config, archiver.name) else [] ) self._revisions = OrderedDict() for d in self.data: self._revisions[d["key"]] = IndexedRevision.fromdict(d) def __len__(self): """Use length of revisions as len.""" return len(self._revisions) @property def revisions(self): """ List of all the revisions. :rtype: ``list`` of :class:`LazyRevision` """ return list(self._revisions.values()) @property def revision_keys(self): """ List of all the revision indexes. :rtype: ``list`` of ``str`` """ return list(self._revisions.keys()) def __contains__(self, item): """ Check if index contains `item`. :param item: The item to search for :type item: ``str``, :class:`Revision` or :class:`LazyRevision` :return: ``True`` for contains, ``False`` for not. """ if isinstance(item, Revision): return item.key in self._revisions elif isinstance(item, str): return item in self._revisions else: raise TypeError("Invalid type for __contains__ in Index.") def __getitem__(self, index): """Get the revision for a specific index.""" return self._revisions[index] def add(self, revision, operators): """ Add a revision to the index. :param revision: The revision. :type revision: :class:`Revision` or :class:`LazyRevision` """ ir = IndexedRevision( revision=revision, operators=[operator.name for operator in operators] ) self._revisions[revision.key] = ir return ir def save(self): """Save the index data back to the wily cache.""" data = [i.asdict() for i in self._revisions.values()] logger.debug("Saving data") cache.store_archiver_index(self.config, self.archiver, data) class State(object): """ The wily process state. Includes indexes for each archiver. """ def __init__(self, config, archiver=None): """ Instantiate a new process state. :param config: The wily configuration. :type config: :class:`WilyConfig` :param archiver: The archiver (optional). :type archiver: :class:`wily.archivers.Archiver` """ if archiver: self.archivers = [archiver.name] else: self.archivers = cache.list_archivers(config) logger.debug(f"Initialised state indexes for archivers {self.archivers}") self.config = config self.index = {} for archiver in self.archivers: self.index[archiver] = Index(self.config, resolve_archiver(archiver)) self.default_archiver = self.archivers[0] def ensure_exists(self): """Ensure that cache directory exists.""" if not cache.exists(self.config): logger.debug("Wily cache not found, creating.") cache.create(self.config) logger.debug("Created wily cache") else: logger.debug(f"Cache {self.config.cache_path} exists") PKRyMj~~wily/archivers/__init__.py""" Archivers module. Specifies a standard interface for finding revisions (versions) of a path and switching to them. """ from collections import namedtuple from dataclasses import dataclass class BaseArchiver(object): """Abstract Archiver Class.""" def revisions(self, path, max_revisions): """ Get the list of revisions. :param path: the path to target. :type path: ``str`` :param max_revisions: the maximum number of revisions. :type max_revisions: ``int`` :return: A list of revisions. :rtype: ``list`` of :class:`Revision` """ raise NotImplementedError def checkout(self, revision, **options): """ Checkout a specific revision. :param revision: The revision identifier. :type revision: :class:`Revision` :param options: Any additional options. :type options: ``dict`` """ raise NotImplementedError def finish(self): """Clean up any state if processing completed/failed.""" pass @dataclass class Revision: """Represents a revision in the archiver.""" key: str author_name: str author_email: str date: str message: str from wily.archivers.git import GitArchiver from wily.archivers.filesystem import FilesystemArchiver """Type for an operator""" Archiver = namedtuple("Archiver", "name cls description") """Git Operator defined in `wily.archivers.git`""" ARCHIVER_GIT = Archiver(name="git", cls=GitArchiver, description="Git archiver") """Filesystem archiver""" ARCHIVER_FILESYSTEM = Archiver( name="filesystem", cls=FilesystemArchiver, description="Filesystem archiver" ) """Set of all available archivers""" ALL_ARCHIVERS = {a.name: a for a in [ARCHIVER_GIT, ARCHIVER_FILESYSTEM]} def resolve_archiver(name): """ Get the :class:`wily.archivers.Archiver` for a given name. :param name: The name of the archiver :type name: ``str`` :return: The archiver type """ if name not in ALL_ARCHIVERS: raise ValueError(f"Resolver {name} not recognised.") else: return ALL_ARCHIVERS[name.lower()] PK6O7wily/archivers/filesystem.py""" Filesystem Archiver. Implementation of the archiver API for a standard directory (no revisions) """ import logging import os.path import hashlib from wily.archivers import BaseArchiver, Revision logger = logging.getLogger(__name__) class FilesystemArchiver(BaseArchiver): """Basic implementation of the base archiver.""" name = "filesystem" def __init__(self, config): """ Instantiate a new Filesystem Archiver. :param config: The wily configuration :type config: :class:`wily.config.WilyConfig` """ self.config = config def revisions(self, path, max_revisions): """ Get the list of revisions. :param path: the path to target. :type path: ``str`` :param max_revisions: the maximum number of revisions. :type max_revisions: ``int`` :return: A list of revisions. :rtype: ``list`` of :class:`Revision` """ mtime = os.path.getmtime(path) key = hashlib.sha1(str(mtime).encode()).hexdigest()[:7] return [ Revision( key=key, author_name="Local User", # Don't want to leak local data author_email="-", # as above date=int(mtime), message="None", ) ] def checkout(self, revision, options): """ Checkout a specific revision. :param revision: The revision identifier. :type revision: :class:`Revision` :param options: Any additional options. :type options: ``dict`` """ # effectively noop since there are no revision pass PK6OV}p p wily/archivers/git.py""" Git Archiver. Implementation of the archiver API for the gitpython module. """ import logging from git import Repo import git.exc from wily.archivers import BaseArchiver, Revision logger = logging.getLogger(__name__) class InvalidGitRepositoryError(Exception): """Error for when a folder is not a git repo.""" pass class DirtyGitRepositoryError(Exception): """Error for a dirty git repository (untracked files).""" def __init__(self, untracked_files): """ Raise error for untracked files. :param untracked_files: List of untracked files :param untracked_files: ``list`` """ self.untracked_files = untracked_files self.message = "Dirty repository, make sure you commit/stash files first" class GitArchiver(BaseArchiver): """Gitpython implementation of the base archiver.""" name = "git" def __init__(self, config): """ Instantiate a new Git Archiver. :param config: The wily configuration :type config: :class:`wily.config.WilyConfig` """ try: self.repo = Repo(config.path) except git.exc.InvalidGitRepositoryError as e: raise InvalidGitRepositoryError from e self.config = config self.current_branch = self.repo.active_branch assert not self.repo.bare, "Not a Git repository" def revisions(self, path, max_revisions): """ Get the list of revisions. :param path: the path to target. :type path: ``str`` :param max_revisions: the maximum number of revisions. :type max_revisions: ``int`` :return: A list of revisions. :rtype: ``list`` of :class:`Revision` """ if self.repo.is_dirty(): raise DirtyGitRepositoryError(self.repo.untracked_files) revisions = [] for commit in self.repo.iter_commits( self.current_branch, max_count=max_revisions ): rev = Revision( key=commit.name_rev.split(" ")[0], author_name=commit.author.name, author_email=commit.author.email, date=commit.committed_date, message=commit.message, ) revisions.append(rev) return revisions def checkout(self, revision, options): """ Checkout a specific revision. :param revision: The revision identifier. :type revision: :class:`Revision` :param options: Any additional options. :type options: ``dict`` """ rev = revision.key self.repo.git.checkout(rev) def finish(self): """ Clean up any state if processing completed/failed. For git, will checkout HEAD on the original branch when finishing """ self.repo.git.checkout(self.current_branch) self.repo.close() PKRyMh''wily/commands/__init__.py"""Command implementations for CLI.""" PKg6O ddwily/commands/build.py""" Builds a cache based on a source-control history. TODO : Convert .gitignore to radon ignore patterns to make the build more efficient. """ import pathlib import multiprocessing from progress.bar import Bar from wily import logger from wily.state import State from wily.archivers.git import InvalidGitRepositoryError from wily.archivers import FilesystemArchiver from wily.operators import resolve_operator def run_operator(operator, revision, config): """Run an operator for the multiprocessing pool. Not called directly.""" instance = operator.cls(config) logger.debug(f"Running {operator.name} operator on {revision.key}") return operator.name, instance.run(revision, config) def build(config, archiver, operators): """ Build the history given a archiver and collection of operators. :param config: The wily configuration :type config: :namedtuple:`wily.config.WilyConfig` :param archiver: The archiver to use :type archiver: :namedtuple:`wily.archivers.Archiver` :param operators: The list of operators to execute :type operators: `list` of :namedtuple:`wily.operators.Operator` """ try: logger.debug(f"Using {archiver.name} archiver module") archiver = archiver.cls(config) revisions = archiver.revisions(config.path, config.max_revisions) except InvalidGitRepositoryError: # TODO: This logic shouldn't really be here (SoC) logger.info(f"Defaulting back to the filesystem archiver, not a valid git repo") archiver = FilesystemArchiver(config) revisions = archiver.revisions(config.path, config.max_revisions) except Exception as e: if hasattr(e, "message"): logger.error(f"Failed to setup archiver: '{e.message}'") else: logger.error(f"Failed to setup archiver: '{type(e)} - {e}'") exit(1) state = State(config, archiver=archiver) # Check for existence of cache, else provision state.ensure_exists() index = state.index[archiver.name] # remove existing revisions from the list revisions = [revision for revision in revisions if revision not in index] logger.info( f"Found {len(revisions)} revisions from '{archiver.name}' archiver in '{config.path}'." ) _op_desc = ",".join([operator.name for operator in operators]) logger.info(f"Running operators - {_op_desc}") bar = Bar("Processing", max=len(revisions) * len(operators)) state.operators = operators try: with multiprocessing.Pool(processes=len(operators)) as pool: for revision in revisions: # Checkout target revision archiver.checkout(revision, config.checkout_options) stats = {"operator_data": {}} # Run each operator as a seperate process data = pool.starmap( run_operator, [(operator, revision, config) for operator in operators], ) # Map the data back into a dictionary for operator_name, result in data: # aggregate values to directories roots = [] # find all unique directories in the results for entry in result.keys(): parent = pathlib.Path(entry).parents[0] if parent not in roots: roots.append(parent) for root in roots: # find all matching entries recursively aggregates = [ path for path in result.keys() if root in pathlib.Path(path).parents ] result[str(root)] = {"total": {}} # aggregate values for metric in resolve_operator(operator_name).cls.metrics: func = metric.aggregate values = [ result[aggregate]["total"][metric.name] for aggregate in aggregates if aggregate in result and metric.name in result[aggregate]["total"] ] if len(values) > 0: result[str(root)]["total"][metric.name] = func(values) stats["operator_data"][operator_name] = result bar.next() ir = index.add(revision, operators=operators) ir.store(config, archiver, stats) index.save() bar.finish() except Exception as e: logger.error(f"Failed to build cache: '{e}'") raise e finally: # Reset the archive after every run back to the head of the branch archiver.finish() PKg6O؏8Jsswily/commands/diff.py""" Diff command. Compares metrics between uncommitted files and indexed files. """ import os import tabulate from wily import logger from wily.config import DEFAULT_GRID_STYLE from wily.operators import ( resolve_metric, resolve_operator, get_metric, GOOD_COLORS, BAD_COLORS, OperatorLevel, ) from wily.state import State def diff(config, files, metrics, changes_only=True, detail=True): """ Show the differences in metrics for each of the files. :param config: The wily configuration :type config: :namedtuple:`wily.config.WilyConfig` :param files: The files to compare. :type files: ``list`` of ``str`` :param metrics: The metrics to measure. :type metrics: ``list`` of ``str`` :param changes_only: Only include changes files in output. :type changes_only: ``bool`` :param detail: Show details (function-level) :type detail: ``bool`` """ config.targets = files files = list(files) state = State(config) last_revision = state.index[state.default_archiver].revisions[0] # Convert the list of metrics to a list of metric instances operators = {resolve_operator(metric.split(".")[0]) for metric in metrics} metrics = [(metric.split(".")[0], resolve_metric(metric)) for metric in metrics] data = {} results = [] # Build a set of operators _operators = [operator.cls(config) for operator in operators] cwd = os.getcwd() os.chdir(config.path) for operator in _operators: logger.debug(f"Running {operator.name} operator") data[operator.name] = operator.run(None, config) os.chdir(cwd) # Write a summary table.. extra = [] for operator, metric in metrics: if detail and resolve_operator(operator).level == OperatorLevel.Object: for file in files: try: extra.extend( [ f"{file}:{k}" for k in data[operator][file]["detailed"].keys() if k != metric.name and isinstance(data[operator][file]["detailed"][k], dict) ] ) except KeyError: logger.debug(f"File {file} not in cache") logger.debug("Cache follows -- ") logger.debug(data[operator]) files.extend(extra) logger.debug(files) for file in files: metrics_data = [] has_changes = False for operator, metric in metrics: try: current = last_revision.get( config, state.default_archiver, operator, file, metric.name ) except KeyError as e: current = "-" try: new = get_metric(data, operator, file, metric.name) except KeyError as e: new = "-" if new != current: has_changes = True if metric.type in (int, float) and new != "-" and current != "-": if current > new: metrics_data.append( "{0:n} -> \u001b[{2}m{1:n}\u001b[0m".format( current, new, BAD_COLORS[metric.measure] ) ) elif current < new: metrics_data.append( "{0:n} -> \u001b[{2}m{1:n}\u001b[0m".format( current, new, GOOD_COLORS[metric.measure] ) ) else: metrics_data.append("{0:n} -> {1:n}".format(current, new)) else: if current == "-" and new == "-": metrics_data.append("-") else: metrics_data.append("{0} -> {1}".format(current, new)) if has_changes or not changes_only: results.append((file, *metrics_data)) else: logger.debug(metrics_data) descriptions = [metric.description for operator, metric in metrics] headers = ("File", *descriptions) if len(results) > 0: print( # But it still makes more sense to show the newest at the top, so reverse again tabulate.tabulate( headers=headers, tabular_data=results, tablefmt=DEFAULT_GRID_STYLE ) ) PK6OIIwily/commands/graph.py""" Draw graph in HTML for a specific metric. TODO: Add multiple lines for multiple files """ import pathlib import plotly.graph_objs as go import plotly.offline from wily import logger, format_datetime from wily.operators import resolve_metric, resolve_metric_as_tuple from wily.state import State def metric_parts(metric): """Convert a metric name into the operator and metric names.""" operator, met = resolve_metric_as_tuple(metric) return operator.name, met.name def graph(config, path, metrics, output=None, x_axis=None, changes=True, text=False): """ Graph information about the cache and runtime. :param config: The configuration. :type config: :class:`wily.config.WilyConfig` :param path: The path to the files. :type path: ``list`` :param metrics: The Y and Z-axis metrics to report on. :type metrics: ``tuple`` :param output: Save report to specified path instead of opening browser. :type output: ``str`` """ logger.debug("Running report command") data = [] state = State(config) abs_path = config.path / pathlib.Path(path) if x_axis is None: x_axis = "history" else: x_operator, x_key = metric_parts(x_axis) if abs_path.is_dir(): paths = [ p.relative_to(config.path) for p in pathlib.Path(abs_path).glob("**/*.py") ] else: paths = [path] operator, key = metric_parts(metrics[0]) if len(metrics) == 1: # only y-axis z_axis = None else: z_axis = resolve_metric(metrics[1]) z_operator, z_key = metric_parts(metrics[1]) for path in paths: x = [] y = [] z = [] labels = [] last_y = None for rev in state.index[state.default_archiver].revisions: labels.append(f"{rev.revision.author_name}
{rev.revision.message}") try: val = rev.get(config, state.default_archiver, operator, str(path), key) if val != last_y or not changes: y.append(val) if z_axis: z.append( rev.get( config, state.default_archiver, z_operator, str(path), z_key, ) ) if x_axis == "history": x.append(format_datetime(rev.revision.date)) else: x.append( rev.get( config, state.default_archiver, x_operator, str(path), x_key, ) ) last_y = val except KeyError: # missing data pass # Create traces trace = go.Scatter( x=x, y=y, mode="lines+markers+text" if text else "lines+markers", name=f"{path}", ids=state.index[state.default_archiver].revision_keys, text=labels, marker=dict( size=0 if z_axis is None else z, color=list(range(len(y))), # colorscale='Viridis', ), xcalendar="gregorian", hoveron="points+fills", ) data.append(trace) if output: filename = output auto_open = False else: filename = "wily-report.html" auto_open = True y_metric = resolve_metric(metrics[0]) title = f"{x_axis.capitalize()} of {y_metric.description} for {path}" plotly.offline.plot( { "data": data, "layout": go.Layout( title=title, xaxis={"title": x_axis}, yaxis={"title": y_metric.description}, ), }, auto_open=auto_open, filename=filename, ) PKRyMabbwily/commands/index.py""" Print command. Print information about the wily cache and what is in the index. """ import tabulate from wily import logger, format_date, format_revision, MAX_MESSAGE_WIDTH from wily.config import DEFAULT_GRID_STYLE from wily.state import State def index(config, include_message=False): """ Show information about the cache and runtime. :param config: The wily configuration :type config: :namedtuple:`wily.config.WilyConfig` :param include_message: Include revision messages :type include_message: ``bool`` """ state = State(config=config) logger.debug("Running show command") logger.info("--------Configuration---------") logger.info(f"Path: {config.path}") logger.info(f"Archiver: {config.archiver}") logger.info(f"Operators: {config.operators}") logger.info("") logger.info("-----------History------------") data = [] for archiver in state.archivers: for rev in state.index[archiver].revisions: if include_message: data.append( ( format_revision(rev.revision.key), rev.revision.author_name, rev.revision.message[:MAX_MESSAGE_WIDTH], format_date(rev.revision.date), ) ) else: data.append( ( format_revision(rev.revision.key), rev.revision.author_name, format_date(rev.revision.date), ) ) if include_message: headers = ("Revision", "Author", "Message", "Date") else: headers = ("Revision", "Author", "Date") print( tabulate.tabulate( headers=headers, tabular_data=data, tablefmt=DEFAULT_GRID_STYLE ) ) PKRyMAEHwily/commands/list_metrics.py""" List available metrics across all providers. TODO : Only show metrics for the operators that the cache has? """ import tabulate from wily.config import DEFAULT_GRID_STYLE from wily.operators import ALL_OPERATORS def list_metrics(): """List metrics available.""" for name, operator in ALL_OPERATORS.items(): print(f"{name} operator:") if len(operator.cls.metrics) > 0: print( tabulate.tabulate( headers=("Name", "Description", "Type"), tabular_data=operator.cls.metrics, tablefmt=DEFAULT_GRID_STYLE, ) ) PKg6OG'wily/commands/report.py""" Report command. The report command gives a table of metrics for a specified list of files. Will compare the values between revisions and highlight changes in green/red. """ import tabulate from pathlib import Path from shutil import copytree from string import Template from wily import logger, format_date, format_revision, MAX_MESSAGE_WIDTH from wily.helper.custom_enums import ReportFormat from wily.operators import resolve_metric_as_tuple, MetricType from wily.state import State def report( config, path, metrics, n, output, include_message=False, format=ReportFormat.CONSOLE, console_format=None, ): """ Show information about the cache and runtime. :param config: The configuration :type config: :class:`wily.config.WilyConfig` :param path: The path to the file :type path: ``str`` :param metrics: Name of the metric to report on :type metrics: ``str`` :param n: Number of items to list :type n: ``int`` :param output: Output path :type output: ``Path`` :param include_message: Include revision messages :type include_message: ``bool`` :param format: Output format :type format: ``ReportFormat`` :param console_format: Grid format style for tabulate :type console_format: ``str`` """ logger.debug("Running report command") logger.info(f"-----------History for {metrics}------------") data = [] metric_metas = [] for metric in metrics: operator, metric = resolve_metric_as_tuple(metric) key = metric.name operator = operator.name # Set the delta colors depending on the metric type if metric.measure == MetricType.AimHigh: good_color = 32 bad_color = 31 elif metric.measure == MetricType.AimLow: good_color = 31 bad_color = 32 elif metric.measure == MetricType.Informational: good_color = 33 bad_color = 33 metric_meta = { "key": key, "operator": operator, "good_color": good_color, "bad_color": bad_color, "title": metric.description, "type": metric.type, } metric_metas.append(metric_meta) state = State(config) for archiver in state.archivers: # We have to do it backwards to get the deltas between releases history = state.index[archiver].revisions[:n][::-1] last = {} for rev in history: vals = [] for meta in metric_metas: try: logger.debug( f"Fetching metric {meta['key']} for {meta['operator']} in {path}" ) val = rev.get(config, archiver, meta["operator"], path, meta["key"]) last_val = last.get(meta["key"], None) # Measure the difference between this value and the last if meta["type"] in (int, float): if last_val: delta = val - last_val else: delta = 0 last[meta["key"]] = val else: # TODO : Measure ranking increases/decreases for str types? delta = 0 if delta == 0: delta_col = delta elif delta < 0: delta_col = f"\u001b[{meta['good_color']}m{delta:n}\u001b[0m" else: delta_col = f"\u001b[{meta['bad_color']}m+{delta:n}\u001b[0m" if meta["type"] in (int, float): k = f"{val:n} ({delta_col})" else: k = f"{val}" except KeyError as e: k = f"Not found {e}" vals.append(k) if include_message: data.append( ( format_revision(rev.revision.key), rev.revision.message[:MAX_MESSAGE_WIDTH], rev.revision.author_name, format_date(rev.revision.date), *vals, ) ) else: data.append( ( format_revision(rev.revision.key), rev.revision.author_name, format_date(rev.revision.date), *vals, ) ) descriptions = [meta["title"] for meta in metric_metas] if include_message: headers = ("Revision", "Message", "Author", "Date", *descriptions) else: headers = ("Revision", "Author", "Date", *descriptions) if format == ReportFormat.HTML: if output.is_file and output.suffix == ".html": report_path = output.parents[0] report_output = output else: report_path = output report_output = output.joinpath("index.html") report_path.mkdir(exist_ok=True, parents=True) templates_dir = (Path(__file__).parents[1] / "templates").resolve() report_template = Template((templates_dir / "report_template.html").read_text()) table_headers = "".join([f"{header}" for header in headers]) table_content = "" for line in data[::-1]: table_content += "" for element in line: element = element.replace("[32m", "") element = element.replace("[31m", "") element = element.replace("[33m", "") element = element.replace("[0m", "") table_content += f"{element}" table_content += "" report_template = report_template.safe_substitute( headers=table_headers, content=table_content ) with report_output.open("w") as output: output.write(report_template) try: copytree(str(templates_dir / "css"), str(report_path / "css")) except FileExistsError: pass logger.info(f"wily report was saved to {report_path}") else: print( # But it still makes more sense to show the newest at the top, so reverse again tabulate.tabulate( headers=headers, tabular_data=data[::-1], tablefmt=console_format ) ) PK+bCN&wily/helper/__init__.py"""Helper package for wily.""" PK+bCNA`^ ==wily/helper/custom_enums.py"""A module containing custom enums for wily.""" from enum import Enum class ReportFormat(Enum): """Represent the available report formats.""" CONSOLE = 1 HTML = 2 @classmethod def get_all(cls): """Return a list with all Enumerations.""" return [format.name for format in cls] PKg6OBwily/operators/__init__.py"""Models and types for "operators" the basic measure of a module that measures code.""" from collections import namedtuple from enum import Enum from functools import lru_cache class MetricType(Enum): """Type of metric, used in trends.""" AimLow = 1 # Low is good, high is bad AimHigh = 2 # High is good, low is bad Informational = 3 # Doesn't matter Metric = namedtuple("Metric", "name description type measure aggregate") GOOD_COLORS = { MetricType.AimHigh: 32, MetricType.AimLow: 31, MetricType.Informational: 33, } BAD_COLORS = { MetricType.AimHigh: 31, MetricType.AimLow: 32, MetricType.Informational: 33, } class OperatorLevel(Enum): """Level of operator.""" File = 1 Object = 2 class BaseOperator(object): """Abstract Operator Class.""" """Name of the operator.""" name = "abstract" """Default settings.""" defaults = {} """Available metrics as a list of tuple ("name", "description", "type", "metric_type").""" metrics = () """Which metric is the default to display in the report command.""" default_metric_index = None """Level at which the operator goes to.""" level = OperatorLevel.File def run(self, module, options): """ Run the operator. :param module: The target module path. :type module: ``str`` :param options: Any runtime options. :type options: ``dict`` :return: The operator results. :rtype: ``dict`` """ raise NotImplementedError from wily.operators.cyclomatic import CyclomaticComplexityOperator from wily.operators.maintainability import MaintainabilityIndexOperator from wily.operators.raw import RawMetricsOperator from wily.operators.halstead import HalsteadOperator """Type for an operator.""" Operator = namedtuple("Operator", "name cls description level") OPERATOR_CYCLOMATIC = Operator( name="cyclomatic", cls=CyclomaticComplexityOperator, description="Cyclomatic Complexity of modules", level=OperatorLevel.Object, ) OPERATOR_RAW = Operator( name="raw", cls=RawMetricsOperator, description="Raw Python statistics", level=OperatorLevel.File, ) OPERATOR_MAINTAINABILITY = Operator( name="maintainability", cls=MaintainabilityIndexOperator, description="Maintainability index (lines of code and branching)", level=OperatorLevel.File, ) OPERATOR_HALSTEAD = Operator( name="halstead", cls=HalsteadOperator, description="Halstead metrics", level=OperatorLevel.Object, ) """Dictionary of all operators""" ALL_OPERATORS = { operator.name: operator for operator in { OPERATOR_CYCLOMATIC, OPERATOR_MAINTAINABILITY, OPERATOR_RAW, OPERATOR_HALSTEAD, } } """Set of all metrics""" ALL_METRICS = { (operator, metric) for operator in ALL_OPERATORS.values() for metric in operator.cls.metrics } @lru_cache(maxsize=128) def resolve_operator(name): """ Get the :namedtuple:`wily.operators.Operator` for a given name. :param name: The name of the operator :return: The operator type """ if name.lower() in ALL_OPERATORS: return ALL_OPERATORS[name.lower()] else: raise ValueError(f"Operator {name} not recognised.") def resolve_operators(operators): """ Resolve a list of operator names to their corresponding types. :param operators: The list of operators :type operators: iterable or ``str`` :rtype: ``list`` of :class:`Operator` """ return [resolve_operator(operator) for operator in iter(operators)] @lru_cache(maxsize=128) def resolve_metric(metric): """ Resolve metric key to a given target. :param metric: the metric name. :type metric: ``str`` :rtype: :class:`Metric` """ return resolve_metric_as_tuple(metric)[1] @lru_cache(maxsize=128) def resolve_metric_as_tuple(metric): """ Resolve metric key to a given target. :param metric: the metric name. :type metric: ``str`` :rtype: :class:`Metric` """ if "." in metric: _, metric = metric.split(".") r = [(operator, match) for operator, match in ALL_METRICS if match[0] == metric] if not r or len(r) == 0: raise ValueError(f"Metric {metric} not recognised.") else: return r[0] def get_metric(revision, operator, path, key): """ Get a metric from the cache. :param revision: The revision data. :type revision: ``dict`` :param operator: The operator name. :type operator: ``str`` :param path: The path to the file/function :type path: ``str`` :param key: The key of the data :type key: ``str`` :return: Data from the cache :rtype: ``dict`` """ if ":" in path: part, entry = path.split(":") val = revision[operator][part]["detailed"][entry][key] else: val = revision[operator][path]["total"][key] return val PKg6O0  wily/operators/cyclomatic.py""" Cyclomatic complexity metric for each function/method. Provided by the radon library. """ import statistics import radon import radon.cli.harvest as harvesters from radon.cli import Config from radon.visitors import Function, Class from wily import logger from wily.operators import BaseOperator, Metric, MetricType class CyclomaticComplexityOperator(BaseOperator): """Cyclomatic complexity operator.""" name = "cyclomatic" defaults = { "exclude": None, "ignore": None, "min": "A", "max": "F", "no_assert": True, "show_closures": False, "order": radon.complexity.SCORE, } metrics = ( Metric( "complexity", "Cyclomatic Complexity", float, MetricType.AimLow, statistics.mean, ), ) default_metric_index = 0 # MI def __init__(self, config): """ Instantiate a new Cyclomatic Complexity operator. :param config: The wily configuration. :type config: :class:`WilyConfig` """ # TODO: Import config for harvester from .wily.cfg logger.debug(f"Using {config.targets} with {self.defaults} for CC metrics") self.harvester = harvesters.CCHarvester( config.targets, config=Config(**self.defaults) ) def run(self, module, options): """ Run the operator. :param module: The target module path. :type module: ``str`` :param options: Any runtime options. :type options: ``dict`` :return: The operator results. :rtype: ``dict`` """ logger.debug("Running CC harvester") results = {} for filename, details in dict(self.harvester.results).items(): results[filename] = {"detailed": {}, "total": {}} total = 0 # running CC total for instance in details: if isinstance(instance, Class): i = self._dict_from_class(instance) elif isinstance(instance, Function): i = self._dict_from_function(instance) else: if isinstance(instance, str) and instance == "error": logger.warning( f"Failed to run CC harvester on {filename} : {details['error']}" ) continue else: logger.warning( f"Unexpected result from Radon : {instance} of {type(instance)}. Please report on Github." ) continue results[filename]["detailed"][i["fullname"]] = i del i["fullname"] total += i["complexity"] results[filename]["total"]["complexity"] = total return results @staticmethod def _dict_from_function(l): return { "name": l.name, "is_method": l.is_method, "classname": l.classname, "closures": l.closures, "complexity": l.complexity, "fullname": l.fullname, "loc": l.endline - l.lineno, } @staticmethod def _dict_from_class(l): return { "name": l.name, "inner_classes": l.inner_classes, "real_complexity": l.real_complexity, "complexity": l.complexity, "fullname": l.fullname, "loc": l.endline - l.lineno, } PKg6O  wily/operators/halstead.py""" Halstead operator. Measures all of the halstead metrics (volume, vocab, difficulty) """ import radon.cli.harvest as harvesters from radon.cli import Config from wily import logger from wily.operators import BaseOperator, MetricType, Metric class HalsteadOperator(BaseOperator): """Halstead Operator.""" name = "halstead" defaults = { "exclude": None, "ignore": None, "min": "A", "max": "C", "multi": True, "show": False, "sort": False, "by_function": True, } metrics = ( Metric("h1", "Unique Operands", int, MetricType.AimLow, sum), Metric("h2", "Unique Operators", int, MetricType.AimLow, sum), Metric("N1", "Number of Operands", int, MetricType.AimLow, sum), Metric("N2", "Number of Operators", int, MetricType.AimLow, sum), Metric( "vocabulary", "Unique vocabulary (h1 + h2)", int, MetricType.AimLow, sum ), Metric("length", "Length of application", int, MetricType.AimLow, sum), Metric("volume", "Code volume", float, MetricType.AimLow, sum), Metric("difficulty", "Difficulty", float, MetricType.AimLow, sum), Metric("effort", "Effort", float, MetricType.AimLow, sum), ) default_metric_index = 0 # MI def __init__(self, config): """ Instantiate a new HC operator. :param config: The wily configuration. :type config: :class:`WilyConfig` """ # TODO : Import config from wily.cfg logger.debug(f"Using {config.targets} with {self.defaults} for HC metrics") self.harvester = harvesters.HCHarvester( config.targets, config=Config(**self.defaults) ) def run(self, module, options): """ Run the operator. :param module: The target module path. :type module: ``str`` :param options: Any runtime options. :type options: ``dict`` :return: The operator results. :rtype: ``dict`` """ logger.debug("Running halstead harvester") results = {} for filename, details in dict(self.harvester.results).items(): results[filename] = {"detailed": {}, "total": {}} for instance in details: if isinstance(instance, list): for item in instance: function, report = item results[filename]["detailed"][function] = self._report_to_dict(report) else: if isinstance(instance, str) and instance == "error": logger.warning( f"Failed to run Halstead harvester on {filename} : {details['error']}" ) continue results[filename]["total"] = self._report_to_dict(instance) return results def _report_to_dict(self, report): return { "h1": report.h1, "h2": report.h2, "N1": report.N1, "N2": report.N2, "vocabulary": report.vocabulary, "volume": report.volume, "length": report.length, "effort": report.effort, "difficulty": report.difficulty, } PKg6O!wily/operators/maintainability.py""" Maintainability operator. Measures the "maintainability" using the Halstead index. """ import statistics from collections import Counter import radon.cli.harvest as harvesters from radon.cli import Config from wily import logger from wily.operators import BaseOperator, MetricType, Metric def mode(data): """ Return the modal value of a iterable with discrete values. If there is more than 1 modal value, arbritrarily return the first top n. """ c = Counter(data) mode, freq = c.most_common(1)[0] return mode class MaintainabilityIndexOperator(BaseOperator): """MI Operator.""" name = "maintainability" defaults = { "exclude": None, "ignore": None, "min": "A", "max": "C", "multi": True, "show": False, "sort": False, } metrics = ( Metric("rank", "Maintainability Ranking", str, MetricType.Informational, mode), Metric( "mi", "Maintainability Index", float, MetricType.AimLow, statistics.mean ), ) default_metric_index = 1 # MI def __init__(self, config): """ Instantiate a new MI operator. :param config: The wily configuration. :type config: :class:`WilyConfig` """ # TODO : Import config from wily.cfg logger.debug(f"Using {config.targets} with {self.defaults} for MI metrics") self.harvester = harvesters.MIHarvester( config.targets, config=Config(**self.defaults) ) def run(self, module, options): """ Run the operator. :param module: The target module path. :type module: ``str`` :param options: Any runtime options. :type options: ``dict`` :return: The operator results. :rtype: ``dict`` """ logger.debug("Running maintainability harvester") results = {} for filename, metrics in dict(self.harvester.results).items(): results[filename] = {"total": metrics} return results PKg6Owily/operators/raw.py""" Raw statistics operator. Includes insights like lines-of-code, number of comments. Does not measure complexity. """ import radon.cli.harvest as harvesters from radon.cli import Config from wily import logger from wily.operators import BaseOperator, MetricType, Metric class RawMetricsOperator(BaseOperator): """Raw Metrics Operator.""" name = "raw" defaults = {"exclude": None, "ignore": None, "summary": False} metrics = ( Metric("loc", "Lines of Code", int, MetricType.Informational, sum), Metric("lloc", "L Lines of Code", int, MetricType.AimLow, sum), Metric("sloc", "S Lines of Code", int, MetricType.AimLow, sum), Metric("comments", "Multi-line comments", int, MetricType.AimHigh, sum), Metric("multi", "Multi lines", int, MetricType.Informational, sum), Metric("blank", "blank lines", int, MetricType.Informational, sum), Metric( "single_comments", "Single comment lines", int, MetricType.Informational, sum, ), ) default_metric_index = 0 # LOC def __init__(self, config): """ Instantiate a new raw operator. :param config: The wily configuration. :type config: :class:`WilyConfig` """ # TODO: Use config from wily.cfg for harvester logger.debug(f"Using {config.targets} with {self.defaults} for Raw metrics") self.harvester = harvesters.RawHarvester( config.targets, config=Config(**self.defaults) ) def run(self, module, options): """ Run the operator. :param module: The target module path. :type module: ``str`` :param options: Any runtime options. :type options: ``dict`` :return: The operator results. :rtype: ``dict`` """ logger.debug("Running raw harvester") results = {} for filename, metrics in dict(self.harvester.results).items(): results[filename] = {"total": metrics} return results PK+bCNzz#wily/templates/report_template.html wily report
$headers $content
PK6ONh wily/templates/css/main.css /*////////////////////////////////////////////////////////////////// [ RESTYLE TAG ]*/ * { margin: 0px; padding: 0px; box-sizing: border-box; } body, html { height: 100%; font-family: sans-serif; } /* ------------------------------------ */ a { margin: 0px; transition: all 0.4s; -webkit-transition: all 0.4s; -o-transition: all 0.4s; -moz-transition: all 0.4s; } a:focus { outline: none !important; } a:hover { text-decoration: none; } /* ------------------------------------ */ h1,h2,h3,h4,h5,h6 {margin: 0px;} p {margin: 0px;} ul, li { margin: 0px; list-style-type: none; } /*////////////////////////////////////////////////////////////////// [ Table ]*/ .limiter { width: 100%; margin: 0 auto; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; } .container-table100 { width: 100%; min-height: 100vh; background: #c4d3f6; display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms-flexbox; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; padding: 33px 30px; } .wrap-table100 { width: 960px; border-radius: 10px; overflow: hidden; } table { width: 100%; display: table; margin: 0; } td, th { text-align: center; padding: 10px; } @media screen and (max-width: 768px) { table { display: block; } } tr { display: table-row; background: #fff; } thead tr { color: #ffffff; background: #6c7ae0; } @media screen and (max-width: 768px) { tr { display: block; } thead tr { padding: 0; height: 0px; } thead tr th { display: none; } tr td:before { font-size: 12px; color: #808080; line-height: 1.2; text-transform: uppercase; font-weight: unset !important; margin-bottom: 13px; content: attr(data-title); min-width: 98px; display: block; } } td { display: table-cell; } @media screen and (max-width: 768px) { td { display: block; } } tr td { font-size: 15px; color: #666666; line-height: 1.2; font-weight: unset !important; padding-top: 20px; padding-bottom: 20px; border-bottom: 1px solid #f2f2f2; } tbody tr th { font-size: 18px; color: #fff; line-height: 1.2; font-weight: unset !important; padding-top: 19px; padding-bottom: 19px; } table, tr { width: 100% !important; } tr:hover { background-color: #ececff; cursor: pointer; } @media (max-width: 768px) { tr { border-bottom: 1px solid #f2f2f2; padding-bottom: 18px; padding-top: 30px; padding-right: 15px; margin: 0; } tr td { border: none; padding-left: 30px; padding-top: 16px; padding-bottom: 16px; } tr td:nth-child(1) { padding-left: 30px; } tr td { font-size: 18px; color: #555555; line-height: 1.2; font-weight: unset !important; } table, tr, td { width: 100% !important; } } .green-color { color: green; } .red-color { color: darkred; } .orange-color { color: darkorange; }PK!H=d)*&wily-1.12.4.dist-info/entry_points.txtN+I/N.,()*̩z񹉙yV9\\PK\OM]{],],wily-1.12.4.dist-info/LICENSE Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. PK!H>*RQwily-1.12.4.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,rzd&Y)r$[)T&UrPK!H+%B b%wily-1.12.4.dist-info/METADATAZnΧ$+T9ɝ b_$jZnIdS'((Т@ 4yΐ+ZV.- g8̏Y?X.{옏ERآ}/ۏojF`B *5P*!NEsp y\7$[)&p9vBYY~FWYg`dOgD8'yULu)䩉xJ cAJX@G@Ƨ+HPe.J0CBI&P 3HgfPjd-؉~eON| J)T EAV5XP)L$G,jϑE3؁I4VQ U2`@fvF"<]6B4OBk&G\J#`CJi텃Ak)F2g|pʶ"o.(p+Zc=RL ꟢IMhmJvR9-:r$g*hU*gSwA["᪟O [iBh =l@p:gvzJHk@Õ!D )bFLQ4EG2' p6@;>cWqY+g_=?>y g/}|z:G™r{=TߎE4' Gǧ}]r\C uT i ~ ؟v? SM<u) G1Nt1)ʦ5;`E#ч%)>>I~ƘQZj]HxN"\Ia;@3ءt)}Zhq0 VjC*B)8f袸TH$!)&>-X1zАQF^(ɋ9!QOo G Pgw0Wg@D1i CP %KT(GpS^D9$=/YSxaUFm O1I!]?Z}en x[y&㣆0֚ ldǀUw-| |O1|)ƛm dM|wLX._/~2Cŋ%|C|7o}Y_6u}2l"=T!YKf҄H{r_ }l}=ikʛwfIL7C$ctɹ^bZm9s촞u9.K8QIc)Wp?7BI LXctUj^ݔ|E&NFWGjs45@F/Lû2SN"sXᨤhcN' "[v*Z[#,176Bȧ)kk9w]Τc=u Jr4.ψAv8n2A]/ 'E؃`2s'jD+3+2b*áwaE1Dk%fpTK'Jޜ?~ W"ś0B%c_%#1\S>߯ Om#D)[m(4(=GBٽnPK!H;jC wily-1.12.4.dist-info/RECORDuGh%QArdn5N*Sߛ:ov,@p~!*/S50Y6I;âh )өIҲ^lsιdSfy3X MƏ&4|Aafb tm%Jj3(':[VeQM $/<}cS7!64R VVXQ,cW}iZ!'acQ4zW?ئa4 Oa.S@;˲K9 NA0{X7w0.۩ؗn5<!̟)[,t@}sj4mc{?FR&ǂ1w2x؆1P 8Qm-orŧb*yGɉe&_45{za ":G3f Ț`#JpFW1*=ҡ L5B1,Hs@o} fe!ǿaAx0qQ$%{ѕz[~9؝6i\ 9&Fߴk8m4qZݞ.هGpTw=8k^GeG.%S(rXpƤU0EC߸2[}V,7əpT+j>m_T :!DCm~!kLcXM,X \%+ VA|=26-rs4aM8?PuS8iב>P'-nq=U'.["cT\)8U#+y %4p(B.QThtʔW:bǖrBdeSc7OIPȎݐw/v.f!oʟg ~M#ڧ;~͞c\^DtVUͤLdL=$bZˣ Ew0ȳ/s]G0z}&P<+k^o;O\PU^`?΅*>6rDB QLȘ '[qC7ڒݰO8lvl4}ʨ'?:*PK 6O'pwily/__init__.pyPKg6O0%%Cwily/__main__.pyPK6O@>UU )wily/cache.pyPK6OYGwily/config.pyPK~MGrWwily/decorators.pyPK6O / 2Ywily/state.pyPKRyMj~~Zqwily/archivers/__init__.pyPK6O7zwily/archivers/filesystem.pyPK6OV}p p ހwily/archivers/git.pyPKRyMh''wily/commands/__init__.pyPKg6O ddߌwily/commands/build.pyPKg6O؏8Jsswwily/commands/diff.pyPK6OIIwily/commands/graph.pyPKRyMabbwily/commands/index.pyPKRyMAEH0wily/commands/list_metrics.pyPKg6OG'wily/commands/report.pyPK+bCN&/wily/helper/__init__.pyPK+bCNA`^ ==wily/helper/custom_enums.pyPKg6OBwily/operators/__init__.pyPKg6O0  wily/operators/cyclomatic.pyPKg6O   wily/operators/halstead.pyPKg6O!Pwily/operators/maintainability.pyPKg6O wily/operators/raw.pyPK+bCNzz#(wily/templates/report_template.htmlPK6ONh 큭+wily/templates/css/main.cssPK!H=d)*&v8wily-1.12.4.dist-info/entry_points.txtPK\OM]{],],8wily-1.12.4.dist-info/LICENSEPK!H>*RQ{ewily-1.12.4.dist-info/WHEELPK!H+%B b%fwily-1.12.4.dist-info/METADATAPK!H;jC pwily-1.12.4.dist-info/RECORDPK;Cv