Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from jmchilton/smart_diff
Smart shed_diff.
- Loading branch information
Showing
12 changed files
with
288 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from __future__ import print_function | ||
|
||
import os | ||
import sys | ||
from xml.etree import ElementTree | ||
|
||
from planemo.xml import diff | ||
|
||
|
||
def diff_and_remove(working, label_a, label_b, f): | ||
a_deps = os.path.join(working, label_a, "tool_dependencies.xml") | ||
b_deps = os.path.join(working, label_b, "tool_dependencies.xml") | ||
a_repos = os.path.join(working, label_a, "repository_dependencies.xml") | ||
b_repos = os.path.join(working, label_b, "repository_dependencies.xml") | ||
|
||
deps_diff = 0 | ||
if os.path.exists(a_deps) and os.path.exists(b_deps): | ||
deps_diff = _shed_diff(a_deps, b_deps, f) | ||
os.remove(a_deps) | ||
os.remove(b_deps) | ||
|
||
repos_diff = 0 | ||
if os.path.exists(a_repos) and os.path.exists(b_repos): | ||
repos_diff = _shed_diff(a_repos, b_repos, f) | ||
os.remove(a_repos) | ||
os.remove(b_repos) | ||
|
||
return deps_diff and repos_diff | ||
|
||
|
||
def _shed_diff(file_a, file_b, f=sys.stdout): | ||
xml_a = ElementTree.parse(file_a).getroot() | ||
xml_b = ElementTree.parse(file_b).getroot() | ||
_strip_shed_attributes(xml_a) | ||
_strip_shed_attributes(xml_b) | ||
return diff.diff(xml_a, xml_b, reporter=f.write) | ||
|
||
|
||
def _strip_shed_attributes(xml_element): | ||
if xml_element.tag == "repository": | ||
_remove_attribs(xml_element) | ||
children = xml_element.getchildren() | ||
if len(children) > 0: | ||
for child in children: | ||
_strip_shed_attributes(child) | ||
|
||
|
||
def _remove_attribs(xml_element): | ||
for attrib in ["changeset_revision", "toolshed"]: | ||
if attrib in xml_element.attrib: | ||
del xml_element.attrib[attrib] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
def diff(x1, x2, reporter=None): | ||
return 0 if xml_compare(x1, x2, reporter) else 1 | ||
|
||
|
||
# From | ||
# bitbucket.org/ianb/formencode/src/tip/formencode/doctest_xml_compare.py | ||
# with (PSF license) | ||
def xml_compare(x1, x2, reporter=None): | ||
if reporter is None: | ||
def reporter(x): | ||
return None | ||
|
||
if x1.tag != x2.tag: | ||
reporter('Tags do not match: %s and %s' % (x1.tag, x2.tag)) | ||
return False | ||
for name, value in x1.attrib.items(): | ||
if x2.attrib.get(name) != value: | ||
reporter('Attributes do not match: %s=%r, %s=%r' | ||
% (name, value, name, x2.attrib.get(name))) | ||
return False | ||
for name in x2.attrib.keys(): | ||
if name not in x1.attrib: | ||
reporter('x2 has an attribute x1 is missing: %s' | ||
% name) | ||
return False | ||
if not text_compare(x1.text, x2.text): | ||
reporter('text: %r != %r' % (x1.text, x2.text)) | ||
return False | ||
if not text_compare(x1.tail, x2.tail): | ||
reporter('tail: %r != %r' % (x1.tail, x2.tail)) | ||
return False | ||
return _compare_children(x1, x2, reporter) | ||
|
||
|
||
def _compare_children(x1, x2, reporter): | ||
cl1 = x1.getchildren() | ||
cl2 = x2.getchildren() | ||
if len(cl1) != len(cl2): | ||
reporter('children length differs, %i != %i' | ||
% (len(cl1), len(cl2))) | ||
return False | ||
i = 0 | ||
for c1, c2 in zip(cl1, cl2): | ||
i += 1 | ||
if not xml_compare(c1, c2, reporter=reporter): | ||
reporter('children %i do not match: %s' | ||
% (i, c1.tag)) | ||
return False | ||
return True | ||
|
||
|
||
def text_compare(t1, t2): | ||
if not t1 and not t2: | ||
return True | ||
return (t1 or '').strip() == (t2 or '').strip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0"?> | ||
<repositories description="A suite of Galaxy tools designed to work with version 1.2 of the SAMtools package."> | ||
<repository changeset_revision="cf875cbe2df4" name="data_manager_sam_fasta_index_builder" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="af7c50162f0b" name="bam_to_sam" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="d04d9f1c6791" name="sam_to_bam" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="8c3472790020" name="samtools_bedcov" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="1ebb4ecdc1ef" name="samtools_calmd" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="0072bf593791" name="samtools_flagstat" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="87398ae795c7" name="samtools_idxstats" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="c6fdfe3331d6" name="samtools_mpileup" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="020e144b5f78" name="samtools_reheader" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="3735f950b2f5" name="samtools_rmdup" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="2b474ebbfc7d" name="samtools_slice_bam" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="a430da4f04cd" name="samtools_sort" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="57f3e32f809d" name="samtools_split" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
<repository changeset_revision="0d71d9467847" name="samtools_stats" owner="devteam" toolshed="https://toolshed.g2.bx.psu.edu" /> | ||
</repositories> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import os | ||
|
||
from .test_utils import TEST_DIR | ||
from planemo.shed import diff | ||
|
||
|
||
def test_compare(): | ||
local = os.path.join(TEST_DIR, "repository_dependencies.xml") | ||
shed = os.path.join(TEST_DIR, "repository_dependencies_shed.xml") | ||
assert not diff._shed_diff(local, shed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.