summaryrefslogtreecommitdiff
path: root/util.py
blob: 07dcafe6f0f3711ad0c26d3d1f7fa4a06d7225f8 (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
62
63
64
65
66
67
68
69
70
71
import subprocess
import logging
import os
import re


class Logger():
    _instance = None

    def __new__(cls, fileName="leningrade.log", loggerName="ourLogger"):
        if cls._instance is None:
            logger = logging.getLogger(loggerName)
            formatter = logging.Formatter(
                '[%(asctime)s][%(levelname)8s][%(filename)s %(lineno)3s]%(message)s'
            )
            logger.setLevel(logging.ERROR)
            streamHandler = logging.StreamHandler()
            streamHandler.setFormatter(formatter)
            streamHandler.setLevel(logging.WARNING)
            fileHandler = logging.FileHandler(filename=fileName)
            fileHandler.setFormatter(formatter)
            fileHandler.setLevel(logging.DEBUG)
            logger.addHandler(fileHandler)
            logger.addHandler(streamHandler)
            cls._instance = logger
        return cls._instance


def first(iterable, condition=lambda x: True):
    try:
        return next(x for x in iterable if condition(x))
    except StopIteration:
        return None


def getProjRepoName(arg):
    id_, name, projNum, *_ = arg
    eng = re.sub('[\u4e00-\u9fa5]', '', name)
    eng = ''.join([word[0].capitalize() + word[1:] for word in eng.split()])
    return f"{eng}{id_}-p{projNum}"


def passCodeQuality(path, language):
    if language == "matlab":
        with open(path, encoding='utf-8', errors='replace') as f:
            res = f.read()
        return "global " not in res
    if language in ["c", "llvm-c"]:
        res = subprocess.check_output(
            ["ctags", "-R", "-x", "--sort=yes", "--c-kinds=v", path])
        lines = res.splitlines()
        return len([line for line in lines if b"const" not in line]) == 0
    if language in ["cc", "llvm-cc"]:
        res = subprocess.check_output(
            ["ctags", "-R", "-x", "--sort=yes", "--c++-kinds=v", path])
        lines = res.splitlines()
        return len([line for line in lines if b"const" not in line]) == 0


def getAllFiles(root):
    for f in os.listdir(root):
        if os.path.isfile(os.path.join(root, f)):
            yield os.path.join(f)
    dirs = [
        d for d in os.listdir(root)
        if os.path.isdir(os.path.join(root, d)) and d != ".git"
    ]
    for d in dirs:
        dirfiles = getAllFiles(os.path.join(root, d))
        for f in dirfiles:
            yield os.path.join(d, f)