Home Download Buy Blog Forum Support

executing a shell command with proper environment?

executing a shell command with proper environment?

Postby coldnebo on Tue Jul 05, 2011 3:02 pm

Hi, I've been trying to get a simple perforce edit command working... this is what I have so far:

Code: Select all
import sublime, sublime_plugin

class P4EditCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      self.view.window().run_command('exec', {'cmd': ['p4', 'edit', self.view.file_name()]})

However, when I run it on a file (bound to F8) it simply returns:
Client 'foo' unknown - use 'client' command to create it.
[Finished]

In this case, "foo" is my hostname. The environment in the script isn't working the way it works on the command line under linux. What's wrong? Or more importantly, how can I debug what's wrong?

Thanks!
coldnebo
 
Posts: 10
Joined: Fri Jul 01, 2011 9:11 pm

Re: executing a shell command with proper environment?

Postby bizoo on Tue Jul 05, 2011 3:13 pm

This is the signature of the exec command (look at \Sublime Text 2\Packages\Default\exec.py)
Code: Select all
    def run(self, cmd = [], file_regex = "", line_regex = "", working_dir = "",
            encoding = "utf-8", env = {}, quiet = False, kill = False,
            # Catches "path" and "shell"
            **kwargs):

If I was you, I first try to set the working_dir argument and/or the shell argument (set it to true).
If I remember well, the executed command is printed in the console.

Good luck.
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: executing a shell command with proper environment?

Postby coldnebo on Tue Jul 05, 2011 3:37 pm

Ok, I tried adding those options... not sure if I did it right,

Code: Select all
import sublime, sublime_plugin, os

class P4EditCommand(sublime_plugin.TextCommand):
   def run(self, edit):
      if self.view.file_name():
         folder_name, file_name = os.path.split(self.view.file_name())

      self.view.window().run_command('exec', {'cmd': ['p4', 'edit', file_name], 'working_dir':folder_name, 'shell':True} )


All it outputs now is:
Code: Select all

    Perforce -- the Fast Software Configuration Management System.

    p4 is Perforce's client tool for the command line.  Try:

   p4 help simple        list most common commands
   p4 help commands      list all commands
   p4 help command       help on a specific command

   p4 help charset       help on character set translation
   p4 help configurables list server configuration variables
   p4 help environment   list environment and registry variables
   p4 help filetypes     list supported file types
   p4 help jobview       help on jobview syntax
   p4 help revisions     help on specifying file revisions
   p4 help usage         generic command line arguments
   p4 help views         help on view syntax

    The full user manual is available at http://www.perforce.com/manual.

    Server 2010.2/322263.

[Finished]


I've checked the values of the variables and this seems to work from the command line just fine. BTW, is there an easy way to print debug text to the python console from inside sublime? I'm sure this is a simple syntax thing, but I'm having trouble finding examples of anything similar.
coldnebo
 
Posts: 10
Joined: Fri Jul 01, 2011 9:11 pm

Re: executing a shell command with proper environment?

Postby bizoo on Tue Jul 05, 2011 4:53 pm

What you see is the exec output buffer, to show the console go to menu View -> Show Console
To print something in the console from Python plugin:
Code: Select all
print "My text:", file_name

Doesn't know Perforce client so difficult to help you, look like something wrong with the arguments.

You could try to make a Build file (http://sublimetext.info/docs/en/core/build_systems.html) maybe you will have better luck.
The Build system use the exec command, so if your Build file work, there must be an exec command that work the same.

Good luck.
bizoo
 
Posts: 889
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: executing a shell command with proper environment?

Postby coldnebo on Tue Jul 05, 2011 7:12 pm

Thanks for the help... got this working ok now.
Last edited by coldnebo on Tue Jul 05, 2011 9:01 pm, edited 1 time in total.
coldnebo
 
Posts: 10
Joined: Fri Jul 01, 2011 9:11 pm

Re: executing a shell command with proper environment?

Postby coldnebo on Tue Jul 05, 2011 8:10 pm

I added a status bar notification.

I put everything in a gist here: https://gist.github.com/1065808

Enjoy!
coldnebo
 
Posts: 10
Joined: Fri Jul 01, 2011 9:11 pm


Return to Plugin Development

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests