def replaceItem(self, item, reportChanges): curitem = self.getItem(item.id) opath = os.path.join(self.engine.rootDir, curitem.name) if reportChanges: orig = adict(name=curitem.name) with open(opath, 'rb') as fp: orig.body = fp.read() npath = os.path.join(self.engine.rootDir, item.name) with open(npath, 'wb') as fp: fp.write(item.body) curitem.name = item.name curitem.inode = os.stat(npath).st_ino curitem.sha256 = hashlib.sha256(item.body).hexdigest() cspec = None if reportChanges: merger = self.mfactory.newMerger() merger.pushChange('name', orig.name, item.name) merger.pushChange('body', orig.body, item.body) cspec = merger.getChangeSpec() delattr(orig, 'body') delattr(item, 'body') if npath != opath: os.unlink(opath) log.debug('updated: %s', curitem) return cspec
def handleRequest(self): syncengine.dbsession = sessionmaker(bind=syncengine.dbengine)() # TODO: enforce authentication... # if len(sconf.users) > 0: # ... # self.assertEqual(pysyncml.Context.getAuthInfo(request, None), # adict(auth=pysyncml.NAMESPACE_AUTH_BASIC, # username='******', password='******')) # context, adapter = syncengine._makeAdapter() clen = 0 if 'Content-Length' in self.headers: clen = int(self.headers['Content-Length']) request = pysyncml.adict(headers=dict((('content-type', 'application/vnd.syncml+xml'),)), body=self.rfile.read(clen)) self.session.syncml.effectiveID = pysyncml.Context.getTargetID(request) # todo: this should be a bit more robust... urlparts = list(urlparse.urlsplit(self.session.syncml.effectiveID)) if self.path_params.get('sessionid') != self.session.id: urlparts[2] += ';sessionid=' + self.session.id self.session.syncml.returnUrl = urlparse.SplitResult(*urlparts).geturl() response = pysyncml.Response() self.stats = adapter.handleRequest(self.session.syncml, request, response) syncengine.dbsession.commit() return response
def _scanfile(self, path, name): log.debug('analyzing file "%s"...', path) with open(path,'rb') as fp: return adict( inode = os.stat(path).st_ino, name = os.path.normpath(name), sha256 = hashstream(hashlib.sha256(), fp).hexdigest(), # todo: implement lastmod... lastmod = None, )
def do_POST(self): self._parsePathParameters() log.debug('handling POST request to "%s" (parameters: %r)', self.path, self.path_params) sid = None self.session = None if 'Cookie' in self.headers: cks = Cookie.SimpleCookie(self.headers["Cookie"]) if 'sessionid' in cks: sid = cks['sessionid'].value if sid in sessions: self.session = sessions[sid] self.session.count += 1 else: sid = None if sid is None: log.debug('no valid session ID found in cookies - checking path parameters') sid = self.path_params.get('sessionid') if sid in sessions: self.session = sessions[sid] self.session.count += 1 else: sid = None if sid is None: while sid is None or sid in sessions: sid = str(uuid.uuid4()) log.debug('request without valid session ID - creating new session: %s', sid) self.session = pysyncml.adict(id=sid, count=1, syncml=pysyncml.Session()) sessions[sid] = self.session log.debug('session: id=%s, count=%d', self.session.id, self.session.count) try: response = self.handleRequest() except Exception, e: self.send_response(500) self.end_headers() self.wfile.write(traceback.format_exc()) return