summaryrefslogtreecommitdiff
path: root/scripts/find.py
blob: cbefcbe6b1ed8eda08c7af1c27154317f2165e63 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#! /usr/bin/env python3
#===============================
#
# find
#
# 2019/02/07 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
#===============================
import sys
import os
import re

import base

#====================================
#
# find
#
#====================================
class find(base.base):

    #--------------------
    # default_arg
    #--------------------
    def default_arg(self, arg):
        # add "-ns Done" automatically
        # if user doesn't specify "-s" or "-ns"
        match = 0
        for cmd in arg:
            if (cmd == "-a"):
                return []
            if ((cmd == "-s") or (cmd == "-ns")):
                match = 1

        if (not match):
            return ["-ns", "Done"]

        return []

    #--------------------
    # parse_option
    #--------------------
    def parse_option(self, arg, option, char, hit):

        op = "-l"	# hit
        if (not hit):
            op = "-L"	# not hit

        match = 0
        for cmd in arg:
            if (cmd == option):
                match = 1
            elif (match):
                # Wolfram,Shimoda
                # ->
                # (Wolfram|Shimoda)
                tgt = "({})".format(cmd.replace(",", "|"))

                self.files = self.run("echo \"{}\" | xargs egrep {} \"{}:\s+{}\"".\
                                       format(self.files, op, char, tgt))
                return

    #--------------------
    # parse_files
    #--------------------
    def parse_files(self, arg):

        # find specified key files if exist
        for key in arg:
            if (not self.is_key(key)):
                continue

            if (len(self.files) > 0):
                self.files += "\n"

            self.files += self.run("egrep -l \"key:\s+{}\" -r {}/projects".\
                                   format(key, self.top()))

        # find specified folder files if exist
        if (len(self.files) == 0):
            did = 0
            for folder in arg:
                if (os.path.isdir(folder)):
                    did = 1
                    self.files += self.run("find {} -mindepth 1 -maxdepth 1 -name \"*.yaml\" | grep -v schema".\
                                           format(folder))
            if (did):
                return

        # all project files if no files
        if (len(self.files) == 0):
            self.files = self.run("find {}/projects -name \"*.yaml\" | grep -v schema".\
                                      format(self.top()))

    #--------------------
    # __init__
    #--------------------
    def __init__(self, arg):
        super().__init__()

        self.files = ""

        self.parse_files(arg)

        arg += self.default_arg(arg)

        # -a  : all
        # -s  :     matched status
        # -ns : not matched status
        # -a  :     matched assignee
        # -na : not matched assignee
        # -t  :     matched team
        # -nt : not matched team
        if (self.files):
            self.parse_option(arg, "-s",  "status",	1)
            self.parse_option(arg, "-ns", "status",	0)
            self.parse_option(arg, "-a",  "assignee",	1)
            self.parse_option(arg, "-na", "assignee",	0)
            self.parse_option(arg, "-t",  "team",	1)
            self.parse_option(arg, "-nt", "team",	0)

    #--------------------
    # get
    #--------------------
    def get(self):
        return self.tolist(self.files)

    #--------------------
    # show
    #--------------------
    def show(self):
        if (self.files):
            print(self.files)

#====================================
#
# As command
#
#====================================
if __name__=='__main__':
    find(sys.argv).show()