Commit a1fb5cbf authored by jacquemier's avatar jacquemier
Browse files

All commands accepted

parent 783e8e78
......@@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
......@@ -11,32 +11,89 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"webdav = webdavacc.connect('lapp-xdc01.in2p3.fr', 'webdavuser', 'webdavuser', protocol='http', port=4)"
"webdav = webdavacc.connect('lapp-xdc01.in2p3.fr', 'webdavuser', 'webdavuser', protocol='http')"
]
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"result = webdav.ls(\"/test%20manual/Jean\")"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"file_res=result[0]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/test%20manual/Jean/'"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"file_res.name"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[File(name='/test%20manual/Jean/', size=0, mtime='Fri, 25 Oct 2019 11:54:40 GMT', ctime='2019-10-25T11:54:40Z', contenttype='httpd/unix-directory')]"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Connexion error\n"
]
"data": {
"text/plain": [
"File(name='/test%20manual/Jean/', size=0, mtime='Fri, 25 Oct 2019 11:54:40 GMT', ctime='2019-10-25T11:54:40Z', contenttype='httpd/unix-directory')"
]
},
"execution_count": 53,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"try: \n",
" webdav.ls()\n",
" print('Connexion success')\n",
"except:\n",
" print('Connexion error')"
"result[0]"
]
},
{
......
import easywebdav
import textwrap
import argparse
'''
cd(path)
ls(path=None)
exists(remote_path)
mkdir(path, safe=False)
mkdirs(path)
rmdir(path, safe=False)
delete(file_path)
upload(local_path_or_fileobj, remote_path)
download(remote_path, local_path_or_fileobj)
black = lambda text: '\033[0;30m' + text + '\033[0m'
red = lambda text: '\033[0;31m' + text + '\033[0m'
green = lambda text: '\033[0;32m' + text + '\033[0m'
......@@ -82,15 +72,91 @@ def connect(server, username, password, port =80, protocol='https'):
raise Exception
def execute(webdav, command, arguments):
'''
WebDav command execution.
Parameters
----------
webdav: easywebdav.client.Client
webdav connected instance
command : string
command to execute.
arguments : string
command arguments
Returns
-------
command result
Raises
------
Exception
If command failed or does not exist
'''
available_command = ('cd', 'ls', 'exists', 'mkdir', 'mkdirs', 'rmdir',
'delete', 'upload', 'download')
#if command not in available_command:
find = False
for cmd in available_command:
if command.find(cmd) == 0:
find = True
if not find:
raise Exception("command {} not available".format(command))
if not arguments:
arguments = ''
if command == 'cd':
return webdav.cd(arguments)
elif command == 'ls':
return webdav.ls(arguments)
elif command == 'exists':
return webdav.exists(arguments)
elif command == 'mkdir':
return webdav.mkdir(arguments)
elif command == 'mkdirs':
return webdav.mkdirs(arguments)
elif command == 'rmdir':
return webdav.rmdir(arguments)
elif command == 'delete':
return webdav.delete(arguments)
elif command == 'upload':
return webdav.upload(arguments)
elif command == 'download':
return webdav.download(arguments)
def main():
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(formatter_class = argparse.RawDescriptionHelpFormatter,
epilog = textwrap.dedent('''\
Command arguments:
cd -a path
ls -a path=None
exists -a remote_path
mkdir -a path
mkdirs -a path
rmdir -a path
delete -a file_path
upload -a "local_path_or_fileobj remote_path"
download -a "remote_path, local_path_or_fileobj"
'''))
parser.add_argument("server")
parser.add_argument("--verbose", help="increase output verbosity",
parser.add_argument("-v", "--verbose", help="increase output verbosity",
action="store_true")
parser.add_argument("-u", "--username", help="username", required=True)
parser.add_argument("-p", "--password", help="password", required=True)
parser.add_argument("-a", "--arguments", help="protocol", required=False)
parser.add_argument("-t", "--port", help="username", required=False)
parser.add_argument("-c", "--protocol", help="protocol", required=False)
parser.add_argument("-r", "--protocol", help="protocol", required=False)
parser.add_argument("-c", "--command", help="protocol", required=True)
args = parser.parse_args()
......@@ -109,6 +175,9 @@ def main():
'protocol[{' \
'}] failed'.format(args.server, args.username, args.protocol))
result = execute(webdav, args.command, args.arguments)
log(LogLevel.INFO, str(result))
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment