wciia.py 2.9 KB

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