def full_url(self, path, safe=None): """ Determines if the given path is media or content based on the configuration and returns the appropriate url. The return value is url encoded. """ if parse.urlparse(path)[:2] != ("", ""): return path if self.is_media(path): relative_path = File(path).get_relative_path( Folder(self.config.media_root)) return self.media_url(relative_path, safe) else: return self.content_url(path, safe)
def translate_path(self, path): """ Finds the absolute path of the requested file by referring to the `site` variable in the server. """ site = self.server.site path = unquote(self.path) if not PY3: path = path.decode('utf-8') result = parse.urlparse(path) logger.debug( "Trying to load file based on request: [%s]" % result.path) path = result.path.lstrip('/') res = None if path.strip() == "" or File(path).kind.strip() == "": deployed = site.config.deploy_root_path.child(path) deployed = Folder.file_or_folder(deployed) if isinstance(deployed, Folder): node = site.content.node_from_relative_path(path) res = node.get_resource('index.html') elif hasattr(site.config, 'urlcleaner') and hasattr( site.config.urlcleaner, 'strip_extensions'): for ext in site.config.urlcleaner.strip_extensions: res = site.content.resource_from_relative_deploy_path( path + '.' + ext) if res: break for ext in site.config.urlcleaner.strip_extensions: new_path = site.config.deploy_root_path.child( path + '.' + ext) if File(new_path).exists: return new_path else: res = site.content.resource_from_relative_deploy_path(path) if not res: logger.error("Cannot load file: [%s]" % path) return site.config.deploy_root_path.child(path) else: self.server.generate_resource(res) new_path = site.config.deploy_root_path.child( res.relative_deploy_path) return new_path
def do_GET(self): """ Identify the requested path. If the query string contains `refresh`, regenerat the entire site. Otherwise, regenerate only the requested resource and serve. """ self.server.request_time = datetime.now() logger.debug("Processing request: [%s]" % self.path) result = parse.urlparse(self.path) query = parse.parse_qs(result.query) if 'refresh' in query or result.query == 'refresh': self.server.regenerate() if 'refresh' in query: del query['refresh'] parts = list(tuple(result)) parts[4] = urllib.urlencode(query) parts = tuple(parts) new_url = parse.urlunparse(parts) logger.info('Redirecting... [%s]' % new_url) self.redirect(new_url) else: SimpleHTTPRequestHandler.do_GET(self)
def publish(self): super(PyPI, self).publish() tf = tempfile.TemporaryFile() try: # Bundle it up into a zipfile logger.info("building the zipfile") root = self.site.config.deploy_root_path zf = zipfile.ZipFile(tf, "w", zipfile.ZIP_DEFLATED) try: for item in root.walker.walk_files(): logger.info(" adding file: %s", item.path) zf.write(item.path, item.get_relative_path(root)) finally: zf.close() # Formulate the necessary bits for the HTTP POST. # Multipart/form-data encoding. Yuck. authz = self.username + ":" + self.password authz = "Basic " + standard_b64encode(authz) boundary = "-----------" + os.urandom(20).encode("hex") # *F841 local variable 'sep_boundary' is assigned to but never used # sep_boundary = "\r\n--" + boundary # *F841 local variable 'end_boundary' is assigned to but never used # end_boundary = "\r\n--" + boundary + "--\r\n" content_type = "multipart/form-data; boundary=%s" % (boundary,) items = ((":action", "doc_upload"), ("name", self.project)) body_prefix = "" for (name, value) in items: body_prefix += "--" + boundary + "\r\n" body_prefix += "Content-Disposition: form-data; name=\"" body_prefix += name + "\"\r\n\r\n" body_prefix += value + "\r\n" body_prefix += "--" + boundary + "\r\n" body_prefix += "Content-Disposition: form-data; name=\"content\"" body_prefix += "; filename=\"website.zip\"\r\n\r\n" body_suffix = "\r\n--" + boundary + "--\r\n" content_length = len(body_prefix) + tf.tell() + len(body_suffix) # POST it up to PyPI logger.info("uploading to PyPI") url = parse.urlparse(self.url) if url.scheme == "https": con = HTTPSConnection(url.netloc) else: con = HTTPConnection(url.netloc) con.connect() try: con.putrequest("POST", self.url) con.putheader("Content-Type", content_type) con.putheader("Content-Length", str(content_length)) con.putheader("Authorization", authz) con.endheaders() con.send(body_prefix) tf.seek(0) data = tf.read(1024 * 32) while data: con.send(data) data = tf.read(1024 * 32) con.send(body_suffix) r = con.getresponse() try: # PyPI tries to redirect to the page on success. if r.status in (200, 301,): logger.info("success!") else: msg = "Upload failed: %s %s" % (r.status, r.reason,) raise Exception(msg) finally: r.close() finally: con.close() finally: tf.close()
def publish(self): super(PyPI, self).publish() tf = tempfile.TemporaryFile() try: # Bundle it up into a zipfile logger.info("building the zipfile") root = self.site.config.deploy_root_path zf = zipfile.ZipFile(tf, "w", zipfile.ZIP_DEFLATED) try: for item in root.walker.walk_files(): logger.info(" adding file: %s", item.path) zf.write(item.path, item.get_relative_path(root)) finally: zf.close() # Formulate the necessary bits for the HTTP POST. # Multipart/form-data encoding. Yuck. authz = self.username + ":" + self.password authz = "Basic " + standard_b64encode(authz) boundary = "-----------" + os.urandom(20).encode("hex") # *F841 local variable 'sep_boundary' is assigned to but never used # sep_boundary = "\r\n--" + boundary # *F841 local variable 'end_boundary' is assigned to but never used # end_boundary = "\r\n--" + boundary + "--\r\n" content_type = "multipart/form-data; boundary=%s" % (boundary, ) items = ((":action", "doc_upload"), ("name", self.project)) body_prefix = "" for (name, value) in items: body_prefix += "--" + boundary + "\r\n" body_prefix += "Content-Disposition: form-data; name=\"" body_prefix += name + "\"\r\n\r\n" body_prefix += value + "\r\n" body_prefix += "--" + boundary + "\r\n" body_prefix += "Content-Disposition: form-data; name=\"content\"" body_prefix += "; filename=\"website.zip\"\r\n\r\n" body_suffix = "\r\n--" + boundary + "--\r\n" content_length = len(body_prefix) + tf.tell() + len(body_suffix) # POST it up to PyPI logger.info("uploading to PyPI") url = parse.urlparse(self.url) if url.scheme == "https": con = HTTPSConnection(url.netloc) else: con = HTTPConnection(url.netloc) con.connect() try: con.putrequest("POST", self.url) con.putheader("Content-Type", content_type) con.putheader("Content-Length", str(content_length)) con.putheader("Authorization", authz) con.endheaders() con.send(body_prefix) tf.seek(0) data = tf.read(1024 * 32) while data: con.send(data) data = tf.read(1024 * 32) con.send(body_suffix) r = con.getresponse() try: # PyPI tries to redirect to the page on success. if r.status in ( 200, 301, ): logger.info("success!") else: msg = "Upload failed: %s %s" % ( r.status, r.reason, ) raise Exception(msg) finally: r.close() finally: con.close() finally: tf.close()