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 list(): templates_archive = download_archive() print("** The following templates are available:") for x in templates_archive.infolist(): if x.filename.count("/") == 2 and x.filename.endswith("/"): print(" " + info(basename(dirname(x.filename)))) print("**")
def run_test(server_base_url, test_filename): """ Run test case fro mthe test file """ print("Runing tests at %s using %s" % (info(server_base_url), info(test_filename))) app = TestApp(server_base_url) with open(test_filename, "r") as yaml_file: doc = yaml.safe_load_all(yaml_file) for test_doc in doc: title = test_doc["title"] padding = (80 - len(title)) * " " print("- %s%s- " % (test_doc["title"], padding), end="") test_input = test_doc.get("input") if not test_input: continue # webtest does the status validation based on the status input parameter expect_status = test_doc["validate"].get("status") response = app.get(test_input["url"], status=expect_status) validate(response, test_doc["validate"]) print_success("OK")
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_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 run(app_directory, listener_address=None, no_logs=False, running_describe=False): """ When an application is run, the following is performed: - Identify application root - Check for qwstart.py on - Startup script directory - Current working directory - Setup port number, if $PORT is not set, use a random port """ start_t = time() # Use for startup time calculation print("** Starting application %s using QuickWeb %s " % (info(app_directory), info(quickweb.version.version))) startup_cwd = os.getcwd() # Check if beeing run from gunicorn is_gunicorn = "gunicorn" in os.environ.get("SERVER_SOFTWARE", "") if is_gunicorn: sys.stderr.write( "Quickweb provides it's own HTTP server module.\n" "Running from another HTTP server is not supported at this time\n") sys.exit(1) # Identify the application root directory app_root_directory = app_directory or os.getcwd() startup.setup_app("app_name", app_root_directory, no_logs) if running_describe: return colored_elapsed_time = info("%0.2fms" % ((time() - start_t) * 1000.0)) print("=" * 10 + " Startup completed in " + colored_elapsed_time) # Determine the HTTP listener port listener_port = int(os.getenv("PORT", 8080)) if os.name == "posix": socket_host = "0.0.0.0" else: socket_host = "127.0.0.1" if listener_address is not None: socket_host = listener_address cherrypy.config.update({"server.socket_host": socket_host}) cherrypy.config.update({"server.socket_port": listener_port}) ssl_certificate = os.environ.get("SSL_CERTIFICATE") if ssl_certificate: ssl_adapter = BuiltinSSLAdapter( certificate=ssl_certificate, private_key=os.environ["SSL_PRIVATE_KEY"], certificate_chain=os.environ.get("SSL_CERTIFICATE_CHAIN"), ) verify_mode = ssl.CERT_NONE if os.getenv("SSL_VERIFY_CLIENT_CERT") == "required": verify_mode = ssl.CERT_REQUIRED if os.getenv("SSL_VERIFY_CLIENT_CERT") == "optional": verify_mode = ssl.CERT_OPTIONAL ssl_adapter.context.verify_mode = verify_mode HTTPServer.ssl_adapter = ssl_adapter # In some platforms signals are not available: if hasattr(cherrypy.engine, "signals"): cherrypy.engine.signals.subscribe() cherrypy.engine.subscribe("stop", lambda: os.chdir(startup_cwd)) if os.environ.get("DAEMON_MODE"): daemon = Daemonizer(cherrypy.engine, stdout='stdout.log', stderr='stderr.log') daemon.subscribe() PIDFile(cherrypy.engine, 'quickweb.pid').subscribe() cherrypy.engine.start() cherrypy.engine.block()
def download_archive(): print("** Downloading templates archive from", info(TEMPLATES_REPO)) page = requests.get(TEMPLATES_REPO) file_like_object = io.BytesIO(page.content) templates_archive = zipfile.ZipFile(file_like_object) return templates_archive