import os

from perf_insights import map_single_trace
from perf_insights import function_handle
from perf_insights.mre import file_handle
from perf_insights.mre import job as job_module

_METRIC_MAP_FUNCTION_FILENAME = 'metric_map_function.html'

_METRIC_MAP_FUNCTION_NAME = 'metricMapFunction'

def _GetMetricsDir():
  return os.path.dirname(os.path.abspath(__file__))

def _GetMetricRunnerHandle(metric):
  assert isinstance(metric, basestring)
  metrics_dir = _GetMetricsDir()
  metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME)

  modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)]
  map_function_handle = function_handle.FunctionHandle(
      modules_to_load, _METRIC_MAP_FUNCTION_NAME, {'metric': metric})

  return job_module.Job(map_function_handle, None)

def RunMetric(filename, metric, extra_import_options=None):
  th = file_handle.URLFileHandle(filename, 'file://' + filename)
  result = map_single_trace.MapSingleTrace(
      th, _GetMetricRunnerHandle(metric), extra_import_options)

  return result

import os

from perf_insights import map_single_trace
from perf_insights import function_handle
from perf_insights.mre import file_handle
from perf_insights.mre import job as job_module

_METRIC_MAP_FUNCTION_FILENAME = 'metric_map_function.html'

_METRIC_MAP_FUNCTION_NAME = 'metricMapFunction'

def _GetMetricsDir():
  return os.path.dirname(os.path.abspath(__file__))

def _GetMetricRunnerHandle(metric):
  assert isinstance(metric, basestring)
  metrics_dir = _GetMetricsDir()
  metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME)

  modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)]
  map_function_handle = function_handle.FunctionHandle(
      modules_to_load, _METRIC_MAP_FUNCTION_NAME, {'metric': metric})

  return job_module.Job(map_function_handle, None)

def RunMetric(filename, metric, extra_import_options=None):
  url = 'file://' + os.path.abspath(filename)
  th = file_handle.URLFileHandle(filename, url)
  result = map_single_trace.MapSingleTrace(
      th, _GetMetricRunnerHandle(metric), extra_import_options)

  return result

Support relative paths in bin/run_metric
Support relative paths in bin/run_metric

As a result of this patch, it will be possible to run:

  bin/run_metric MemoryMetric test_data/memory_dumps.json
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^

instead of:

  bin/run_metric MemoryMetric $PWD/test_data/memory_dumps.json
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Review URL: https://codereview.chromium.org/1836283008

  import os
  
  from perf_insights import map_single_trace
  from perf_insights import function_handle
  from perf_insights.mre import file_handle
  from perf_insights.mre import job as job_module
  
  _METRIC_MAP_FUNCTION_FILENAME = 'metric_map_function.html'
  
  _METRIC_MAP_FUNCTION_NAME = 'metricMapFunction'
  
  def _GetMetricsDir():
    return os.path.dirname(os.path.abspath(__file__))
  
  def _GetMetricRunnerHandle(metric):
    assert isinstance(metric, basestring)
    metrics_dir = _GetMetricsDir()
    metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME)
  
    modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)]
    map_function_handle = function_handle.FunctionHandle(
        modules_to_load, _METRIC_MAP_FUNCTION_NAME, {'metric': metric})
  
    return job_module.Job(map_function_handle, None)
  
  def RunMetric(filename, metric, extra_import_options=None):
+   url = 'file://' + os.path.abspath(filename)
-   th = file_handle.URLFileHandle(filename, 'file://' + filename)
+   th = file_handle.URLFileHandle(filename, url)
    result = map_single_trace.MapSingleTrace(
        th, _GetMetricRunnerHandle(metric), extra_import_options)
  
    return result
  
Support relative paths in bin/run_metric
## Code Before:
import os

from perf_insights import map_single_trace
from perf_insights import function_handle
from perf_insights.mre import file_handle
from perf_insights.mre import job as job_module

_METRIC_MAP_FUNCTION_FILENAME = 'metric_map_function.html'

_METRIC_MAP_FUNCTION_NAME = 'metricMapFunction'

def _GetMetricsDir():
  return os.path.dirname(os.path.abspath(__file__))

def _GetMetricRunnerHandle(metric):
  assert isinstance(metric, basestring)
  metrics_dir = _GetMetricsDir()
  metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME)

  modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)]
  map_function_handle = function_handle.FunctionHandle(
      modules_to_load, _METRIC_MAP_FUNCTION_NAME, {'metric': metric})

  return job_module.Job(map_function_handle, None)

def RunMetric(filename, metric, extra_import_options=None):
  th = file_handle.URLFileHandle(filename, 'file://' + filename)
  result = map_single_trace.MapSingleTrace(
      th, _GetMetricRunnerHandle(metric), extra_import_options)

  return result

## Instruction:
Support relative paths in bin/run_metric
## Code After:
import os

from perf_insights import map_single_trace
from perf_insights import function_handle
from perf_insights.mre import file_handle
from perf_insights.mre import job as job_module

_METRIC_MAP_FUNCTION_FILENAME = 'metric_map_function.html'

_METRIC_MAP_FUNCTION_NAME = 'metricMapFunction'

def _GetMetricsDir():
  return os.path.dirname(os.path.abspath(__file__))

def _GetMetricRunnerHandle(metric):
  assert isinstance(metric, basestring)
  metrics_dir = _GetMetricsDir()
  metric_mapper_path = os.path.join(metrics_dir, _METRIC_MAP_FUNCTION_FILENAME)

  modules_to_load = [function_handle.ModuleToLoad(filename=metric_mapper_path)]
  map_function_handle = function_handle.FunctionHandle(
      modules_to_load, _METRIC_MAP_FUNCTION_NAME, {'metric': metric})

  return job_module.Job(map_function_handle, None)

def RunMetric(filename, metric, extra_import_options=None):
  url = 'file://' + os.path.abspath(filename)
  th = file_handle.URLFileHandle(filename, url)
  result = map_single_trace.MapSingleTrace(
      th, _GetMetricRunnerHandle(metric), extra_import_options)

  return result
