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.platform import platform_backend
90529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochfrom telemetry.util import support_binaries
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 FlushSystemCacheForDirectory(self, directory, ignoring=None):
18a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    assert directory and os.path.exists(directory), \
19a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        'Target directory %s must exist' % directory
200529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    flush_command = support_binaries.FindPath('clear_system_cache',
210529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                                              self.GetOSName())
220529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch    assert flush_command, 'You must build clear_system_cache first'
23a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    args = []
25a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    directory_contents = os.listdir(directory)
26a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    for item in directory_contents:
27c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch      if not ignoring or item not in ignoring:
28a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)        args.append(os.path.join(directory, item))
29a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # According to msdn:
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    # there's a maximum allowable command line of 32,768 characters on windows.
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    while args:
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # Small note about [:256] and [256:]
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # [:N] will return a list with the first N elements, ie.
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # with [1,2,3,4,5], [:2] -> [1,2], and [2:] -> [3,4,5]
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      # with [1,2,3,4,5], [:5] -> [1,2,3,4,5] and [5:] -> []
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch      subprocess.check_call([flush_command, '--recurse'] + args[:256])
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      args = args[256:]
40