Bug 1168409 - part 2 - avoiding importing usecounters in histogram_tools.py; r=gfritzsche
authorNathan Froyd <froydnj@mozilla.com>
Tue, 02 Jun 2015 11:33:43 -0400
changeset 269379 1b20b43558e980a21b4d244e577b706ee74f6195
parent 269378 c495a978dfaf537fb019388391a450e84a09e869
child 269380 f86c710c4074ea4700404c6d050c3d544e5c4655
push id2501
push userjosea.olivera@gmail.com
push dateWed, 03 Jun 2015 06:55:32 +0000
reviewersgfritzsche
bugs1168409
milestone41.0a1
Bug 1168409 - part 2 - avoiding importing usecounters in histogram_tools.py; r=gfritzsche
toolkit/components/telemetry/histogram_tools.py
--- a/toolkit/components/telemetry/histogram_tools.py
+++ b/toolkit/components/telemetry/histogram_tools.py
@@ -19,18 +19,16 @@ try:
     # Need to update sys.path to be able to find usecounters.
     sys.path.append(os.path.join(buildconfig.topsrcdir, 'dom/base/'))
 except ImportError:
     # Must be in an out-of-tree usage scenario.  Trust that whoever is
     # running this script knows we need the usecounters module and has
     # ensured it's in our sys.path.
     pass
 
-import usecounters
-
 from collections import OrderedDict
 
 def table_dispatch(kind, table, body):
     """Call body with table[kind] if it exists.  Raise an error otherwise."""
     if kind in table:
         return body(table[kind])
     else:
         raise BaseException, "don't know how to handle a histogram of kind %s" % kind
@@ -278,19 +276,27 @@ def from_Histograms_json(filename):
             raise BaseException, "error parsing histograms in %s: %s" % (filename, e.message)
     return histograms
 
 def from_UseCounters_conf(filename):
     return usecounters.generate_histograms(filename)
 
 FILENAME_PARSERS = {
     'Histograms.json': from_Histograms_json,
-    'UseCounters.conf': from_UseCounters_conf,
 }
 
+# Similarly to the dance above with buildconfig, usecounters may not be
+# available, so handle that gracefully.
+try:
+    import usecounters
+
+    FILENAME_PARSERS['UseCounters.conf'] = from_UseCounters_conf
+except ImportError:
+    pass
+
 def from_files(filenames):
     """Return an iterator that provides a sequence of Histograms for
 the histograms defined in filenames.
     """
     all_histograms = OrderedDict()
     for filename in filenames:
         parser = FILENAME_PARSERS[os.path.basename(filename)]
         histograms = parser(filename)