summaryrefslogtreecommitdiffstats
path: root/meta/lib/patchtest/utils.py
blob: 8eddf3e85fba9c87befa3988e3d516e8fc6745e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# utils: common methods used by the patchtest framework
#
# Copyright (C) 2016 Intel Corporation
#
# SPDX-License-Identifier: GPL-2.0-only
#

import os
import subprocess
import logging
import re
import mailbox

def logger_create(name):
    logger = logging.getLogger(name)
    loggerhandler = logging.StreamHandler()
    loggerhandler.setFormatter(logging.Formatter("%(message)s"))
    logger.addHandler(loggerhandler)
    logger.setLevel(logging.INFO)
    return logger

def valid_branch(branch):
    """ Check if branch is valid name """
    lbranch = branch.lower()

    invalid  = lbranch.startswith('patch') or \
               lbranch.startswith('rfc') or \
               lbranch.startswith('resend') or \
               re.search(r'^v\d+', lbranch) or \
               re.search(r'^\d+/\d+', lbranch)

    return not invalid

def get_branch(path):
    """ Get the branch name from mbox """
    fullprefix = ""
    mbox = mailbox.mbox(path)

    if len(mbox):
        subject = mbox[0]['subject']
        if subject:
            pattern = re.compile(r"(\[.*\])", re.DOTALL)
            match = pattern.search(subject)
            if match:
                fullprefix = match.group(1)

    branch, branches, valid_branches = None, [], []

    if fullprefix:
        prefix = fullprefix.strip('[]')
        branches = [ b.strip() for b in prefix.split(',')]
        valid_branches = [b for b in branches if valid_branch(b)]

    if len(valid_branches):
        branch = valid_branches[0]

    return branch