Sharing: Dump echo output to a file

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Sharing: Dump echo output to a file

runsun
Occasionally there might be a need to dump the echo output to a file. For example, "automatically generate BOMs and debug measurements and information to files." as described by carlosgs here [1], or in the application of my doctest lib ( here ).

Unfortunately OpenScad can't do that, and there seems to be no plan (according to [1]) for that feature.

But there are ways to get around it:

(1) You can dump echo output to a file under command line (Linux):

>>> openscad <scad_file> -o dump.stl -D "<commands_to_execute>" 2> <output_file>

For example, one of my uses looks like this:

>>> time openscad mylib.scad -o dump.stl -D "FORHTML=true;runtest()" 2> mylib_api.htm

(2) The above approach works, but every echo( ) inserts "ECHO: " to the beginning of output and a trailing " in the end. It is inconvenient to have multiple execution-unrelated "ECHO: " in an output file, especially if the main purpose of dumping to a file is for further automatic processing.

The easiest way for OpenSCAD to solve this is to have an option to suppress the display of "ECHO: ".

But, before that being considered a feature to add, we can use the following simple python script. Note that: (1) It is coded on Linux and hasn't been tested on Win or Mac; (2) It assumes that all echo output are strings. It can be used this way:

>>> time python runscad.py mylib.scad "FORHTML=true;runtest()" mylib_api.htm

############################## ## runscad.py ## Run an openscad file and dump the echo output to a file ## >>> python runscad.py <scad_file> "<commands_to_execute>" <output_file> ## Like: ## >>> python runscad.py mylib.scad "FORHTML=true;runtest()" mylib_api.htm ## by Runsun Pan (2015.5.1) ############################## import sys, os, re if len(sys.argv)<4: print ''' # runscad.py # # Not enough arguments. Required 3 arguments: # # python runscad.py <scad_file> "<command(s)>" <output_file> # # For example: # # python runscad.py myprog.scad "FORHTML=true;MODE=10;run()" tests.html ''' else: cmd = 'openscad %s -o dump.stl -D "%s" 2> %s'% tuple(sys.argv[1:]) os.system( cmd ) output_file= sys.argv[3] lines = open( output_file, 'r').readlines() # Remove the starting 'ECHO: "' and trailing " on each line ptn = re.compile(r'ECHO: \"|"$') for i,line in enumerate(lines): lines[i] = re.sub( ptn,'', line) # Save to file. Exclude the last line, which reads: # "Current top level object is empty." open( output_file, 'w').write( '\n'.join( lines[:-1] ) ) ##############################
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: Sharing: Dump echo output to a file

jdawgaz

On Fri, May 1, 2015 at 9:58 AM, runsun <[hidden email]> wrote:
openscad <scad_file> -o dump.stl -D "<commands_to_execute>" 2> <output_file>


how about this?

openscad testthingie.scad -o junk.stl 2>&1 >/dev/null | sed 's/ECHO: "//' | sed 's/"$//' > outfile

Jerry

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. 
- Anonymous

If writing good code requires very little comments, then writing really excellent code requires no comments at all!
- Ken Thompson


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Sharing: Dump echo output to a file

runsun
Hi Jerry, that's a much shorter one :)

In fact I started this with a sed version, too. Not as elegant as yours but works. I then move on to the python one 'cos I thought it'd be cross-platform.

Nonetheless, will steal your idea and make an alias outta it. Thx.

jdawgaz wrote
openscad testthingie.scad -o junk.stl 2>&1 >/dev/null | sed 's/ECHO: "//' | sed 's/"$//' > outfile

Jerry
$ Runsun Pan, PhD
$ libs: scadx, doctest, faces(git), offline doc(git), runscad.py(2,git), editor of choice: CudaText ( OpenSCAD lexer); $ Tips; $ Snippets
Reply | Threaded
Open this post in threaded view
|

Re: Sharing: Dump echo output to a file

jdawgaz
no problemo.

the python one would work in windoze.

don't get me wrong, I love python too. And ruby.
Before retiring, I did 5 years of every day python work. grew to love it.
Now I play with my raspberry pi and python (and ruby)




Jerry

--
Extra Ham Operator: K7AZJ
Registered Linux User: 275424
Raspberry Pi and Arduino developer

The most exciting phrase to hear in science - the one that heralds new discoveries - is not "Eureka!" but "That's funny...".
- Isaac. Asimov

If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime. 
- Anonymous

If writing good code requires very little comments, then writing really excellent code requires no comments at all!
- Ken Thompson


On Fri, May 1, 2015 at 10:55 AM, runsun <[hidden email]> wrote:
Hi Jerry, that's a much shorter one :)

In fact I started this with a sed version, too. Not as elegant as yours but
works. I then move on to the python one 'cos I thought it'd be
cross-platform.

Nonetheless, will steal your idea and make an alias outta it. Thx.


jdawgaz wrote
> openscad testthingie.scad -o junk.stl 2>&1 >/dev/null | sed 's/ECHO: "//'
> | sed 's/"$//' > outfile
>
> Jerry





-----

$  Runsun Pan, PhD

$ -- OpenScad_DocTest: doc and unit test ( Github , Thingiverse  )

$ -- hash parameter model: here , here

$ -- Linux Mint 17.1 Rebecca x64  + OpenSCAD 2015.03.15/2015.04.01.nightly




--
View this message in context: http://forum.openscad.org/Sharing-Dump-echo-output-to-a-file-tp12529p12533.html
Sent from the OpenSCAD mailing list archive at Nabble.com.

_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org


_______________________________________________
OpenSCAD mailing list
[hidden email]
http://lists.openscad.org/mailman/listinfo/discuss_lists.openscad.org
Reply | Threaded
Open this post in threaded view
|

Re: Sharing: Dump echo output to a file

thejollygrimreaper
This post has NOT been accepted by the mailing list yet.
In reply to this post by runsun
i think the success of this seems to depend on what version you are running, i've been trying to do it in 2015.03 which is the latest and all console output seems to have disappeared completely so you can't even pipe the output to a text file like in previous versions,

there was a issue on github talking about this https://github.com/openscad/openscad/pull/845#issuecomment-98149532

the trouble was something to do with stability and order the solution was to pipe the output to a file and strip out the "echo"s however since that no longer works somthing else needs to be done, i'm contacting carlogs atm to see if he still has a copy of openscad with this feature running as it would be so much easier to be able to write directly to a file instead of mucking around with piped output and python scripts