[LNT] r255956 - Provide a mechanism to implement LNT polices

Chris Matthews via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 17 17:28:51 PST 2015


Author: cmatthews
Date: Thu Dec 17 19:28:51 2015
New Revision: 255956

URL: http://llvm.org/viewvc/llvm-project?rev=255956&view=rev
Log:
Provide a mechanism to implement LNT polices

This commit adds a flexible rule mechanism to LNT.  Rules are Python
files that are loaded at server start. Rules can bind to hooks in the
system.  This give a simple way to implement site specific policy.
Rules are intended to do things like filter LNT data, and modify the
database based on policies.

First use for this will be my next commit which uses rules to move
regressions between states.   Later rules may drop data from some views,
for example, not showing tests outside of the benchmarking-only set.

Added:
    lnt/trunk/lnt/server/db/rules/
    lnt/trunk/lnt/server/db/rules.py
    lnt/trunk/lnt/server/db/rules/rule_testhook.py
    lnt/trunk/lnt/server/ui/templates/rules.html
    lnt/trunk/tests/server/db/rules.py
Modified:
    lnt/trunk/lnt/server/ui/views.py
    lnt/trunk/tests/server/ui/V4Pages.py

Added: lnt/trunk/lnt/server/db/rules.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/rules.py?rev=255956&view=auto
==============================================================================
--- lnt/trunk/lnt/server/db/rules.py (added)
+++ lnt/trunk/lnt/server/db/rules.py Thu Dec 17 19:28:51 2015
@@ -0,0 +1,69 @@
+"""
+Define facilities for automatically applying rules to data.
+"""
+
+import logging
+import os
+import re
+
+def load_rules():
+    """
+    Load available rules scripts from a directory.
+
+    Rules are organized as:
+
+    <current dir>/rules/
+    <current dir>/rules/rule_.*.py
+    ...
+    """
+
+    rule_script_rex = re.compile(
+        r'^rule_(.*)\.py$')
+    rule_scripts = {}
+
+    rules_path = os.path.join(os.path.dirname(__file__),
+                                              'rules')
+    for item in os.listdir(rules_path):
+        # Ignore certain known non-scripts.
+        if item in ('README.txt', '__init__.py') or item.endswith('.pyc'):
+            continue
+
+        # Ignore non-matching files.
+        m = rule_script_rex.match(item)
+        if m is None:
+            logger.warning(
+                "ignoring item %r in rule  directory: %r",
+                item, rules_path)
+            continue
+
+        name = m.groups()[0]
+        # Allow rules to be disabled by name
+        if name.endswith("disabled"):
+            continue
+            
+        rule_scripts[name] = os.path.join(rules_path, item)
+
+    return rule_scripts
+
+# Places our rules can hook to.
+HOOKS = {'post_test_hook':[],
+         'post_submission_hook':[],
+         'post_regression_create_hook':[]}
+
+DESCRIPTIONS = {}
+
+def register_hooks():
+    """Exec all the rules files.  Gather the hooks from them
+    and load them into the hook dict for later use.
+    """
+    for name, path in load_rules().items():
+        globals = {}
+        execfile(path, globals)
+        DESCRIPTIONS[name] = globals['__doc__']
+        for hook_name in HOOKS.keys():
+            if hook_name in globals:
+                HOOKS[hook_name].append(globals[hook_name])
+    return HOOKS
+
+
+logger = logging.getLogger(__name__)

Added: lnt/trunk/lnt/server/db/rules/rule_testhook.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/db/rules/rule_testhook.py?rev=255956&view=auto
==============================================================================
--- lnt/trunk/lnt/server/db/rules/rule_testhook.py (added)
+++ lnt/trunk/lnt/server/db/rules/rule_testhook.py Thu Dec 17 19:28:51 2015
@@ -0,0 +1,9 @@
+"""
+Test that this hook can be run.
+"""
+
+
+def test():
+    return "Foo."
+    
+post_test_hook = test

Added: lnt/trunk/lnt/server/ui/templates/rules.html
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/ui/templates/rules.html?rev=255956&view=auto
==============================================================================
--- lnt/trunk/lnt/server/ui/templates/rules.html (added)
+++ lnt/trunk/lnt/server/ui/templates/rules.html Thu Dec 17 19:28:51 2015
@@ -0,0 +1,31 @@
+{% import "utils.html" as utils %}
+
+{% extends "layout.html" %}
+{% set components = [] %}
+
+{% block title %}Rules{%endblock%}
+
+{% block body %}
+  {{ utils.regex_filter_box('filter', '.searchable tr', "Rules...") }}
+
+  <section id="rules" />
+  {# List all rules which are loaded into the system. #}
+  <h3>Rules</h3>
+  <table class="table table-striped table-hover table-condensed">
+    <thead>
+      <tr>
+        <th>Name</th>
+        <th>Description<th>
+      </tr>
+    </thead>
+    <tbody class="searchable">
+      {% for name, desc in rules.items() %}
+      <tr>
+        <td>{{ name }}</td>
+        <td>{{ desc }}</td>
+
+      </tr>
+      {% endfor %}
+    </tbody>
+  </table>
+{% endblock %}

Modified: lnt/trunk/lnt/server/ui/views.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/lnt/server/ui/views.py?rev=255956&r1=255955&r2=255956&view=diff
==============================================================================
--- lnt/trunk/lnt/server/ui/views.py (original)
+++ lnt/trunk/lnt/server/ui/views.py Thu Dec 17 19:28:51 2015
@@ -28,6 +28,7 @@ from lnt.server.ui.decorators import fro
 import lnt.server.ui.util
 import lnt.server.reporting.dailyreport
 import lnt.server.reporting.summaryreport
+import lnt.server.db.rules
 from collections import namedtuple
 
 integral_rex = re.compile(r"[\d]+")
@@ -1161,3 +1162,10 @@ You must define a summary report configu
         return flask.jsonify(**json_obj)
 
     return render_template("v4_summary_report.html", report=report)
+
+
+ at frontend.route('/rules')
+def rules():
+    lnt.server.db.rules.register_hooks()
+    discovered_rules = lnt.server.db.rules.DESCRIPTIONS
+    return render_template("rules.html",rules=discovered_rules)

Added: lnt/trunk/tests/server/db/rules.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/db/rules.py?rev=255956&view=auto
==============================================================================
--- lnt/trunk/tests/server/db/rules.py (added)
+++ lnt/trunk/tests/server/db/rules.py Thu Dec 17 19:28:51 2015
@@ -0,0 +1,32 @@
+# Check the rule import and execution facility
+# RUN: python %s
+"""Test the rule import"""
+import unittest
+import logging
+import sys
+
+logging.basicConfig(level=logging.DEBUG)
+
+import lnt.server.db.rules as rules
+
+class RuleProcssingTests(unittest.TestCase):
+    """Test the Rules facility."""
+
+    def setUp(self):
+        pass
+        
+    def test_rule_loading(self):
+        """Can we load the testhook rule?"""
+        found_rules = rules.load_rules()
+        self.assertIn('testhook', found_rules)
+    
+    def test_hook_loading(self):
+        """Can we load and execute the test hook?"""
+        hooks = rules.register_hooks()
+        self.assertTrue(len(hooks['post_test_hook']) == 1)
+        ret = hooks['post_test_hook'][0]()
+        self.assertEqual(ret, "Foo.")
+        self.assertIn('testhook', rules.DESCRIPTIONS)
+
+if __name__ == '__main__':
+    unittest.main(argv=[sys.argv[0], ])

Modified: lnt/trunk/tests/server/ui/V4Pages.py
URL: http://llvm.org/viewvc/llvm-project/lnt/trunk/tests/server/ui/V4Pages.py?rev=255956&r1=255955&r2=255956&view=diff
==============================================================================
--- lnt/trunk/tests/server/ui/V4Pages.py (original)
+++ lnt/trunk/tests/server/ui/V4Pages.py Thu Dec 17 19:28:51 2015
@@ -167,6 +167,9 @@ def main():
 
     # Fetch the index page.
     check_code(client, '/')
+    
+    # Rules the index page.
+    check_code(client, '/rules')
 
     # Get the V4 overview page.
     check_code(client, '/v4/nts/')




More information about the llvm-commits mailing list