Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a command line client #44

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,29 @@ Running the tests:
Running the tests with WebDAV server logs:

WEBDAV_LOGS=1 nosetests --with-yanc --nologcapture --nocapture -v tests

Command Line Usage
------------------

You can invoke the command line client either by calling teh easywebdav module:

python -m easywebdav [opts] <command> [args]

or by using the entry point script that will be created when you install the library:

easywebdav [opts] <command> [args]

CLI usage is as follows:

easywebdav [opts] <command> [args]

ops:
-i disable verifing SSL (insecure)

commands:
upload <local> <remote> uploads the file or folder
list <remote> list the remote folder
ls <remote> list the remote folder

Examples:
easywebdav -i list https://username:[email protected]/path
108 changes: 107 additions & 1 deletion easywebdav/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,111 @@
from .client import *
import sys
import os
import argparse
import urlparse
from easywebdav.client import Client


def connect(*args, **kwargs):
"""connect(host, port=0, auth=None, username=None, password=None, protocol='http', path="/")"""
return Client(*args, **kwargs)


def cmd_connect(url, verify_ssl=True):
"""Connect to the remote server and return the Client object"""
parts = urlparse.urlparse(url)

return connect(parts.hostname, username=parts.username,
password=parts.password, protocol=parts.scheme,
path=parts.path, verify_ssl=verify_ssl), parts


def do_list(remote, verify_ssl):
"""Perform a listing of a folder"""
con, parts = cmd_connect(remote, verify_ssl)
for item in con.ls():
print item


def do_mkdir(remote, verify_ssl):
con, parts = cmd_connect(remote, verify_ssl)
con.mkdir('/')
print "Made", remote


def do_rmdir(remote, verify_ssl):
con, parts = cmd_connect(remote, verify_ssl)
con.rmdir('/')
print "Deleted", remote


def do_upload(local, remote, verify_ssl):
"""Upload a file or directory to a remote location"""
con, parts = cmd_connect(remote, verify_ssl)
if os.path.isfile(local):
try:
con.upload(local, os.path.basename(local))
print "Uploaded OK: " + local
except:
print "Upload failed: " + local
raise
else:
# Uploading a directory
os.path.walk(local, _do_upload_dir, (con, local))


def _do_upload_dir(args, dirname, files):
"""Callback for walking the directory tree and uploading files and folders.
This overwrites any existing files"""
con, basepath = args
relpath = os.path.relpath(dirname, basepath)
if not relpath.endswith('/'):
relpath += '/'
if not con.exists(relpath):
con.mkdirs(relpath)
for f_name in files:
f_path = os.path.join(basepath, relpath, f_name)
if not os.path.isfile(f_path):
continue
try:
con.upload(f_path, relpath + f_name)
print "Uploaded OK: " + (relpath + f_name)
except:
print "Upload failed: " + (relpath + f_name)
raise


def main():
parser = argparse.ArgumentParser()
parser.add_argument('-i', help="ignore SSL errors")
subparsers = parser.add_subparsers(dest='action')

cmd_list = subparsers.add_parser('list')
cmd_list.add_argument('url', help="remote URL")

cmd_upload = subparsers.add_parser('upload')
cmd_upload.add_argument('file', help="local file")
cmd_upload.add_argument('url', help="remote directory URL")

cmd_delete = subparsers.add_parser('delete')
cmd_delete.add_argument('url', help="remote file URL")

cmd_mkdir = subparsers.add_parser('mkdir')
cmd_mkdir.add_argument('url', help="URL to create")

cmd_rmdir = subparsers.add_parser('rmdir')
cmd_rmdir.add_argument('url', help="remote dir URL to delete")

args = parser.parse_args()

if args.action == 'list':
do_list(args.url, verify_ssl=args.i)
elif args.action == 'upload':
do_upload(args.file, args.url, verify_ssl=args.i)
elif args.action == 'mkdir':
do_mkdir(args.url, verify_ssl=args.i)
elif args.action == 'rmdir':
do_rmdir(args.url, verify_ssl=args.i)
return 0

if __name__ == '__main__':
sys.exit(main())
6 changes: 6 additions & 0 deletions easywebdav/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import sys
import easywebdav


if __name__ == '__main__':
sys.exit(easywebdav.main())
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"requests",
],
entry_points=dict(
console_scripts=[],
console_scripts=['easywebdav = easywebdav:main'],
),
)

Expand All @@ -38,4 +38,4 @@
"PyWebDAV",
))

setup(**properties)
setup(**properties)