Home Download Buy Blog Forum Support

[SOLVED] Running external command from plugin

[SOLVED] Running external command from plugin

Postby phillip.koebbe on Fri Feb 17, 2012 2:32 am

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.

Code: Select all
   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.
Last edited by phillip.koebbe on Fri Feb 17, 2012 2:01 pm, edited 1 time in total.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Running external command from plugin

Postby Clams on Fri Feb 17, 2012 5:09 am

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
Clams
 
Posts: 41
Joined: Fri Dec 30, 2011 7:44 am

Re: Running external command from plugin

Postby phillip.koebbe on Fri Feb 17, 2012 12:13 pm

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.
-- Phillip

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm

Re: Running external command from plugin

Postby phillip.koebbe on Fri Feb 17, 2012 2:01 pm

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

Code: Select all
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:

Code: Select all
Encoding.default_external = 'utf-8'


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

Code: Select all
while self.living? do
    mistake = Mistake.new
    self.learn_from!(mistake) unless mistake.fatal?
end
phillip.koebbe
 
Posts: 208
Joined: Tue Dec 20, 2011 4:58 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 6 guests