我編碼多個服務器維護模塊使用python結構,我遇到了一個問題,我需要在隨機服務器上創建一個文件夾,這可以是Linux服務器或Windows,所以這些機器上的命令是不同的。如何使用fabric在任意主機,多平臺,Linux和Windows上創建文件夾/目錄?
但是這可以很容易地使用本地計算機上完成蟒,像
if not os.path.exists(MyDir):
os.makedirs(MyDir)
但如何做任意平臺上使用衣物一些事情嗎?
我的想法是這樣一個,
class FabricSupport:
def __init__(self):
pass
def run(self, host = 'localhost', port = '22', command = None):
if host != 'localhost':
env.host_string = "%s:%s" % (host, port)
with hide('output','running','warnings'):
return run(command, shell=False)
else:
with hide('running'):
return local(command)
所以,我希望這個類可以有一個函數來創建一個文件夾。 然後我發現我會遇到一個問題,使它在不同的平臺上工作。
def createdir(self, targetdir, host = 'localhost', port = '22'):
if host != 'localhost':
env.host_string = "%s:%s" % (host, port)
with hide('output','running','warnings'):
if not exists(targetdir):
return run('mkdir %s' % targetdir, shell=False)
else:
with hide('running'):
return local('mkdir %s' % targetdir)
我發現fabric.contrib.files.exists可以檢查一個文件夾是否存在,但如何創建一個? 如果'mkdir'不起作用怎麼辦?
更新解決方案:
感謝user865368,我得到了一個完整的解決方案在這裏。這可能看起來很愚蠢,但我沒有看到更好的。
class FabricRunReturnSimulator:
def __init__(self):
self.info = None
def __str__(self):
return "%s" % (self.info)
pass
def createdir(self, targetdir, host = 'localhost', port = '22'):
"""
Create a input target dir on local or remote.
targetdir can be a single string or a list (the sequence of the elements must follow the intended path)
return code 2: makedir runs into trouble, maybe permission problem
return code 3: target directory exists
return code 0: runs normal
"""
if host != 'localhost':
env.host_string = "%s:%s" % (host, port)
with hide('output','running','warnings'), settings(warn_only=True, shell=False):
if not isinstance(targetdir, list):
return run('''python -c "import os\nif not os.path.exists('%s'):\ntry:\n os.makedirs('%s')\nexcept: exit(2)" ''' % (targetdir, targetdir), capture=True)
else:
targetdirpass = repr(targetdir)
return run('''python -c "import os\ntd = os.path.join(*%s)\nif not os.path.exists(td):\ntry:\n os.makedirs(td)\nexcept: exit(2)" ''' % (targetdirpass), capture=True)
else:
simulatereturn = FabricRunReturnSimulator()
if not isinstance(targetdir, list):
if not os.path.exists(targetdir):
try:
os.makedirs('%s' % targetdir)
simulatereturn.return_code = 0
simulatereturn.failed = False
simulatereturn.succeeded = True
simulatereturn.info = ''.join((targetdir , " Folder Created."))
except:
simulatereturn.return_code = 2
simulatereturn.failed = True
simulatereturn.succeeded = False
simulatereturn.info = ''.join(("Unable to create folder ",targetdir))
else:
simulatereturn.return_code = 3
simulatereturn.failed = True
simulatereturn.succeeded = False
simulatereturn.info = ''.join((targetdir , " already exists."))
else:
td = os.path.join(*targetdir)
if not os.path.exists(td):
try:
os.makedirs('%s' % td)
simulatereturn.return_code = 0
simulatereturn.failed = False
simulatereturn.succeeded = True
simulatereturn.info = ''.join((td , " Folder Created."))
except:
simulatereturn.return_code = 2
simulatereturn.failed = True
simulatereturn.succeeded = False
simulatereturn.info = ''.join(("Unable to create folder " , td))
else:
simulatereturn.return_code = 3
simulatereturn.failed = True
simulatereturn.succeeded = False
simulatereturn.info = ''.join((td , " already exists."))
return simulatereturn
Linux在Windows使用'md'創建目錄時使用'mkdir',這是否意味着我必須處理不同的工廠? –
看看[這種方式](http://django-fab-deploy.readthedocs.org/en/latest/_modules/fab_deploy/utils.html)來檢測平臺。 – alecxe
@alecxe,該方法仍然需要遠程運行python,所以我想下面的答案比使用python –