dir.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #
  2. # Metrix++, Copyright 2009-2013, Metrix++ Project
  3. # Link: http://swi.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. self.exclude_rules.append(re_compiled_pattern)
  51. def is_file_excluded(self, file_name):
  52. for each in self.exclude_rules:
  53. if re.match(each, file_name) != None:
  54. return True
  55. return False
  56. class DirectoryReader():
  57. def run(self, plugin, directory):
  58. def run_recursively(plugin, directory):
  59. for fname in os.listdir(directory):
  60. full_path = os.path.join(directory, fname)
  61. if plugin.is_file_excluded(fname) == False:
  62. if os.path.isdir(full_path):
  63. if plugin.non_recursively == False:
  64. run_recursively(plugin, full_path)
  65. else:
  66. logging.info("Processing: " + full_path)
  67. ts = time.time()
  68. f = open(full_path, 'r');
  69. text = f.read();
  70. f.close()
  71. checksum = binascii.crc32(text) & 0xffffffff # to match python 3
  72. data = plugin.get_plugin_loader().get_database_loader().create_file_data(full_path, checksum, text)
  73. plugin.notify_children(data)
  74. if plugin.is_proctime_enabled == True:
  75. data.set_data('general', 'proctime', time.time() - ts)
  76. plugin.get_plugin_loader().get_database_loader().save_file_data(data)
  77. logging.debug("-" * 60)
  78. else:
  79. logging.info("Excluding: " + full_path)
  80. logging.debug("-" * 60)
  81. run_recursively(plugin, directory)