blob: 3e8f614b9165c77d8637b2fe3c62ed76f6894e62 [file] [log] [blame]
# Copyright 2015 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Select an Android build, and download symbols for it."""
from __future__ import print_function
import json
from chromite.lib import osutils
class ProcessedBuildsStorage(object):
"""A context manager for storing processed builds.
This is a context manager that loads recent builds, and allows them to be
manipulated, and then saves them on exit. Processed builds are stored per
branch/target as a list of integers.
def __init__(self, filename):
self.filename = filename
self.value = self._read()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
def _read(self):
"""Load from disk, and default to an empty store on error."""
return json.loads(osutils.ReadFile(self.filename))
except (ValueError, IOError):
# If there was no file, or it was corrupt json, return default.
return {}
def _write(self, new_value):
"""Write the current store to disk."""
return osutils.WriteFile(self.filename,
json.dumps(new_value, sort_keys=True))
def GetProcessedBuilds(self, branch, target):
"""Get a list of builds for a branch/target.
branch: Name of branch as a string.
target: Name of target as a string.
List of integers associated with the given branch/target.
self.value.setdefault(branch, {})
self.value[branch].setdefault(target, [])
return self.value[branch][target]
def PurgeOldBuilds(self, branch, target, retain_list):
"""Removes uninteresting builds for a branch/target.
Any build ids not in the retain list are removed.
branch: Name of branch as a string.
target: Name of target as a string.
retain_list: List of build ids that are still relevent.
processed = set(self.GetProcessedBuilds(branch, target))
retained_processed = processed.intersection(retain_list)
self.value[branch][target] = list(retained_processed)
def AddProcessedBuild(self, branch, target, build_id):
"""Adds build_id to list for a branch/target.
It's safe to add a build_id that is already present.
branch: Name of branch as a string.
target: Name of target as a string.
build_id: build_id to add, as an integer.
processed = set(self.GetProcessedBuilds(branch, target))
self.value[branch][target] = sorted(processed)