dir.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 every file [default: %default]")
  36. def configure(self, options):
  37. self.non_recursively = options.__dict__['general.non_recursively']
  38. self.add_exclude_rule(re.compile(options.__dict__['general.exclude_files']))
  39. self.is_proctime_enabled = options.__dict__['general.proctime.on']
  40. def initialize(self):
  41. if self.is_proctime_enabled == True:
  42. namespace = self.get_plugin_loader().get_database_loader().create_namespace('general')
  43. namespace.add_field('proctime', self.measure_proctime_type)
  44. def run(self, args):
  45. if len(args) == 0:
  46. self.reader.run(self, "./")
  47. for directory in args:
  48. self.reader.run(self, directory)
  49. def add_exclude_rule(self, re_compiled_pattern):
  50. # TODO file name may have special regexp symbols what causes an exception
  51. # For example try to run a collection with "--general.db-file=metrix++" option
  52. self.exclude_rules.append(re_compiled_pattern)
  53. def is_file_excluded(self, file_name):
  54. for each in self.exclude_rules:
  55. if re.match(each, file_name) != None:
  56. return True
  57. return False
  58. class DirectoryReader():
  59. def run(self, plugin, directory):
  60. def run_recursively(plugin, directory):
  61. for fname in os.listdir(directory):
  62. full_path = os.path.join(directory, fname)
  63. norm_path = re.sub(r'''[\\]''', "/", full_path)
  64. if plugin.is_file_excluded(fname) == False:
  65. if os.path.isdir(full_path):
  66. if plugin.non_recursively == False:
  67. run_recursively(plugin, full_path)
  68. else:
  69. parser = plugin.get_plugin_loader().get_parser(full_path)
  70. if parser == None:
  71. logging.info("Skipping: " + norm_path)
  72. else:
  73. logging.info("Processing: " + norm_path)
  74. ts = time.time()
  75. f = open(full_path, 'r');
  76. text = f.read();
  77. f.close()
  78. checksum = binascii.crc32(text) & 0xffffffff # to match python 3
  79. (data, is_updated) = plugin.get_plugin_loader().get_database_loader().create_file_data(full_path, checksum, text)
  80. parser.process(plugin, data, is_updated)
  81. if plugin.is_proctime_enabled == True:
  82. data.set_data('general', 'proctime', time.time() - ts)
  83. plugin.get_plugin_loader().get_database_loader().save_file_data(data)
  84. logging.debug("-" * 60)
  85. else:
  86. logging.info("Excluding: " + norm_path)
  87. logging.debug("-" * 60)
  88. run_recursively(plugin, directory)