scent.show_notification()   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
"""Configuration file for sniffer."""
2
3
import time
4
import subprocess
5
6
from sniffer.api import select_runnable, file_validator, runnable
7
try:
8
    from pync import Notifier
9
except ImportError:
10
    notify = None
11
else:
12
    notify = Notifier.notify
13
14
15
watch_paths = ["coveragespace", "tests"]
16
17
18
class Options:
19
    group = int(time.time())  # unique per run
20
    show_coverage = False
21
    rerun_args = None
22
23
    targets = [
24
        (('make', 'test-unit', 'DISABLE_COVERAGE=true'), "Unit Tests", True),
25
        (('make', 'test-all'), "Integration Tests", False),
26
        (('make', 'check'), "Static Analysis", True),
27
        (('make', 'docs'), None, True),
28
    ]
29
30
31
@select_runnable('run_targets')
32
@file_validator
33
def python_files(filename):
34
    return filename.endswith('.py')
35
36
37
@select_runnable('run_targets')
38
@file_validator
39
def html_files(filename):
40
    return filename.split('.')[-1] in ['html', 'css', 'js']
41
42
43
@runnable
44
def run_targets(*args):
45
    """Run targets for Python."""
46
    Options.show_coverage = 'coverage' in args
47
48
    count = 0
49
    for count, (command, title, retry) in enumerate(Options.targets, start=1):
50
51
        success = call(command, title, retry)
52
        if not success:
53
            message = "✅ " * (count - 1) + "❌"
54
            show_notification(message, title)
55
56
            return False
57
58
    message = "✅ " * count
59
    title = "All Targets"
60
    show_notification(message, title)
61
    show_coverage()
62
63
    return True
64
65
66
def call(command, title, retry):
67
    """Run a command-line program and display the result."""
68
    if Options.rerun_args:
69
        command, title, retry = Options.rerun_args
70
        Options.rerun_args = None
71
        success = call(command, title, retry)
72
        if not success:
73
            return False
74
75
    print("")
76
    print("$ %s" % ' '.join(command))
77
    failure = subprocess.call(command)
78
79
    if failure and retry:
80
        Options.rerun_args = command, title, retry
81
82
    return not failure
83
84
85
def show_notification(message, title):
86
    """Show a user notification."""
87
    if notify and title:
88
        notify(message, title=title, group=Options.group)
89
90
91
def show_coverage():
92
    """Launch the coverage report."""
93
    if Options.show_coverage:
94
        subprocess.call(['make', 'read-coverage'])
95
96
    Options.show_coverage = False
97