Sublime Forum

[SOLVED] Running external command from plugin

#1

I’m having some difficulty calling something external on the command line. The gist is something like a build tool, but I’d like it to be more configurable. I’m trying to create a plugin that runs arbitrary commands when a file is saved. For instance, I’m working on a new project using CoffeeScript and, and when I save a CoffeeScript or JavaScript file, I want to run a rake task to compile the assets together. The catch is that I want to do it only in one directory since that’s the only project I’m currently using CoffeeScript in. Anyway, I have everything working the way I want except the command doesn’t execute.

	def perform_action(self, action):
		name = action.get("name")

		command = action.get("command")
		if not command:
			print 'Action ' + name + ': No command supplied'
			return

		wd = action.get("working_dir")
		if wd:
			os.chdir(wd)
	
		print 'Performing action: ' + name + ' (' + command + ') in ' + os.getcwd()

		subprocess.Popen(command, cwd=wd, shell=True)

In this particular case, the working directory is

/Users/phillip/Development/Projects/Self/Web/Grounect/client

and the command is

rake assets:compile:javascripts

It may make a difference that I’m using RVM and have a trusted .rvmrc in this directory. The rake task works if I run it on the command line myself.

In addition to the subprocess.Popen you see here, I’ve also tried os.system, subprocess.call, and something else that I can’t now remember. I’ve searched around on this forum, as well as Google, and have tried the various nuggets that apparently work for other people.

I’d be extremely appreciative if someone could show me what I’m doing wrong.

Thanks.

EDIT: I’m on Mac OS X 10.7.3 and ST2 2178.

0 Likes

Mac, subprocess.Popen and "Command not found"
#2

I think you should try passing your command as an array of string with all your different arguments, so in your case “rake”,“assets:compile:javascripts”]

When i’m running this kind of command I also add some option to popen to get the result :

p = subprocess.Popen(cmd_a, cwd=wd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
if p.stdout is not None :
msg = p.stdout.readlines()
print msg

0 Likes

#3

Thanks for the help, Clams. Having the output is very helpful. I now see that it’s an RVM environment issue. At present, I’m using a fully-qualified path to the proper rake executable, but the RVM environment is not getting picked up. At least now I have a diretion to go.

0 Likes

#4

Well, I got it sorted out. Ultimately, I had to source the rvm script that sets up the environment (which is all my .bashrc does … the rest of the environment setup is in .bash_profile). My final command looks like this

source ~/.bashrc && rvm 1.9.2-p290@grounect-client && rake assets:compile:javascripts

An interesting thing happened, though. While the rake task works at the command line without error, when run via the plugin in ST2, I kept getting an invalid US-ASCII byte in a javascript file. It really baffled me, but ultimately it was corrected by forcing the encoding in my rake task with:

Encoding.default_external = 'utf-8'

Thanks again, Clams. I would still be pulling my hair out if it wasn’t for your helpful suggestion.

0 Likes