Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Unified Diff: tools/telemetry/telemetry/util/file_handle.py

Issue 545523002: [Telemetry] Add capability for values to reference external files. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Work around GPU triggered tests not knowing about trace_viewer Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/telemetry/telemetry/results/results_options.py ('k') | tools/telemetry/telemetry/value/__init__.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/telemetry/telemetry/util/file_handle.py
diff --git a/tools/telemetry/telemetry/util/file_handle.py b/tools/telemetry/telemetry/util/file_handle.py
new file mode 100644
index 0000000000000000000000000000000000000000..456d5b90b923a94f15a84dda567002d146753300
--- /dev/null
+++ b/tools/telemetry/telemetry/util/file_handle.py
@@ -0,0 +1,90 @@
+# Copyright 2014 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.
+
+import os
+import shutil
+
+_next_file_id = 0
+
+class FileHandle(object):
+ def __init__(self, tf, absolute_path=None):
+ """Constructs a FileHandle object.
+
+ This constructor should not be used by the user; rather it is preferred to
+ use the module-level GetRelPath and FromTempFile functions.
+
+ Args:
+ tf: An instance of a temporary file object.
+ absolute_path: A path; should not be passed if tempfile is and vice-versa.
+ """
+ # We should never build a file handle from nothing.
+ assert (absolute_path is None) != (tf is None)
+ self._tf = tf
+ self._absolute_path = absolute_path
+
+ global _next_file_id
+ self._id = _next_file_id
+ _next_file_id += 1
+
+ @property
+ def id(self):
+ return self._id
+
+ def GetRelPath(self, start=os.curdir):
+ """Returns the path to the pointed-to file relative to the given start path.
+
+ Args:
+ start: A string representing a starting path.
+ Returns:
+ A string giving the relative path from path to this file.
+ """
+ if self._tf:
+ name = self._tf.name
+ else:
+ name = self._absolute_path
+
+ return os.path.relpath(name, start)
+
+def FromTempFile(tf):
+ """Constructs a FileHandle pointing to a temporary file.
+
+ Returns:
+ A FileHandle referring to a named temporary file.
+ """
+ return FileHandle(tf)
+
+def FromPath(path):
+ """Constructs a FileHandle from an absolute path.
+
+ Args:
+ path: A string giving the absolute path to a file.
+ Returns:
+ A FileHandle referring to the file at the specified path.
+ """
+ return FileHandle(None, os.path.abspath(path))
+
+def OutputFiles(file_handles, prefix, extension):
+ """Outputs a list of file_handles by ID with a given prefix and extension.
+
+ For the normal use case where we generate a collection of FileHandles
+ corresponding to temporary files, it is often necessary to collate the
+ represented files into a single place. This function copies each file
+ referenced by an element of file_handles to a standardized location indicated
+ by prefix.
+
+ Args:
+ file_handles: A list of file handles
+ prefix: A string, usually a path.
+ extension: Extension to append.
+ Returns:
+ A dict mapping IDs to output filenames.
+ """
+ files = dict()
+
+ for fh in file_handles:
+ name = prefix + str(fh.id) + extension
+ shutil.copy(fh.GetRelPath(), name)
+ files[fh.id] = os.path.abspath(name)
+
+ return files
« no previous file with comments | « tools/telemetry/telemetry/results/results_options.py ('k') | tools/telemetry/telemetry/value/__init__.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698