News for hackers. Stuff that matters.
Re:Sh.py by russelldavis on Monday September 17, 2012 @03:59AM (#4531645)
For a similar library with a slightly different take, check out plumbum:

http://plumbum.readthedocs.org/en/latest/index.html

Here's the explanation on the differences:

"The project has been inspired by PBS of Andrew Moffat, and has borrowed some of his ideas (namely treating programs like functions and the nice trick for importing commands). However, I felt there was too much magic going on in PBS, and that the syntax wasn’t what I had in mind when I came to write shell-like programs. I contacted Andrew about these issues, but he wanted to keep PBS this way. Other than that, the two libraries go in different directions, where Plumbum attempts to provide a more wholesome approach."

Re:Sh.py by mixmastamyk on Monday September 17, 2012 @12:36PM (#4533359)
Ooh, I like the piping syntax better. The module name is a bit long and hard to remember though.
Re:Sh.py by jdnier on Monday September 17, 2012 @06:03PM (#4535151)
Plumbum is the latin name for lead, periodic table symbol "Pb", perhaps memorable enough considering Plumbum was inspired by an earlier project called PBS.
Re:Sh.py by mixmastamyk on Tuesday September 18, 2012 @02:49AM (#4536744)
Still doesn't roll of the keyboard, does it?
Re:Sh.py by sartakdotorg on Sunday September 16, 2012 @09:21PM (#4530966)
Perl has this too: https://metacpan.org/module/Shell

Written in 1994, by Larry himself!

Re:Sh.py by mleonhard on Tuesday September 18, 2012 @11:48PM (#4541947)
The module documentation doesn't mention error handling. Perl has no consistent exception system and this makes it very difficult to build reliable programs in the language. The standard library has inconsistent error handling. And CPAN modules like this usually have none at all.
Re:Sh.py by evincarofautumn on Monday September 17, 2012 @10:07AM (#4532599)
CPAN rarely disappoints.
Re:Sh.py by Sidnicious on Sunday September 16, 2012 @09:43PM (#4531009)
So, this feature:

http://amoffat.github.com/sh/index.html#interactive-callback...

Lets you replace `expect` with Python code pretty darn easily.

Re:Sh.py by bryogenic on Sunday September 16, 2012 @10:50PM (#4531154)
why not use pexpect, it has worked great for me in the past.

http://www.noah.org/wiki/pexpect

Re:Sh.py by playhard on Monday September 17, 2012 @03:47AM (#4531626)
+1 for pexpect. it is very handy to automate user inputs
Re:Sh.py by subhobroto on Sunday September 16, 2012 @09:47PM (#4531026)
+1, and this can be complimentary to fabric like scripts too to make pretty powerful scripts!
Re:Sh.py by jonpaul on Sunday September 16, 2012 @10:43PM (#4531141)
Shameless plug, I wrote an `expect` replacement utility in Node.js: https://github.com/jprichardson/node-suppose
Re:Sh.py by SoftwareMaven on Sunday September 16, 2012 @09:34PM (#4530993)
I like PBS (now sh.py) for certain use cases. If I'm writing an actual shell script, I think it is brilliant. It keeps the script focused on the task at hand instead of Python's somewhat painful process communication.

On the other hand, if I have an application that needs to communicate with a subprocess as a small piece of the whole, I'll use other methods that are less "magical". It's not that I'm inherently against magic, but rather that, in that use case, I generally want very explicit control over what is happening.

Re:Sh.py by okal on Monday September 17, 2012 @03:10AM (#4531558)
Not sure I agree that this is magic. Syntactic sugar? Definitely. But it doesn't obscure any of the underlying logic.
Re:Sh.py by lmm on Monday September 17, 2012 @05:47AM (#4531837)
It causes "import" to behave in ways you wouldn't expect.
Re:Sh.py by im3w1l on Monday September 17, 2012 @09:02AM (#4532327)
Could you elaborate on this point? What happens and what would you expect?
Re:Sh.py by apl on Monday September 17, 2012 @09:39AM (#4532476)
This tool hijacks the import mechanism by directly writing to Python's look-up tables. After all, when you do

  from sh import git
there's no module 'sh' invoked in the normal sense. Instead, the library generates a wrapper for the shell command 'git' on the fly. While that kind of monkey patching may be neat, it's also a bit brittle and a potential security issue.
Re:Sh.py by jerf on Monday September 17, 2012 @12:46PM (#4533427)
Python has a lot of built-in support for overriding how imports work, and modules have always been just namespaces. As hacks go, this isn't very hacky in Python, and it isn't particularly brittle, as it's just implementing existing interfaces.

It is also not "monkeypatching", which ought to be reserved for things that involve reaching into an existing class and modifying things. This on-demand loading, which doesn't have anywhere near the same evil factor, but is rather more like dynamic programming languages working-as-designed.

You may find this distasteful. I do, actually, though I'm not 100% sure why. But it's not because it's some sort of abuse of Python. Python is very nearly designed to do this, and the last little bit that it isn't designed for isn't that big a deal, especially compared to something like the "import python modules through zipfiles" functionality, which now ships with the core.

Re:Sh.py by emidln on Monday September 17, 2012 @10:26AM (#4532694)
Doubtful that this is more damaging than:

    import os
    os.system('insert local exploit here')
It's true that it is another place that you can get a python script to execute code outside of its environment, but you get a ton of those for free with the stdlib.
Re:Sh.py by SilasX on Monday September 17, 2012 @07:36PM (#4535461)
You must consider Ruby on Rails to be pure hellspawn then. (I agree, but for different reasons ...)
Re:Sh.py by MBlume on Monday September 17, 2012 @07:58PM (#4535528)
Well, the Python and Ruby communities have different engineering cultures and this is part of that. "Explicit is better than implicit" is one of the mantras on the Python side.

http://www.python.org/dev/peps/pep-0020/

Re:Sh.py by timtadh on Monday September 17, 2012 @09:37AM (#4532468)
You expect import imports names which have actually been declared somewhere. Names which you can find and look at the definition. This library essentially hijacks the import process to allow you to import any name, each name is then actually a wrapper on a system to run shell commands. Definitely a hack, but a cool and useful looking hack.
Re:Sh.py by esrauch on Monday September 17, 2012 @08:00PM (#4535532)
That is a reasonable assumption in the common case, but it's reasonable to not always assume that. The same is true if you have obj.v(), you would normally expect there to be some v property defined on the object but really it might be coming dynamically from __getattr__().

This is something that python specifically has language level support for, I wouldn't say it's a hack just because its using a feature that isn't taught in Python 101.

Re:Sh.py by lmm on Monday September 17, 2012 @10:09AM (#4532612)
Rightly or wrongly I expect import to be simple, fail only when packages haven't been installed properly, and more generally depend only on PYTHONPATH. When I'm debugging I don't normally even look at the import statements. Other replies have described what this does; I think I'd rather offer such functionality as something that looks like a method call that might fail (something like git = sh.getProxyForShellCommand("git") - probably not that verbose, my head's in java-land at the moment, but you get the idea)
Re:Sh.py by daenz on Monday September 17, 2012 @02:56PM (#4534206)

    import sh
    git = sh.Command("/usr/bin/git")
the Command object takes a full path, but you can use it together with sh's "which":

    import sh
    git = sh.Command(sh.which("git"))
Re:Sh.py by jon6 on Sunday September 16, 2012 @11:58PM (#4531276)
I spent a few minutes trying to figure out how to install the thing so for anyone that is equally as lost

    $ pip install sh
or goto the github page https://github.com/amoffat/sh
Re:Sh.py by pattern on Monday September 17, 2012 @12:13AM (#4531304)
To the parent and anyone else who hasn't experienced the wonders of `pip`, check out the article "Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip" [1]. All three of these tools are invaluable for even the most trivial of Python projects, and make developing in Python much more enjoyable.

[1]: http://www.clemesha.org/blog/modern-python-hacker-tools-virt...

Re:Sh.py by bduerst on Monday September 17, 2012 @12:02PM (#4533172)
I just learned Python a week ago, and PIP was an important part of that.

For those of you who are just learning, PIP will automatically download, install, and then compile any python modules you point it at. Just make sure you have the module's recommended compilers installed first.

Re:Sh.py by agrover on Monday September 17, 2012 @02:53AM (#4531515)
packaged as python-sh in Fedora 17+
Re:Sh.py by subhobroto on Sunday September 16, 2012 @09:45PM (#4531016)
I have found this extremely useful - used it to write many things - from a set of scripts that bootstrap chef server onto a node from scratch to a file chunking program that optimizes log files to align with hadoop block sizes using multiprocessing and this. It made a lot of things very easy.

This version introduces many positive changes: specially 'Iterating over output' that I have been waiting for a long time.

Andrew wants to increase his support for MacOS and would like to have test results from "python setup.py test" (to run the whole test suite). One identified bug is: http://bugs.python.org/issue15898

I would love to see more people use this to simplify their work!

If anyone is interested in looking into the scripts I wrote to see what's possible, let me know.

Re:Sh.py by europa on Sunday September 16, 2012 @10:27PM (#4531113)
Yes. Please publish it.
Re:Sh.py by philp on Monday September 17, 2012 @01:36AM (#4531422)
Could somebody explain to me how this is different from envoy? https://github.com/kennethreitz/envoy

Not meant as a snide remark; genuinely curious.

Re:Sh.py by Aissen on Monday September 17, 2012 @06:26AM (#4531916)
Just look at how it's used. It's not the same. sh imports shell commands as functions. envoy allows you to run shell commands very easily, like perl's ``.
Re:Sh.py by saikat on Sunday September 16, 2012 @09:48PM (#4531030)
For anyone looking for a nice subprocess library for Ruby, my friend Greg released one earlier this week - https://github.com/gdb/rubysh
Re:Sh.py by b6fan on Monday September 17, 2012 @12:49PM (#4533445)
I write another one for ruby, check out samples here: https://github.com/quark-zju/easysh

Although its functions are a subset of sh for python. It has many synatactic sugars, makes full use of ruby Enumerable, and does lazy-execute.

Re:Sh.py by b6fan on Monday September 17, 2012 @12:04PM (#4533185)
Seems not working with cruby 1.9.3 ? SyntaxError: .../rubysh-0.0.2/lib/rubysh/subprocess/parallel_io.rb:77: Invalid next
Re:Sh.py by arturadib on Monday September 17, 2012 @11:16AM (#4532931)
Definitely neat, but of course platform-dependent.

Due to the cross-platform needs of Mozilla's PDF.js build scripts, we've been writing a Node.js lib on top of Node's APIs that enables you to write shell-like scripts that run seamlessly on multiple platforms:

http://shelljs.org

Like Sh.py, you can (if you must) also run external commands, either synchronously or asynchronously.

Re:Sh.py by sciurus on Monday September 17, 2012 @09:05PM (#4535735)
This seems only tangentially related. The point of something like sh.py is to ease the use of external commands you need to use. The functions that shelljs partially implements (cd, pwd, ls, find, cp, rm, mv, mkdir, test, cat, sed, grep, which, echo, exit, env) are already trivially accomplished in Python.
Re:Sh.py by AntiRush on Sunday September 16, 2012 @09:44PM (#4531014)
I wrote something similar on top of nodejs to simplify some problems at Game Closure.

http://www.github.com/gameclosure/jash

It's probably not ready for prime time - past it's initial use cases it hasn't been tested much. Things like sh.py and jash are a really neat solution for some problems.

Re:Sh.py by tiles on Sunday September 16, 2012 @10:35PM (#4531130)
I was hoping for a Node.js alternative. One question, have you thought about using https://github.com/samshull/node-proxy instead of searching through the environment to find binaries? Using .get() you would only need to identify programs that are requested, rather than knowing them all up front.
Re:Sh.py by jlgreco on Monday September 17, 2012 @01:42AM (#4531427)
Rather tempting to use this with aa Python REPL to replace a more traditional shell.
Re:Sh.py by nvmc on Monday September 17, 2012 @10:47PM (#4536028)
I foresee a lot of frustrated users trying to google things about this project.
Re:Sh.py by nodesocket on Monday September 17, 2012 @12:36AM (#4531339)
Founder of Commando.io (http://commando.io) here. The tutorial on SSH was particularly interesting, since we are doing some of the same sort of things to help with orchestration of servers. Currently we are using `libssh2` via a PHP module, but switching to a sparkling new node.js interface for the SSH and SCP connections and executions shortly.
Re:Sh.py by notatoad on Sunday September 16, 2012 @11:27PM (#4531213)
This is beautiful. Thank you.
Re:Sh.py by nimrody on Monday September 17, 2012 @03:04AM (#4531539)
Throwing exceptions when a command returns non-zero exit status is very useful indeed. However, this isn't very different from using the shell's own && operator.

I still believe that wrapping shell commands with functions is the way to go. Functions can intelligently check their arguments and prevent propagation of dangerous (or otherwise obviously incorrect) arguments.

Re:Sh.py by justincormack on Monday September 17, 2012 @03:59AM (#4531646)
Surely implementing the shell commands natively is the way to go?

This way you then have to parse the output of ifconfig, say.

eg I have been doing this for Lua [1] you can do

> i = nl.interfaces()

> print(i.lo)

lo Link encap:Local Loopback inet addr: 127.0.0.1/8 inet6 addr: ::1/128 UP LOOPBACK RUNNING LOWER_UP MTU: 16436 RX packets:261454 errors:0 dropped:0 TX packets:261454 errors:0 dropped:0

> print(i.eth0.macaddr)

f0:da:f0:38:36:39

The functionality is now reasonably comprehensive, so you can rename interfaces, add addresses, although it is still a work in progress, as there is a fair amount of work involved as there is a lot of shell to implement, but it can be incrementally useful.

[1] https://github.com/justincormack/ljsyscall

[deleted]
Re:Sh.py by forgotusername on Monday September 17, 2012 @06:46AM (#4531963)
It's worse than that. It replaces sys.modules['sh'] with something that isn't a types.ModuleType in the middle of the module's initialization.

Cute, but definitely on the list of things I'd remove on sight if encountered in a commercial project.

Re:Sh.py by bthomas on Monday September 17, 2012 @12:09AM (#4531295)
Do the python3 print statements imply anything about whether it's compatible with python2?

And what's the best way to quickly look and see which versions a package is compatible with?

Re:Sh.py by daenz on Monday September 17, 2012 @12:15AM (#4531308)
It's confirmed compatible with python 2.6-3.2
Re:Sh.py by ragmondo on Monday September 17, 2012 @07:28AM (#4532058)
I would rather have py.sh ... a unix shell running python.
Re:Sh.py by ompemi on Monday September 17, 2012 @06:17PM (#4535210)
It saved my day, I expected partials with cwd parameter and they were there. I used this instead of GitPython + manual popen for some git management tasks.
Re:Sh.py by riffraff on Monday September 17, 2012 @02:22AM (#4531473)
for those wondering how "import madeupname" works, basically the incantation is:

    # unless __name__ == "__main__" 
    self = sys.modules[__name__]
    # SelfWrapper has a custom __getattr__
    sys.modules[__name__] = SelfWrapper(self) 
which seems somewhat unpythonesque (aren't import hooks supposed to be used for this?) but it's cool and I hadn't seen it before.
Re:Sh.py by fijal on Monday September 17, 2012 @02:26PM (#4534032)
import hooks leave crap in your global state. this only modifies sys.modules, which is probably a much better option.
Re:Sh.py by arnarbi on Sunday September 16, 2012 @11:59PM (#4531277)
What happens to the order of keyword arguments?
Re:Sh.py by davidism on Monday September 17, 2012 @12:02AM (#4531286)
Good point. I guess if order matters you can do everything as positional args, then order will be preserved.
Re:Sh.py by OrdojanAndrius on Monday September 17, 2012 @03:19AM (#4531580)
Ohh this looks awesome, I wish it would work for windows thought.
Re:Sh.py by scdoshi on Sunday September 16, 2012 @10:49PM (#4531153)
This is cool. Could have used it yesterday, literally.
Re:Sh.py by andybak on Monday September 17, 2012 @04:26AM (#4531690)
In that case you'll have to hurry.
Re:Sh.py by SIULHT on Monday September 17, 2012 @12:50AM (#4531355)
Does this include scp/rsync?
Re:Sh.py by andybak on Monday September 17, 2012 @04:25AM (#4531689)
from sh import anythingyoudamnwellplease
Re:Sh.py by Tyr42 on Monday September 17, 2012 @04:04AM (#4531650)
if it's on your path...
Re:Sh.py by maskedinvader on Monday September 17, 2012 @11:24PM (#4536140)
this is awesome, thank you for posting
Re:Sh.py by esschul on Monday September 17, 2012 @02:38AM (#4531494)
Hey, just like groovy's "".execute()
Re:Sh.py by freehunter on Monday September 17, 2012 @02:38PM (#4534109)
Lua has a function called os.execute() where you can call programs and utilities from the command line. Cross platform as well; I've got more than one Lua script where it's working in the command line and can handle being on Unix or on Windows.

A common example of this that I use quite often is "clear" or "cls". It makes the determination what OS it's using, then issues the appropriate command to clear the terminal window.