summaryrefslogtreecommitdiffstats
path: root/components
diff options
context:
space:
mode:
authorisherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-05 02:05:21 +0000
committerisherman@chromium.org <isherman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-04-05 02:05:21 +0000
commit4d999c0e16a37d67571968749bcb2d042bcfe4e7 (patch)
tree14bdc6ad2bbdd02f0544290e3463da0a6bc28b3d /components
parenta86ca865ec8d532e0fa9013f8354e7a20d6c110c (diff)
downloadchromium_src-4d999c0e16a37d67571968749bcb2d042bcfe4e7.zip
chromium_src-4d999c0e16a37d67571968749bcb2d042bcfe4e7.tar.gz
chromium_src-4d999c0e16a37d67571968749bcb2d042bcfe4e7.tar.bz2
Move //tools/metrics into //components/ directory.
BUG=223859 R=joi@chromium.org Review URL: https://chromiumcodereview.appspot.com/12782019 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@192443 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components')
-rwxr-xr-xcomponents/tools/metrics/browser_components_metrics.py65
-rwxr-xr-xcomponents/tools/metrics/count_ifdefs.py77
-rwxr-xr-xcomponents/tools/metrics/count_ifdefs_unittest.py30
-rw-r--r--components/tools/metrics/testdata/foo.cc20
-rw-r--r--components/tools/metrics/testdata/foo_ignored.txt4
-rw-r--r--components/tools/metrics/testdata/subdir/foo_test.mm8
6 files changed, 204 insertions, 0 deletions
diff --git a/components/tools/metrics/browser_components_metrics.py b/components/tools/metrics/browser_components_metrics.py
new file mode 100755
index 0000000..7252953
--- /dev/null
+++ b/components/tools/metrics/browser_components_metrics.py
@@ -0,0 +1,65 @@
+#!/usr/bin/env python
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Generates the metrics collected weekly for the Browser Components project.
+
+See
+http://www.chromium.org/developers/design-documents/browser-components
+for details.
+"""
+
+import os
+import sys
+
+
+# This is done so that we can import checkdeps. If not invoked as
+# main, our user must ensure it is in PYTHONPATH.
+if __name__ == '__main__':
+ sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', '..',
+ 'tools', 'checkdeps'))
+
+
+import count_ifdefs
+import checkdeps
+import results
+
+
+# Preprocessor pattern to find OS_XYZ defines.
+PREPROCESSOR_PATTERN = 'OS_[A-Z]+'
+
+
+class BrowserComponentsMetricsGenerator(object):
+ def __init__(self, checkout_root):
+ self.checkout_root = checkout_root
+ self.chrome_browser = os.path.join(checkout_root, 'chrome', 'browser')
+
+ def CountIfdefs(self, skip_tests):
+ return count_ifdefs.CountIfdefs(
+ PREPROCESSOR_PATTERN, self.chrome_browser, skip_tests)
+
+ def CountViolations(self, skip_tests):
+ deps_checker = checkdeps.DepsChecker(self.checkout_root,
+ ignore_temp_rules=True,
+ skip_tests=skip_tests)
+ deps_checker.results_formatter = results.CountViolationsFormatter()
+ deps_checker.CheckDirectory(os.path.join('chrome', 'browser'))
+ return int(deps_checker.results_formatter.GetResults())
+
+
+def main():
+ generator = BrowserComponentsMetricsGenerator(
+ os.path.join(os.path.dirname(__file__), '..', '..', '..'))
+
+ print "All metrics are for chrome/browser.\n"
+ print "OS ifdefs, all: %d" % generator.CountIfdefs(False)
+ print "OS ifdefs, -tests: %d" % generator.CountIfdefs(True)
+ print ("Intended DEPS violations, all: %d" %
+ generator.CountViolations(False))
+ print "Intended DEPS violations, -tests: %d" % generator.CountViolations(True)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/components/tools/metrics/count_ifdefs.py b/components/tools/metrics/count_ifdefs.py
new file mode 100755
index 0000000..2e3c2ad
--- /dev/null
+++ b/components/tools/metrics/count_ifdefs.py
@@ -0,0 +1,77 @@
+#!/usr/bin/env python
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Counts the number of #if or #ifdef lines containing at least one
+preprocessor token that is a full match for the given pattern, in the
+given directory.
+"""
+
+
+import optparse
+import os
+import re
+import sys
+
+
+# Filename extensions we know will be handled by the C preprocessor.
+# We ignore files not matching these.
+CPP_EXTENSIONS = [
+ '.h',
+ '.cc',
+ '.m',
+ '.mm',
+]
+
+
+def _IsTestFile(filename):
+ """Does a rudimentary check to try to skip test files; this could be
+ improved but is good enough for basic metrics generation.
+ """
+ return re.match('(test|mock|dummy)_.*|.*_[a-z]*test\.(h|cc|mm)', filename)
+
+
+def CountIfdefs(token_pattern, directory, skip_tests=False):
+ """Returns the number of lines in files in |directory| and its
+ subdirectories that have an extension from |CPP_EXTENSIONS| and are
+ an #if or #ifdef line with a preprocessor token fully matching
+ the string |token_pattern|.
+
+ If |skip_tests| is true, a best effort is made to ignore test files.
+ """
+ token_line_re = re.compile(r'^#if(def)?.*\b(%s)\b.*$' % token_pattern)
+ count = 0
+ for root, dirs, files in os.walk(directory):
+ for filename in files:
+ if os.path.splitext(filename)[1] in CPP_EXTENSIONS:
+ if not skip_tests or not _IsTestFile(filename):
+ with open(os.path.join(root, filename)) as f:
+ for line in f:
+ line = line.strip()
+ if token_line_re.match(line):
+ count += 1
+ return count
+
+
+def PrintUsage():
+ print "Usage: %s [--skip-tests] TOKEN_PATTERN DIRECTORY" % sys.argv[0]
+
+
+def main():
+ option_parser = optparse.OptionParser()
+ option_parser.add_option('', '--skip-tests', action='store_true',
+ dest='skip_tests', default=False,
+ help='Skip test files.')
+ options, args = option_parser.parse_args()
+
+ if len(args) < 2:
+ PrintUsage()
+ return 1
+ else:
+ print CountIfdefs(args[0], args[1], options.skip_tests)
+ return 0
+
+
+if __name__ == '__main__':
+ sys.exit(main())
diff --git a/components/tools/metrics/count_ifdefs_unittest.py b/components/tools/metrics/count_ifdefs_unittest.py
new file mode 100755
index 0000000..9b8c322
--- /dev/null
+++ b/components/tools/metrics/count_ifdefs_unittest.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+# Copyright (c) 2012 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Tests for count_ifdefs.
+"""
+
+import os
+import unittest
+
+import count_ifdefs
+
+
+class CountIfdefsTest(unittest.TestCase):
+
+ def setUp(self):
+ self.root = os.path.join(os.path.dirname(__file__), 'testdata')
+
+ def testNormal(self):
+ count = count_ifdefs.CountIfdefs('OS_[A-Z]+', self.root)
+ self.failUnless(count == 6)
+
+ def testSkipTests(self):
+ count = count_ifdefs.CountIfdefs('OS_[A-Z]+', self.root, True)
+ self.failUnless(count == 4)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/components/tools/metrics/testdata/foo.cc b/components/tools/metrics/testdata/foo.cc
new file mode 100644
index 0000000..bd3d3c7
--- /dev/null
+++ b/components/tools/metrics/testdata/foo.cc
@@ -0,0 +1,20 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// Should not match
+#ifndef FOO_OS_ANDROID_BLAT
+#define FOO_OS_ANDROID_BLAT
+
+#if defined(OS_ANDROID)
+
+#if defined(OS_ANDROID) || defined(OS_IOS) || defined(OS_CHROMEOS)
+
+#if !defined(OS_CAT)
+
+#if defined(OS_WIN)
+
+#endif // !OS_ANDROID && !OS_IOS
+#endif // OS_CAT
+
+#endif // FOO_OS_ANDROID_BLAT
diff --git a/components/tools/metrics/testdata/foo_ignored.txt b/components/tools/metrics/testdata/foo_ignored.txt
new file mode 100644
index 0000000..2b186f9
--- /dev/null
+++ b/components/tools/metrics/testdata/foo_ignored.txt
@@ -0,0 +1,4 @@
+#if defined(OS_ANDROID)
+
+#if defined(OS_WIN)
+
diff --git a/components/tools/metrics/testdata/subdir/foo_test.mm b/components/tools/metrics/testdata/subdir/foo_test.mm
new file mode 100644
index 0000000..adf1f89
--- /dev/null
+++ b/components/tools/metrics/testdata/subdir/foo_test.mm
@@ -0,0 +1,8 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#if defined(OS_ANDROID)
+
+#if defined(OS_WIN)
+