def validate(response, validation_rules): """ Validates an HTTP response, headers and body (when provided for validation) """ validate_headers(response, validation_rules) validate_body_list = validation_rules.get("body", []) if validate_body_list and isinstance(validate_body_list, str): validate_body_list = [validate_body_list] last_found_pos = None last_validate_body = None # webtest resturns the body as bytes() on python3, we need to decode it for find() response_body = str(response.body) for validate_body in validate_body_list: find_pos = response_body.find(validate_body, last_found_pos) if find_pos == -1: print() # previous print does not have end of line if last_validate_body: print_error( "FAILED: '%s' was not found after '%s', in body:\n%s " % (validate_body, last_validate_body, response_body)) else: print_error("FAILED: '%s' is missing from body:\n%s " % (validate_body, response_body)) exit(1) last_found_pos = find_pos + len(validate_body) last_validate_body = validate_body
def create(app_directory, template_name, force): """ Create an application using a template """ print("** Creating app on directory %s " % (info(app_directory + "/"))) if exists(app_directory): if force: shutil.rmtree(app_directory) else: print_error(app_directory + " already exists!") print("Use %s if you want to overwrite." % warning("--force")) sys.exit(2) download_template(template_name, app_directory) doc_path = os.path.dirname(doc.__file__) quickweb_required_mask = join(doc_path, "*.md") required_files = glob(quickweb_required_mask) base_required_files = [basename(x) for x in required_files] print("** Adding startup files %s from %s" % (info(str(base_required_files)), info(doc_path))) for filename in required_files: shutil.copy(filename, app_directory) print_success("Application successfully created.") print_success("You can start it with:") print(" " + success("quickweb run " + app_directory)) print_success("Or read about the app structure with:") print(" " + success("more " + join(app_directory, "QuickWeb_Application.md"))) print("**")
def download_template(template_name, app_directory): templates_archive = download_archive() with TemporaryDirectory() as tmpdirname: templates_archive.extractall(tmpdirname) templates_archive_tmp = join(tmpdirname, "QuickWeb-templates-master") template_dirs = os.listdir(templates_archive_tmp) if template_name not in template_dirs: print_error("Unable to find template %s !" % template_name) sys.exit(2) template_root = join(templates_archive_tmp, template_name) template_provides = [x for x in os.listdir(template_root)] print("** The template provides: %s" % info(str(template_provides))) shutil.copytree(template_root, app_directory)
def setup(self): """ chdir() into the provided directory """ content_directory = self._content_directory = "webroot" lib_directory = "lib" if not isdir(content_directory): print_error("Unable to find content directory '%s'" % content_directory) sys.exit(1) cherrypy.engine.autoreload.files.add(content_directory) cherrypy.engine.autoreload.files.add(lib_directory)
def setup_docker_deployment(app_directory): app_directory = app_directory or os.getcwd() webroot_dir = join(app_directory, "webroot") if not isdir(webroot_dir): print_error("Unable to find webroot directory '%s'" % webroot_dir) exit(2) dockerfile_txt = """\ FROM python:alpine WORKDIR /usr/src/app COPY requirements.txt ./ RUN pip install --no-cache-dir -r requirements.txt COPY . . CMD [ "quickweb", "run", "/usr/src/app", "--no-logs" ] """ with open(join(app_directory, "Dockerfile"), "w") as manifest_file: manifest_file.write(dockerfile_txt) with open(join(app_directory, "requirements.txt"), "w") as requests_file: requests_file.write("quickweb>={0}\n".format(quickweb.version()))
def setup_cf_deployment(app_directory): app_directory = app_directory or os.getcwd() app_name = basename(app_directory) webroot_dir = join(app_directory, "webroot") if not isdir(webroot_dir): print_error("Unable to find webroot directory '%s'" % webroot_dir) exit(2) manifest_yaml = """\ --- applications: - name: {0} memory: 128M buildpack: python_buildpack random-route: true # Choose a proper hostname with host: name """.format(app_name) with open(join(app_directory, "manifest.yaml"), "w") as manifest_file: manifest_file.write(manifest_yaml) with open(join(app_directory, "requirements.txt"), "w") as requests_file: requests_file.write("quickweb>={0}\n".format(quickweb.version())) with open(join(app_directory, "Procfile"), "w") as procfile: procfile.write("web: quickweb run . --no-logs\n")
def setup_features(): """ Call the features setup function """ core_features = {"web": ["content_directory", "controllers", "templates"]} imported_features = [] for feature_type, feature_list in core_features.items(): features_list_names = ", ".join(feature_list) print("** Setting up {0} features {1}".format( info(feature_type), info(features_list_names))) for feature_name in feature_list: script_dir = dirname(abspath(__file__)) module_fname = join(script_dir, "features", feature_type, feature_name + ".py") feature_dict = {} with open(module_fname) as source_file: exec(compile(source_file.read(), module_fname, "exec"), feature_dict) try: feature = feature_dict["Feature"]() except KeyError: print_error( "Feature module '%s' does not provide a Feature class!" % feature_name) sys.exit(1) try: feature.setup() except: # NOQA: E722 print_error("Failed setting up feature '%s' !" % feature_name) raise imported_features.append(feature) for feature in imported_features: if hasattr(feature, "activate"): feature.activate()
def validate_headers(response, validation_rules): validate_headers = validation_rules.get("headers") if validate_headers: for header_match in validate_headers: (header_name, header_value) = list(header_match.items())[0] found_header = False for resp_header_name, resp_header_value in response.headerlist: if header_name.lower() == resp_header_name.lower(): if resp_header_value.lower() != header_value.lower(): print() # previous print does not have end of line print_error( "FAILED: %s %s != %s " % (header_name, resp_header_value, header_value)) exit(1) else: found_header = True break if not found_header: print() # previous print does not have end of line print_error("FAILED: Header %s missing from response" % header_name) print_error("Available Headers" + str(response.headerlist)) exit(1)