Shadow-Here


Server : Apache/2.4.41 (Ubuntu)
System : Linux cls 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User : iscuser ( 1001)
PHP Version : 7.4.12
Disable Function : shell_exec,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /home/akriveia/FW/FW-MYSQL-orig/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Command :
Current File : //home/akriveia/FW/FW-MYSQL-orig/haf-config.py
#!/usr/local/bin/python

#
# Configure a HAF application to run
#
#

import os
import sys
import subprocess
import getopt
import pprint
import pickle

HAF_DIR = os.path.abspath(os.path.dirname( __file__ ))

#
sys.path.append(os.path.join(HAF_DIR, "src"))

#
from mako.lookup import TemplateLookup

#
_HAF_VERSION_FILE = os.path.join(HAF_DIR, "haf-version.txt")
_HAF_TEMPLATES_DIR = os.path.join(HAF_DIR, "config-templates")
_APP_HAF_REQUIRED_VERSION_FILE = "haf-required-version.txt"
_APP_DOMAIN_FILE = "app-domain.txt"
_APP_REQUIRED_SUBDIRS = ["src", "www"]


#
def hafconfig_pickle(hafconfig):
    return pickle.dumps(hafconfig)

#
_FILES_TO_GEN = [
    #(hafconfig_pickle, "hafconfig.pickle"),
    ("haflink.py.mako", ["src", "haflink.py"]),
    #("appconfig.py.mako", ["src", "appconfig.py"]),
    ("apache-setup-notes.txt.mako", "hafconfig-apache-setup.txt"),
    ("htaccess.mako", ["www", ".htaccess"]),
    #("haf-cst.py.mako", ["www", "haf-cst.py"]),
    ("haf-mako.py.mako", ["www", "haf-mako.py"]),
    ("haf-jsonrpc.py.mako", ["www", "haf-jsonrpc.py"]),
]



#
_ERROR_BANNER = """
--------------------------------------------
        ****  ***   ***    **   ***
        *     *  *  *  *  *  *  *  *
        ****  ***   ***   *  *  ***
        *     *  *  *  *  *  *  *  *
        ****  *  *  *  *   **   *  *
--------------------------------------------
"""

#

def usage():
    print
    print r"HAF Application Configuration Script"
    print
    print r"Usage:  haf-config.py [-w] <app-dir>"
    print
    print r"    -w    Actually write files (even if they exist)"
    print r"              otherwise it's only a trial run"
    print
    print r"    e.g.:    haf-config.py example-app"
    print r"          OR"
    print r"             haf-config.py -w c:\work\myapp\myapp-v1 /1"
    print


class UserError(Exception):
    pass

def main():
    #
    args = sys.argv[1:]
    #
    usage_ok = False
    write_files = False
    if args and args[0] == "-w":
        write_files = True
        args = args[1:]
    if len(args) == 1:
        usage_ok = True
    #
    if not usage_ok:
        if len(args):
            print "ERROR: Bad arguments"
        usage()
        sys.exit(2)
    #
    app_dir = args[0]
    #
    try:
        print
        print r"HAF Application Configuration Script"
        print
        app_config(app_dir, write_files)
    except UserError, e:
        print _ERROR_BANNER
        print "ERROR:", e
        sys.exit(1)
    except:
        print _ERROR_BANNER
        raise


def app_config(app_dir, write_files):

    # HAF Info
    actual_haf_version = open(_HAF_VERSION_FILE).read().strip()
    print "HAF Dir     :", HAF_DIR
    print "HAF Version :", actual_haf_version
    print

    # Project Info
    app_dir = os.path.abspath(app_dir)
    print "Application Dir         :", app_dir
    # Check project directory
    if not os.path.exists(app_dir):
        raise UserError, "Application directory does not exist"
    if not os.path.isdir(app_dir):
        raise UserError, "Application directory is not a directory"
    # Check project marker file
    app_haf_required_version_file = os.path.join(app_dir, _APP_HAF_REQUIRED_VERSION_FILE)
    if not os.path.exists(app_haf_required_version_file):
        raise UserError, "Application HAF required version file missing: %s" % app_haf_required_version_file
    app_haf_required_version = open(app_haf_required_version_file).read().strip()
    print "Application HAF Required Version :", app_haf_required_version
    if app_haf_required_version != actual_haf_version:
        raise UserError, \
            "This HAF version does not match application's required version"
    print

    #
    app_domain_file = os.path.join(app_dir, _APP_DOMAIN_FILE)
    if not os.path.exists(app_domain_file):
        raise UserError, "Application domain file missing: %s" % app_domain_file
    app_domain = open(app_domain_file).read().strip()

    if app_domain.find(".") < 0:
        raise UserError, "Application domain must contain '.'"
    if app_domain.endswith("."):
        raise UserError, "Application domain must NOT end with '.'"

    if sys.platform in ["win32", "darwin"]:
        app_dev_domain = app_domain.split(".")[:-1]
        app_dev_domain.append("dev")
        app_dev_domain = ".".join(app_dev_domain)
        print "Application Domain : ", app_dev_domain, "( DEV for", app_domain, ")"
        app_domain = app_dev_domain
    else:
	app_dev_domain = ''
        print "Application Domain : ", app_domain
    print

    #
    hafconfig = {
            "generated_by": "Generated by haf-config.py (HAF %s) - DO NOT EDIT!" % actual_haf_version,
            "haf_dir": HAF_DIR,
            "app_dir": app_dir,
            "app_domain": app_domain,
            "app_dev_domain": app_dev_domain,
            #"actual_haf_version": actual_haf_version,
            #"app_haf_version": app_haf_version,
        }
    print "hafconfig=",
    pprint.pprint(hafconfig)
    print

    # Check for required subdirectories
    print "Checking for required subdirectories:", _APP_REQUIRED_SUBDIRS
    for subdir in _APP_REQUIRED_SUBDIRS:
        if not os.path.exists(os.path.join(app_dir, subdir)):
            raise UserError, "Application does not have required subdirectory '%s'" % subdir
    print

    #
    if write_files:
        print "File Generation: Writing files..."
    else:
        print "File Generation: Only check existing files..."
    content_mismatch = app_config_gen(hafconfig, write_files)
    print

    #
    if content_mismatch:
        print "Note: some existing files did NOT match"
    else:
        print "Note: all existing files matched"
    print

    #
    print "Done!"
    print


def dos2unix(f):
    s = open(f).read()
    s = s.replace('\r\n', '\n')
    s = s.replace('\r', '\n')
    open(f, 'w').write(s)


def app_config_gen(hafconfig, write_files):
    content_mismatch = False
    app_dir = hafconfig["app_dir"]
    #
    lookup = TemplateLookup(
                directories=[_HAF_TEMPLATES_DIR],
                output_encoding='ascii',
                default_filters=['unicode'],
            )
    #
    for template, target in _FILES_TO_GEN:
        if type(target) is str:
            target = os.path.join(app_dir, target)
        else:
            target = os.path.join(app_dir, *target)
        print "   %-50s" % target,
        #
        data = dict(hafconfig)
        hafconfig_fix_unix_executable = []
        data["hafconfig_fix_unix_executable"] = hafconfig_fix_unix_executable
        #
        if type(template) is str:
            t = lookup.get_template(template)
            new_content = t.render(**data)
        else:
            new_content = template(data)
        #
        if os.path.exists(target):
            old_content = open(target, "rb").read()
            if new_content == old_content:
                print "Matches,",
            else:
                content_mismatch = True
                print "Does NOT Match,",
        else:
            print "Does NOT Exist",
        if write_files:
            print "(overwriting...)"
            f = open(target, "wb")
            f.write(new_content)
            f.close()
            if hafconfig_fix_unix_executable:
                print " "*55, "(unix executable fix:",
                if sys.platform == 'win32':
                    print "skip for win32)"
                else:
                    print "fixing...)"
                    dos2unix(target)
                    r = subprocess.call(["chmod", "+x", target])
                    assert r == 0, "Error running 'chmod'"
        else:
            if hafconfig_fix_unix_executable:
                print "(not writing) (ux-fix)"
            else:
                print "(not writing)"

    return content_mismatch


if __name__ == "__main__":
    main()


Samx