Skip to content

Commit

Permalink
Add pre-commit hooks, codecov and isort-check. Sort imports
Browse files Browse the repository at this point in the history
  • Loading branch information
emmanvg committed Sep 13, 2017
1 parent 3934321 commit a51e982
Show file tree
Hide file tree
Showing 23 changed files with 118 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ target/
# Automatically-built grammar tokens
*.tokens

# PyCharm
.idea/

MANIFEST
7 changes: 7 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[settings]
check=1
diff=1
known_third_party=antlr4,enum,dateutil,six,stix2patterns,typing
known_first_party=stix2matcher
not_skip=__init__.py
force_sort_within_sections=1
12 changes: 12 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
- repo: https://github.com/pre-commit/pre-commit-hooks
sha: ea227f024bd89d638aea319c92806737e3375979
hooks:
- id: trailing-whitespace
- id: flake8
args:
- --max-line-length=160
- id: check-merge-conflict
- repo: https://github.com/FalconSocial/pre-commit-python-sorter
sha: b57843b0b874df1d16eb0bef00b868792cb245c2
hooks:
- id: python-import-sorter
11 changes: 9 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
sudo: false
language: python
cache: pip
python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "3.6"
install:
- pip install -U pip setuptools
- pip install tox-travis
script: tox
- pip install tox-travis pre-commit
- pip install codecov
script:
- tox
- if [[ $TRAVIS_PYTHON_VERSION != 2.6 ]]; then pre-commit run --all-files; fi
after_success:
- codecov
18 changes: 9 additions & 9 deletions Notes.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# Implementation Notes

This document gives some additional information regarding how the matcher is
This document gives some additional information regarding how the matcher is
implemented, and some usage caveats. The emphasis in this implementation is on
simplicity, clarity, and correctness, in order to help people understand the
simplicity, clarity, and correctness, in order to help people understand the
STIX pattern language. It is not designed for "real" high-intensity usage.

## Background

Matching a pattern is equivalent to finding a set of *bindings*. A binding
is a mapping from observation expressions in the pattern to observations. The
matcher does its job in a single traversal of the pattern parse tree. It
is a mapping from observation expressions in the pattern to observations. The
matcher does its job in a single traversal of the pattern parse tree. It
maintains a list of candidate bindings and other state, pruning away those which
don't work as it goes. At the end, the result is a list of many bindings, not
just the first one found.

## Caveats

- **The number of bindings found can be large.** If each observation
expression matches a lot of observations, the number of bindings can grow
expression matches a lot of observations, the number of bindings can grow
exponentially. In the worst case, if N observations match N observation
expressions (i.e. every observation expression matches every observation), and
only the `AND` observation operator is used, that's N! different possible
Expand All @@ -42,9 +42,9 @@ first, then temporal filtering occurs second. They don't occur at the same
time. So you could still get large growth in the number of bindings in
the first step. A more clever implementation could do both at the same time.

- **"Creative" use of references can cause large memory usage.** This is a
corner case and unlikely to occur in a real pattern, but it's interesting. It's
especially easy to demonstrate with circular references. Consider the
- **"Creative" use of references can cause large memory usage.** This is a
corner case and unlikely to occur in a real pattern, but it's interesting. It's
especially easy to demonstrate with circular references. Consider the
pattern: `[foo:some_refs[*].some_refs[*].some_refs[*].some_refs[*].size > 100]`
and the observation data:
```json
Expand All @@ -67,4 +67,4 @@ and the observation data:
replaced with the same objects, which have two references apiece, etc.
Also, because of the `[*]` index steps, nothing gets pruned away; all paths
must be considered. This is exponential growth.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ source files. (The .jar file is not needed for normal use of the validator).
```bash
$ java -jar "/path/to/antlr-4.7-complete.jar" -Dlanguage=Python2 STIXPattern.g4 -o /path/to/cti-pattern-matcher/stix2matcher/grammars
```

5. Commit the resulting files to git.

## Governance
Expand Down
9 changes: 7 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
pytest==3.2.0
tox==2.7.0
bumpversion
pre-commit
pytest
pytest-cov
sphinx
sphinx-prompt
tox

-e .
10 changes: 10 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[bumpversion]
current_version = 0.1.0
commit = True
tag = True

[bumpversion:file:setup.py]

[bdist_wheel]
universal = 1

21 changes: 11 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
from setuptools import setup, find_packages
from setuptools import find_packages, setup

setup(
name='stix2-matcher',
version="0.1.0",
version='0.1.0',
packages=find_packages(),
description='Match STIX content against STIX patterns',
install_requires=[
"antlr4-python2-runtime==4.7 ; python_version < '3'",
"antlr4-python3-runtime==4.7 ; python_version >= '3'",
'typing ; python_version<"3.5" and python_version>="3"',
"enum34 ; python_version ~= '3.3.0'",
"python-dateutil",
"six",
"stix2-patterns>=0.5.0",
'antlr4-python2-runtime==4.7 ; python_version < "3"',
'antlr4-python3-runtime==4.7 ; python_version >= "3"',
'enum34 ; python_version ~= "3.3.0"',
'python-dateutil',
'six',
'stix2-patterns>=0.5.0',
'typing ; python_version < "3.5" and python_version >= "3"',
],
tests_require=[
"pytest>=2.9.2"
'pytest>=2.9.2'
],
entry_points={
'console_scripts': [
Expand All @@ -31,5 +31,6 @@
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)
12 changes: 5 additions & 7 deletions stix2matcher/matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
import base64
import binascii
import datetime
import dateutil.relativedelta
import dateutil.tz
import io
import itertools
import json
import operator
import pprint
import re
import six
import socket
import struct
import sys
import unicodedata

import antlr4
import antlr4.error.Errors
import antlr4.error.ErrorListener

import stix2patterns.pattern
import antlr4.error.Errors
import dateutil.relativedelta
import dateutil.tz
import six
from stix2patterns.grammars.STIXPatternListener import STIXPatternListener
from stix2patterns.grammars.STIXPatternParser import STIXPatternParser

import stix2patterns.pattern

# Example observed-data SDO. This represents N observations, where N is
# the value of the "number_observed" property (in this case, 5).
Expand Down
4 changes: 1 addition & 3 deletions stix2matcher/test/test_basic_ops.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest

from stix2matcher.matcher import match, MatcherException

from stix2matcher.matcher import MatcherException, match

_observations = [
{
Expand Down
2 changes: 0 additions & 2 deletions stix2matcher/test/test_binary.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest

from stix2matcher.matcher import match


_observations = [
{
"type": "observed-data",
Expand Down
2 changes: 0 additions & 2 deletions stix2matcher/test/test_comparison_exprs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import pytest

from stix2matcher.matcher import match


_observations = [
{
"type": "observed-data",
Expand Down
1 change: 0 additions & 1 deletion stix2matcher/test/test_complex.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from stix2matcher.matcher import match

_observations = [
Expand Down
3 changes: 1 addition & 2 deletions stix2matcher/test/test_matching_sdos.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import six
import pytest

import pytest
from stix2matcher.matcher import match


_observations = [
{
"id": "observed-data--a49751b8-b041-4c00-96c2-76af472bfbbe",
Expand Down
6 changes: 3 additions & 3 deletions stix2matcher/test/test_null.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from stix2matcher.matcher import match, MatcherException
from stix2patterns.pattern import ParseException

import pytest
from stix2matcher.matcher import match

_observations = [
{
"type": "observed-data",
Expand Down
4 changes: 2 additions & 2 deletions stix2matcher/test/test_object_path_quoting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pytest
from stix2patterns.pattern import ParseException

import pytest
from stix2matcher.matcher import match
from stix2patterns.pattern import ParseException

_observations = [
{
Expand Down
1 change: 0 additions & 1 deletion stix2matcher/test/test_observation_exprs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from stix2matcher.matcher import match

_observations = [
Expand Down
1 change: 0 additions & 1 deletion stix2matcher/test/test_references.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from stix2matcher.matcher import match

_observations = [
Expand Down
11 changes: 4 additions & 7 deletions stix2matcher/test/test_temporal_qualifiers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import pytest

from stix2matcher.matcher import match, MatcherException
from stix2patterns.pattern import ParseException

# I'll specially test some critical internal time-interval related code,
# since it's easier to test it separately than create lots of SDOs and
# patterns.
from stix2matcher.matcher import (_overlap, _OVERLAP_NONE, _OVERLAP,
import pytest
from stix2matcher.matcher import (_OVERLAP, _OVERLAP_NONE,
_OVERLAP_TOUCH_INNER, _OVERLAP_TOUCH_OUTER,
_OVERLAP_TOUCH_POINT)
from stix2matcher.matcher import _timestamp_intervals_within

_OVERLAP_TOUCH_POINT, MatcherException,
_overlap, _timestamp_intervals_within, match)

_observations = [
{
Expand Down
6 changes: 3 additions & 3 deletions stix2matcher/test/test_timestamps.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import pytest

from stix2matcher.matcher import match, MatcherException
from stix2patterns.pattern import ParseException

import pytest
from stix2matcher.matcher import MatcherException, match

_observations = [
{
"type": "observed-data",
Expand Down
1 change: 0 additions & 1 deletion stix2matcher/test/test_unicode_normalization.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from stix2matcher.matcher import match

_observations = [
Expand Down
39 changes: 30 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,22 +1,43 @@
[tox]
envlist = py27,py33,py34,py35,pycodestyle
envlist = py27,py33,py34,py35,py36,pycodestyle,isort-check

[testenv]
deps = pytest
commands = py.test
deps =
-U
tox
pytest
pytest-cov
coverage
commands =
py.test --cov=stix2matcher stix2matcher/test/ --cov-report term-missing

passenv = CI TRAVIS TRAVIS_*

[testenv:pycodestyle]
deps = pycodestyle
commands = pycodestyle ./stix2matcher
deps =
pycodestyle
flake8
commands =
pycodestyle ./stix2matcher
flake8

[testenv:isort-check]
deps = isort
commands =
isort -rc stix2matcher -df
isort -rc stix2matcher -c

[pycodestyle]
ignore=
max-line-length=160
exclude=grammars

[flake8]
max-line-length=160

[travis]
python =
2.7: py27, pycodestyle
3.3: py33
3.4: py34
3.5: py35
3.3: py33, pycodestyle
3.4: py34, pycodestyle
3.5: py35, pycodestyle
3.6: py36, pycodestyle

0 comments on commit a51e982

Please sign in to comment.