gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 1 | from urlparse import urlparse |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 2 | from gspread import WorksheetNotFound, login |
| 3 | import json |
| 4 | import os |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 5 | from config import ROW_COUNT, DOCUMENT_ID, WORK_SHEET |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 6 | |
| 7 | |
| 8 | def get_work_sheet(sheet, name, column_names): |
| 9 | try: |
| 10 | work_sheet = sheet.worksheet(name) |
| 11 | except WorksheetNotFound: |
| 12 | work_sheet = sheet.add_worksheet(title=name, rows=ROW_COUNT, |
| 13 | cols=max(40, len(column_names))) |
| 14 | |
| 15 | for i in range(1, len(column_names) + 1): |
| 16 | work_sheet.update_cell(1, i, column_names[i - 1]) |
| 17 | |
| 18 | return work_sheet |
| 19 | |
| 20 | |
| 21 | def get_row_number(work_sheet): |
| 22 | num = 2 |
| 23 | |
| 24 | while num < work_sheet.row_count and work_sheet.cell(num, 1).value != "": |
| 25 | num += 1 |
| 26 | |
| 27 | if num == work_sheet.row_count: |
| 28 | work_sheet.append_row(["" for x in range(work_sheet.col_count)]) |
| 29 | |
| 30 | return num |
| 31 | |
| 32 | |
| 33 | def append_row(work_sheet, row): |
| 34 | row_number = get_row_number(work_sheet) |
| 35 | |
| 36 | i = 1 |
| 37 | for k in row.keys(): |
| 38 | work_sheet.update_cell(row_number, i, row[k]) |
| 39 | i += 1 |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 40 | |
| 41 | |
| 42 | class Measurement(object): |
| 43 | def __init__(self): |
| 44 | self.build = "" |
| 45 | self.build_type = 0 # GA/Master/Other |
| 46 | self.md5 = "" |
| 47 | self.results = { |
| 48 | "": (float, float) |
| 49 | } |
| 50 | |
| 51 | |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 52 | def create_storage(url, email, password): |
| 53 | u = urlparse(url) |
| 54 | |
| 55 | if u.scheme == 'file': |
| 56 | storage = DiskStorage(u.path) |
| 57 | else: |
| 58 | storage = GoogleDocsStorage(DOCUMENT_ID, WORK_SHEET, email, password) |
| 59 | |
| 60 | return storage |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 61 | |
| 62 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 63 | class Storage(object): |
gstepanov | bd7c329 | 2015-02-03 15:23:41 +0200 | [diff] [blame] | 64 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 65 | def store(self, data): |
| 66 | pass |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 67 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 68 | def retrieve(self, id): |
| 69 | pass |
| 70 | |
| 71 | |
| 72 | class GoogleDocsStorage(Storage): |
| 73 | |
| 74 | def __init__(self, doc_id, work_sheet_name, email=None, password=None): |
| 75 | self.gc = login(email, password) |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 76 | self.sh = self.gc.open_by_key(doc_id) |
| 77 | self.work_sheet = get_work_sheet(self.sh, work_sheet_name, 40) |
| 78 | |
| 79 | def store(self, data): |
| 80 | append_row(self.work_sheet, data) |
| 81 | |
| 82 | def retrieve(self, id): |
| 83 | row_number = self.find_by_id(id) |
| 84 | |
| 85 | if row_number != -1: |
| 86 | vals = self.work_sheet.row_values(row_number) |
| 87 | m = Measurement() |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 88 | m.build = vals.pop("build_id") |
| 89 | m.build_type = vals.pop("type") |
| 90 | m.md5 = vals.pop("iso_md5") |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 91 | m.results = {k: vals[k] for k in vals.keys()} |
| 92 | else: |
| 93 | return None |
| 94 | |
| 95 | def find_by_id(self, row_id): |
| 96 | for i in range(1, self.work_sheet): |
| 97 | if self.work_sheet.cell(i, 1) == row_id: |
| 98 | return i |
| 99 | |
| 100 | return -1 |
| 101 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 102 | def recent_builds(self): |
| 103 | i = self.work_sheet.row_count - 1 |
| 104 | d = {} |
| 105 | result = [] |
| 106 | |
| 107 | while i > 0: |
| 108 | vals = self.work_sheet.row_values(i) |
| 109 | |
| 110 | if vals["type"] not in d: |
| 111 | d[vals["type"]] = vals |
| 112 | m = Measurement() |
| 113 | m.build = vals.pop("build_id") |
| 114 | m.build_type = vals.pop("type") |
| 115 | m.md5 = vals.pop("iso_md5") |
| 116 | m.results = {k: vals[k] for k in vals.keys()} |
| 117 | result.append(m) |
| 118 | |
| 119 | i -= 1 |
| 120 | |
| 121 | return result |
| 122 | |
gstepanov | 71764e9 | 2015-02-02 16:33:11 +0200 | [diff] [blame] | 123 | |
gstepanov | e0f26ab | 2015-02-02 18:09:37 +0200 | [diff] [blame] | 124 | class DiskStorage(Storage): |
| 125 | def __init__(self, file_name): |
| 126 | self.file_name = file_name |
| 127 | |
| 128 | if not os.path.exists(file_name): |
| 129 | with open(file_name, "w+") as f: |
| 130 | f.write(json.dumps([])) |
| 131 | |
| 132 | def store(self, data): |
| 133 | with open(self.file_name, "rt") as f: |
| 134 | raw_data = f.read() |
| 135 | document = json.loads(raw_data) |
| 136 | document.append(data) |
| 137 | |
| 138 | with open(self.file_name, "w+") as f: |
| 139 | f.write(json.dumps(document)) |
| 140 | |
| 141 | def retrieve(self, id): |
| 142 | with open(self.file_name, "rt") as f: |
| 143 | raw_data = f.read() |
| 144 | document = json.loads(raw_data) |
| 145 | |
| 146 | for row in document: |
| 147 | if row["build_id"] == id: |
| 148 | m = Measurement() |
| 149 | m.build = row.pop("build_id") |
| 150 | m.build_type = row.pop("type") |
| 151 | m.md5 = row.pop("iso_md5") |
| 152 | m.results = {k: row[k] for k in row.keys()} |
| 153 | |
| 154 | return m |
| 155 | return None |
| 156 | |
gstepanov | 82d2a94 | 2015-02-03 15:10:11 +0200 | [diff] [blame] | 157 | def recent_builds(self): |
| 158 | with open(self.file_name, "rt") as f: |
| 159 | raw_data = f.read() |
| 160 | document = json.loads(raw_data) |
| 161 | d = {} |
| 162 | result = [] |
| 163 | |
| 164 | for i in range(len(document) - 1, -1, - 1): |
| 165 | if document[i]["type"] not in d: |
| 166 | d[document[i]["type"]] = document[i] |
| 167 | |
| 168 | for k in d.keys(): |
| 169 | m = Measurement() |
| 170 | m.build = d[k].pop("build_id") |
| 171 | m.build_type = d[k].pop("type") |
| 172 | m.md5 = d[k].pop("iso_md5") |
| 173 | m.results = {k: d[key] for key in d.keys()} |
| 174 | result.append(m) |
| 175 | |
| 176 | return d |
| 177 | |
gstepanov | 7cc6ba0 | 2015-02-03 17:18:42 +0200 | [diff] [blame] | 178 | if __name__ == "__main__": |
| 179 | create_storage("file:///home/gstepanov/bla?email=aaa.gmail.com&password=1234") |