• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (C) 2010 Google Inc. All rights reserved.
2#
3# Redistribution and use in source and binary forms, with or without
4# modification, are permitted provided that the following conditions are
5# met:
6#
7#     * Redistributions of source code must retain the above copyright
8# notice, this list of conditions and the following disclaimer.
9#     * Redistributions in binary form must reproduce the above
10# copyright notice, this list of conditions and the following disclaimer
11# in the documentation and/or other materials provided with the
12# distribution.
13#     * Neither the name of Google Inc. nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29from datetime import datetime
30import logging
31
32from google.appengine.ext import db
33
34from datastorefile import DataStoreFile
35
36
37class TestFile(DataStoreFile):
38    master = db.StringProperty()
39    builder = db.StringProperty()
40    test_type = db.StringProperty()
41
42    @classmethod
43    def delete_file(cls, key, master, builder, test_type, name, before, limit):
44        if key:
45            file = db.get(key)
46            if not file:
47                logging.warning("File not found, key: %s.", key)
48                return 0
49
50            file._delete_all()
51            return 1
52
53        files = cls.get_files(master, builder, test_type, name, before, load_data=False, limit=limit)
54        if not files:
55            logging.warning(
56                "File not found, master: %s, builder: %s, test_type:%s, name: %s, before: %s.",
57                master, builder, test_type, name, before)
58            return 0
59
60        for file in files:
61            file._delete_all()
62
63        return len(files)
64
65    @classmethod
66    def get_files(cls, master, builder, test_type, name, before=None, load_data=True, limit=1):
67        query = TestFile.all()
68        if master:
69            query = query.filter("master =", master)
70        if builder:
71            query = query.filter("builder =", builder)
72        if test_type:
73            query = query.filter("test_type =", test_type)
74        if name:
75            query = query.filter("name =", name)
76        if before:
77            date = datetime.strptime(before, "%Y-%m-%dT%H:%M:%SZ")
78            query = query.filter("date <", date)
79
80        files = query.order("-date").fetch(limit)
81        if load_data:
82            for file in files:
83                file.load_data()
84
85        return files
86
87    @classmethod
88    def save_file(cls, file, data):
89        file_information = "master: %s, builder: %s, test_type: %s, name: %s." % (file.master, file.builder, file.test_type, file.name)
90        if file.save(data):
91            status_string = "Saved file. %s" % file_information
92            status_code = 200
93        else:
94            status_string = "Couldn't save file. %s" % file_information
95            status_code = 500
96        return status_string, status_code
97
98    @classmethod
99    def overwrite_or_add_file(cls, master, builder, test_type, name, data):
100        files = TestFile.get_files(master, builder, test_type, name)
101        if not files:
102            return cls.add_file(master, builder, test_type, name, data)
103        return cls.save_file(files[0], data)
104
105    @classmethod
106    def add_file(cls, master, builder, test_type, name, data):
107        file = TestFile()
108        file.master = master
109        file.builder = builder
110        file.test_type = test_type
111        file.name = name
112        return cls.save_file(file, data)
113
114    def save(self, data):
115        if not self.save_data(data):
116            return False
117
118        self.date = datetime.now()
119        self.put()
120
121        return True
122
123    def _delete_all(self):
124        self.delete_data()
125        self.delete()
126