Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / TestResultServer / model / testfile.py
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
29 from datetime import datetime
30 import logging
31
32 from google.appengine.ext import db
33
34 from datastorefile import DataStoreFile
35
36
37 class TestFile(DataStoreFile):
38     master = db.StringProperty()
39     builder = db.StringProperty()
40     test_type = db.StringProperty()
41     build_number = db.IntegerProperty()
42
43     @property
44     def file_information(self):
45         return "master: %s, builder: %s, test_type: %s, build_number: %r, name: %s." % (
46             self.master, self.builder, self.test_type, self.build_number, self.name)
47
48     @classmethod
49     def delete_file(cls, key, master, builder, test_type, build_number, name, before, limit):
50         if key:
51             file = db.get(key)
52             if not file:
53                 logging.warning("File not found, key: %s.", key)
54                 return 0
55
56             file._delete_all()
57             return 1
58
59         files = cls.get_files(master, builder, test_type, build_number, name, before, load_data=False, limit=limit)
60         if not files:
61             logging.warning(
62                 "File not found, master: %s, builder: %s, test_type:%s, name: %s, before: %s.",
63                 master, builder, test_type, name, before)
64             return 0
65
66         for file in files:
67             file._delete_all()
68
69         return len(files)
70
71     @classmethod
72     def get_files(cls, master, builder, test_type, build_number, name, before=None, load_data=True, limit=1):
73         query = TestFile.all()
74         if master:
75             query = query.filter("master =", master)
76         if builder:
77             query = query.filter("builder =", builder)
78         if test_type:
79             query = query.filter("test_type =", test_type)
80         if build_number == 'latest':
81             query = query.sort('-build_number')
82         elif build_number is not None and build_number != 'None':
83             # TestFile objects that were added to the persistent DB before the build_number
84             # property was added will have a default build_number of None.  When those files
85             # appear in a list view generated from templates/showfilelist.html, the URL
86             # to get the individual file contents will have '&buildnumber=None' in it.
87             # Hence, we ignore the string 'None' in build_number query parameter.
88             try:
89                 query = query.filter("build_number =", int(build_number))
90             except (TypeError, ValueError):
91                 logging.error("Could not convert buildnumber parameter '%s' to an integer", build_number)
92                 return None
93         if name:
94             query = query.filter("name =", name)
95         if before:
96             date = datetime.strptime(before, "%Y-%m-%dT%H:%M:%SZ")
97             query = query.filter("date <", date)
98
99         files = query.order("-date").fetch(limit)
100         if load_data:
101             for file in files:
102                 file.load_data()
103
104         return files
105
106     @classmethod
107     def save_file(cls, file, data):
108         if file.save(data):
109             status_string = "Saved file. %s" % file.file_information
110             status_code = 200
111         else:
112             status_string = "Couldn't save file. %s" % file.file_information
113             status_code = 500
114         return status_string, status_code
115
116     @classmethod
117     def add_file(cls, master, builder, test_type, build_number, name, data):
118         file = TestFile()
119         file.master = master
120         file.builder = builder
121         file.test_type = test_type
122         file.build_number = build_number
123         file.name = name
124         return cls.save_file(file, data)
125
126     def save(self, data):
127         if not self.save_data(data):
128             return False
129
130         self.date = datetime.now()
131         self.put()
132
133         return True
134
135     def _delete_all(self):
136         self.delete_data()
137         self.delete()