1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: vcsrevisions.py 62484 2016-07-22 18:35:33Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Test Manager - VcsRevisions
|
---|
6 | """
|
---|
7 |
|
---|
8 | __copyright__ = \
|
---|
9 | """
|
---|
10 | Copyright (C) 2012-2016 Oracle Corporation
|
---|
11 |
|
---|
12 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
13 | available from http://www.virtualbox.org. This file is free software;
|
---|
14 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
15 | General Public License (GPL) as published by the Free Software
|
---|
16 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
17 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
18 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
19 |
|
---|
20 | The contents of this file may alternatively be used under the terms
|
---|
21 | of the Common Development and Distribution License Version 1.0
|
---|
22 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
23 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
24 | CDDL are applicable instead of those of the GPL.
|
---|
25 |
|
---|
26 | You may elect to license modified versions of this file under the
|
---|
27 | terms and conditions of either the GPL or the CDDL or both.
|
---|
28 | """
|
---|
29 | __version__ = "$Revision: 62484 $"
|
---|
30 |
|
---|
31 |
|
---|
32 | # Standard python imports.
|
---|
33 | import unittest;
|
---|
34 |
|
---|
35 | # Validation Kit imports.
|
---|
36 | from testmanager.core.base import ModelDataBase, ModelDataBaseTestCase, ModelLogicBase, TMExceptionBase;
|
---|
37 |
|
---|
38 |
|
---|
39 | class VcsRevisionData(ModelDataBase):
|
---|
40 | """
|
---|
41 | A version control system (VCS) revision.
|
---|
42 | """
|
---|
43 |
|
---|
44 | #kasIdAttr = ['sRepository',iRevision];
|
---|
45 |
|
---|
46 | ksParam_sRepository = 'VcsRevision_sRepository';
|
---|
47 | ksParam_iRevision = 'VcsRevision_iRevision';
|
---|
48 | ksParam_tsCreated = 'VcsRevision_tsCreated';
|
---|
49 | ksParam_sAuthor = 'VcsRevision_sAuthor';
|
---|
50 | ksParam_sMessage = 'VcsRevision_sMessage';
|
---|
51 |
|
---|
52 | kasAllowNullAttributes = [ ];
|
---|
53 | kfAllowUnicode_sMessage = True;
|
---|
54 |
|
---|
55 | def __init__(self):
|
---|
56 | ModelDataBase.__init__(self);
|
---|
57 |
|
---|
58 | #
|
---|
59 | # Initialize with defaults.
|
---|
60 | # See the database for explanations of each of these fields.
|
---|
61 | #
|
---|
62 | self.sRepository = None;
|
---|
63 | self.iRevision = None;
|
---|
64 | self.tsCreated = None;
|
---|
65 | self.sAuthor = None;
|
---|
66 | self.sMessage = None;
|
---|
67 |
|
---|
68 | def initFromDbRow(self, aoRow):
|
---|
69 | """
|
---|
70 | Re-initializes the object from a SELECT * FROM VcsRevisions row.
|
---|
71 | Returns self. Raises exception if aoRow is None.
|
---|
72 | """
|
---|
73 | if aoRow is None:
|
---|
74 | raise TMExceptionBase('VcsRevision not found.');
|
---|
75 |
|
---|
76 | self.sRepository = aoRow[0];
|
---|
77 | self.iRevision = aoRow[1];
|
---|
78 | self.tsCreated = aoRow[2];
|
---|
79 | self.sAuthor = aoRow[3];
|
---|
80 | self.sMessage = aoRow[4];
|
---|
81 | return self;
|
---|
82 |
|
---|
83 | def initFromDbWithRepoAndRev(self, oDb, sRepository, iRevision):
|
---|
84 | """
|
---|
85 | Initialize from the database, given the tree and revision of a row.
|
---|
86 | """
|
---|
87 | oDb.execute('SELECT * FROM VcsRevisions WHERE sRepository = %s AND iRevision = %u', (sRepository, iRevision,));
|
---|
88 | aoRow = oDb.fetchOne()
|
---|
89 | if aoRow is None:
|
---|
90 | raise TMExceptionBase('sRepository = %s iRevision = %u not found' % (sRepository, iRevision, ));
|
---|
91 | return self.initFromDbRow(aoRow);
|
---|
92 |
|
---|
93 | def initFromValues(self, sRepository, iRevision, tsCreated, sAuthor, sMessage):
|
---|
94 | """
|
---|
95 | Reinitializes form a set of values.
|
---|
96 | return self.
|
---|
97 | """
|
---|
98 | self.sRepository = sRepository;
|
---|
99 | self.iRevision = iRevision;
|
---|
100 | self.tsCreated = tsCreated;
|
---|
101 | self.sAuthor = sAuthor;
|
---|
102 | self.sMessage = sMessage;
|
---|
103 | return self;
|
---|
104 |
|
---|
105 |
|
---|
106 | class VcsRevisionLogic(ModelLogicBase): # pylint: disable=R0903
|
---|
107 | """
|
---|
108 | VCS revisions database logic.
|
---|
109 | """
|
---|
110 |
|
---|
111 | #
|
---|
112 | # Standard methods.
|
---|
113 | #
|
---|
114 |
|
---|
115 | def fetchForListing(self, iStart, cMaxRows, tsNow):
|
---|
116 | """
|
---|
117 | Fetches VCS revisions for listing.
|
---|
118 |
|
---|
119 | Returns an array (list) of VcsRevisionData items, empty list if none.
|
---|
120 | Raises exception on error.
|
---|
121 | """
|
---|
122 | _ = tsNow;
|
---|
123 | self._oDb.execute('SELECT *\n'
|
---|
124 | 'FROM VcsRevisions\n'
|
---|
125 | 'ORDER BY tsCreated, sRepository, iRevision\n'
|
---|
126 | 'LIMIT %s OFFSET %s\n'
|
---|
127 | , (cMaxRows, iStart,));
|
---|
128 |
|
---|
129 | aoRows = [];
|
---|
130 | for _ in range(self._oDb.getRowCount()):
|
---|
131 | aoRows.append(VcsRevisionData().initFromDbRow(self._oDb.fetchOne()));
|
---|
132 | return aoRows;
|
---|
133 |
|
---|
134 | def tryFetch(self, sRepository, iRevision):
|
---|
135 | """
|
---|
136 | Tries to fetch the specified tree revision record.
|
---|
137 | Returns VcsRevisionData instance if found, None if not found.
|
---|
138 | Raises exception on input and database errors.
|
---|
139 | """
|
---|
140 | self._oDb.execute('SELECT * FROM VcsRevisions WHERE sRepository = %s AND iRevision = %s',
|
---|
141 | ( sRepository, iRevision, ));
|
---|
142 | aaoRows = self._oDb.fetchAll();
|
---|
143 | if len(aaoRows) == 1:
|
---|
144 | return VcsRevisionData().initFromDbRow(aaoRows[0]);
|
---|
145 | if len(aaoRows) != 0:
|
---|
146 | raise TMExceptionBase('VcsRevisions has a primary key problem: %u duplicates' % (len(aaoRows),));
|
---|
147 | return None
|
---|
148 |
|
---|
149 |
|
---|
150 | #
|
---|
151 | # Other methods.
|
---|
152 | #
|
---|
153 |
|
---|
154 | def addVcsRevision(self, oData, fCommit = False):
|
---|
155 | """
|
---|
156 | Adds (or updates) a tree revision record.
|
---|
157 | Raises exception on input and database errors.
|
---|
158 | """
|
---|
159 |
|
---|
160 | # Check VcsRevisionData before do anything
|
---|
161 | dDataErrors = oData.validateAndConvert(self._oDb, oData.ksValidateFor_Add);
|
---|
162 | if len(dDataErrors) > 0:
|
---|
163 | raise TMExceptionBase('Invalid data passed to addVcsRevision(): %s' % (dDataErrors,));
|
---|
164 |
|
---|
165 | # Does it already exist?
|
---|
166 | oOldData = self.tryFetch(oData.sRepository, oData.iRevision);
|
---|
167 | if oOldData is None:
|
---|
168 | # New row.
|
---|
169 | self._oDb.execute('INSERT INTO VcsRevisions (sRepository, iRevision, tsCreated, sAuthor, sMessage)\n'
|
---|
170 | 'VALUES (%s, %s, %s, %s, %s)\n'
|
---|
171 | , ( oData.sRepository,
|
---|
172 | oData.iRevision,
|
---|
173 | oData.tsCreated,
|
---|
174 | oData.sAuthor,
|
---|
175 | oData.sMessage,
|
---|
176 | ));
|
---|
177 | elif not oOldData.isEqual(oData):
|
---|
178 | # Update old row.
|
---|
179 | self._oDb.execute('UPDATE VcsRevisions\n'
|
---|
180 | ' SET tsCreated = %s,\n'
|
---|
181 | ' sAuthor = %s,\n'
|
---|
182 | ' sMessage = %s\n'
|
---|
183 | 'WHERE sRepository = %s\n'
|
---|
184 | ' AND iRevision = %s'
|
---|
185 | , ( oData.tsCreated,
|
---|
186 | oData.sAuthor,
|
---|
187 | oData.sMessage,
|
---|
188 | oData.sRepository,
|
---|
189 | oData.iRevision,
|
---|
190 | ));
|
---|
191 |
|
---|
192 | self._oDb.maybeCommit(fCommit);
|
---|
193 | return oData;
|
---|
194 |
|
---|
195 | def getLastRevision(self, sRepository):
|
---|
196 | """
|
---|
197 | Get the last known revision number for the given repository, returns 0
|
---|
198 | if the repository is not known to us:
|
---|
199 | """
|
---|
200 | self._oDb.execute('SELECT iRevision\n'
|
---|
201 | 'FROM VcsRevisions\n'
|
---|
202 | 'WHERE sRepository = %s\n'
|
---|
203 | 'ORDER BY iRevision DESC\n'
|
---|
204 | 'LIMIT 1\n'
|
---|
205 | , ( sRepository, ));
|
---|
206 | if self._oDb.getRowCount() == 0:
|
---|
207 | return 0;
|
---|
208 | return self._oDb.fetchOne()[0];
|
---|
209 |
|
---|
210 | def fetchTimeline(self, sRepository, iRevision, cEntriesBack):
|
---|
211 | """
|
---|
212 | Fetches a VCS timeline portion for a repository.
|
---|
213 |
|
---|
214 | Returns an array (list) of VcsRevisionData items, empty list if none.
|
---|
215 | Raises exception on error.
|
---|
216 | """
|
---|
217 | self._oDb.execute('SELECT *\n'
|
---|
218 | 'FROM VcsRevisions\n'
|
---|
219 | 'WHERE sRepository = %s\n'
|
---|
220 | ' AND iRevision > %s\n'
|
---|
221 | ' AND iRevision <= %s\n'
|
---|
222 | 'ORDER BY iRevision DESC\n'
|
---|
223 | 'LIMIT %s\n'
|
---|
224 | , ( sRepository, iRevision - cEntriesBack*2 + 1, iRevision, cEntriesBack));
|
---|
225 | aoRows = [];
|
---|
226 | for _ in range(self._oDb.getRowCount()):
|
---|
227 | aoRows.append(VcsRevisionData().initFromDbRow(self._oDb.fetchOne()));
|
---|
228 | return aoRows;
|
---|
229 |
|
---|
230 |
|
---|
231 | #
|
---|
232 | # Unit testing.
|
---|
233 | #
|
---|
234 |
|
---|
235 | # pylint: disable=C0111
|
---|
236 | class VcsRevisionDataTestCase(ModelDataBaseTestCase):
|
---|
237 | def setUp(self):
|
---|
238 | self.aoSamples = [VcsRevisionData(),];
|
---|
239 |
|
---|
240 | if __name__ == '__main__':
|
---|
241 | unittest.main();
|
---|
242 | # not reached.
|
---|
243 |
|
---|