desktop_platform_backend.py revision f2477e01787aa58f445919b809d89e252beef54f
1a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)# Copyright 2013 The Chromium Authors. All rights reserved.
2a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)# Use of this source code is governed by a BSD-style license that can be
3a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)# found in the LICENSE file.
4a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
5a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)import os
6a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)import subprocess
7a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
8a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)from telemetry.core import util
9a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)from telemetry.core.platform import platform_backend
10a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
11a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
12a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)class DesktopPlatformBackend(platform_backend.PlatformBackend):
13a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
14a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  # This is an abstract class. It is OK to have abstract methods.
15a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  # pylint: disable=W0223
16a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
17a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  def GetFlushUtilityName(self):
18a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    return NotImplementedError()
19a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
20a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  def FlushSystemCacheForDirectory(self, directory, ignoring=None):
21a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    assert directory and os.path.exists(directory), \
22a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        'Target directory %s must exist' % directory
233551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)    flush_command = util.FindSupportBinary(self.GetFlushUtilityName())
24a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    assert flush_command, \
25a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        'You must build %s first' % self.GetFlushUtilityName()
26a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    args = []
28a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    directory_contents = os.listdir(directory)
29a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    for item in directory_contents:
30c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      if not ignoring or item not in ignoring:
31a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        args.append(os.path.join(directory, item))
32a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    if not args:
34c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      return
35c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # According to msdn:
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # there's a maximum allowable command line of 32,768 characters on windows.
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    while args:
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # Small note about [:256] and [256:]
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # [:N] will return a list with the first N elements, ie.
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # with [1,2,3,4,5], [:2] -> [1,2], and [2:] -> [3,4,5]
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # with [1,2,3,4,5], [:5] -> [1,2,3,4,5] and [5:] -> []
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      p = subprocess.Popen([flush_command, '--recurse'] + args[:256])
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      p.wait()
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      assert p.returncode == 0, 'Failed to flush system cache'
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      args = args[256:]
48