Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check If Path Exists Using Fabric2.x

I am using Fabric2 version and I don't see It has exist method in it to check if folder path has existed in the remote server. Please let me know how can I achieve this in Fabric 2 http://docs.fabfile.org/en/stable/.

I have seen a similar question Check If Path Exists Using Fabric, But this is for fabric 1.x version

like image 534
Reddi Mohan Avatar asked Nov 23 '18 08:11

Reddi Mohan


3 Answers

You can execute the test command remotely with the -d option to test if the file exist and is a directory while passing the warn parameter to the run method so the execution doesn't stop in case of a non-zero exit status code. Then the value failed on the result will be True in case that the folder doesn't exist and False otherwise.

folder = '/path/to/folder'
if c.run('test -d {}'.format(folder), warn=True).failed:
    # Folder doesn't exist
    c.run('mkdir {}'.format(folder))
like image 51
Lester M Avatar answered Oct 02 '22 00:10

Lester M


exists method from fabric.contrib.files was moved to patchwork.files with a small signature change, so you can use it like this:

from fabric2 import Connection
from patchwork.files import exists

conn = Connection('host')
if exists(conn, SOME_REMOTE_DIR):
   do_something()
like image 39
MikeL Avatar answered Oct 01 '22 23:10

MikeL


The below code is to check the existence of the file (-f), just change to '-d' to check the existence of a directory.

from fabric import Connection
c = Connection(host="host")
if c.run('test -f /opt/mydata/myfile', warn=True).failed:
   do.thing()

You can find it in the Fabric 2 documentation below:
https://docs.fabfile.org/en/2.5/getting-started.html?highlight=failed#bringing-it-all-together

like image 29
DagaReiN Avatar answered Oct 01 '22 22:10

DagaReiN