Skip to content

Commit

Permalink
Merge pull request #2 from mcg1969/filter
Browse files Browse the repository at this point in the history
Add filtering
  • Loading branch information
mcg1969 authored Apr 1, 2019
2 parents d2e3f18 + 786f605 commit 2ea8d30
Show file tree
Hide file tree
Showing 38 changed files with 10,083 additions and 65 deletions.
19 changes: 18 additions & 1 deletion project_inspect/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,19 @@
help="Deliver the output to the named file instead of standard output.",
action="store")
parser.add_argument(
"--root", '-r',
"--root",
help="Specify the root directory of the project store.",
action="store")
parser.add_argument(
"--package", action="append",
help="""Limit the list/summary to the given package. The argument can
simply be the name of a package, or a conda-style package/version spec;
e.g., 'pandas<0.20'. Multiple --package arguments can be supplied.""")
parser.add_argument(
"--package-file", action="store",
help="""Read a list of package specs from the given file. One spec
should be supplied per line. The spec can simply be the name of a package,
or a conda-style package/version spec; e.g., 'pandas<0.20'.""")
parser.add_argument(
"--summarize", '-s',
help="""Optionally summarize the inventory. Choices include
Expand Down Expand Up @@ -64,6 +74,13 @@ def main(**kwargs):
raise RuntimeError('Must supply --owner with --project')
else:
df = project.build_node_inventory(root)
packages = kwargs.get('package') or []
package_file = kwargs.get('package_file')
if package_file:
with open(package_file, 'rt') as fp:
packages.extend(spec for spec in map(str.strip, fp) if spec)
if packages:
df = project.filter_data(df, packages)
summary = kwargs.get('summarize')
if summary:
df = project.summarize_data(df, summary)
Expand Down
2 changes: 1 addition & 1 deletion project_inspect/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def find_python_imports(code):
imports = set()
for line in map(str.strip, code.splitlines()):
if not line.startswith('#'):
imports.update(find_python_imports(line))
imports.update(find_python_imports(line.lstrip()))
return imports


Expand Down
21 changes: 21 additions & 0 deletions project_inspect/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@
from .imports import find_python_imports, find_r_imports

from .utils import logger, load_file, shortpath, set_log_root
from .version import VersionSpec

from os.path import join, isdir, isfile, basename, dirname, exists, abspath
from textwrap import TextWrapper
from glob import glob

import os
import re
import json
import pandas as pd
import numpy as np


def visible_project_environments(project_home):
Expand Down Expand Up @@ -223,6 +226,24 @@ def _build_df(records):
return df


def filter_data(df, packages):
if not packages:
return df
mask = np.zeros(len(df), dtype=bool)
for package in packages:
spec = re.match(r'^([A-Za-z0-9-_.]+)\s*(.*)', package)
if not spec:
raise RuntimeError('Invalid package spec: {}'.format(package))
name, version = spec.groups()
t_mask = df['package'] == name
if t_mask.any() and version:
vspec = VersionSpec(version)
t2_mask = df['version'][t_mask].apply(vspec.match).values
t_mask[t_mask] = t2_mask
mask = mask | t_mask
return df[mask]


def validate_summarize(level):
sep = '_' if '_' in level else '/'
parts = set(level.lower().split(sep))
Expand Down
Loading

0 comments on commit 2ea8d30

Please sign in to comment.