# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2013 IBM Corp.
#
#   Licensed under the Apache License, Version 2.0 (the "License"); you may
#   not use this file except in compliance with the License. You may obtain
#   a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#   WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#   License for the specific language governing permissions and limitations
#   under the License.

import re


SKIP_DECORATOR = '@testtools.skip('


def skip_bugs(physical_line):
    """Check skip lines for proper bug entries

    T101: Bug not in skip line
    T102: Bug in message formatted incorrectly
    """

    pos = physical_line.find(SKIP_DECORATOR)

    skip_re = re.compile(r'^\s*@testtools.skip.*')

    if pos != -1 and skip_re.match(physical_line):
        bug = re.compile(r'^.*\bbug\b.*', re.IGNORECASE)
        if bug.match(physical_line) is None:
            return (pos, 'T101: skips must have an associated bug')

        bug_re = re.compile(r'.*skip\(.*Bug\s\#\d+', re.IGNORECASE)

        if bug_re.match(physical_line) is None:
            return (pos, 'T102: Bug number formatted incorrectly')


def factory(register):
    register(skip_bugs)

@@ -644,43 +644,268 @@
 e%0A%0A%0A
-SKIP_DECORATOR = '@testtools.skip('
+PYTHON_CLIENTS = %5B'cinder', 'glance', 'keystone', 'nova', 'swift', 'quantum'%5D%0A%0ASKIP_DECORATOR_RE = re.compile(r'%5Cs*@testtools.skip%5C((.*)%5C)')%0ASKIP_STR_RE = re.compile(r'.*Bug #%5Cd+.*')%0APYTHON_CLIENT_RE = re.compile('import (%25s)client' %25 '%7C'.join(PYTHON_CLIENTS))
 %0A%0A%0Ad
@@ -995,75 +995,46 @@
 01: 
-Bug not in skip line%0A    T102: Bug in message formatted incorrectly
+skips must contain %22Bug #%3Cbug_number%3E%22
 %0A   
@@ -1047,254 +1047,205 @@
     
-po
+re
 s = 
-physical_line.find(SKIP_DECORATOR)%0A%0A    skip_re = re.compile(r'%5E%5Cs*@testtools.skip.*')%0A%0A    if pos != -1 and skip_re.match(physical_line):%0A        bug = re.compile(r'%5E.*%5Cbbug%5Cb.*', re.IGNORECASE)%0A        if bug.match(physical_line) is None:
+SKIP_DECORATOR_RE.match(physical_line)%0A    if res:%0A        content = res.group(1)%0A        res = SKIP_STR_RE.match(content)%0A        if not res:%0A            return (physical_line.find(content),
 %0A   
@@ -1249,36 +1249,31 @@
             
-return (pos,
+       
  'T101: skip
@@ -1283,120 +1283,273 @@
 ust 
-have an associated bug')%0A%0A        bug_re = re.compile(r'.*skip%5C(.*Bug%5Cs%5C#%5Cd+', re.IGNORECASE)%0A%0A        if bug_re
+contain %22Bug #%3Cbug_number%3E%22')%0A%0A%0Adef import_no_clients_in_api(physical_line, filename):%0A    %22%22%22Check for client imports from tempest/api tests%0A%0A    T102: Cannot import OpenStack python clients%0A    %22%22%22%0A%0A    if %22tempest/api%22 in filename:%0A        res = PYTHON_CLIENT_RE
 .mat
@@ -1569,16 +1569,23 @@
 ine)
- is None
+%0A        if res
 :%0A  
@@ -1607,52 +1607,151 @@
 n (p
-os, 'T102: Bug number formatted incorrectly'
+hysical_line.find(res.group(1)),%0A                    (%22T102: python clients import not allowed%22%0A                     %22 in tempest/api/* tests%22)
 )%0A%0A%0A
@@ -1797,8 +1797,47 @@
 p_bugs)%0A
+    register(import_no_clients_in_api)%0A

Add Flake8 extension for python client import checks