comparison env/lib/python3.9/site-packages/planemo/shed/diff.py @ 0:4f3585e2f14b draft default tip

"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
author shellac
date Mon, 22 Mar 2021 18:12:50 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4f3585e2f14b
1 """Utilities for calculating effective repository diffs.
2
3 Some intelligence is required because the tool shed updates attributes that it
4 is beneficial to ignore.
5 """
6 from __future__ import print_function
7
8 import os
9 import sys
10 from xml.etree import ElementTree
11
12 from planemo.xml import diff
13
14
15 def diff_and_remove(working, label_a, label_b, f):
16 """Remove tool shed XML files and use a smart XML diff on them.
17
18 Return 0 if and only if the XML content is the sam after stripping
19 attirbutes the tool shed updates.
20 """
21 assert label_a != label_b
22 special = ["tool_dependencies.xml", "repository_dependencies.xml"]
23 deps_diff = 0
24 # Could walk either A or B; will only compare if in same relative location
25 for dirpath, dirnames, filenames in os.walk(os.path.join(working, label_a)):
26 for filename in filenames:
27 if filename in special:
28 a = os.path.join(dirpath, filename)
29 b = os.path.join(working, label_b, os.path.relpath(a, os.path.join(working, label_a)))
30 files_exist = os.path.exists(a) and os.path.exists(b)
31 if files_exist:
32 deps_diff |= _shed_diff(a, b, f)
33 os.remove(a)
34 os.remove(b)
35 return deps_diff
36
37
38 def _shed_diff(file_a, file_b, f=sys.stdout):
39 """Strip attributes the tool shed writes and do smart XML diff.
40
41 Returns 0 if and only if the XML content is the same after stripping
42 ``tool_shed`` and ``changeset_revision`` attributes.
43 """
44 xml_a = ElementTree.parse(file_a).getroot()
45 xml_b = ElementTree.parse(file_b).getroot()
46 _strip_shed_attributes(xml_a)
47 _strip_shed_attributes(xml_b)
48 return diff.diff(xml_a, xml_b, reporter=f.write)
49
50
51 def _strip_shed_attributes(xml_element):
52 if xml_element.tag == "repository":
53 _remove_attribs(xml_element)
54 for child in xml_element:
55 _strip_shed_attributes(child)
56
57
58 def _remove_attribs(xml_element):
59 for attrib in ["changeset_revision", "toolshed"]:
60 if attrib in xml_element.attrib:
61 del xml_element.attrib[attrib]
62
63
64 __all__ = (
65 "diff_and_remove",
66 )