Explorar o código

Added code line metrics per file.

avkonst %!s(int64=10) %!d(string=hai) anos
pai
achega
c57201ae04

+ 27 - 0
mainline/ext/std/code/filelines.ini

@@ -0,0 +1,27 @@
+;
+;    Metrix++, Copyright 2009-2013, Metrix++ Project
+;    Link: http://metrixplusplus.sourceforge.net
+;    
+;    This file is a part of Metrix++ Tool.
+;    
+;    Metrix++ is free software: you can redistribute it and/or modify
+;    it under the terms of the GNU General Public License as published by
+;    the Free Software Foundation, version 3 of the License.
+;    
+;    Metrix++ is distributed in the hope that it will be useful,
+;    but WITHOUT ANY WARRANTY; without even the implied warranty of
+;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;    GNU General Public License for more details.
+;    
+;    You should have received a copy of the GNU General Public License
+;    along with Metrix++.  If not, see <http://www.gnu.org/licenses/>.
+;
+
+[Plugin]
+version: 1.1
+package: std.code
+module:  filelines
+class:   Plugin
+depends: None
+actions: collect
+enabled: True

+ 74 - 0
mainline/ext/std/code/filelines.py

@@ -0,0 +1,74 @@
+#
+#    Metrix++, Copyright 2009-2013, Metrix++ Project
+#    Link: http://metrixplusplus.sourceforge.net
+#    
+#    This file is a part of Metrix++ Tool.
+#    
+#    Metrix++ is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU General Public License as published by
+#    the Free Software Foundation, version 3 of the License.
+#    
+#    Metrix++ is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+#    GNU General Public License for more details.
+#    
+#    You should have received a copy of the GNU General Public License
+#    along with Metrix++.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+import mpp.api
+import re
+
+class Plugin(mpp.api.Plugin, mpp.api.MetricPluginMixin, mpp.api.Child, mpp.api.IConfigurable):
+    
+    def declare_configuration(self, parser):
+        parser.add_option("--std.code.filelines.code", "--scflc", action="store_true", default=False,
+                         help="Enables collection of lines of code metric (per file detalization) - "
+                         "number of non-empty lines of code, excluding comments "
+                         "[default: %default]")
+        parser.add_option("--std.code.filelines.preprocessor", "--scflp", action="store_true", default=False,
+                         help="Enables collection of lines of preprocessor code metric (per file detalization) - "
+                         "number of non-empty lines of preprocessor code "
+                         "[default: %default]")
+        parser.add_option("--std.code.filelines.comments", "--scflcom", action="store_true", default=False,
+                         help="Enables collection of lines of comments metric (per file detalization) - "
+                         "number of non-empty lines of comments "
+                         "[default: %default]")
+        parser.add_option("--std.code.filelines.total", "--scflt", action="store_true", default=False,
+                         help="Enables collection of total lines metric (per file detalization) - "
+                         "number of any type of lines (blank, code, comments, etc.)"
+                         "[default: %default]")
+    
+    def configure(self, options):
+        self.is_active_code = options.__dict__['std.code.filelines.code']
+        self.is_active_preprocessor = options.__dict__['std.code.filelines.preprocessor']
+        self.is_active_comments = options.__dict__['std.code.filelines.comments']
+        self.is_active_total = options.__dict__['std.code.filelines.total']
+        
+    pattern_line = re.compile(r'''[^\s].*''')
+
+    def initialize(self):
+        self.declare_metric(self.is_active_code,
+                       self.Field('code', int),
+                       self.pattern_line,
+                       mpp.api.Marker.T.CODE | mpp.api.Marker.T.STRING,
+                       merge_markers=True)
+        self.declare_metric(self.is_active_preprocessor,
+                       self.Field('preprocessor', int),
+                       self.pattern_line,
+                       mpp.api.Marker.T.PREPROCESSOR)
+        self.declare_metric(self.is_active_comments,
+                       self.Field('comments', int),
+                       self.pattern_line,
+                       mpp.api.Marker.T.COMMENT)
+        self.declare_metric(self.is_active_total,
+                       self.Field('total', int),
+                       self.pattern_line,
+                       mpp.api.Marker.T.ANY,
+                       merge_markers=True)
+
+        super(Plugin, self).initialize(fields=self.get_fields(), support_regions=False)
+
+        if self.is_active() == True:
+            self.subscribe_by_parents_interface(mpp.api.ICode)

+ 4 - 4
mainline/ext/std/code/lines.py

@@ -24,19 +24,19 @@ class Plugin(mpp.api.Plugin, mpp.api.MetricPluginMixin, mpp.api.Child, mpp.api.I
     
     def declare_configuration(self, parser):
         parser.add_option("--std.code.lines.code", "--sclc", action="store_true", default=False,
-                         help="Enables collection of lines of code metric - "
+                         help="Enables collection of lines of code metric (per region detalization) - "
                          "number of non-empty lines of code, excluding comments "
                          "[default: %default]")
         parser.add_option("--std.code.lines.preprocessor", "--sclp", action="store_true", default=False,
-                         help="Enables collection of lines of preprocessor code metric - "
+                         help="Enables collection of lines of preprocessor code metric (per region detalization) - "
                          "number of non-empty lines of preprocessor code "
                          "[default: %default]")
         parser.add_option("--std.code.lines.comments", "--sclcom", action="store_true", default=False,
-                         help="Enables collection of lines of comments metric - "
+                         help="Enables collection of lines of comments metric (per region detalization) - "
                          "number of non-empty lines of comments "
                          "[default: %default]")
         parser.add_option("--std.code.lines.total", "--sclt", action="store_true", default=False,
-                         help="Enables collection of total lines metric - "
+                         help="Enables collection of total lines metric (per region detalization) - "
                          "number of any type of lines (blank, code, comments, etc.)"
                          "[default: %default]")
     

+ 20 - 4
mainline/mpp/api.py

@@ -1037,6 +1037,7 @@ class Plugin(BasePlugin):
             self.name = name
             self.type = ftype
             self.non_zero = non_zero
+            self._regions_supported = True
 
     class Property(object):
         def __init__(self, name, value):
@@ -1071,6 +1072,7 @@ class Plugin(BasePlugin):
                                                        version=self.get_version())
             for field in fields:
                 is_created = namespace_obj.add_field(field.name, field.type, non_zero=field.non_zero)
+                field._regions_supported = support_regions
                 assert(is_created != None)
                 # if field is created (not cloned from the previous db),
                 # mark the plug-in as updated in order to trigger full rescan
@@ -1210,18 +1212,32 @@ class MetricPluginMixin(Parent):
                 alias = '*'
         (pattern_to_search, counter_class) = field_data[4][alias]
         
-        for region in data.iterate_regions(filter_group=field_data[5]):
-            counter = counter_class(namespace, field, self, alias, data, region)
+        if field_data[0]._regions_supported == True:
+            for region in data.iterate_regions(filter_group=field_data[5]):
+                counter = counter_class(namespace, field, self, alias, data, region)
+                if field_data[1] != Marker.T.NONE:
+                    for marker in data.iterate_markers(
+                                    filter_group = field_data[1],
+                                    region_id = region.get_id(),
+                                    exclude_children = field_data[2],
+                                    merge=field_data[3]):
+                        counter.count(marker, pattern_to_search)
+                count = counter.get_result()
+                if count != 0 or field_data[0].non_zero == False:
+                    region.set_data(namespace, field, count)
+        else:
+            counter = counter_class(namespace, field, self, alias, data, None)
             if field_data[1] != Marker.T.NONE:
                 for marker in data.iterate_markers(
                                 filter_group = field_data[1],
-                                region_id = region.get_id(),
+                                region_id = None,
                                 exclude_children = field_data[2],
                                 merge=field_data[3]):
                     counter.count(marker, pattern_to_search)
             count = counter.get_result()
             if count != 0 or field_data[0].non_zero == False:
-                region.set_data(namespace, field, count)
+                data.set_data(namespace, field, count)
+            
 
 
 

+ 19 - 0
mainline/tests/general/test_basic.py

@@ -295,6 +295,25 @@ class Test(tests.common.TestCase):
         runner = tests.common.ToolRunner('view', ['--format=txt'], prefix='txt')
         self.assertExec(runner.run())
 
+    def test_std_filelines_metrics(self):
+
+        runner = tests.common.ToolRunner('collect',
+                                         ['--std.code.filelines.code',
+                                          '--std.code.filelines.preprocessor',
+                                          '--std.code.filelines.comments',
+                                          '--std.code.filelines.total'])
+        self.assertExec(runner.run())
+
+        runner = tests.common.ToolRunner('view',
+                                         ['--nest-regions', '--format=txt'],
+                                         prefix='nest_per_file',
+                                         dirs_list=['./simple.cpp'])
+        self.assertExec(runner.run())
+
+        runner = tests.common.ToolRunner('view', ['--format=txt'], prefix='txt')
+        self.assertExec(runner.run())
+
+
     def test_std_complexity_maxindent(self):
 
         runner = tests.common.ToolRunner('collect',

+ 27 - 10
mainline/tests/general/test_basic/test_help_collect_default_stdout.gold.txt

@@ -28,26 +28,43 @@ Options:
                         Enables collection of maximum indent level metric
                         [default: False]
   --std.code.lines.code, --sclc
-                        Enables collection of lines of code metric - number of
-                        non-empty lines of code, excluding comments [default:
-                        False]
+                        Enables collection of lines of code metric (per region
+                        detalization) - number of non-empty lines of code,
+                        excluding comments [default: False]
   --std.code.lines.preprocessor, --sclp
                         Enables collection of lines of preprocessor code
-                        metric - number of non-empty lines of preprocessor
-                        code [default: False]
+                        metric (per region detalization) - number of non-empty
+                        lines of preprocessor code [default: False]
   --std.code.lines.comments, --sclcom
-                        Enables collection of lines of comments metric -
-                        number of non-empty lines of comments [default: False]
+                        Enables collection of lines of comments metric (per
+                        region detalization) - number of non-empty lines of
+                        comments [default: False]
   --std.code.lines.total, --sclt
-                        Enables collection of total lines metric - number of
-                        any type of lines (blank, code, comments,
-                        etc.)[default: False]
+                        Enables collection of total lines metric (per region
+                        detalization) - number of any type of lines (blank,
+                        code, comments, etc.)[default: False]
   --std.code.mi.simple, --scms
                         Enables collection of simple maintainability index
                         metric. It uses std.code.line:code,
                         std.code.complexity:cyclomatic metrics to rank level
                         of maintainability. Lower value of this metric
                         indicates better maintainability. [default: False]
+  --std.code.filelines.code, --scflc
+                        Enables collection of lines of code metric (per file
+                        detalization) - number of non-empty lines of code,
+                        excluding comments [default: False]
+  --std.code.filelines.preprocessor, --scflp
+                        Enables collection of lines of preprocessor code
+                        metric (per file detalization) - number of non-empty
+                        lines of preprocessor code [default: False]
+  --std.code.filelines.comments, --scflcom
+                        Enables collection of lines of comments metric (per
+                        file detalization) - number of non-empty lines of
+                        comments [default: False]
+  --std.code.filelines.total, --scflt
+                        Enables collection of total lines metric (per file
+                        detalization) - number of any type of lines (blank,
+                        code, comments, etc.)[default: False]
   --std.code.length.total, --sclent
                         Enables collection of size metric (in number of
                         symbols per region) [default: False]

+ 0 - 0
mainline/tests/general/test_basic/test_std_filelines_metrics_collect_default_stdout.gold.txt


+ 120 - 0
mainline/tests/general/test_basic/test_std_filelines_metrics_view_nest_per_file_stdout.gold.txt

@@ -0,0 +1,120 @@
+./simple.cpp:0: info: Metrics per '__global__' region
+	Region name    : __global__
+	Region type    : global
+	Offsets        : 0-697
+	Line numbers   : 1-71
+	Modified       : None
+
+.   ./simple.cpp:4: info: Metrics per 'hmm' region
+    	Region name    : hmm
+    	Region type    : namespace
+    	Offsets        : 2-696
+    	Line numbers   : 3-70
+    	Modified       : None
+
+.   .   ./simple.cpp:9: info: Metrics per 'A' region
+        	Region name    : A
+        	Region type    : class
+        	Offsets        : 94-692
+        	Line numbers   : 9-68
+        	Modified       : None
+
+.   .   .   ./simple.cpp:12: info: Metrics per 'A' region
+            	Region name    : A
+            	Region type    : function
+            	Offsets        : 106-252
+            	Line numbers   : 12-23
+            	Modified       : None
+
+.   .   .   ./simple.cpp:26: info: Metrics per 'func' region
+            	Region name    : func
+            	Region type    : function
+            	Offsets        : 256-405
+            	Line numbers   : 26-40
+            	Modified       : None
+
+.   .   .   .   ./simple.cpp:28: info: Metrics per 'embeded' region
+                	Region name    : embeded
+                	Region type    : class
+                	Offsets        : 285-391
+                	Line numbers   : 28-38
+                	Modified       : None
+
+.   .   .   .   .   ./simple.cpp:30: info: Metrics per 'embeded' region
+                    	Region name    : embeded
+                    	Region type    : function
+                    	Offsets        : 306-387
+                    	Line numbers   : 30-37
+                    	Modified       : None
+
+.   .   .   ./simple.cpp:42: info: Metrics per 'func_to_be_removed_in_new_sources' region
+            	Region name    : func_to_be_removed_in_new_sources
+            	Region type    : function
+            	Offsets        : 408-596
+            	Line numbers   : 42-56
+            	Modified       : None
+
+.   .   .   .   ./simple.cpp:44: info: Metrics per 'embeded' region
+                	Region name    : embeded
+                	Region type    : class
+                	Offsets        : 466-577
+                	Line numbers   : 44-54
+                	Modified       : None
+
+.   .   .   .   .   ./simple.cpp:46: info: Metrics per 'embeded' region
+                    	Region name    : embeded
+                    	Region type    : function
+                    	Offsets        : 487-573
+                    	Line numbers   : 46-53
+                    	Modified       : None
+
+.   .   .   ./simple.cpp:58: info: Metrics per 'never' region
+            	Region name    : never
+            	Region type    : function
+            	Offsets        : 599-669
+            	Line numbers   : 58-65
+            	Modified       : None
+
+./simple.cpp:0: info: Metrics per file
+	std.code.filelines:code: 53
+	std.code.filelines:total: 58
+	std.code.filelines:preprocessor: 1
+	std.code.filelines:comments: 5
+
+./simple.cpp:: info: Overall metrics for 'std.code.filelines:total' metric
+	Average        : 58.0
+	Minimum        : 58
+	Maximum        : 58
+	Total          : 58.0
+	Distribution   : 1 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	            58 : 1.000 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./simple.cpp:: info: Overall metrics for 'std.code.filelines:code' metric
+	Average        : 53.0
+	Minimum        : 53
+	Maximum        : 53
+	Total          : 53.0
+	Distribution   : 1 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	            53 : 1.000 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./simple.cpp:: info: Overall metrics for 'std.code.filelines:preprocessor' metric
+	Average        : 1.0
+	Minimum        : 1
+	Maximum        : 1
+	Total          : 1.0
+	Distribution   : 1 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	             1 : 1.000 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./simple.cpp:: info: Overall metrics for 'std.code.filelines:comments' metric
+	Average        : 5.0
+	Minimum        : 5
+	Maximum        : 5
+	Total          : 5.0
+	Distribution   : 1 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	             5 : 1.000 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
+

+ 44 - 0
mainline/tests/general/test_basic/test_std_filelines_metrics_view_txt_stdout.gold.txt

@@ -0,0 +1,44 @@
+./:: info: Overall metrics for 'std.code.filelines:total' metric
+	Average        : 41.5
+	Minimum        : 25
+	Maximum        : 58
+	Total          : 83.0
+	Distribution   : 2 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	            25 : 0.500 : 0.500 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||
+	            58 : 0.500 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./:: info: Overall metrics for 'std.code.filelines:code' metric
+	Average        : 37.0
+	Minimum        : 21
+	Maximum        : 53
+	Total          : 74.0
+	Distribution   : 2 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	            21 : 0.500 : 0.500 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||
+	            53 : 0.500 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./:: info: Overall metrics for 'std.code.filelines:preprocessor' metric
+	Average        : 0.5
+	Minimum        : 0
+	Maximum        : 1
+	Total          : 1.0
+	Distribution   : 2 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	             0 : 0.500 : 0.500 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||
+	             1 : 0.500 : 1.000 : 1	||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./:: info: Overall metrics for 'std.code.filelines:comments' metric
+	Average        : 5.0
+	Minimum        : 5
+	Maximum        : 5
+	Total          : 10.0
+	Distribution   : 2 files in total (including 0 suppressed)
+	  Metric value : Ratio : R-sum : Number of files
+	             5 : 1.000 : 1.000 : 2	||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
+./:: info: Directory content:
+	File           : file_deleted_in_new_sources.cpp
+	File           : simple.cpp
+
+