2
0

diffstat.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. #!/usr/bin/env python
  2. # vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
  3. # SPDX-License-Identifier: MIT
  4. # Copyright (c) 2020 Kevin B. Hendricks, Stratford Ontario Canada
  5. # All rights reserved.
  6. #
  7. # This diffstat code was extracted and heavily modified from:
  8. #
  9. # https://github.com/techtonik/python-patch
  10. # Under the following license:
  11. #
  12. # Patch utility to apply unified diffs
  13. # Brute-force line-by-line non-recursive parsing
  14. #
  15. # Copyright (c) 2008-2016 anatoly techtonik
  16. #
  17. # Permission is hereby granted, free of charge, to any person obtaining a copy
  18. # of this software and associated documentation files (the "Software"), to deal
  19. # in the Software without restriction, including without limitation the rights
  20. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. # copies of the Software, and to permit persons to whom the Software is
  22. # furnished to do so, subject to the following conditions:
  23. #
  24. # The above copyright notice and this permission notice shall be included in
  25. # all copies or substantial portions of the Software.
  26. #
  27. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  28. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  30. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  32. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  33. # THE SOFTWARE.
  34. import re
  35. import sys
  36. from typing import Optional
  37. # only needs to detect git style diffs as this is for
  38. # use with dulwich
  39. _git_header_name = re.compile(rb"diff --git a/(.*) b/(.*)")
  40. _GIT_HEADER_START = b"diff --git a/"
  41. _GIT_BINARY_START = b"Binary file"
  42. _GIT_RENAMEFROM_START = b"rename from"
  43. _GIT_RENAMETO_START = b"rename to"
  44. _GIT_CHUNK_START = b"@@"
  45. _GIT_ADDED_START = b"+"
  46. _GIT_DELETED_START = b"-"
  47. _GIT_UNCHANGED_START = b" "
  48. # emulate original full Patch class by just extracting
  49. # filename and minimal chunk added/deleted information to
  50. # properly interface with diffstat routine
  51. def _parse_patch(
  52. lines: list[bytes],
  53. ) -> tuple[list[bytes], list[bool], list[tuple[int, int]]]:
  54. """Parse a git style diff or patch to generate diff stats.
  55. Args:
  56. lines: list of byte string lines from the diff to be parsed
  57. Returns: A tuple (names, is_binary, counts) of three lists
  58. """
  59. names = []
  60. nametypes = []
  61. counts = []
  62. in_patch_chunk = in_git_header = binaryfile = False
  63. currentfile: Optional[bytes] = None
  64. added = deleted = 0
  65. for line in lines:
  66. if line.startswith(_GIT_HEADER_START):
  67. if currentfile is not None:
  68. names.append(currentfile)
  69. nametypes.append(binaryfile)
  70. counts.append((added, deleted))
  71. m = _git_header_name.search(line)
  72. assert m
  73. currentfile = m.group(2)
  74. binaryfile = False
  75. added = deleted = 0
  76. in_git_header = True
  77. in_patch_chunk = False
  78. elif line.startswith(_GIT_BINARY_START) and in_git_header:
  79. binaryfile = True
  80. in_git_header = False
  81. elif line.startswith(_GIT_RENAMEFROM_START) and in_git_header:
  82. currentfile = line[12:]
  83. elif line.startswith(_GIT_RENAMETO_START) and in_git_header:
  84. assert currentfile
  85. currentfile += b" => %s" % line[10:]
  86. elif line.startswith(_GIT_CHUNK_START) and (in_patch_chunk or in_git_header):
  87. in_patch_chunk = True
  88. in_git_header = False
  89. elif line.startswith(_GIT_ADDED_START) and in_patch_chunk:
  90. added += 1
  91. elif line.startswith(_GIT_DELETED_START) and in_patch_chunk:
  92. deleted += 1
  93. elif not line.startswith(_GIT_UNCHANGED_START) and in_patch_chunk:
  94. in_patch_chunk = False
  95. # handle end of input
  96. if currentfile is not None:
  97. names.append(currentfile)
  98. nametypes.append(binaryfile)
  99. counts.append((added, deleted))
  100. return names, nametypes, counts
  101. # note must all done using bytes not string because on linux filenames
  102. # may not be encodable even to utf-8
  103. def diffstat(lines, max_width=80):
  104. """Generate summary statistics from a git style diff ala
  105. (git diff tag1 tag2 --stat).
  106. Args:
  107. lines: list of byte string "lines" from the diff to be parsed
  108. max_width: maximum line length for generating the summary
  109. statistics (default 80)
  110. Returns: A byte string that lists the changed files with change
  111. counts and histogram.
  112. """
  113. names, nametypes, counts = _parse_patch(lines)
  114. insert = []
  115. delete = []
  116. namelen = 0
  117. maxdiff = 0 # max changes for any file used for histogram width calc
  118. for i, filename in enumerate(names):
  119. i, d = counts[i]
  120. insert.append(i)
  121. delete.append(d)
  122. namelen = max(namelen, len(filename))
  123. maxdiff = max(maxdiff, i + d)
  124. output = b""
  125. statlen = len(str(maxdiff)) # stats column width
  126. for i, n in enumerate(names):
  127. binaryfile = nametypes[i]
  128. # %-19s | %-4d %s
  129. # note b'%d' % namelen is not supported until Python 3.5
  130. # To convert an int to a format width specifier for byte
  131. # strings use str(namelen).encode('ascii')
  132. format = (
  133. b" %-"
  134. + str(namelen).encode("ascii")
  135. + b"s | %"
  136. + str(statlen).encode("ascii")
  137. + b"s %s\n"
  138. )
  139. binformat = b" %-" + str(namelen).encode("ascii") + b"s | %s\n"
  140. if not binaryfile:
  141. hist = b""
  142. # -- calculating histogram --
  143. width = len(format % (b"", b"", b""))
  144. histwidth = max(2, max_width - width)
  145. if maxdiff < histwidth:
  146. hist = b"+" * insert[i] + b"-" * delete[i]
  147. else:
  148. iratio = (float(insert[i]) / maxdiff) * histwidth
  149. dratio = (float(delete[i]) / maxdiff) * histwidth
  150. iwidth = dwidth = 0
  151. # make sure every entry that had actual insertions gets
  152. # at least one +
  153. if insert[i] > 0:
  154. iwidth = int(iratio)
  155. if iwidth == 0 and 0 < iratio < 1:
  156. iwidth = 1
  157. # make sure every entry that had actual deletions gets
  158. # at least one -
  159. if delete[i] > 0:
  160. dwidth = int(dratio)
  161. if dwidth == 0 and 0 < dratio < 1:
  162. dwidth = 1
  163. hist = b"+" * int(iwidth) + b"-" * int(dwidth)
  164. output += format % (
  165. bytes(names[i]),
  166. str(insert[i] + delete[i]).encode("ascii"),
  167. hist,
  168. )
  169. else:
  170. output += binformat % (bytes(names[i]), b"Bin")
  171. output += b" %d files changed, %d insertions(+), %d deletions(-)" % (
  172. len(names),
  173. sum(insert),
  174. sum(delete),
  175. )
  176. return output
  177. def main() -> int:
  178. argv = sys.argv
  179. # allow diffstat.py to also be used from the command line
  180. if len(sys.argv) > 1:
  181. diffpath = argv[1]
  182. data = b""
  183. with open(diffpath, "rb") as f:
  184. data = f.read()
  185. lines = data.split(b"\n")
  186. result = diffstat(lines)
  187. print(result.decode("utf-8"))
  188. return 0
  189. # if no path argument to a diff file is passed in, run
  190. # a self test. The test case includes tricky things like
  191. # a diff of diff, binary files, renames with further changes
  192. # added files and removed files.
  193. # All extracted from Sigil-Ebook/Sigil's github repo with
  194. # full permission to use under this license.
  195. selftest = b"""
  196. diff --git a/docs/qt512.7_remove_bad_workaround.patch b/docs/qt512.7_remove_bad_workaround.patch
  197. new file mode 100644
  198. index 00000000..64e34192
  199. --- /dev/null
  200. +++ b/docs/qt512.7_remove_bad_workaround.patch
  201. @@ -0,0 +1,15 @@
  202. +--- qtbase/src/gui/kernel/qwindow.cpp.orig 2019-12-12 09:15:59.000000000 -0500
  203. ++++ qtbase/src/gui/kernel/qwindow.cpp 2020-01-10 10:36:53.000000000 -0500
  204. +@@ -218,12 +218,6 @@
  205. + QGuiApplicationPrivate::window_list.removeAll(this);
  206. + if (!QGuiApplicationPrivate::is_app_closing)
  207. + QGuiApplicationPrivate::instance()->modalWindowList.removeOne(this);
  208. +-
  209. +- // focus_window is normally cleared in destroy(), but the window may in
  210. +- // some cases end up becoming the focus window again. Clear it again
  211. +- // here as a workaround. See QTBUG-75326.
  212. +- if (QGuiApplicationPrivate::focus_window == this)
  213. +- QGuiApplicationPrivate::focus_window = 0;
  214. + }
  215. +
  216. + void QWindowPrivate::init(QScreen *targetScreen)
  217. diff --git a/docs/testplugin_v017.zip b/docs/testplugin_v017.zip
  218. new file mode 100644
  219. index 00000000..a4cf4c4c
  220. Binary files /dev/null and b/docs/testplugin_v017.zip differ
  221. diff --git a/ci_scripts/macgddeploy.py b/ci_scripts/gddeploy.py
  222. similarity index 73%
  223. rename from ci_scripts/macgddeploy.py
  224. rename to ci_scripts/gddeploy.py
  225. index a512d075..f9dacd33 100644
  226. --- a/ci_scripts/macgddeploy.py
  227. +++ b/ci_scripts/gddeploy.py
  228. @@ -1,19 +1,32 @@
  229. #!/usr/bin/env python3
  230. import os
  231. +import sys
  232. import subprocess
  233. import datetime
  234. import shutil
  235. +import glob
  236. gparent = os.path.expandvars('$GDRIVE_DIR')
  237. grefresh_token = os.path.expandvars('$GDRIVE_REFRESH_TOKEN')
  238. -travis_branch = os.path.expandvars('$TRAVIS_BRANCH')
  239. -travis_commit = os.path.expandvars('$TRAVIS_COMMIT')
  240. -travis_build_number = os.path.expandvars('$TRAVIS_BUILD_NUMBER')
  241. +if sys.platform.lower().startswith('darwin'):
  242. + travis_branch = os.path.expandvars('$TRAVIS_BRANCH')
  243. + travis_commit = os.path.expandvars('$TRAVIS_COMMIT')
  244. + travis_build_number = os.path.expandvars('$TRAVIS_BUILD_NUMBER')
  245. +
  246. + origfilename = './bin/Sigil.tar.xz'
  247. + newfilename = './bin/Sigil-{}-{}-build_num-{}.tar.xz'.format(travis_branch, travis_commit[:7],travis_build_numbe\
  248. r)
  249. +else:
  250. + appveyor_branch = os.path.expandvars('$APPVEYOR_REPO_BRANCH')
  251. + appveyor_commit = os.path.expandvars('$APPVEYOR_REPO_COMMIT')
  252. + appveyor_build_number = os.path.expandvars('$APPVEYOR_BUILD_NUMBER')
  253. + names = glob.glob('.\\installer\\Sigil-*-Setup.exe')
  254. + if not names:
  255. + exit(1)
  256. + origfilename = names[0]
  257. + newfilename = '.\\installer\\Sigil-{}-{}-build_num-{}-Setup.exe'.format(appveyor_branch, appveyor_commit[:7], ap\
  258. pveyor_build_number)
  259. -origfilename = './bin/Sigil.tar.xz'
  260. -newfilename = './bin/Sigil-{}-{}-build_num-{}.tar.xz'.format(travis_branch, travis_commit[:7],travis_build_number)
  261. shutil.copy2(origfilename, newfilename)
  262. folder_name = datetime.date.today()
  263. diff --git a/docs/qt512.6_backport_009abcd_fix.patch b/docs/qt512.6_backport_009abcd_fix.patch
  264. deleted file mode 100644
  265. index f4724347..00000000
  266. --- a/docs/qt512.6_backport_009abcd_fix.patch
  267. +++ /dev/null
  268. @@ -1,26 +0,0 @@
  269. ---- qtbase/src/widgets/kernel/qwidget.cpp.orig 2019-11-08 10:57:07.000000000 -0500
  270. -+++ qtbase/src/widgets/kernel/qwidget.cpp 2019-12-11 12:32:24.000000000 -0500
  271. -@@ -8934,6 +8934,23 @@
  272. - }
  273. - }
  274. - switch (event->type()) {
  275. -+ case QEvent::PlatformSurface: {
  276. -+ // Sync up QWidget's view of whether or not the widget has been created
  277. -+ switch (static_cast<QPlatformSurfaceEvent*>(event)->surfaceEventType()) {
  278. -+ case QPlatformSurfaceEvent::SurfaceCreated:
  279. -+ if (!testAttribute(Qt::WA_WState_Created))
  280. -+ create();
  281. -+ break;
  282. -+ case QPlatformSurfaceEvent::SurfaceAboutToBeDestroyed:
  283. -+ if (testAttribute(Qt::WA_WState_Created)) {
  284. -+ // Child windows have already been destroyed by QWindow,
  285. -+ // so we skip them here.
  286. -+ destroy(false, false);
  287. -+ }
  288. -+ break;
  289. -+ }
  290. -+ break;
  291. -+ }
  292. - case QEvent::MouseMove:
  293. - mouseMoveEvent((QMouseEvent*)event);
  294. - break;
  295. diff --git a/docs/Building_Sigil_On_MacOSX.txt b/docs/Building_Sigil_On_MacOSX.txt
  296. index 3b41fd80..64914c78 100644
  297. --- a/docs/Building_Sigil_On_MacOSX.txt
  298. +++ b/docs/Building_Sigil_On_MacOSX.txt
  299. @@ -113,7 +113,7 @@ install_name_tool -add_rpath @loader_path/../../Frameworks ./bin/Sigil.app/Content
  300. # To test if the newly bundled python 3 version of Sigil is working properly ypou can do the following:
  301. -1. download testplugin_v014.zip from https://github.com/Sigil-Ebook/Sigil/tree/master/docs
  302. +1. download testplugin_v017.zip from https://github.com/Sigil-Ebook/Sigil/tree/master/docs
  303. 2. open Sigil.app to the normal nearly blank template epub it generates when opened
  304. 3. use Plugins->Manage Plugins menu and make sure the "Use Bundled Python" checkbox is checked
  305. 4. use the "Add Plugin" button to navigate to and add testplugin.zip and then hit "Okay" to exit the Manage Plugins Dialog
  306. """
  307. testoutput = b""" docs/qt512.7_remove_bad_workaround.patch | 15 ++++++++++++
  308. docs/testplugin_v017.zip | Bin
  309. ci_scripts/macgddeploy.py => ci_scripts/gddeploy.py | 0
  310. docs/qt512.6_backport_009abcd_fix.patch | 26 ---------------------
  311. docs/Building_Sigil_On_MacOSX.txt | 2 +-
  312. 5 files changed, 16 insertions(+), 27 deletions(-)"""
  313. # return 0 on success otherwise return -1
  314. result = diffstat(selftest.split(b"\n"))
  315. if result == testoutput:
  316. print("self test passed")
  317. return 0
  318. print("self test failed")
  319. print("Received:")
  320. print(result.decode("utf-8"))
  321. print("Expected:")
  322. print(testoutput.decode("utf-8"))
  323. return -1
  324. if __name__ == "__main__":
  325. sys.exit(main())