VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/core/vcsrevisions.py@ 76553

Last change on this file since 76553 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette