1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | # $Id: vcs_import.py 76553 2019-01-01 01:45:53Z vboxsync $
|
---|
4 | # pylint: disable=C0301
|
---|
5 |
|
---|
6 | """
|
---|
7 | Cron job for importing revision history for a repository.
|
---|
8 | """
|
---|
9 |
|
---|
10 | from __future__ import print_function;
|
---|
11 |
|
---|
12 | __copyright__ = \
|
---|
13 | """
|
---|
14 | Copyright (C) 2012-2019 Oracle Corporation
|
---|
15 |
|
---|
16 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
17 | available from http://www.virtualbox.org. This file is free software;
|
---|
18 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
19 | General Public License (GPL) as published by the Free Software
|
---|
20 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
21 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
22 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
23 |
|
---|
24 | The contents of this file may alternatively be used under the terms
|
---|
25 | of the Common Development and Distribution License Version 1.0
|
---|
26 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
27 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
28 | CDDL are applicable instead of those of the GPL.
|
---|
29 |
|
---|
30 | You may elect to license modified versions of this file under the
|
---|
31 | terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | """
|
---|
33 | __version__ = "$Revision: 76553 $"
|
---|
34 |
|
---|
35 | # Standard python imports
|
---|
36 | import sys;
|
---|
37 | import os;
|
---|
38 | from optparse import OptionParser; # pylint: disable=deprecated-module
|
---|
39 | import xml.etree.ElementTree as ET;
|
---|
40 |
|
---|
41 | # Add Test Manager's modules path
|
---|
42 | g_ksTestManagerDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))));
|
---|
43 | sys.path.append(g_ksTestManagerDir);
|
---|
44 |
|
---|
45 | # Test Manager imports
|
---|
46 | from testmanager.core.db import TMDatabaseConnection;
|
---|
47 | from testmanager.core.vcsrevisions import VcsRevisionData, VcsRevisionLogic;
|
---|
48 | from common import utils;
|
---|
49 |
|
---|
50 | class 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 |
|
---|
142 | if __name__ == '__main__':
|
---|
143 | sys.exit(VcsImport().main());
|
---|
144 |
|
---|