VirtualBox

source: vbox/trunk/src/VBox/ValidationKit/testmanager/batch/vcs_import.py@ 95481

最後變更 在這個檔案從95481是 94129,由 vboxsync 提交於 3 年 前

testmanager: pylint 2.9.6 adjustments (mostly about using sub-optimal looping and 'with' statements).

  • 屬性 svn:eol-style 設為 LF
  • 屬性 svn:executable 設為 *
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 8.2 KB
 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: vcs_import.py 94129 2022-03-08 14:57:25Z vboxsync $
4
5"""
6Cron job for importing revision history for a repository.
7"""
8
9from __future__ import print_function;
10
11__copyright__ = \
12"""
13Copyright (C) 2012-2022 Oracle Corporation
14
15This file is part of VirtualBox Open Source Edition (OSE), as
16available from http://www.alldomusa.eu.org. This file is free software;
17you can redistribute it and/or modify it under the terms of the GNU
18General Public License (GPL) as published by the Free Software
19Foundation, in version 2 as it comes in the "COPYING" file of the
20VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22
23The contents of this file may alternatively be used under the terms
24of the Common Development and Distribution License Version 1.0
25(CDDL) only, as it comes in the "COPYING.CDDL" file of the
26VirtualBox OSE distribution, in which case the provisions of the
27CDDL are applicable instead of those of the GPL.
28
29You may elect to license modified versions of this file under the
30terms and conditions of either the GPL or the CDDL or both.
31"""
32__version__ = "$Revision: 94129 $"
33
34# Standard python imports
35import sys;
36import os;
37from optparse import OptionParser; # pylint: disable=deprecated-module
38import xml.etree.ElementTree as ET;
39
40# Add Test Manager's modules path
41g_ksTestManagerDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
42sys.path.append(g_ksTestManagerDir);
43
44# Test Manager imports
45from testmanager.config import g_kdBugTrackers;
46from testmanager.core.db import TMDatabaseConnection;
47from testmanager.core.vcsrevisions import VcsRevisionData, VcsRevisionLogic;
48from testmanager.core.vcsbugreference import VcsBugReferenceData, VcsBugReferenceLogic;
49from common import utils;
50
51# Python 3 hacks:
52if sys.version_info[0] >= 3:
53 long = int; # pylint: disable=redefined-builtin,invalid-name
54
55
56class 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 oBugTracker in g_kdBugTrackers.values():
161 for sTag in oBugTracker.asCommitTags:
162 off = sRawMsg.find(sTag);
163 while off >= 0:
164 off += len(sTag);
165 while off < len(sRawMsg) and sRawMsg[off].isspace():
166 off += 1;
167
168 if off < len(sRawMsg) and sRawMsg[off].isdigit():
169 offNum = off;
170 while off < len(sRawMsg) and sRawMsg[off].isdigit():
171 off += 1;
172 try:
173 iBugNo = long(sRawMsg[offNum:off]);
174 except Exception as oXcpt:
175 utils.printErr(u'error! exception(r%s,"%s"): -> %s' % (iRevision, sRawMsg[offNum:off], oXcpt,));
176 else:
177 if not self.oConfig.fQuiet:
178 utils.printOut(u' r%u -> sBugTracker=%s iBugNo=%s'
179 % (iRevision, oBugTracker.sDbId, iBugNo,));
180
181 oBugData = VcsBugReferenceData().initFromValues(self.oConfig.sRepository, iRevision,
182 oBugTracker.sDbId, iBugNo);
183 oBugLogic.addVcsBugReference(oBugData);
184
185 # next
186 off = sRawMsg.find(sTag, off);
187
188 oDb.commit();
189
190 oDb.close();
191 return 0;
192
193if __name__ == '__main__':
194 sys.exit(VcsImport().main());
195
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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