|
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 import urllib |
|
32 import urllib2 |
|
33 |
|
34 from google.appengine.ext import db |
|
35 |
|
36 SVN_PATH_DASHBOARD = ("http://src.chromium.org/viewvc/chrome/trunk/tools/" |
|
37 "dashboards/") |
|
38 |
|
39 class DashboardFile(db.Model): |
|
40 name = db.StringProperty() |
|
41 data = db.BlobProperty() |
|
42 date = db.DateTimeProperty(auto_now_add=True) |
|
43 |
|
44 @classmethod |
|
45 def get_files(cls, name, limit=1): |
|
46 query = DashboardFile.all() |
|
47 if name: |
|
48 query = query.filter("name =", name) |
|
49 return query.order("-date").fetch(limit) |
|
50 |
|
51 @classmethod |
|
52 def add_file(cls, name, data): |
|
53 file = DashboardFile() |
|
54 file.name = name |
|
55 file.data = db.Blob(data) |
|
56 file.put() |
|
57 |
|
58 logging.debug("Dashboard file saved, name: %s.", name) |
|
59 |
|
60 return file |
|
61 |
|
62 @classmethod |
|
63 def grab_file_from_svn(cls, name): |
|
64 logging.debug("Grab file from SVN, name: %s.", name) |
|
65 |
|
66 url = SVN_PATH_DASHBOARD + urllib.quote_plus(name) |
|
67 |
|
68 logging.info("Grab file from SVN, url: %s.", url) |
|
69 try: |
|
70 file = urllib2.urlopen(url) |
|
71 if not file: |
|
72 logging.error("Failed to grab dashboard file: %s.", url) |
|
73 return None |
|
74 |
|
75 return file.read() |
|
76 except urllib2.HTTPError, e: |
|
77 logging.error("Failed to grab dashboard file: %s", str(e)) |
|
78 except urllib2.URLError, e: |
|
79 logging.error("Failed to grab dashboard file: %s", str(e)) |
|
80 |
|
81 return None |
|
82 |
|
83 @classmethod |
|
84 def update_file(cls, name): |
|
85 data = cls.grab_file_from_svn(name) |
|
86 if not data: |
|
87 return None |
|
88 |
|
89 logging.info("Got file from SVN.") |
|
90 |
|
91 files = cls.get_files(name) |
|
92 if not files: |
|
93 logging.info("No existing file, added as new file.") |
|
94 return cls.add_file(name, data) |
|
95 |
|
96 logging.debug("Updating existing file.") |
|
97 file = files[0] |
|
98 file.data = data |
|
99 file.date = datetime.now() |
|
100 file.put() |
|
101 |
|
102 logging.info("Dashboard file replaced, name: %s.", name) |
|
103 |
|
104 return file |
|
105 |
|
106 @classmethod |
|
107 def delete_file(cls, name): |
|
108 files = cls.get_files(name) |
|
109 if not files: |
|
110 logging.warning("File not found, name: %s.", name) |
|
111 return False |
|
112 |
|
113 for file in files: |
|
114 file.delete() |
|
115 |
|
116 return True |