dir.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://metrixplusplus.sourceforge.net
  4. #
  5. # This file is a part of Metrix++ Tool.
  6. #
  7. # Metrix++ is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, version 3 of the License.
  10. #
  11. # Metrix++ is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Metrix++. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import core.api
  20. import re
  21. import os
  22. import logging
  23. import time
  24. import binascii
  25. class Plugin(core.api.Plugin, core.api.Parent, core.api.IConfigurable, core.api.IRunable):
  26. def __init__(self):
  27. self.reader = DirectoryReader()
  28. self.exclude_rules = []
  29. def declare_configuration(self, parser):
  30. parser.add_option("--general.non-recursively", action="store_true", default=False,
  31. help="If the option is set (True), sub-directories are not processed [default: %default]")
  32. parser.add_option("--general.exclude-files", default=r'^[.]',
  33. help="Defines the pattern to exclude files from processing [default: %default]")
  34. parser.add_option("--general.proctime-on", action="store_true", default=False,
  35. help="If the option is set (True), the tool measures processing time per file [default: %default]")
  36. parser.add_option("--general.procerrors-on", action="store_true", default=False,
  37. help="If the option is set (True), the tool counts number of processing/parsing errors per file [default: %default]")
  38. def configure(self, options):
  39. self.non_recursively = options.__dict__['general.non_recursively']
  40. self.add_exclude_rule(re.compile(options.__dict__['general.exclude_files']))
  41. self.is_proctime_enabled = options.__dict__['general.proctime_on']
  42. self.is_procerrors_enabled = options.__dict__['general.procerrors_on']
  43. def initialize(self):
  44. namespace = self.get_plugin_loader().get_database_loader().create_namespace('general')
  45. if self.is_proctime_enabled == True:
  46. namespace.add_field('proctime', float)
  47. if self.is_procerrors_enabled == True:
  48. namespace.add_field('procerrors', int)
  49. def run(self, args):
  50. if len(args) == 0:
  51. return self.reader.run(self, "./")
  52. for directory in args:
  53. return self.reader.run(self, directory)
  54. def add_exclude_rule(self, re_compiled_pattern):
  55. # TODO file name may have special regexp symbols what causes an exception
  56. # For example try to run a collection with "--general.db-file=metrix++" option
  57. self.exclude_rules.append(re_compiled_pattern)
  58. def is_file_excluded(self, file_name):
  59. for each in self.exclude_rules:
  60. if re.match(each, file_name) != None:
  61. return True
  62. return False
  63. class DirectoryReader():
  64. def run(self, plugin, directory):
  65. def run_recursively(plugin, directory):
  66. exit_code = 0
  67. for fname in os.listdir(directory):
  68. full_path = os.path.join(directory, fname)
  69. norm_path = re.sub(r'''[\\]''', "/", full_path)
  70. if plugin.is_file_excluded(fname) == False:
  71. if os.path.isdir(full_path):
  72. if plugin.non_recursively == False:
  73. exit_code += run_recursively(plugin, full_path)
  74. else:
  75. parser = plugin.get_plugin_loader().get_parser(full_path)
  76. if parser == None:
  77. logging.info("Skipping: " + norm_path)
  78. else:
  79. logging.info("Processing: " + norm_path)
  80. ts = time.time()
  81. f = open(full_path, 'r');
  82. text = f.read();
  83. f.close()
  84. checksum = binascii.crc32(text) & 0xffffffff # to match python 3
  85. (data, is_updated) = plugin.get_plugin_loader().get_database_loader().create_file_data(full_path, checksum, text)
  86. procerrors = parser.process(plugin, data, is_updated)
  87. if plugin.is_proctime_enabled == True:
  88. data.set_data('general', 'proctime', time.time() - ts)
  89. if plugin.is_procerrors_enabled == True and procerrors != None and procerrors != 0:
  90. data.set_data('general', 'procerrors', procerrors)
  91. plugin.get_plugin_loader().get_database_loader().save_file_data(data)
  92. logging.debug("-" * 60)
  93. exit_code += procerrors
  94. else:
  95. logging.info("Excluding: " + norm_path)
  96. logging.debug("-" * 60)
  97. return exit_code
  98. total_errors = run_recursively(plugin, directory)
  99. total_errors = total_errors # used, warnings are per file if not zero
  100. return 0 # ignore errors, collection is successful anyway