# $Id: SkyProjectFileManager.py,v 1.1 2003/08/19 12:01:56 helge Exp $ import sys,base64,getpass,httplib,string from pprint import pprint class SkyProjectFolderDataSource: def __init__(self, fileManager, path): self.fm = fileManager self.path = path def fetchObjects(self): results = [] onlyNames = "\n" + \ "\n\n" +\ "\n"; if (len(self.path) == 0): print "Wrong path"; return None; http = self.fm._http('PROPFIND', self.path); http.putheader('Authorization', self.fm.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(onlyNames))); http.endheaders(); http.send(onlyNames); errcode, errmsg, headers = http.getreply(); if (errcode != 207): print "got error: ", errcode, " text ", errmsg, " header " , headers; return None; f = http.getfile(); data = f.read(); f.close(); data = self.fm._removeXMLHeader(data); if data == None: print "got unexpected data"; return None; lines = string.split(data, "\n") inResponse = 0 inPropStat = 0 inProp = 0 attrs = {} href = "" for line in lines: if line[:12] == " 0: if href != self.path: intKeys = ( "NSFileSize", "SkyOwnerId", "projectId" ) for ik in intKeys: if attrs.has_key(ik): attrs[ik] = int(attrs[ik]) results.append(attrs) attrs = {} continue if line[:13] == "": inPropStat = 0 inProp = 0 continue if line[:9] == "": inProp = 0 #pprint(attrs) continue if line[:11] == "": tstart = string.find(line, ">") tfin = string.rfind(line, "") propName = line[tstart + 1:tfin] tstart = tfin + 1 tfin = string.rfind(line, "'); attrs[propName] = propValue #for return results class SkyProjectFileManager: """ SkyProjectFileManager A SkyProjectFileManager allows to connect to a skyprojectd using HTTP. To access a skyprojectd, you need to know the host it runs on, the port it runs on and a login/pwd to log into the server. Setup: fm = SkyProjectFileManager('donald', 'xxxx', 'test.skyrix.com', 10000) Note that the connection isn't setup immediatly and therefore the parameters are not checked immediatly ! """ def __init__(self, login=None, pwd=None, host='localhost', port=15056): """ SkyProjectFileManager([login],[pwd],[host],[port] If any of the parameters isn't provided, it will be asked for. """ if login is None: sys.stdout.write("SKYRiX %s:%i login: " % ( host, port )) sys.stdout.flush() login = sys.stdin.readline() if len(login) > 0: login = login[:-1] # cut off newline if pwd is None: pwd = getpass.getpass("Password: ") self.host = host; self.port = int(port); authText = login + ':' + pwd; authText = base64.encodestring(authText); al = len(authText); if authText[al - 1] == '\12': authText = authText[0:-1]; authText = 'Basic ' + authText; self.auth = authText; def _http(self, _method, _path): http = httplib.HTTP(self.host, self.port); http.putrequest(_method, _path); http.putheader('User-Agent', 'SKYRiX-FileManager'); return http; def _removeXMLHeader(self, _s): xmlStart = "\n" + \ "\n"; if string.find(_s, xmlStart) == 0: return _s[len(xmlStart):len(_s)]; return None; def _fileAttrsDictFromXML(self, _s): lines = string.split(_s, "\n") inResponse = 0 inPropStat = 0 inProp = 0 attrs = {} href = "" hrefToAttrs= {} for line in lines: if line[:12] == " 0: hrefToAttrs[href] = attrs attrs = {} continue if line[:13] == "": inPropStat = 0 inProp = 0 continue if line[:9] == "": inProp = 0 pprint(attrs) continue if line[:11] == "": tstart = string.find(line, ">") tfin = string.rfind(line, "") propName = line[tstart + 1:tfin] tstart = tfin + 1 tfin = string.rfind(line, "'); attrs[propName] = propValue #for rest = "" return ( rest, attrs ) def _OLD_fileAttrsDictFromXML(self, _s): isOk = 1; xmlStart = "\n\n" + \ "\n"; xmlEnd = "\nHTTP/1.1 200 OK\n" \ "\n\n"; keyBStart = "'); dict[key] = s; if isOk == 0: return (None, None); _s = _s[len(xmlEnd):len(_s)]; return (_s, dict); def contentsAtPath(self, _path): """ contentsAtPath(path) Returns the contents of the file at 'path' as a Python string object. Sample: s = fm.contentsAtPath('/test.txt') """ if (len(_path) == 0): print "wrong path"; return None; http = self._http('GET', _path); http.putheader('Accept', '*/*'); http.putheader('Authorization', self.auth); http.endheaders(); errcode, errmsg, headers = http.getreply(); if (errcode != 200): print "got error: ", errcode, " text ", errmsg, " header " , headers; return None; f = http.getfile(); data = f.read(); f.close(); return data; def writeContentsAtPath(self, _content, _path): """ writeContentsAtPath(content, path) Write the Python String 'content' to the file at 'path'. Returns 0 on error and 1 on success. Sample: if not fm.writeContentsAtPath('Hello World !', '/test.txt'): print 'write failed ...' """ if (len(_path) == 0): print "Wrong path"; return 0; lenStr = str(len(_content)); http = self._http('PUT', _path); http.putheader('Authorization', self.auth); http.putheader('content-length', lenStr); http.endheaders(); http.send(_content); errcode, errmsg, headers = http.getreply(); if (errcode != 204 and errcode != 201): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; return 1; def attributesAtPath(self, _path): """ attributesAtPath(path) Returns as a Python dictionary the file attributes of the file at path. Returns 0 on error. Sample: dict = fm.attributesAtPath('/test.txt') print dict['color'] """ onlyAttrs = "\n" + \ "\n\n" +\ "\n"; if (len(_path) == 0): print "Wrong path"; return 0; http = self._http('PROPFIND', _path); http.putheader('Authorization', self.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(onlyAttrs))); http.endheaders(); http.send(onlyAttrs); errcode, errmsg, headers = http.getreply(); if (errcode != 207): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; f = http.getfile(); data = f.read(); f.close(); data = self._removeXMLHeader(data); if data == None: print "got unexpected data"; return None; lines = string.split(data, "\n") inResponse = 0 inPropStat = 0 inProp = 0 attrs = {} href = "" for line in lines: if line[:12] == " 0: break attrs = {} continue if line[:13] == "": inPropStat = 0 inProp = 0 continue if line[:9] == "": inProp = 0 pprint(attrs) continue if line[:11] == "": tstart = string.find(line, ">") tfin = string.rfind(line, "") propName = line[tstart + 1:tfin] tstart = tfin + 1 tfin = string.rfind(line, "'); attrs[propName] = propValue #for return attrs def writeAttributesAtPath(self, _path, _attrs): attrsBegin = "\n" + \ "\n \n"; attrsEnd = ""; keys = _attrs.keys() attrs = attrsBegin; for key in keys: attrs = attrs + ""; s = str(_attrs[key]); s = string.replace(s, '&', '&'); s = string.replace(s, '<', '<'); s = string.replace(s, '>', '>'); attrs = attrs + s; attrs = attrs + "\n"; attrs = attrs + attrsEnd; if (len(_path) == 0): print "Wrong path"; return 0; http = self._http('PROPPATCH', _path); http.putheader('Authorization', self.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(attrs))); http.endheaders(); http.send(attrs); errcode, errmsg, headers = http.getreply(); if (errcode != 200): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; return 1; def deleteAttributesAtPath(self, _path, _attrs): attrsBegin = "\n" + \ "\n \n"; attrsEnd = ""; keys = _attrs attrs = attrsBegin; for key in keys: attrs = attrs + ""; attrs = attrs + "\n"; attrs = attrs + attrsEnd; if (len(_path) == 0): print "Wrong path"; return 0; http = self._http('PROPPATCH', _path); http.putheader('Authorization', self.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(attrs))); http.endheaders(); http.send(attrs); errcode, errmsg, headers = http.getreply(); if (errcode != 200): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; return 1; def directoryContentsAtPath(self, _path): """ directoryContentsAtPath(path) Returns the contents of the directory at 'path' as a Python sequence. Sample: for p in fm.directoryContentsAtPath('/'): print p """ onlyNames = "\n" + \ "\n\n" +\ "\n"; if (len(_path) == 0): print "Wrong path"; return 0; http = self._http('PROPFIND', _path); http.putheader('Authorization', self.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(onlyNames))); http.endheaders(); http.send(onlyNames); errcode, errmsg, headers = http.getreply(); if (errcode != 204 and errcode != 201 and errcode != 207): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; if (errcode == 207): f = http.getfile(); data = f.read(); f.close(); data = self._removeXMLHeader(data); if data == None: print "got unexpected data"; return None; array = []; lines = string.split(data, "\n") for line in lines: if line[:8] == "": tstart = string.find(line, ">") tfin = string.rfind(line, "\n" + \ "\n\n" +\ "\n"; if (len(_path) == 0): print "Wrong path"; return 0; http = self._http('PROPFIND', _path); http.putheader('Authorization', self.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(xmlContent))); http.endheaders(); http.send(xmlContent); errcode, errmsg, headers = http.getreply(); if (errcode != 207 and errcode != 404): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; if (errcode == 404): return 0; return 1; def fileExistsAtPathIsDirectory(self, _path): """ fileExistsAtPathIsDirectory(path) Checks whether a directory exists at path. Returns 1 if it does and 0 if no directory exists at the specified path. """ xmlContent = "\n" + \ "\n\n" +\ "\n"; if (len(_path) == 0): print "Wrong path"; return 0; http = self._http('PROPFIND', _path); http.putheader('Authorization', self.auth); http.putheader('Accept', '*/*'); http.putheader('content-length', str(len(xmlContent))); http.endheaders(); http.send(xmlContent); errcode, errmsg, headers = http.getreply(); if (errcode != 207 and errcode != 404): print "got error: ", errcode, " text ", errmsg, " header " , headers; return 0; if (errcode == 404): return 0; if (errcode == 207): f = http.getfile(); data = f.read(); f.close(); data = self._removeXMLHeader(data); if data == None: print "got unexpected data"; return None; lines = string.split(data, "\n") for line in lines: if line[:14] != "": continue line = line[14:] tfin = string.find(line, "