Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #101738
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | "Adriaan Renting" <renting@astron.nl> |
| Newsgroups | comp.lang.python |
| Subject | Re: ignoring or replacing white lines in a diff |
| Date | Fri, 15 Jan 2016 10:44:32 +0100 |
| Lines | 205 |
| Message-ID | <mailman.3.1452851060.15297.python-list@python.org> (permalink) |
| References | <mailman.159.1452786947.13488.python-list@python.org> <5697f08f$0$23822$e4fe514c@news.xs4all.nl> <5698118F0200001B0003B68D@gwsmtp1.astron.nl> <n792jc$j7e$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=US-ASCII |
| Content-Transfer-Encoding | 7bit |
| X-Trace | news.uni-berlin.de swOtLfrn/5bVsd2oUeL8rQecOxUh8K9M62Z0REGVjqdg== |
| Return-Path | <renting@astron.nl> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'diff': 0.05; 'fixes': 0.05; 'below)': 0.07; 'lines.': 0.07; 'rewrite': 0.07; 'suppress': 0.07; 'trailing': 0.07; '101': 0.09; 'behave': 0.09; 'cmd': 0.09; 'here?': 0.09; 'long"': 0.09; 'python:': 0.09; 'regression': 0.09; 'xml.': 0.09; 'python': 0.10; 'template': 0.11; 'output': 0.13; 'options.': 0.15; 'properly': 0.15; '(also': 0.16; '(diff': 0.16; "(it's": 0.16; '...,': 0.16; 'bits,': 0.16; 'comma': 0.16; 'comments:': 0.16; 'fluent': 0.16; 'ignores': 0.16; 'investigate': 0.16; 'loops': 0.16; 'name]': 0.16; 'newline:': 0.16; 'observatory': 0.16; 'osx': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'scripts.': 0.16; 'skip:/ 50': 0.16; 'to:addr:web.de': 0.16; 'url:gnu': 0.16; 'wrote:': 0.16; 'string': 0.17; 'version.': 0.18; '>>>': 0.20; 'essential': 0.20; 'to:2**1': 0.21; '(on': 0.22; 'arguments': 0.22; 'file:': 0.22; 'rid': 0.22; 'file.': 0.22; 'trying': 0.22; 'help.': 0.23; 'needed.': 0.23; 'replacing': 0.23; 'split': 0.23; 'tried': 0.24; 'import': 0.24; 'xml': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'script': 0.25; "i've": 0.25; "doesn't": 0.26; 'command': 0.26; 'appreciated.': 0.27; 'earlier': 0.27; 'compare': 0.27; 'raw': 0.27; 'separate': 0.27; 'skip:f 40': 0.27; 'specify': 0.27; "skip:' 10": 0.28; '---': 0.28; 'url:software': 0.29; 'whitespace': 0.29; "i'm": 0.30; 'print': 0.30; 'work.': 0.30; 'help,': 0.32; 'maybe': 0.33; 'problem': 0.33; 'option.': 0.33; 'quotes': 0.33; 'phone:': 0.33; '(for': 0.34; 'running': 0.34; 'add': 0.34; 'quite': 0.35; 'according': 0.36; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'url:org': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'thanks': 0.37; 'charset:us-ascii': 0.37; 'seem': 0.37; 'things': 0.38; 'difference': 0.38; 'version': 0.38; 'wrong': 0.38; 'someone': 0.38; 'thank': 0.38; 'test': 0.39; 'whatever': 0.39; 'does': 0.39; "didn't": 0.39; 'received:192': 0.39; 'skip:x 10': 0.40; 'to:addr:python.org': 0.40; 'some': 0.40; 'software': 0.40; 'your': 0.60; 'header:Message-Id:1': 0.61; 'further': 0.62; 'email:': 0.62; 'making': 0.62; 'between': 0.65; 'differences': 0.66; 'here': 0.66; 'results': 0.66; 'future.': 0.67; 'reply': 0.68; 'received:nl': 0.72; '100': 0.79; 'commands.': 0.84; 'escaping': 0.84; 'netherlands': 0.84; 'otten': 0.84 |
| X-Mailer | Novell GroupWise Internet Agent 12.0.3 |
| In-Reply-To | <n792jc$j7e$1@ger.gmane.org> |
| Content-Disposition | inline |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:101738 |
Show key headers only | View raw
Thanks for the various people that provided help.
Peter Otten provided me with a working solution:
I had to split the "-I '^[[:space:]]*$'" into two commands.
cmd = ["diff", "-w", "-I", r"^[[:space:]]*$", "./xml/%s.xml" %
name, "test.xml"]
p = subprocess.Popen(cmd, stdin=open('/dev/null'),
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
logs = p.communicate()
diffs = logs[0].splitlines() #stdout
This also works:
cmd = ["diff -w -I '^[[:space:]]*$' ./xml/%s.xml test.xml" %
name]
p = subprocess.Popen(cmd, stdin=open('/dev/null'),
stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
logs = p.communicate()
diffs = logs[0].splitlines() #stdout
As to other comments:
- I've found that stdin=open('/dev/null') is essential in
subprocess.Popen to make it work from automated (headless) scripts.
- print line, did remove the extra newlines, but didn't get rid of the
blank lines.
- making it a raw string with r"-I '^[[:space:]]*$'" made no difference
(also tried r"-I ^[[:space:]]*$")
- I didn't investigate difflib further but will keep it in mind for the
future.
Thank you for your help,
Adriaan.
Adriaan Renting | Email: renting@astron.nl
Software Engineer Radio Observatory
ASTRON | Phone: +31 521 595 100 (797 direct)
P.O. Box 2 | GSM: +31 6 24 25 17 28
NL-7990 AA Dwingeloo | FAX: +31 521 595 101
The Netherlands | Web: http://www.astron.nl/~renting/
>>> On 14-1-2016 at 22:05, Peter Otten <__peter__@web.de> wrote:
> Adriaan Renting wrote:
>
>>
>> Maybe someone here has a clue what is going wrong here? Any help is
>> appreciated.
>>
>> I'm writing a regression test for a module that generates XML.
>>
>> I'm using diff to compare the results with a pregenerated one from
an
>> earlier version.
>>
>> I'm running into two problems:
>>
>> The diff doesn't seem to behave properly with the -B option. (diff
(GNU
>> diffutils) 2.8.1 on OSX 10.9)
>>
>> Replacing -B with -I '^[[:space:]]*$' fixes it on the command line,
>> which should be exactly the same according to:
>>
>
http://www.gnu.org/software/diffutils/manual/html_node/Blank-Lines.html#Blank-L
> ines
>>
>> (for Python problem continue below)
>>
>> MacRenting 21:00-159> diff -w -B test.xml xml/Ticket_6923.xml
>> 3,5c3,5
>> < <version>2.15.0</version>
>> < <template version="2.15.0" author="Alwin de Jong,Adriaan
Renting"
>> changedBy="Adriaan Renting">
>> < <description>XML Template generator version
2.15.0</description>
>> ---
>>> <version>2.6.0</version>
>>> <template version="2.6.0" author="Alwin de Jong"
>> changedBy="Alwin de Jong">
>>> <description>XML Template generator version
>> 2.6.0</description>
>> 113d112
>> <
>> 163d161
>> <
>> 213d210
>> <
>> 258d254
>> <
>> 369d364
>> <
>> 419d413
>> <
>> 469d462
>> <
>> 514d506
>> <
>> 625d616
>> <
>> 675d665
>> <
>> 725d714
>> <
>> 770d758
>> <
>> 881d868
>> <
>> 931d917
>> <
>> 981d966
>> <
>> 1026d1010
>> <
>> 1137d1120
>> <
>> 1187d1169
>> <
>> 1237d1218
>> <
>> 1282d1262
>> <
>>
>> /Users/renting/src/CEP4-DevelopClusterModel-Story-Task8432-
> SAS/XML_generator/test
>> MacRenting 21:00-160> diff -w -I '^[[:space:]]*$' test.xml
>> xml/Ticket_6923.xml
>> 3,5c3,5
>> < <version>2.15.0</version>
>> < <template version="2.15.0" author="Alwin de Jong,Adriaan
Renting"
>> changedBy="Adriaan Renting">
>> < <description>XML Template generator version
2.15.0</description>
>> ---
>>> <version>2.6.0</version>
>>> <template version="2.6.0" author="Alwin de Jong"
>> changedBy="Alwin de Jong">
>>> <description>XML Template generator version
>> 2.6.0</description>
>>
>>
>> Now I try to use this in Python:
>>
>> cmd = ["diff", "-w", "-I '^[[:space:]]*$'", "./xml/%s.xml"
%
>> name, "test.xml"]
>
> Instead of
>
> ..., "-I '^[[:space:]]*$'", ...
>
> try two separate arguments
>
> ..., "-I", "^[[:space:]]*$", ...
>
>> ## -w ignores differences in whitespace
>> ## -I '^[[:space:]]*$' because -B doesn't work for blank
lines
>> (on OSX?)
>> p = subprocess.Popen(cmd, stdin=open('/dev/null'),
>> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>
> I don't think you need to specify stdin.
>
>> logs = p.communicate()
>> diffs = logs[0].splitlines() #stdout
>> print "diff reply was %i lines long" % len(diffs)
>>
>> This doesn't work. I've tried escaping the various bits, like the *
and
>> $, even though with single quotes that should not be needed.
>>
>> I tried first removing the blank lines from the file:
>>
>> import fileinput
>> for line in fileinput.FileInput("test.xml",inplace=1):
>> if line.rstrip():
>> print line
>>
>> This makes it worse, as it adds and empty line for each line in the
>> file.
>
> Add a trailing comma to suppress the newline:
>
> print line,
>
>> I've tried various other options. The only thing I can think of, is
>> ditching Python and trying to rewrite the whole script in Bash.
>> (It's quite complicated, as it loops over various things and does
some
>> pretty output in between and I'm not very fluent in Bash)
>>
>> Any suggestions?
>
> Whatever floats your boat ;)
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
problem Shivam Gupta <mailtoshivamgupta@gmail.com> - 2016-01-14 21:23 +0530
Re: problem Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2016-01-14 20:01 +0100
ignoring or replacing white lines in a diff "Adriaan Renting" <renting@astron.nl> - 2016-01-14 21:22 +0100
Re: ignoring or replacing white lines in a diff Zachary Ware <zachary.ware+pylist@gmail.com> - 2016-01-14 14:54 -0600
Re: ignoring or replacing white lines in a diff Nathan Hilterbrand <nhilterbrand@gmail.com> - 2016-01-14 15:57 -0500
Re: ignoring or replacing white lines in a diff "Martin A. Brown" <martin@linux-ip.net> - 2016-01-14 12:57 -0800
Re: ignoring or replacing white lines in a diff Peter Otten <__peter__@web.de> - 2016-01-14 22:05 +0100
Re: problem Chris Angelico <rosuav@gmail.com> - 2016-01-15 08:10 +1100
Re: problem eryk sun <eryksun@gmail.com> - 2016-01-14 15:39 -0600
Re: ignoring or replacing white lines in a diff "Adriaan Renting" <renting@astron.nl> - 2016-01-15 10:44 +0100
Re: ignoring or replacing white lines in a diff Peter Otten <__peter__@web.de> - 2016-01-15 11:20 +0100
csiph-web