forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathwciia.py
executable file
·126 lines (106 loc) · 2.92 KB
/
wciia.py
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
#!/usr/bin/env python
"""
wciia - Whose Code Is It Anyway
Determines code owner of the file/folder relative to the llvm source root.
Code owner is determined from the content of the CODE_OWNERS.TXT
by parsing the D: field
usage:
utils/wciia.py path
limitations:
- must be run from llvm source root
- very simplistic algorithm
- only handles * as a wildcard
- not very user friendly
- does not handle the proposed F: field
"""
from __future__ importprint_function
importos
code_owners= {}
defprocess_files_and_folders(owner):
filesfolders=owner['filesfolders']
# paths must be in ( ... ) so strip them
lpar=filesfolders.find('(')
rpar=filesfolders.rfind(')')
ifrpar<=lpar:
# give up
return
paths=filesfolders[lpar+1:rpar]
# split paths
owner['paths'] = []
forpathinpaths.split():
owner['paths'].append(path)
defprocess_code_owner(owner):
if'filesfolders'inowner:
filesfolders=owner['filesfolders']
else:
# print "F: field missing, using D: field"
owner['filesfolders'] =owner['description']
process_files_and_folders(owner)
code_owners[owner['name']] =owner
# process CODE_OWNERS.TXT first
code_owners_file=open("CODE_OWNERS.TXT", "r").readlines()
code_owner= {}
forlineincode_owners_file:
forwordinline.split():
ifword=="N:":
name=line[2:].strip()
ifcode_owner:
process_code_owner(code_owner)
code_owner= {}
# reset the values
code_owner['name'] =name
ifword=="E:":
email=line[2:].strip()
code_owner['email'] =email
ifword=="D:":
description=line[2:].strip()
code_owner['description'] =description
ifword=="F:":
filesfolders=line[2:].strip()
code_owner['filesfolders'].append(filesfolders)
deffind_owners(fpath):
onames= []
lmatch=-1
# very simplistic way of findning the best match
fornameincode_owners:
owner=code_owners[name]
if'paths'inowner:
forpathinowner['paths']:
# print "searching (" + path + ")"
# try exact match
iffpath==path:
returnname
# see if path ends with a *
rstar=path.rfind('*')
ifrstar>0:
# try the longest match,
rpos=-1
iflen(fpath) <len(path):
rpos=path.find(fpath)
ifrpos==0:
onames.append(name)
onames.append('Chris Lattner')
returnonames
# now lest try to find the owner of the file or folder
importsys
iflen(sys.argv) <2:
print("usage "+sys.argv[0] +" file_or_folder")
exit(-1)
# the path we are checking
path=str(sys.argv[1])
# check if this is real path
ifnotos.path.exists(path):
print("path ("+path+") does not exist")
exit(-1)
owners_name=find_owners(path)
# be grammatically correct
print("The owner(s) of the ("+path+") is(are) : "+str(owners_name))
exit(0)
# bottom up walk of the current .
# not yet used
root="."
fordir,subdirList,fileListinos.walk( root , topdown=False ) :
print("dir :" , dir)
forfnameinfileList :
print("-" , fname)
print()