Files
impala/common/thrift/generate_metrics.py
Matthew Jacobs f37682a16f Fix packaging build for Python 2.4
cgroups.py was using unsupported "except <Exception> as <var>" syntax.

generate_metrics.py was using the json module which is not available
in Python 2.4, but contains simplejson which provides the same
functionality.

Change-Id: If2c176c15a9573dd2a2acf5ee459ff24ce891ce3
Reviewed-on: http://gerrit.cloudera.org:8080/396
Reviewed-by: Matthew Jacobs <mj@cloudera.com>
Tested-by: Matthew Jacobs <mj@cloudera.com>
2015-05-19 17:13:33 +00:00

95 lines
3.1 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright 2015 Cloudera Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import sys
import os
import re
try:
import json
except ImportError:
import simplejson as json # For Python 2.4
def load_metrics(source_file):
"""Reads the json file of metric definitions and returns a map of metric names to
metric definitions"""
raw_metrics = json.loads(open(source_file).read())
metrics = { }
for m in raw_metrics:
if m['key'] in metrics:
assert False, "Metric key %s already used, check definition of %s" % (m['key'], m)
m['kind'] = "Metrics.TMetricKind.%s" % m['kind']
m['units'] = "Metrics.TUnit.%s" % m['units']
metrics[m['key']] = m
return metrics
PREAMBLE = """
// Copyright 2015 Cloudera Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//
// THIS FILE IS AUTO GENERATED BY generate_metrics.py DO NOT MODIFY IT BY HAND.
//
namespace cpp impala
namespace java com.cloudera.impala.thrift
include "Metrics.thrift"
// All metadata associated with a metric. Used to instanciate metrics.
struct TMetricDef {
1: optional string key
2: optional Metrics.TMetricKind kind
3: optional Metrics.TUnit units
4: optional list<string> contexts
5: optional string label
6: optional string description
}
"""
if __name__ == "__main__":
thrift_path = os.path.join(os.getenv('IMPALA_HOME'), 'common/thrift')
metrics = load_metrics(os.path.join(thrift_path, "metrics.json"))
metrics_json = json.dumps(metrics, sort_keys=True, indent=2)
# dumps writes the TMetricKind and TUnit as quoted strings which is not
# interpreted by the thrift compiler correctly. Need to remove the quotes around
# the enum values.
metrics_json = re.sub(r'"(Metrics.TMetricKind.\S+)"', r'\1', metrics_json)
metrics_json = re.sub(r'"(Metrics.TUnit.\S+)"', r'\1', metrics_json)
# The script will always generate the file, CMake will take care of running it only if
# necessary.
target_file = os.path.join(thrift_path, "MetricDefs.thrift")
fid = open(target_file, "w")
try:
fid.write(PREAMBLE)
fid.write("const map<string,TMetricDef> TMetricDefs =\n")
fid.write(metrics_json)
finally:
fid.close()
print("%s created." % target_file)