-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Smart shed_diff. #167
Smart shed_diff. #167
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,12 @@ | |
" this to testtoolshed.", | ||
default=None, | ||
) | ||
@click.option( | ||
"--raw", | ||
is_flag=True, | ||
help="Do not attempt smart diff of XML to filter out attributes " | ||
"populated by the Tool Shed.", | ||
) | ||
@options.recursive_shed_option() | ||
@pass_context | ||
def cli(ctx, path, **kwds): | ||
|
@@ -53,7 +59,7 @@ def cli(ctx, path, **kwds): | |
def diff(realized_repository): | ||
working = tempfile.mkdtemp(prefix="tool_shed_diff_") | ||
try: | ||
shed.diff_in(ctx, working, realized_repository, **kwds) | ||
return shed.diff_in(ctx, working, realized_repository, **kwds) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. only return in one place, returning (I'm assuming) an error code, or None. Maybe better to explicitly return None/-1? Sorry, it's late, tipsy code review is probably not the best ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the last statement in the diff - it is wrapped in a finally not exception handling so this |
||
finally: | ||
shutil.rmtree(working) | ||
|
||
|
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 |
||
|
||
|
||
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] |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, used this too. Good choice. |
||
|
||
|
||
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() |
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" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe test some without hardcoded There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well I was a bottle and half in when I wrote this so seems appropriate :). This is meant to emulate the shed version - the plain version is in tests/repository_dependencies.xml - I use this to test that Thanks for the detailed review! |
||
<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> |
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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is used inside of
planemo.shed
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops. Missed that. Sorry!