|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2015 - 2017 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing a dialog to show some patch file statistics. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 try: |
|
12 str = unicode |
|
13 except NameError: |
|
14 pass |
|
15 |
|
16 import os |
|
17 |
|
18 from PyQt5.QtCore import Qt, QProcess |
|
19 from PyQt5.QtWidgets import QDialog, QTreeWidgetItem, QHeaderView |
|
20 |
|
21 from E5Gui import E5MessageBox |
|
22 |
|
23 from .Ui_GitPatchStatisticsDialog import Ui_GitPatchStatisticsDialog |
|
24 |
|
25 import Preferences |
|
26 |
|
27 |
|
28 class GitPatchStatisticsDialog(QDialog, Ui_GitPatchStatisticsDialog): |
|
29 """ |
|
30 Class implementing a dialog to show some patch file statistics. |
|
31 """ |
|
32 def __init__(self, vcs, parent=None): |
|
33 """ |
|
34 Constructor |
|
35 |
|
36 @param vcs reference to the VCS object (Git) |
|
37 @param parent reference to the parent widget (QWidget) |
|
38 """ |
|
39 super(GitPatchStatisticsDialog, self).__init__(parent) |
|
40 self.setupUi(self) |
|
41 self.setWindowFlags(Qt.Window) |
|
42 |
|
43 self.__vcs = vcs |
|
44 |
|
45 self.changesTreeWidget.headerItem().setText( |
|
46 self.changesTreeWidget.columnCount(), "") |
|
47 self.changesTreeWidget.header().setSortIndicator(2, Qt.AscendingOrder) |
|
48 |
|
49 def start(self, projectDir, patchCheckData): |
|
50 """ |
|
51 Public method to start the statistics process. |
|
52 |
|
53 @param projectDir directory name of the project (string) |
|
54 @param patchCheckData tuple of data as returned by the |
|
55 GitPatchFilesDialog.getData() method |
|
56 """ |
|
57 self.__patchCheckData = patchCheckData |
|
58 |
|
59 self.changesTreeWidget.clear() |
|
60 self.summaryEdit.clear() |
|
61 |
|
62 # find the root of the repo |
|
63 repodir = projectDir |
|
64 while not os.path.isdir(os.path.join(repodir, self.__vcs.adminDir)): |
|
65 repodir = os.path.dirname(repodir) |
|
66 if os.path.splitdrive(repodir)[1] == os.sep: |
|
67 return |
|
68 |
|
69 from .GitPatchFilesDialog import GitPatchFilesDialog |
|
70 dlg = GitPatchFilesDialog(repodir, patchCheckData) |
|
71 if dlg.exec_() == QDialog.Accepted: |
|
72 patchFilesList, stripCount, inaccurateEof, recount = dlg.getData() |
|
73 self.__patchCheckData = (patchFilesList, stripCount, |
|
74 inaccurateEof, recount) |
|
75 if patchFilesList: |
|
76 process = QProcess() |
|
77 process.setWorkingDirectory(repodir) |
|
78 |
|
79 # step 1: get the statistics |
|
80 args = self.__vcs.initCommand("apply") |
|
81 args.append("--numstat") |
|
82 if inaccurateEof: |
|
83 args.append("--inaccurate-eof") |
|
84 if recount: |
|
85 args.append("--recount") |
|
86 args.append("-p{0}".format(stripCount)) |
|
87 args.extend(patchFilesList) |
|
88 |
|
89 process.start('git', args) |
|
90 procStarted = process.waitForStarted(5000) |
|
91 if not procStarted: |
|
92 E5MessageBox.critical( |
|
93 self, |
|
94 self.tr('Process Generation Error'), |
|
95 self.tr( |
|
96 'The process {0} could not be started. ' |
|
97 'Ensure, that it is in the search path.' |
|
98 ).format('git')) |
|
99 return |
|
100 else: |
|
101 finished = process.waitForFinished(30000) |
|
102 if finished and process.exitCode() == 0: |
|
103 output = str(process.readAllStandardOutput(), |
|
104 Preferences.getSystem("IOEncoding"), |
|
105 'replace') |
|
106 for line in output.splitlines(): |
|
107 self.__createStatisticsItem(line) |
|
108 |
|
109 # step 2: get the summary |
|
110 args = self.__vcs.initCommand("apply") |
|
111 args.append("--summary") |
|
112 if inaccurateEof: |
|
113 args.append("--inaccurate-eof") |
|
114 if recount: |
|
115 args.append("--recount") |
|
116 args.append("-p{0}".format(stripCount)) |
|
117 args.extend(patchFilesList) |
|
118 |
|
119 process.start('git', args) |
|
120 procStarted = process.waitForStarted(5000) |
|
121 if not procStarted: |
|
122 E5MessageBox.critical( |
|
123 self, |
|
124 self.tr('Process Generation Error'), |
|
125 self.tr( |
|
126 'The process {0} could not be started. ' |
|
127 'Ensure, that it is in the search path.' |
|
128 ).format('git')) |
|
129 return |
|
130 else: |
|
131 finished = process.waitForFinished(30000) |
|
132 if finished and process.exitCode() == 0: |
|
133 output = str(process.readAllStandardOutput(), |
|
134 Preferences.getSystem("IOEncoding"), |
|
135 'replace') |
|
136 for line in output.splitlines(): |
|
137 self.summaryEdit.appendPlainText(line.strip()) |
|
138 |
|
139 def __createStatisticsItem(self, line): |
|
140 """ |
|
141 Private method to create a file statistics entry. |
|
142 |
|
143 @param line string with file statistics data (string) |
|
144 """ |
|
145 insertions, deletions, filename = line.strip().split(None, 2) |
|
146 itm = QTreeWidgetItem(self.changesTreeWidget, |
|
147 [insertions, deletions, filename]) |
|
148 itm.setTextAlignment(0, Qt.AlignRight) |
|
149 itm.setTextAlignment(1, Qt.AlignRight) |
|
150 |
|
151 def __resizeColumns(self): |
|
152 """ |
|
153 Private method to resize the list columns. |
|
154 """ |
|
155 self.changesTreeWidget.header().resizeSections( |
|
156 QHeaderView.ResizeToContents) |
|
157 self.changesTreeWidget.header().setStretchLastSection(True) |
|
158 |
|
159 def getData(self): |
|
160 """ |
|
161 Public method to get the data used to generate the statistics. |
|
162 |
|
163 @return tuple of list of patch files, strip count, flag indicating |
|
164 that the patch has inaccurate end-of-file marker and a flag |
|
165 indicating to not trust the line count information |
|
166 (list of string, integer, boolean, boolean) |
|
167 """ |
|
168 return self.__patchCheckData |