[Pharo-project] Headless PetitParser
Hi all, I have a small parser written in PetitParser ;) I would like to call it from a non Smalltalk application (e.g., Java, javascript) or, more generally, from a Unix command line. I was thinking about something like: ./myParser -i fileToParse.txt -o outputFile.txt Is there any possibility to do that? Can a headless Pharo be a solution? If so, how can I start and use a headless Pharo? Is there any useful documentation/email about it? Thank you a lot for any help. Cheers, Alberto
Hi alberto, i'm not sure about the solution of your problem although i would try to prepare a simple st script were you basically say to the parser which file to parse and simply flush on a file the output of the parser. Than you can start an headless image passing the st file as input. Hope this will help. Cheers, Fabrizio 2011/11/5 Alberto Bacchelli <alberto.bacchelli@usi.ch>
Hi all,
I have a small parser written in PetitParser ;) I would like to call it from a non Smalltalk application (e.g., Java, javascript) or, more generally, from a Unix command line. I was thinking about something like:
./myParser -i fileToParse.txt -o outputFile.txt
Is there any possibility to do that? Can a headless Pharo be a solution? If so, how can I start and use a headless Pharo? Is there any useful documentation/email about it?
Thank you a lot for any help.
Cheers, Alberto
Hi Alberto: On 05 Nov 2011, at 23:29, Alberto Bacchelli wrote:
./myParser -i fileToParse.txt -o outputFile.txt
Is there any possibility to do that? Can a headless Pharo be a solution? If so, how can I start and use a headless Pharo? Is there any useful documentation/email about it?
I attached the stuff I currently use. There might be better ways to do it, but the attached code is Squeak 3.9 and Pharo 1.3 compatible... [There might be bugs with regard to saving images and restarting etc. Have not checked whether that is the latest version of the code. But should be good enough to get an idea, and might be sufficient for your use case.] The HelloWorld class should get you started. On OSX you would need a fairly new VM, I think. The old ones did not play nice with the command-line. The hello world should look like this: $ your-vm-binary -headless your.image HelloWorld foo bar baz Best regards Stefan -- Stefan Marr Software Languages Lab Vrije Universiteit Brussel Pleinlaan 2 / B-1050 Brussels / Belgium http://soft.vub.ac.be/~smarr Phone: +32 2 629 2974 Fax: +32 2 629 3525
Alberto, Have a look at the how TextLint (a PetitParser thingy that creates some output) is integrated into TextMate, Emacs, Vim, ... https://github.com/DamienCassou/textlint The Pharo VM is started through some (shell) scripts that are called by the respective editors (but these scripts also work independently): Emacs: https://github.com/DamienCassou/textlint/blob/master/textlint.bash TextMate: https://github.com/DamienCassou/textlint/blob/master/TextLint.tmbundle/Comma... Lukas On 6 November 2011 01:04, Stefan Marr <pharo@stefan-marr.de> wrote:
Hi Alberto:
On 05 Nov 2011, at 23:29, Alberto Bacchelli wrote:
./myParser -i fileToParse.txt -o outputFile.txt
Is there any possibility to do that? Can a headless Pharo be a solution? If so, how can I start and use a headless Pharo? Is there any useful documentation/email about it?
I attached the stuff I currently use. There might be better ways to do it, but the attached code is Squeak 3.9 and Pharo 1.3 compatible...
[There might be bugs with regard to saving images and restarting etc. Have not checked whether that is the latest version of the code. But should be good enough to get an idea, and might be sufficient for your use case.]
The HelloWorld class should get you started.
On OSX you would need a fairly new VM, I think. The old ones did not play nice with the command-line.
The hello world should look like this:
$ your-vm-binary -headless your.image HelloWorld foo bar baz
Best regards Stefan
-- Stefan Marr Software Languages Lab Vrije Universiteit Brussel Pleinlaan 2 / B-1050 Brussels / Belgium http://soft.vub.ac.be/~smarr Phone: +32 2 629 2974 Fax: Â +32 2 629 3525
-- Lukas Renggli www.lukas-renggli.ch
Thank you everybody for your super-useful help! I managed to create my first smalltalk script for headless Pharo (it's pretty fun ;) ! Cheers, Alberto -- View this message in context: http://forum.world.st/Headless-PetitParser-tp3994446p4024397.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Hi Alberto can you give an example so that people can learn too? Stef On Nov 10, 2011, at 6:16 PM, Alberto Bacchelli wrote:
Thank you everybody for your super-useful help! I managed to create my first smalltalk script for headless Pharo (it's pretty fun ;) !
Cheers, Alberto
-- View this message in context: http://forum.world.st/Headless-PetitParser-tp3994446p4024397.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Stéphane Ducasse wrote:
can you give an example so that people can learn too?
Sure. So I have a simple bash script that calls the vm, the image, and the script: ----------------bash script----------- #!/bin/bash export AN_ENVIRONMENT_VARIABLE=somevalue export SCRIPT_LOG_FILE=afilename "$pharovm_dir"/squeak -plugins "$pharovm_dir" -encoding Latin1 -headless "$pharo_image" "$smalltalk_script" && exit 0 echo Something bad happened! exit 1 ---------------------------------------- the $smalltalk_script is an .st file with the code to be executed (an example later). environment variables, such as AN_ENVIRONMENT_VARIABLE and SCRIPT_LOG_FILE, are useful to pass some info to the smalltalk script in an easy way, here we use SCRIPT_LOG_FILE to save some script's output to a file. Here I put a very simple example based on what I did: ----------------pharo script (aka $smalltalk_script)----------- [| fs environment pid | environment := OSProcess thisOSProcess environment. pid := OSProcess thisOSProcess pid. someValue := (environment at: 'AN_ENVIRONMENT_VARIABLE'). fs := FileStream forceNewFileNamed: (environment at: 'SCRIPT_LOG_FILE'). loggingBlock := [:message | fs nextPutAll: TimeStamp now asString, ' [', pid asString, '] ' . fs nextPutAll: message; nextPut: Character lf.]. loggingBlock value: 'Starting script with ', someValue. "do some interesting stuff here" "..." Smalltalk snapshot: false andQuit: true.] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.] ------------------------------------------ A few comments: - with "OSProcess thisOSProcess environment" you can access environment variables as a dictionary (that's why I use them instead of command line arguments) - "OSProcess thisOSProcess pid" gives you the pid of your process, in this way I can make a unix-like log file - "Smalltalk snapshot: false andQuit: true." closes the image correctly at the end of your script and does NOT save the state on the image. - "[...] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.]" is _VERY_ useful to debug your code. Any time there is an error in the main block, it catches it and prints the message to the console using the standard error. This is essential to be able not to spend ages in trying to understand what went wrong with your script. That's all. I hope it's useful to someone. Cheers, Alberto -- View this message in context: http://forum.world.st/Headless-PetitParser-tp3994446p4075649.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
Thanks Alberto Documentation and sharing are underestimated in our community. What is simple for you may be complex for somebody else and the inverse, we are all more powerful together :) Stef On Nov 16, 2011, at 9:25 AM, Alberto Bacchelli wrote:
Stéphane Ducasse wrote:
can you give an example so that people can learn too?
Sure.
So I have a simple bash script that calls the vm, the image, and the script:
----------------bash script----------- #!/bin/bash
export AN_ENVIRONMENT_VARIABLE=somevalue export SCRIPT_LOG_FILE=afilename
"$pharovm_dir"/squeak -plugins "$pharovm_dir" -encoding Latin1 -headless "$pharo_image" "$smalltalk_script" && exit 0 echo Something bad happened! exit 1 ----------------------------------------
the $smalltalk_script is an .st file with the code to be executed (an example later). environment variables, such as AN_ENVIRONMENT_VARIABLE and SCRIPT_LOG_FILE, are useful to pass some info to the smalltalk script in an easy way, here we use SCRIPT_LOG_FILE to save some script's output to a file.
Here I put a very simple example based on what I did:
----------------pharo script (aka $smalltalk_script)----------- [| fs environment pid | environment := OSProcess thisOSProcess environment. pid := OSProcess thisOSProcess pid.
someValue := (environment at: 'AN_ENVIRONMENT_VARIABLE'). fs := FileStream forceNewFileNamed: (environment at: 'SCRIPT_LOG_FILE').
loggingBlock := [:message | fs nextPutAll: TimeStamp now asString, ' [', pid asString, '] ' . fs nextPutAll: message; nextPut: Character lf.].
loggingBlock value: 'Starting script with ', someValue.
"do some interesting stuff here" "..."
Smalltalk snapshot: false andQuit: true.] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.] ------------------------------------------
A few comments: - with "OSProcess thisOSProcess environment" you can access environment variables as a dictionary (that's why I use them instead of command line arguments) - "OSProcess thisOSProcess pid" gives you the pid of your process, in this way I can make a unix-like log file - "Smalltalk snapshot: false andQuit: true." closes the image correctly at the end of your script and does NOT save the state on the image. - "[...] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.]" is _VERY_ useful to debug your code. Any time there is an error in the main block, it catches it and prints the message to the console using the standard error. This is essential to be able not to spend ages in trying to understand what went wrong with your script.
That's all. I hope it's useful to someone.
Cheers, Alberto
-- View this message in context: http://forum.world.st/Headless-PetitParser-tp3994446p4075649.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
We could put this in the collaborative book. Alberto, let me know if you want a user ;) On Wed, Nov 16, 2011 at 6:19 PM, Stéphane Ducasse <stephane.ducasse@inria.fr
wrote:
Thanks Alberto Documentation and sharing are underestimated in our community. What is simple for you may be complex for somebody else and the inverse, we are all more poWewerful together :)
Stef
On Nov 16, 2011, at 9:25 AM, Alberto Bacchelli wrote:
Stéphane Ducasse wrote:
can you give an example so that people can learn too?
Sure.
So I have a simple bash script that calls the vm, the image, and the
script:
----------------bash script----------- #!/bin/bash
export AN_ENVIRONMENT_VARIABLE=somevalue export SCRIPT_LOG_FILE=afilename
"$pharovm_dir"/squeak -plugins "$pharovm_dir" -encoding Latin1 -headless "$pharo_image" "$smalltalk_script" && exit 0 echo Something bad happened! exit 1 ----------------------------------------
the $smalltalk_script is an .st file with the code to be executed (an example later). environment variables, such as AN_ENVIRONMENT_VARIABLE and
SCRIPT_LOG_FILE,
are useful to pass some info to the smalltalk script in an easy way, here we use SCRIPT_LOG_FILE to save some script's output to a file.
Here I put a very simple example based on what I did:
----------------pharo script (aka $smalltalk_script)----------- [| fs environment pid | environment := OSProcess thisOSProcess environment. pid := OSProcess thisOSProcess pid.
someValue := (environment at: 'AN_ENVIRONMENT_VARIABLE'). fs := FileStream forceNewFileNamed: (environment at: 'SCRIPT_LOG_FILE').
loggingBlock := [:message | fs nextPutAll: TimeStamp now asString, ' [', pid asString, '] ' . fs nextPutAll: message; nextPut: Character lf.].
loggingBlock value: 'Starting script with ', someValue.
"do some interesting stuff here" "..."
Smalltalk snapshot: false andQuit: true.] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.] ------------------------------------------
A few comments: - with "OSProcess thisOSProcess environment" you can access environment variables as a dictionary (that's why I use them instead of command line arguments) - "OSProcess thisOSProcess pid" gives you the pid of your process, in this way I can make a unix-like log file - "Smalltalk snapshot: false andQuit: true." closes the image correctly at the end of your script and does NOT save the state on the image. - "[...] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.]" is _VERY_ useful to debug your code. Any time there is an error in the main block, it catches it and prints the message to the console using the standard error. This is essential to be able not to spend ages in trying to understand what went wrong with your script.
That's all. I hope it's useful to someone.
Cheers, Alberto
-- View this message in context: http://forum.world.st/Headless-PetitParser-tp3994446p4075649.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
-- Mariano http://marianopeck.wordpress.com
yes mariano can you copy and paste the code there? Stef On Nov 17, 2011, at 7:46 PM, Mariano Martinez Peck wrote:
We could put this in the collaborative book. Alberto, let me know if you want a user ;)
On Wed, Nov 16, 2011 at 6:19 PM, Stéphane Ducasse <stephane.ducasse@inria.fr> wrote: Thanks Alberto Documentation and sharing are underestimated in our community. What is simple for you may be complex for somebody else and the inverse, we are all more poWewerful together :)
Stef
On Nov 16, 2011, at 9:25 AM, Alberto Bacchelli wrote:
Stéphane Ducasse wrote:
can you give an example so that people can learn too?
Sure.
So I have a simple bash script that calls the vm, the image, and the script:
----------------bash script----------- #!/bin/bash
export AN_ENVIRONMENT_VARIABLE=somevalue export SCRIPT_LOG_FILE=afilename
"$pharovm_dir"/squeak -plugins "$pharovm_dir" -encoding Latin1 -headless "$pharo_image" "$smalltalk_script" && exit 0 echo Something bad happened! exit 1 ----------------------------------------
the $smalltalk_script is an .st file with the code to be executed (an example later). environment variables, such as AN_ENVIRONMENT_VARIABLE and SCRIPT_LOG_FILE, are useful to pass some info to the smalltalk script in an easy way, here we use SCRIPT_LOG_FILE to save some script's output to a file.
Here I put a very simple example based on what I did:
----------------pharo script (aka $smalltalk_script)----------- [| fs environment pid | environment := OSProcess thisOSProcess environment. pid := OSProcess thisOSProcess pid.
someValue := (environment at: 'AN_ENVIRONMENT_VARIABLE'). fs := FileStream forceNewFileNamed: (environment at: 'SCRIPT_LOG_FILE').
loggingBlock := [:message | fs nextPutAll: TimeStamp now asString, ' [', pid asString, '] ' . fs nextPutAll: message; nextPut: Character lf.].
loggingBlock value: 'Starting script with ', someValue.
"do some interesting stuff here" "..."
Smalltalk snapshot: false andQuit: true.] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.] ------------------------------------------
A few comments: - with "OSProcess thisOSProcess environment" you can access environment variables as a dictionary (that's why I use them instead of command line arguments) - "OSProcess thisOSProcess pid" gives you the pid of your process, in this way I can make a unix-like log file - "Smalltalk snapshot: false andQuit: true." closes the image correctly at the end of your script and does NOT save the state on the image. - "[...] on: Error do: [:err | OSProcess thisOSProcess stdErr nextPutAll: err asString; nextPut: Character lf. Smalltalk snapshot: false andQuit: true.]" is _VERY_ useful to debug your code. Any time there is an error in the main block, it catches it and prints the message to the console using the standard error. This is essential to be able not to spend ages in trying to understand what went wrong with your script.
That's all. I hope it's useful to someone.
Cheers, Alberto
-- View this message in context: http://forum.world.st/Headless-PetitParser-tp3994446p4075649.html Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
-- Mariano http://marianopeck.wordpress.com
participants (6)
-
Alberto Bacchelli -
Fabrizio Perin -
Lukas Renggli -
Mariano Martinez Peck -
Stefan Marr -
Stéphane Ducasse