1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: vcsrevisions.py 56295 2015-06-09 14:29:55Z vboxsync $
|
---|
3 |
|
---|
4 | """
|
---|
5 | Test Manager - VcsRevisions
|
---|
6 | """
|
---|
7 |
|
---|
8 | __copyright__ = \
|
---|
9 | """
|
---|
10 | Copyright (C) 2012-2015 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: 56295 $"
|
---|
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 |
|
---|
54 | def __init__(self):
|
---|
55 | ModelDataBase.__init__(self);
|
---|
56 |
|
---|
57 | #
|
---|
58 | # Initialize with defaults.
|
---|
59 | # See the database for explanations of each of these fields.
|
---|
60 | #
|
---|
61 | self.sRepository = None;
|
---|
62 | self.iRevision = None;
|
---|
63 | self.tsCreated = None;
|
---|
64 | self.sAuthor = None;
|
---|
65 | self.sMessage = None;
|
---|
66 |
|
---|
67 | def initFromDbRow(self, aoRow):
|
---|
68 | """
|
---|
69 | Re-initializes the object from a SELECT * FROM VcsRevisions row.
|
---|
70 | Returns self. Raises exception if aoRow is None.
|
---|
71 | """
|
---|
72 | if aoRow is None:
|
---|
73 | raise TMExceptionBase('VcsRevision not found.');
|
---|
74 |
|
---|
75 | self.sRepository = aoRow[0];
|
---|
76 | self.iRevision = aoRow[1];
|
---|
77 | self.tsCreated = aoRow[2];
|
---|
78 | self.sAuthor = aoRow[3];
|
---|
79 | self.sMessage = aoRow[4];
|
---|
80 | return self;
|
---|
81 |
|
---|
82 | def initFromDbWithRepoAndRev(self, oDb, sRepository, iRevision):
|
---|
83 | """
|
---|
84 | Initialize from the database, given the tree and revision of a row.
|
---|
85 | """
|
---|
86 | oDb.execute('SELECT * FROM VcsRevisions WHERE sRepository = %s AND iRevision = %u', (sRepository, iRevision,));
|
---|
87 | aoRow = oDb.fetchOne()
|
---|
88 | if aoRow is None:
|
---|
89 | raise TMExceptionBase('sRepository = %s iRevision = %u not found' % (sRepository, iRevision, ));
|
---|
90 | return self.initFromDbRow(aoRow);
|
---|
91 |
|
---|
92 | def initFromValues(self, sRepository, iRevision, tsCreated, sAuthor, sMessage):
|
---|
93 | """
|
---|
94 | Reinitializes form a set of values.
|
---|
95 | return self.
|
---|
96 | """
|
---|
97 | self.sRepository = sRepository;
|
---|
98 | self.iRevision = iRevision;
|
---|
99 | self.tsCreated = tsCreated;
|
---|
100 | self.sAuthor = sAuthor;
|
---|
101 | self.sMessage = sMessage;
|
---|
102 | return self;
|
---|
103 |
|
---|
104 |
|
---|
105 | class VcsRevisionLogic(ModelLogicBase): # pylint: disable=R0903
|
---|
106 | """
|
---|
107 | VCS revisions database logic.
|
---|
108 | """
|
---|
109 |
|
---|
110 | #
|
---|
111 | # Standard methods.
|
---|
112 | #
|
---|
113 |
|
---|
114 | def fetchForListing(self, iStart, cMaxRows, tsNow):
|
---|
115 | """
|
---|
116 | Fetches VCS revisions for listing.
|
---|
117 |
|
---|
118 | Returns an array (list) of VcsRevisionData items, empty list if none.
|
---|
119 | Raises exception on error.
|
---|
120 | """
|
---|
121 | _ = tsNow;
|
---|
122 | self._oDb.execute('SELECT *\n'
|
---|
123 | 'FROM VcsRevisions\n'
|
---|
124 | 'ORDER BY tsCreated, sRepository, iRevision\n'
|
---|
125 | 'LIMIT %s OFFSET %s\n'
|
---|
126 | , (cMaxRows, iStart,));
|
---|
127 |
|
---|
128 | aoRows = [];
|
---|
129 | for _ in range(self._oDb.getRowCount()):
|
---|
130 | aoRows.append(VcsRevisionData().initFromDbRow(self._oDb.fetchOne()));
|
---|
131 | return aoRows;
|
---|
132 |
|
---|
133 | def tryFetch(self, sRepository, iRevision):
|
---|
134 | """
|
---|
135 | Tries to fetch the specified tree revision record.
|
---|
136 | Returns VcsRevisionData instance if found, None if not found.
|
---|
137 | Raises exception on input and database errors.
|
---|
138 | """
|
---|
139 | self._oDb.execute('SELECT * FROM VcsRevisions WHERE sRepository = %s AND iRevision = %s',
|
---|
140 | ( sRepository, iRevision, ));
|
---|
141 | aaoRows = self._oDb.fetchAll();
|
---|
142 | if len(aaoRows) == 1:
|
---|
143 | return VcsRevisionData().initFromDbRow(aaoRows[0]);
|
---|
144 | if len(aaoRows) != 0:
|
---|
145 | raise TMExceptionBase('VcsRevisions has a primary key problem: %u duplicates' % (len(aaoRows),));
|
---|
146 | return None
|
---|
147 |
|
---|
148 |
|
---|
149 | #
|
---|
150 | # Other methods.
|
---|
151 | #
|
---|
152 |
|
---|
153 | def addVcsRevision(self, oData, fCommit = False):
|
---|
154 | """
|
---|
155 | Adds (or updates) a tree revision record.
|
---|
156 | Raises exception on input and database errors.
|
---|
157 | """
|
---|
158 |
|
---|
159 | # Check VcsRevisionData before do anything
|
---|
160 | dDataErrors = oData.validateAndConvert(self._oDb);
|
---|
161 | if len(dDataErrors) > 0:
|
---|
162 | raise TMExceptionBase('Invalid data passed to addCvsRevision(): %s' % (dDataErrors,));
|
---|
163 |
|
---|
164 | # Does it already exist?
|
---|
165 | oOldData = self.tryFetch(oData.sRepository, oData.iRevision);
|
---|
166 | if oOldData is None:
|
---|
167 | # New row.
|
---|
168 | self._oDb.execute('INSERT INTO VcsRevisions (sRepository, iRevision, tsCreated, sAuthor, sMessage)\n'
|
---|
169 | 'VALUES (%s, %s, %s, %s, %s)\n'
|
---|
170 | , ( oData.sRepository,
|
---|
171 | oData.iRevision,
|
---|
172 | oData.tsCreated,
|
---|
173 | oData.sAuthor,
|
---|
174 | oData.sMessage,
|
---|
175 | ));
|
---|
176 | elif not oOldData.isEqual(oData):
|
---|
177 | # Update old row.
|
---|
178 | self._oDb.execute('UPDATE VcsRevisions\n'
|
---|
179 | ' SET tsCreated = %s,\n'
|
---|
180 | ' sAuthor = %s,\n'
|
---|
181 | ' sMessage = %s\n'
|
---|
182 | 'WHERE sRepository = %s\n'
|
---|
183 | ' AND iRevision = %s'
|
---|
184 | , ( oData.tsCreated,
|
---|
185 | oData.sAuthor,
|
---|
186 | oData.sMessage,
|
---|
187 | oData.sRepository,
|
---|
188 | oData.iRevision,
|
---|
189 | ));
|
---|
190 |
|
---|
191 | self._oDb.maybeCommit(fCommit);
|
---|
192 | return oData;
|
---|
193 |
|
---|
194 | def getLastRevision(self, sRepository):
|
---|
195 | """
|
---|
196 | Get the last known revision number for the given repository, returns 0
|
---|
197 | if the repository is not known to us:
|
---|
198 | """
|
---|
199 | self._oDb.execute('SELECT iRevision\n'
|
---|
200 | 'FROM VcsRevisions\n'
|
---|
201 | 'WHERE sRepository = %s\n'
|
---|
202 | 'ORDER BY iRevision DESC\n'
|
---|
203 | 'LIMIT 1\n'
|
---|
204 | , ( sRepository, ));
|
---|
205 | if self._oDb.getRowCount() == 0:
|
---|
206 | return 0;
|
---|
207 | return self._oDb.fetchOne()[0];
|
---|
208 |
|
---|
209 | def fetchTimeline(self, sRepository, iRevision, cEntriesBack):
|
---|
210 | """
|
---|
211 | Fetches a VCS timeline portion for a repository.
|
---|
212 |
|
---|
213 | Returns an array (list) of VcsRevisionData items, empty list if none.
|
---|
214 | Raises exception on error.
|
---|
215 | """
|
---|
216 | self._oDb.execute('SELECT *\n'
|
---|
217 | 'FROM VcsRevisions\n'
|
---|
218 | 'WHERE sRepository = %s\n'
|
---|
219 | ' AND iRevision > %s\n'
|
---|
220 | ' AND iRevision <= %s\n'
|
---|
221 | 'ORDER BY iRevision DESC\n'
|
---|
222 | 'LIMIT %s\n'
|
---|
223 | , ( sRepository, iRevision - cEntriesBack*2 + 1, iRevision, cEntriesBack));
|
---|
224 | aoRows = [];
|
---|
225 | for _ in range(self._oDb.getRowCount()):
|
---|
226 | aoRows.append(VcsRevisionData().initFromDbRow(self._oDb.fetchOne()));
|
---|
227 | return aoRows;
|
---|
228 |
|
---|
229 |
|
---|
230 | #
|
---|
231 | # Unit testing.
|
---|
232 | #
|
---|
233 |
|
---|
234 | # pylint: disable=C0111
|
---|
235 | class VcsRevisionDataTestCase(ModelDataBaseTestCase):
|
---|
236 | def setUp(self):
|
---|
237 | self.aoSamples = [VcsRevisionData(),];
|
---|
238 |
|
---|
239 | if __name__ == '__main__':
|
---|
240 | unittest.main();
|
---|
241 | # not reached.
|
---|
242 |
|
---|