Sublime Forum

Remote Python Build System

#1

I trying to run python3 that is on my raspberry pi. I am currently using rsub and can edit the files on my pi with no problems. I would like to run the remote files with the python on the pi vs the system python on the host machine. Is a remote python build system possible?

Jerry

0 Likes

#2

It would basically mean to start an ssh session to start the remote python with. As a windows user I use putty’s plink.exe to remotely run commands this way from within local running build scripts. Not sure how flexible such a build system needs to be for your use-cases. The most difficult task might be the path translation from how a file is represented within ST and how it would look like on the remote machine. Maybe this would need a wrapper around the default build-target (exec.py) in ST.

A simple but less flexible solution might be to place some general build scripts on your pi, which do not need path/file names and run them via plink or ssh.

Example

plink -pw MYPASSWORD MyPIhost "python -u ~/project/make.py"

You can also use key files instead of passwords to connect to the remote.

0 Likes

#3

Thanks. Your comment sure is helpful and I seem to be getting closer. I’m on a mac and as you pointed out the difficult task ahead is getting ST to see the path to the remote python file.

This is the python build system I created as python-pi:

Given that MyKey is the password key created and me@MyPiAddress is the log in I use with ssh.

{
“cmd”: [“ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python”, “$file”],
“selector”: “source.python”,
“file_regex”: “^\sFile "(…?)”, line ([0-9]*)"
}

And the error I get when running the above build system:

[Errno 2] No such file or directory: ‘ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python’
[cmd: [‘ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python’, ‘/Volumes/Home Directory/jerryweather.py’]]
[dir: /Volumes/Home Directory]

ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python works just fine on host machine so that checks out.

I feel I’m hung up on how ST is interpreting it.

Jerry

0 Likes

#4

If you use cmd to specify the command to run, then it has to be a list of command line arguments, and the first item in the list is the program that’s going to be executed. So essentially your problem is that -i and ~/.ssh/stuff need to be in separate arguments, or the program that’s being executed is ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python, which doesn’t exist and is why you get that error. Something like this would work better, I think:

"cmd": ["ssh", "-i",  "~/.ssh/MyKey me@MyPiAddress /usr/bin/python", "$file"],

Alternatively you can also use shell_cmd, which is a single string and represents an entire command line passed directly to the shell. In that case it’s the shell that’s interpreting the arguments, so you also need to guard against there being spaces in your $file argument by quoting that argument, but otherwise the arrangement is more “natural” (except that since double quotes are special in JSON, you need to quote them):

"shell_cmd": "ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python \"$file\"",
0 Likes

#5

Using:

“shell_cmd”: “ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python “$file””,

Almost works but it returns:

/usr/bin/python: can’t open file ‘/Volumes/Home’: [Errno 2] No such file or directory
[Finished in 0.3s with exit code 2]
[shell_cmd: ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python “/Volumes/Home Directory/jerryweather.py”]
[dir: /Volumes/Home Directory]

the file jerryweather.py is in the home directory on the pi and ST sees its location differently.

As an alternative if I invoke ST from the remote pi side via sudo rsub jerryweather.py I get a variant of the same message as follows:

/usr/bin/python: can’t open file ‘/var/folders/80/mm2vn_4964n6bh_pp9z85_d00000gn/T/rsub-lz309c/jerryweather.py’: [Errno 2] No such file or directory
[Finished in 0.3s with exit code 2]
[shell_cmd: ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python “/var/folders/80/mm2vn_4964n6bh_pp9z85_d00000gn/T/rsub-lz309c/jerryweather.py”]

In either case the file path that I’m wanting to execute remotely on the pi’s python is ST’s version of things and thus the “can’t open file” … message.

Which brings me to wonder if an edit to this line is in order:

“file_regex”: “^\s*File “(…?)”, line ([0-9])”

I sure appreciate the help.

Jerry

0 Likes

#6

The file_regex is only used to identify which lines are error messages for the purposes of showing inline errors and navigating through build results (e.g. jumping through error or warning points).

In your case I imagine the problem is that ~ expands to your home directory, and on your Mac that’s a different folder name than on the remote end. Thus the command that’s executed on the pi is getting the Mac path and can’t find things.

Something to try would be to modify your build a bit and pass just the name of the file to run instead of the full path:

    "shell_cmd": "ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python \"$file_name\"",

Now the command would look like ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python "jerryweather.py"; no path.

If the file on the remote end is in your home directory, then this should work out of the box I think; the ssh command should default to your home directory on the remote end. If not you can either try to modify the command to first cd into the appropriate place or, if you know the appropriate place, add that as an absolute path in front of the $file_name portion.

0 Likes

#7

Thanks - That worked!!
Using either:

“shell_cmd”: “ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python “$file_name””,

or

“shell_cmd”: “ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python jerryweather.py”,

since it is in the root directory on the pi.

The only thing that would make this ideal is to not have to make a separate build for each path file I might use on the remote machine.

The concept being the break the file name into to parts:
One being the path to the file name ($jerrys_path_to_pi_filename) and the other being the actual file stored in $file_name with the final build looking like this:

“shell_cmd”: "ssh -i ~/.ssh/MyKey me@MyPiAddress /usr/bin/python $jerrys_path_to_pi_filename/$file_name ,

Since ST sees the file as “/Volumes/Home Directory/jerryweather.py” the only thing I need to do ( I assume) is to replace /Volumes/Home Directory with $jerrys_path_to_pi_filename.

It seems the better solution might be to just strip /Volumes/Home Directory from $file.

I can make a python script to do that but not sure how this works and how I would pass that back to $file or a user defined variable.

Again, thanks for help.

Jerry

0 Likes

#8

It’s a bit of an advanced topic (in that it requires a plugin), but you can create your own custom build target to implement your own variables in a build system. There are some examples here on how to do that, though I just realized that they’re not up to date in that you can’t cancel them while they’re running, so I should probably fix that. In the general case that’s not really an issue, though.

In the example on custom build variables, it assumes that they’re going to be sourced from some outside location; the code has them inlined but it’s trivial to pull it out of a config variable and have it work per project or something along those lines.

0 Likes