Skip to content

Commit

Permalink
adding functiondb extractor, needs more testing
Browse files Browse the repository at this point in the history
Signed-off-by: vsoch <vsochat@stanford.edu>
  • Loading branch information
vsoch committed Jan 13, 2021
1 parent 1ea6a38 commit 5820088
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and **Merged pull requests**. Critical items to know are:
The versions coincide with releases on pip.

## [0.2.x](https://github.com/vsoch/caliper/tree/master) (0.0.x)
- adding functiondb metric extractor (0.0.15)
- small bux fixes and addition of docs (0.0.14)
- adding caliper analyzer (0.0.13)
- ability to specify architecture and python version for packages (0.0.12)
Expand Down
84 changes: 84 additions & 0 deletions caliper/metrics/collection/functiondb/metric.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
__author__ = "Vanessa Sochat"
__copyright__ = "Copyright 2020-2021, Vanessa Sochat"
__license__ = "MPL 2.0"

from caliper.metrics.base import MetricBase
from caliper.utils.file import recursive_find
from inspect import getmembers, isfunction

import inspect
import os
import importlib
import sys


class Functiondb(MetricBase):

name = "functiondb"
description = "for each commit, derive a function database lookup"

def __init__(self, git):
super().__init__(git, __file__)

def _extract(self, commit):

# We will return the lookup
lookup = {}

# Add the temporary directory to the PYTHONPATH
sys.path.insert(0, self.git.folder)

# Helper function to populate lookup
def add_functions(module):
# member[0] is function name, member[1] is function
for member in getmembers(module, isfunction):
lookup[modulepath][member[0]] = {
key: param.default if not inspect._empty else "inspect._empty"
for key, param in dict(
inspect.signature(member[1]).parameters
).items()
}

# Look for folders with an init
for folder in recursive_find(self.git.folder, "__init__.py"):

# First try importing the top level modules
modulepath = ".".join(
os.path.dirname(folder)
.replace(self.git.folder, "")
.strip("/")
.split("/")
)
lookup[modulepath] = {}
module = importlib.import_module(modulepath)
add_functions(module)

# Next look for other modules in each init folder
for modulename in os.listdir(os.path.dirname(folder)):
if (
modulename
in [
"__init__.py",
"__pycache__",
]
or not modulename.endswith(".py")
):
continue

try:
module = importlib.import_module(
"%s.%s" % (modulepath, modulename.replace(".py", ""))
)
add_functions(module)
except:
pass

return lookup

def get_file_results(self):
"""return a lookup of changes, where each change has a list of files"""
return self._data

def get_group_results(self):
"""Get summed values (e.g., lines changed) across files"""
return self._data
2 changes: 1 addition & 1 deletion caliper/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__copyright__ = "Copyright 2020-2021, Vanessa Sochat"
__license__ = "MPL 2.0"

__version__ = "0.0.14"
__version__ = "0.0.15"
AUTHOR = "Vanessa Sochat"
AUTHOR_EMAIL = "vsochat@stanford.edu"
NAME = "caliper"
Expand Down

0 comments on commit 5820088

Please sign in to comment.