1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: vcs_import.py 84550 2020-05-26 18:50:43Z vboxsync $
|
---|
4 |
|
---|
5 | """
|
---|
6 | Cron job for importing revision history for a repository.
|
---|
7 | """
|
---|
8 |
|
---|
9 | from __future__ import print_function;
|
---|
10 |
|
---|
11 | __copyright__ = \
|
---|
12 | """
|
---|
13 | Copyright (C) 2012-2020 Oracle Corporation
|
---|
14 |
|
---|
15 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
16 | available from http://www.virtualbox.org. This file is free software;
|
---|
17 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
18 | General Public License (GPL) as published by the Free Software
|
---|
19 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
20 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
21 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
22 |
|
---|
23 | The contents of this file may alternatively be used under the terms
|
---|
24 | of the Common Development and Distribution License Version 1.0
|
---|
25 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
26 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
27 | CDDL are applicable instead of those of the GPL.
|
---|
28 |
|
---|
29 | You may elect to license modified versions of this file under the
|
---|
30 | terms and conditions of either the GPL or the CDDL or both.
|
---|
31 | """
|
---|
32 | __version__ = "$Revision: 84550 $"
|
---|
33 |
|
---|
34 | # Standard python imports
|
---|
35 | import sys;
|
---|
36 | import os;
|
---|
37 | from optparse import OptionParser; # pylint: disable=deprecated-module
|
---|
38 | import xml.etree.ElementTree as ET;
|
---|
39 |
|
---|
40 | # Add Test Manager's modules path
|
---|
41 | g_ksTestManagerDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
|
---|
42 | sys.path.append(g_ksTestManagerDir);
|
---|
43 |
|
---|
44 | # Test Manager imports
|
---|
45 | from testmanager.config import g_kaBugTrackers;
|
---|
46 | from testmanager.core.db import TMDatabaseConnection;
|
---|
47 | from testmanager.core.vcsrevisions import VcsRevisionData, VcsRevisionLogic;
|
---|
48 | from testmanager.core.vcsbugreference import VcsBugReferenceData, VcsBugReferenceLogic;
|
---|
49 | from common import utils;
|
---|
50 |
|
---|
51 | # Python 3 hacks:
|
---|
52 | if sys.version_info[0] >= 3:
|
---|
53 | long = int; # pylint: disable=redefined-builtin,invalid-name
|
---|
54 |
|
---|
55 |
|
---|
56 | class VcsImport(object): # pylint: disable=too-few-public-methods
|
---|
57 | """
|
---|
58 | Imports revision history from a VSC into the Test Manager database.
|
---|
59 | """
|
---|
60 |
|
---|
61 | class BugTracker(object):
|
---|
62 | def __init__(self, sDbName, sTag):
|
---|
63 | self.sDbName = sDbName;
|
---|
64 | self.sTag = sTag;
|
---|
65 |
|
---|
66 |
|
---|
67 | def __init__(self):
|
---|
68 | """
|
---|
69 | Parse command line.
|
---|
70 | """
|
---|
71 |
|
---|
72 | oParser = OptionParser()
|
---|
73 | oParser.add_option('-b', '--only-bug-refs', dest = 'fBugRefsOnly', action = 'store_true',
|
---|
74 | help = 'Only do bug references, not revisions.');
|
---|
75 | oParser.add_option('-e', '--extra-option', dest = 'asExtraOptions', metavar = 'vcsoption', action = 'append',
|
---|
76 | help = 'Adds a extra option to the command retrieving the log.');
|
---|
77 | oParser.add_option('-f', '--full', dest = 'fFull', action = 'store_true',
|
---|
78 | help = 'Full revision history import.');
|
---|
79 | oParser.add_option('-q', '--quiet', dest = 'fQuiet', action = 'store_true',
|
---|
80 | help = 'Quiet execution');
|
---|
81 | oParser.add_option('-R', '--repository', dest = 'sRepository', metavar = '<repository>',
|
---|
82 | help = 'Version control repository name.');
|
---|
83 | oParser.add_option('-s', '--start-revision', dest = 'iStartRevision', metavar = 'start-revision',
|
---|
84 | type = "int", default = 0,
|
---|
85 | help = 'The revision to start at when doing a full import.');
|
---|
86 | oParser.add_option('-t', '--type', dest = 'sType', metavar = '<type>',
|
---|
87 | help = 'The VCS type (default: svn)', choices = [ 'svn', ], default = 'svn');
|
---|
88 | oParser.add_option('-u', '--url', dest = 'sUrl', metavar = '<url>',
|
---|
89 | help = 'The VCS URL');
|
---|
90 |
|
---|
91 | (self.oConfig, _) = oParser.parse_args();
|
---|
92 |
|
---|
93 | # Check command line
|
---|
94 | asMissing = [];
|
---|
95 | if self.oConfig.sUrl is None: asMissing.append('--url');
|
---|
96 | if self.oConfig.sRepository is None: asMissing.append('--repository');
|
---|
97 | if asMissing:
|
---|
98 | sys.stderr.write('syntax error: Missing: %s\n' % (asMissing,));
|
---|
99 | sys.exit(1);
|
---|
100 |
|
---|
101 | assert self.oConfig.sType == 'svn';
|
---|
102 |
|
---|
103 | def main(self):
|
---|
104 | """
|
---|
105 | Main function.
|
---|
106 | """
|
---|
107 | oDb = TMDatabaseConnection();
|
---|
108 | oLogic = VcsRevisionLogic(oDb);
|
---|
109 | oBugLogic = VcsBugReferenceLogic(oDb);
|
---|
110 |
|
---|
111 | # Where to start.
|
---|
112 | iStartRev = 0;
|
---|
113 | if not self.oConfig.fFull:
|
---|
114 | if not self.oConfig.fBugRefsOnly:
|
---|
115 | iStartRev = oLogic.getLastRevision(self.oConfig.sRepository);
|
---|
116 | else:
|
---|
117 | iStartRev = oBugLogic.getLastRevision(self.oConfig.sRepository);
|
---|
118 | if iStartRev == 0:
|
---|
119 | iStartRev = self.oConfig.iStartRevision;
|
---|
120 |
|
---|
121 | # Construct a command line.
|
---|
122 | os.environ['LC_ALL'] = 'en_US.utf-8';
|
---|
123 | asArgs = [
|
---|
124 | 'svn',
|
---|
125 | 'log',
|
---|
126 | '--xml',
|
---|
127 | '--revision', str(iStartRev) + ':HEAD',
|
---|
128 | ];
|
---|
129 | if self.oConfig.asExtraOptions is not None:
|
---|
130 | asArgs.extend(self.oConfig.asExtraOptions);
|
---|
131 | asArgs.append(self.oConfig.sUrl);
|
---|
132 | if not self.oConfig.fQuiet:
|
---|
133 | print('Executing: %s' % (asArgs,));
|
---|
134 | sLogXml = utils.processOutputChecked(asArgs);
|
---|
135 |
|
---|
136 | # Parse the XML and add the entries to the database.
|
---|
137 | oParser = ET.XMLParser(target = ET.TreeBuilder(), encoding = 'utf-8');
|
---|
138 | oParser.feed(sLogXml.encode('utf-8')); # Does its own decoding; processOutputChecked always gives us decoded utf-8 now.
|
---|
139 | oRoot = oParser.close();
|
---|
140 |
|
---|
141 | for oLogEntry in oRoot.findall('logentry'):
|
---|
142 | iRevision = int(oLogEntry.get('revision'));
|
---|
143 | sAuthor = oLogEntry.findtext('author', 'unspecified').strip(); # cvs2svn entries doesn't have an author.
|
---|
144 | sDate = oLogEntry.findtext('date').strip();
|
---|
145 | sRawMsg = oLogEntry.findtext('msg', '').strip();
|
---|
146 | sMessage = sRawMsg;
|
---|
147 | if sMessage == '':
|
---|
148 | sMessage = ' ';
|
---|
149 | elif len(sMessage) > VcsRevisionData.kcchMax_sMessage:
|
---|
150 | sMessage = sMessage[:VcsRevisionData.kcchMax_sMessage - 4] + ' ...';
|
---|
151 | if not self.oConfig.fQuiet:
|
---|
152 | utils.printOut(u'sDate=%s iRev=%u sAuthor=%s sMsg[%s]=%s'
|
---|
153 | % (sDate, iRevision, sAuthor, type(sMessage).__name__, sMessage));
|
---|
154 |
|
---|
155 | if not self.oConfig.fBugRefsOnly:
|
---|
156 | oData = VcsRevisionData().initFromValues(self.oConfig.sRepository, iRevision, sDate, sAuthor, sMessage);
|
---|
157 | oLogic.addVcsRevision(oData);
|
---|
158 |
|
---|
159 | # Analyze the raw message looking for bug tracker references.
|
---|
160 | for sBugTrackerKey in g_kaBugTrackers:
|
---|
161 | oBugTracker = g_kaBugTrackers[sBugTrackerKey];
|
---|
162 | for sTag in oBugTracker.asCommitTags:
|
---|
163 | off = sRawMsg.find(sTag);
|
---|
164 | while off >= 0:
|
---|
165 | off += len(sTag);
|
---|
166 | while off < len(sRawMsg) and sRawMsg[off].isspace():
|
---|
167 | off += 1;
|
---|
168 |
|
---|
169 | if off < len(sRawMsg) and sRawMsg[off].isdigit():
|
---|
170 | offNum = off;
|
---|
171 | while off < len(sRawMsg) and sRawMsg[off].isdigit():
|
---|
172 | off += 1;
|
---|
173 | try:
|
---|
174 | iBugNo = long(sRawMsg[offNum:off]);
|
---|
175 | except Exception as oXcpt:
|
---|
176 | utils.printErr(u'error! exception(r%s,"%s"): -> %s' % (iRevision, sRawMsg[offNum:off], oXcpt,));
|
---|
177 | else:
|
---|
178 | if not self.oConfig.fQuiet:
|
---|
179 | utils.printOut(u' r%u -> sBugTracker=%s iBugNo=%s'
|
---|
180 | % (iRevision, oBugTracker.sDbId, iBugNo,));
|
---|
181 |
|
---|
182 | oBugData = VcsBugReferenceData().initFromValues(self.oConfig.sRepository, iRevision,
|
---|
183 | oBugTracker.sDbId, iBugNo);
|
---|
184 | oBugLogic.addVcsBugReference(oBugData);
|
---|
185 |
|
---|
186 | # next
|
---|
187 | off = sRawMsg.find(sTag, off);
|
---|
188 |
|
---|
189 | oDb.commit();
|
---|
190 |
|
---|
191 | oDb.close();
|
---|
192 | return 0;
|
---|
193 |
|
---|
194 | if __name__ == '__main__':
|
---|
195 | sys.exit(VcsImport().main());
|
---|
196 |
|
---|