/usr/share/pyshared/pywebdav/lib/davcmd.py is in python-webdav 0.9.8-7.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | """
davcmd.py
---------
containts commands like copy, move, delete for normal
resources and collections
"""
from string import split,replace,joinfields
import urlparse
from utils import create_treelist, is_prefix
from errors import *
def deltree(dc,uri,exclude={}):
""" delete a tree of resources
dc -- dataclass to use
uri -- root uri to delete
exclude -- an optional list of uri:error_code pairs which should not
be deleted.
returns dict of uri:error_code tuples from which
another method can create a multistatus xml element.
Also note that we only know Depth=infinity thus we don't have
to test for it.
"""
tlist=create_treelist(dc,uri)
result={}
for i in range(len(tlist),0,-1):
problem_uris=result.keys()
element=tlist[i-1]
# test here, if an element is a prefix of an uri which
# generated an error before.
# note that we walk here from childs to parents, thus
# we cannot delete a parent if a child made a problem.
# (see example in 8.6.2.1)
ok=1
for p in problem_uris:
if is_prefix(element,p):
ok=None
break
if not ok: continue
# here we test for the exclude list which is the other way round!
for p in exclude.keys():
if is_prefix(p,element):
ok=None
break
if not ok: continue
# now delete stuff
try:
delone(dc,element)
except DAV_Error, (ec,dd):
result[element]=ec
return result
def delone(dc,uri):
""" delete a single object """
if dc.is_collection(uri):
return dc.rmcol(uri) # should be empty
else:
return dc.rm(uri)
###
### COPY
###
# helper function
def copy(dc,src,dst):
""" only copy the element
This is just a helper method factored out from copy and
copytree. It will not handle the overwrite or depth header.
"""
# destination should have been deleted before
if dc.exists(dst):
raise DAV_Error, 412
# source should exist also
if not dc.exists(src):
raise DAV_NotFound
if dc.is_collection(src):
dc.copycol(src, dst) # an exception will be passed thru
else:
dc.copy(src, dst) # an exception will be passed thru
# the main functions
def copyone(dc,src,dst,overwrite=None):
""" copy one resource to a new destination """
if overwrite and dc.exists(dst):
delres = deltree(dc, dst)
else:
delres={}
# if we cannot delete everything, then do not copy!
if delres:
return delres
try:
copy(dc, src, dst) # pass thru exceptions
except DAV_Error, (ec, dd):
return ec
def copytree(dc,src,dst,overwrite=None):
""" copy a tree of resources to another location
dc -- dataclass to use
src -- src uri from where to copy
dst -- dst uri
overwrite -- if 1 then delete dst uri before
returns dict of uri:error_code tuples from which
another method can create a multistatus xml element.
"""
# first delete the destination resource
if overwrite and dc.exists(dst):
delres=deltree(dc,dst)
else:
delres={}
# if we cannot delete everything, then do not copy!
if delres:
return delres
# get the tree we have to copy
tlist = create_treelist(dc,src)
result = {}
# prepare destination URIs (get the prefix)
dpath = urlparse.urlparse(dst)[2]
for element in tlist:
problem_uris = result.keys()
# now URIs get longer and longer thus we have
# to test if we had a parent URI which we were not
# able to copy in problem_uris which is the prefix
# of the actual element. If it is, then we cannot
# copy this as well but do not generate another error.
ok=1
for p in problem_uris:
if is_prefix(p,element):
ok=None
break
if not ok: continue
# now create the destination URI which corresponds to
# the actual source URI. -> actual_dst
# ("subtract" the base src from the URI and prepend the
# dst prefix to it.)
esrc=replace(element,src,"")
actual_dst=dpath+esrc
# now copy stuff
try:
copy(dc,element,actual_dst)
except DAV_Error, (ec,dd):
result[element]=ec
return result
###
### MOVE
###
def moveone(dc,src,dst,overwrite=None):
""" move a single resource
This is done by first copying it and then deleting
the original.
"""
# first copy it
copyone(dc, src, dst, overwrite)
# then delete it
dc.rm(src)
def movetree(dc,src,dst,overwrite=None):
""" move a collection
This is done by first copying it and then deleting
the original.
PROBLEM: if something did not copy then we have a problem
when deleting as the original might get deleted!
"""
# first copy it
res = copytree(dc,src,dst,overwrite)
# then delete it
res = deltree(dc,src,exclude=res)
return res
|