views:

730

answers:

4

I would like to give IronPython and Mono a try. Specifically doing sysadmin tasks. Which often means running OS commands. In CPython I use the subprocess module for such tasks. But in IronPython (v2.0.1, Mono 2.4, Linux) there is no subprocess module. It seems there is not even an 'os' module. So I can't use os.system(). What would be the IronPython way of doing tasks you would normally use 'subprocess' or 'os.system()' for in CPython?

+1  A: 

You can use most of the standard os modules from within ironpython.

import sys
sys.path.append path('...pathtocpythonlib......')
import os
Preet Sangha
Thanks for the tip.But I would like to avoid having to bundle a CPython distribution to make my IronPython stuff work.Besides: It seems even if I extend sys.path with the necessary directories to the CPython intallation 'subprocess' will not work. It depends on 'fcntl'. Which is a shared library (fcntl.so).
Cyberdrow
Is this case I'd suggest using the .net System.Diagnostics.Process class. See the Cyberdrow's answer below.
Preet Sangha
Sorry just worked out it was you.
Preet Sangha
+6  A: 

I have found an answer. Thanks to the "IronPython Cookbook". One can find more information on this subject there: http://www.ironpython.info/index.php/Launching_Sub-Processes

>>> from System.Diagnostics import Process
>>> p = Process()
>>> p.StartInfo.UseShellExecute = False
>>> p.StartInfo.RedirectStandardOutput = True
>>> p.StartInfo.FileName = 'uname'
>>> p.StartInfo.Arguments = '-m -r'
>>> p.Start()
True
>>> p.WaitForExit()
>>> p.StandardOutput.ReadToEnd()
'9.6.0 i386\n'
>>> p.ExitCode
0
>>>
Cyberdrow
Beware: Setting "RedirectStandardOutput = True" and "WaitForExit()" _can_ lead to a deadlock. If the stdoutput buffer is filled, the process will wait for "someone" to empty the buffer, while the (only) one (able) to empty the buffer waits for the process to finish.
Nils
A: 

Consider this C# Interactive Shell too....not sure if it supports IronPhython in the shell, but Mono does as you know.

kenny
A: 

There is a partial subprocess module implementation here:

http://www.bitbucket.org/jdhardy/code/src/tip/subprocess.py

The module (at this time, June 2010) is only supports redirecting STDIO pipes (as in, you cannot provide your own file-like objects to be filled with output or to stream intput), but the basics are enough to get by.

ddotsenko