wciia.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/usr/bin/env python
  2. """
  3. wciia - Whose Code Is It Anyway
  4. Determines code owner of the file/folder relative to the llvm source root.
  5. Code owner is determined from the content of the CODE_OWNERS.TXT
  6. by parsing the D: field
  7. usage:
  8. utils/wciia.py path
  9. limitations:
  10. - must be run from llvm source root
  11. - very simplistic algorithm
  12. - only handles * as a wildcard
  13. - not very user friendly
  14. - does not handle the proposed F: field
  15. """
  16. from __future__ import print_function
  17. import os
  18. code_owners = {}
  19. def process_files_and_folders(owner):
  20. filesfolders = owner['filesfolders']
  21. # paths must be in ( ... ) so strip them
  22. lpar = filesfolders.find('(')
  23. rpar = filesfolders.rfind(')')
  24. if rpar <= lpar:
  25. # give up
  26. return
  27. paths = filesfolders[lpar+1:rpar]
  28. # split paths
  29. owner['paths'] = []
  30. for path in paths.split():
  31. owner['paths'].append(path)
  32. def process_code_owner(owner):
  33. if 'filesfolders' in owner:
  34. filesfolders = owner['filesfolders']
  35. else:
  36. # print "F: field missing, using D: field"
  37. owner['filesfolders'] = owner['description']
  38. process_files_and_folders(owner)
  39. code_owners[owner['name']] = owner
  40. # process CODE_OWNERS.TXT first
  41. code_owners_file = open("CODE_OWNERS.TXT", "r").readlines()
  42. code_owner = {}
  43. for line in code_owners_file:
  44. for word in line.split():
  45. if word == "N:":
  46. name = line[2:].strip()
  47. if code_owner:
  48. process_code_owner(code_owner)
  49. code_owner = {}
  50. # reset the values
  51. code_owner['name'] = name
  52. if word == "E:":
  53. email = line[2:].strip()
  54. code_owner['email'] = email
  55. if word == "D:":
  56. description = line[2:].strip()
  57. code_owner['description'] = description
  58. if word == "F:":
  59. filesfolders = line[2:].strip()
  60. code_owner['filesfolders'].append(filesfolders)
  61. def find_owners(fpath):
  62. onames = []
  63. lmatch = -1
  64. # very simplistic way of findning the best match
  65. for name in code_owners:
  66. owner = code_owners[name]
  67. if 'paths' in owner:
  68. for path in owner['paths']:
  69. # print "searching (" + path + ")"
  70. # try exact match
  71. if fpath == path:
  72. return name
  73. # see if path ends with a *
  74. rstar = path.rfind('*')
  75. if rstar>0:
  76. # try the longest match,
  77. rpos = -1
  78. if len(fpath) < len(path):
  79. rpos = path.find(fpath)
  80. if rpos == 0:
  81. onames.append(name)
  82. onames.append('Chris Lattner')
  83. return onames
  84. # now lest try to find the owner of the file or folder
  85. import sys
  86. if len(sys.argv) < 2:
  87. print("usage " + sys.argv[0] + " file_or_folder")
  88. exit(-1)
  89. # the path we are checking
  90. path = str(sys.argv[1])
  91. # check if this is real path
  92. if not os.path.exists(path):
  93. print("path (" + path + ") does not exist")
  94. exit(-1)
  95. owners_name = find_owners(path)
  96. # be grammatically correct
  97. print("The owner(s) of the (" + path + ") is(are) : " + str(owners_name))
  98. exit(0)
  99. # bottom up walk of the current .
  100. # not yet used
  101. root = "."
  102. for dir,subdirList,fileList in os.walk( root , topdown=False ) :
  103. print("dir :" , dir)
  104. for fname in fileList :
  105. print("-" , fname)
  106. print()