Skip to content

Instantly share code, notes, and snippets.

@emanueleaina
Forked from insom/git-annex-ftp.py
Last active January 18, 2019 15:20
Show Gist options
  • Save emanueleaina/8139438 to your computer and use it in GitHub Desktop.
Save emanueleaina/8139438 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# git config annex.iwebftp-hook "/usr/local/bin/git-annex-ftp.py -s prefix.iweb-storage.com -u prefix-admin -p password"
# git annex initremote iwebftp type=hook hooktype=iwebftp encryption=none
from ftplib import FTP, error_perm
import os, argparse
parser = argparse.ArgumentParser(description='Combined hook for FTP remotes in git-annex')
parser.add_argument('-s', '--server', required=True, help='the FTP host name or IP address')
parser.add_argument('-u', '--user', required=True, help='the user name used to connect')
parser.add_argument('-p', '--password', required=True, help='the password used to connect')
parser.add_argument('-d', '--directory', help='the directory where to store the remote objects',
default="Annex")
args = parser.parse_args()
f = FTP(args.server, args.user, args.password)
f.set_pasv(True)
cmd = os.environ.get('ANNEX_ACTION')
prefix = args.directory
h1 = os.environ.get('ANNEX_HASH_1')
h2 = os.environ.get('ANNEX_HASH_2')
key = os.environ.get('ANNEX_KEY')
file_ = os.environ.get('ANNEX_FILE')
path = "/%s/%s" % (prefix, h1)
if cmd == 'store':
try:
f.mkd(path)
f.delete("%s/%s" % (path, key))
except:
pass
f.storbinary("STOR %s/%s" % (path, key), open(file_, 'rb'))
elif cmd == 'retrieve':
fi = open(file_, 'wb')
f.retrbinary("RETR %s/%s" % (path, key), fi.write)
elif cmd == 'remove':
try:
f.delete("%s/%s" % (path, key))
except error_perm as err:
if err.args[0][:3] != '550': # ignore missing files
raise
elif cmd == 'checkpresent':
f.voidcmd('TYPE I')
try:
if f.size("%s/%s" % (path, key)):
print key
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment