Sublime Forum

Executing a shell command with proper environment?

#1

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

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!

1 Like

#2

This is the signature of the exec command (look at \Sublime Text 2\Packages\Default\exec.py)

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.

0 Likes

#3

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

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:


    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.

0 Likes

#4

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:

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.

0 Likes

#5

Thanks for the help… got this working ok now.

0 Likes

#6

I added a status bar notification.

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

Enjoy!

0 Likes