[PATCH 06/10] parser: Use full regexps for delegation rules paths

Mauro Carvalho Chehab mchehab at osg.samsung.com
Sat Nov 28 23:14:42 AEDT 2015


From: Laurent Pinchart <laurent.pinchart at ideasonboard.com>

Paths are validated by trying to compile it as a regexp using a custom
validator.

Signed-off-by: Laurent Pinchart <laurent.pinchart at ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab at osg.samsung.com>
---
 patchwork/bin/parsemail.py | 19 +++++++++++++++++--
 patchwork/models.py        |  9 ++++++++-
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/patchwork/bin/parsemail.py b/patchwork/bin/parsemail.py
index 4f22c7f2d6a0..5d6ddf45d264 100755
--- a/patchwork/bin/parsemail.py
+++ b/patchwork/bin/parsemail.py
@@ -339,18 +339,33 @@ def get_state(state_name):
             pass
     return get_default_initial_patch_state()
 
+class Rule(object):
+    pass
+
 def auto_delegate(project, filenames):
     if not filenames:
         return None
 
-    rules = list(DelegationRule.objects.filter(project = project))
+    # Precompile the path regexps
+    rules = []
+    for rule in DelegationRule.objects.filter(project = project):
+        r = Rule()
+        r.user = rule.user
+
+        try:
+            r.path = re.compile(rule.path)
+        except:
+            print '%s is not a valid regular expression' % rule.path
+            continue
+
+        rules.append(r)
 
     patch_delegate = None
 
     for filename in filenames:
         file_delegate = None
         for rule in rules:
-            if fnmatch(filename, rule.path):
+            if rule.path.match(filename):
                 file_delegate = rule.user
                 break;
 
diff --git a/patchwork/models.py b/patchwork/models.py
index 101a9af9746f..e552217a3cbc 100644
--- a/patchwork/models.py
+++ b/patchwork/models.py
@@ -19,6 +19,7 @@
 
 from django.db import models
 from django.contrib.auth.models import User
+from django.core.exceptions import ValidationError
 from django.core.urlresolvers import reverse
 from django.contrib.sites.models import Site
 from django.conf import settings
@@ -78,9 +79,15 @@ class Project(models.Model):
         ordering = ['linkname']
 
 
+def validate_rule_path(value):
+    try:
+        re.compile(value)
+    except:
+        raise ValidationError(u'`%s\' is not a valid regulator expression' % value)
+
 class DelegationRule(models.Model):
     user = models.ForeignKey(User)
-    path = models.CharField(max_length=255)
+    path = models.CharField(max_length = 255, validators = [validate_rule_path])
     project = models.ForeignKey(Project)
     priority = models.IntegerField(default = 0)
 
-- 
2.5.0



More information about the Patchwork mailing list