VirtualBox

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

Last change on this file since 75056 was 71157, checked in by vboxsync, 7 years ago

utils.py,vcs_import.py: Added printOut and printErr to utils for dealing console encoding issues in python2; use it in vcs_import.py.

  • Property svn:eol-style set to LF
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# $Id: vcs_import.py 71157 2018-02-28 15:38:15Z vboxsync $
4# pylint: disable=C0301
5
6"""
7Cron job for importing revision history for a repository.
8"""
9
10from __future__ import print_function;
11
12__copyright__ = \
13"""
14Copyright (C) 2012-2017 Oracle Corporation
15
16This file is part of VirtualBox Open Source Edition (OSE), as
17available from http://www.virtualbox.org. This file is free software;
18you can redistribute it and/or modify it under the terms of the GNU
19General Public License (GPL) as published by the Free Software
20Foundation, in version 2 as it comes in the "COPYING" file of the
21VirtualBox OSE distribution. VirtualBox OSE is distributed in the
22hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
23
24The contents of this file may alternatively be used under the terms
25of the Common Development and Distribution License Version 1.0
26(CDDL) only, as it comes in the "COPYING.CDDL" file of the
27VirtualBox OSE distribution, in which case the provisions of the
28CDDL are applicable instead of those of the GPL.
29
30You may elect to license modified versions of this file under the
31terms and conditions of either the GPL or the CDDL or both.
32"""
33__version__ = "$Revision: 71157 $"
34
35# Standard python imports
36import sys;
37import os;
38from optparse import OptionParser; # pylint: disable=deprecated-module
39import xml.etree.ElementTree as ET;
40
41# Add Test Manager's modules path
42g_ksTestManagerDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
43sys.path.append(g_ksTestManagerDir);
44
45# Test Manager imports
46from testmanager.core.db import TMDatabaseConnection;
47from testmanager.core.vcsrevisions import VcsRevisionData, VcsRevisionLogic;
48from common import utils;
49
50class VcsImport(object): # pylint: disable=R0903
51 """
52 Imports revision history from a VSC into the Test Manager database.
53 """
54
55 def __init__(self):
56 """
57 Parse command line.
58 """
59
60 oParser = OptionParser()
61 oParser.add_option('-e', '--extra-option', dest = 'asExtraOptions', action = 'append',
62 help = 'Adds a extra option to the command retrieving the log.');
63 oParser.add_option('-f', '--full', dest = 'fFull', action = 'store_true',
64 help = 'Full revision history import.');
65 oParser.add_option('-q', '--quiet', dest = 'fQuiet', action = 'store_true',
66 help = 'Quiet execution');
67 oParser.add_option('-R', '--repository', dest = 'sRepository', metavar = '<repository>',
68 help = 'Version control repository name.');
69 oParser.add_option('-s', '--start-revision', dest = 'iStartRevision', metavar = 'start-revision',
70 type = "int", default = 0,
71 help = 'The revision to start at when doing a full import.');
72 oParser.add_option('-t', '--type', dest = 'sType', metavar = '<type>',
73 help = 'The VCS type (default: svn)', choices = [ 'svn', ], default = 'svn');
74 oParser.add_option('-u', '--url', dest = 'sUrl', metavar = '<url>',
75 help = 'The VCS URL');
76
77 (self.oConfig, _) = oParser.parse_args();
78
79 # Check command line
80 asMissing = [];
81 if self.oConfig.sUrl is None: asMissing.append('--url');
82 if self.oConfig.sRepository is None: asMissing.append('--repository');
83 if asMissing:
84 sys.stderr.write('syntax error: Missing: %s\n' % (asMissing,));
85 sys.exit(1);
86
87 assert self.oConfig.sType == 'svn';
88
89 def main(self):
90 """
91 Main function.
92 """
93 oDb = TMDatabaseConnection();
94 oLogic = VcsRevisionLogic(oDb);
95
96 # Where to start.
97 iStartRev = 0;
98 if not self.oConfig.fFull:
99 iStartRev = oLogic.getLastRevision(self.oConfig.sRepository);
100 if iStartRev == 0:
101 iStartRev = self.oConfig.iStartRevision;
102
103 # Construct a command line.
104 os.environ['LC_ALL'] = 'en_US.utf-8';
105 asArgs = [
106 'svn',
107 'log',
108 '--xml',
109 '--revision', str(iStartRev) + ':HEAD',
110 ];
111 if self.oConfig.asExtraOptions is not None:
112 asArgs.extend(self.oConfig.asExtraOptions);
113 asArgs.append(self.oConfig.sUrl);
114 if not self.oConfig.fQuiet:
115 print('Executing: %s' % (asArgs,));
116 sLogXml = utils.processOutputChecked(asArgs);
117
118 # Parse the XML and add the entries to the database.
119 oParser = ET.XMLParser(target = ET.TreeBuilder(), encoding = 'utf-8');
120 oParser.feed(sLogXml.encode('utf-8')); # does its own decoding and processOutputChecked always gives us decoded utf-8 now.
121 oRoot = oParser.close();
122
123 for oLogEntry in oRoot.findall('logentry'):
124 iRevision = int(oLogEntry.get('revision'));
125 sAuthor = oLogEntry.findtext('author').strip();
126 sDate = oLogEntry.findtext('date').strip();
127 sMessage = oLogEntry.findtext('msg', '').strip();
128 if sMessage == '':
129 sMessage = ' ';
130 elif len(sMessage) > VcsRevisionData.kcchMax_sMessage:
131 sMessage = sMessage[:VcsRevisionData.kcchMax_sMessage - 4] + ' ...';
132 if not self.oConfig.fQuiet:
133 utils.printOut(u'sDate=%s iRev=%u sAuthor=%s sMsg[%s]=%s'
134 % (sDate, iRevision, sAuthor, type(sMessage).__name__, sMessage));
135 oData = VcsRevisionData().initFromValues(self.oConfig.sRepository, iRevision, sDate, sAuthor, sMessage);
136 oLogic.addVcsRevision(oData);
137 oDb.commit();
138
139 oDb.close();
140 return 0;
141
142if __name__ == '__main__':
143 sys.exit(VcsImport().main());
144
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