2007-08-17 20:23:16 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2007-10-11 08:34:44 +00:00
|
|
|
import re, socket, urllib
|
2007-08-17 20:23:16 +00:00
|
|
|
|
|
|
|
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
|
|
|
|
|
2007-10-12 02:20:41 +00:00
|
|
|
def rm(nodeurl, root_uri, vdrive_pathname, verbosity, stdout, stderr):
|
2007-08-17 20:23:16 +00:00
|
|
|
"""
|
|
|
|
@param verbosity: 0, 1, or 2, meaning quiet, verbose, or very verbose
|
|
|
|
|
|
|
|
@return: a Deferred which eventually fires with the exit code
|
|
|
|
"""
|
|
|
|
mo = NODEURL_RE.match(nodeurl)
|
|
|
|
host = mo.group(1)
|
|
|
|
port = int(mo.group(3))
|
|
|
|
|
2007-10-11 08:34:44 +00:00
|
|
|
url = "/uri/%s/" % urllib.quote(root_uri.replace("/","!"))
|
2007-08-17 20:23:16 +00:00
|
|
|
if vdrive_pathname:
|
|
|
|
url += vdrive_pathname
|
|
|
|
|
|
|
|
so = socket.socket()
|
|
|
|
so.connect((host, port,))
|
|
|
|
|
|
|
|
CHUNKSIZE=2**16
|
|
|
|
data = "DELETE %s HTTP/1.1\r\nConnection: close\r\nHostname: %s\r\n\r\n" % (url, host,)
|
|
|
|
sent = so.send(data)
|
|
|
|
|
|
|
|
respbuf = []
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
while data:
|
|
|
|
respbuf.append(data)
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
|
|
|
|
so.shutdown(socket.SHUT_WR)
|
|
|
|
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
while data:
|
|
|
|
respbuf.append(data)
|
|
|
|
data = so.recv(CHUNKSIZE)
|
|
|
|
|
|
|
|
respstr = ''.join(respbuf)
|
|
|
|
|
|
|
|
headerend = respstr.find('\r\n\r\n')
|
|
|
|
if headerend == -1:
|
|
|
|
headerend = len(respstr)
|
|
|
|
header = respstr[:headerend]
|
|
|
|
RESP_RE=re.compile("^HTTP/[0-9]\.[0-9] ([0-9]*) *([A-Za-z_ ]*)") # This regex is soooo ad hoc... --Zooko 2007-08-16
|
|
|
|
mo = RESP_RE.match(header)
|
|
|
|
if mo:
|
|
|
|
code = int(mo.group(1))
|
|
|
|
word = mo.group(2)
|
|
|
|
|
|
|
|
if code == 200:
|
2007-10-12 02:20:41 +00:00
|
|
|
print >>stdout, "%s %s" % (code, word,)
|
2007-08-17 20:23:16 +00:00
|
|
|
return 0
|
|
|
|
|
2007-10-12 02:20:41 +00:00
|
|
|
print >>stderr, respstr[headerend:]
|
2007-08-17 20:23:16 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
def main():
|
|
|
|
import optparse, re
|
|
|
|
parser = optparse.OptionParser()
|
|
|
|
parser.add_option("-u", "--node-url", dest="nodeurl")
|
2007-10-11 07:30:36 +00:00
|
|
|
parser.add_option("-r", "--root-uri", dest="rooturi")
|
2007-08-17 20:23:16 +00:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
NODEURL_RE=re.compile("http://([^:]*)(:([1-9][0-9]*))?")
|
|
|
|
if not isinstance(options.nodeurl, basestring) or not NODEURL_RE.match(options.nodeurl):
|
|
|
|
raise ValueError("--node-url is required to be a string and look like \"http://HOSTNAMEORADDR:PORT\", not: %r" % (options.nodeurl,))
|
2007-10-11 07:30:36 +00:00
|
|
|
|
|
|
|
if not options.rooturi:
|
|
|
|
raise ValueError("must provide --root-uri")
|
2007-08-17 20:23:16 +00:00
|
|
|
|
|
|
|
vdrive_pathname = args[0]
|
|
|
|
|
2007-10-11 07:30:36 +00:00
|
|
|
return rm(options.nodeurl, options.rooturi, vdrive_pathname, 0)
|
2007-08-17 20:23:16 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|