#! /usr/bin/env python3 #=============================== # # find # # 2019/02/07 Kuninori Morimoto #=============================== import sys import os import re import base #==================================== # # find # #==================================== class find(base.base): #-------------------- # default_arg #-------------------- def default_arg(self, arg): # return [] for -all # # replace -a NoAssignee to -na "" # # add "-ns Done,Abandoned" automatically # if user doesn't specify "-s" or "-ns" or "-is" match = 0 for i in range(len(arg)): if (arg[i] == "-all"): return [] if ((arg[i] == "-a") and (arg[i+1] == "NoAssignee")): arg[i] = "-na" arg[i+1] = "" if ((arg[i] == "-s") or (arg[i] == "-ns") or (arg[i] == "-is")): match = 1 if (not match): arg += ["-ns", "Done,Abandoned"] return arg #-------------------- # parse_option #-------------------- def parse_option(self, arg, option, tag, hit): op = "-l" # hit if (not hit): op = "-L" # not hit match = 0 for cmd in arg: if (cmd == option): match = 1 elif (match): if (option == "-b"): tag = cmd tgt = "" else: # Wolfram,Shimoda # -> # (Wolfram|Shimoda) tgt = "\s+({})".format(cmd.replace(",", "|")) self.files = self.run("echo \"{}\" | xargs egrep {} \"{}:{}\"".\ format(self.files, op, tag, 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 project.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 project.schema".\ format(self.top())) #-------------------- # __init__ #-------------------- def __init__(self, arg): super().__init__() self.files = "" self.parse_files(arg) arg = self.default_arg(arg) # -all: all # -s : matched status # -ns : not matched status # -a : matched assignee # -na : not matched assignee # -t : matched team # -nt : not matched team # -b : bsp 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) self.parse_option(arg, "-b", None, 1) #-------------------- # 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()