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