예제 #1
0
def build(config):
    """Build function
    """
    global OUTPUT_IO
    global OBJS

    if not os.path.isdir(config["source"]):
        ERROR("Can't find %s", config["source"])
        return

    OUTPUT_IO = open_file(config["target"]["path"], "wb")
    OUTPUT_IO.write(
        """<?xml version="1.0" encoding="utf-8"?>\n"""
        """<Application>\n"""
    )
    OBJS = {}

    write_app_info(config)
    write_pages(config)
    write_e2vdom(config)
    write_libraries(config)
    # write_structure(config)

    OUTPUT_IO.write("""  <Structure/>\n""")
    OUTPUT_IO.write("""  <Backupfiles/>\n""")

    write_resources(config)
    write_databases(config)
    write_security(config)

    OUTPUT_IO.write("</Application>")
    OUTPUT_IO.close()
예제 #2
0
    def child_end(self, tagname):
        if self.current_mode == "Users":
            if tagname == "Users":
                self.current_mode = ""
                self.current_node = None

            elif tagname == "User":

                user = dict(self.current_node)
                if user.get("Rights", None):
                    user["Rights"].sort(key=lambda right: right["Target"])

                else:
                    del user["Rights"]

                self.groups_and_users["users"].append(sort_dict(user))

            elif tagname in ("Login", "Password", "FirstName", "LastName",
                             "Email", "SecurityLevel", "MemberOf"):

                self.is_data_allowed = False
                self.current_node[-1][1] = "".join(self.current_node[-1][1])

        elif self.current_mode == "Groups":
            ERROR("Group are unsupported")
예제 #3
0
    def child_start(self, tagname, attrs):
        if tagname in ("Groups", "Users", "LDAP"):
            self.current_mode = tagname

        if self.current_mode == "Users":
            if tagname == "User":
                self.current_node = []

            elif tagname in ("Login", "Password", "FirstName", "LastName",
                             "Email", "SecurityLevel", "MemberOf"):

                self.current_node.append([tagname, []])
                self.is_data_allowed = True

            elif tagname == "Rights":
                self.current_node.append([tagname, []])

            elif tagname == "Right":
                self.current_node[-1][1].append(sort_dict(attrs))

        elif self.current_mode == "LDAP":
            self.ldapf = cStringIO.StringIO()
            self.is_data_allowed = True

        elif self.current_mode == "Groups":
            ERROR("Group are unsupported")
예제 #4
0
    def child_data(self, data):
        if self.is_data_allowed:
            if self.current_mode == "Users":
                self.current_node[-1][1].append(encode(data))

            elif self.current_mode == "LDAP":
                self.ldapf.write(data)

            elif self.current_mode == "Groups":
                ERROR("Group are unsupported")
예제 #5
0
    def create_base_dir(self):
        self.parent.create_dir()

        PARSER.append_to_current_path("Actions-{}".format(
            self.parent.attrs["Name"]))
        try:
            PARSER.create_folder_from_current_path()

        except OSError:
            ERROR("Folder already exists: %s",
                  build_path(PARSER.current_path()))
예제 #6
0
def parse_ignore_file(config):
    """Convert strings to regexps
    """
    global IGNORE

    if not config["ignore"]:
        config["ignore"] = {}

    if not isinstance(config["ignore"], dict):
        ERROR("Ignore settings must be dict")
        emergency_exit()

    keys = ["Resources", "Libraries", "Databases", "Actions", "Pages"]

    for key in keys:
        data = config["ignore"].get(key, [])
        if not isinstance(data, (list, tuple)):
            data = (data, )

        config["ignore"][key] = convert_to_regexp(data)

    IGNORE = config["ignore"]
예제 #7
0
def write_structure(config):

    INFO("Structure Data: Processing...")

    structure_path = os.path.join(config["source"], constants.STRUCT_FILE)

    if not os.path.exists(structure_path):
        ERROR("Can't find: {}".format(structure_path))
        write_xml("Structure", indent=2, close=True)
        return

    write_xml("Structure", indent=2)

    with open_file(structure_path) as struct_file:
        struct_json = json_load(struct_file, critical=True)

    for obj in struct_json:
        write_xml("Object", attrs=obj, data="", close=True, indent=4)

    write_xml("Structure", indent=2, closing=True)

    INFO("Structure Data: Done!")
예제 #8
0
def detect_libraries(script_path):
    """Find all libs used by script
    """
    if ACTION_EXT != ".py":
        return

    if "current" not in PARSER.pages:
        return

    page_id = PARSER.pages["current"]

    finder = ModuleFinder()
    try:
        DEBUG("Parsing: %s", script_path)
        finder.run_script(script_path)
        DEBUG("Done: %s", script_path)

    except Exception:
        ERROR("Can't parse script: %s", script_path)
        EXCEPTION("")
        return

    PARSER.pages[page_id]["libraries"].extend(finder.modules.keys())
    PARSER.pages[page_id]["libraries"].extend(finder.badmodules.keys())
예제 #9
0
def walk(path, name, indent):
    new_path = os.path.join(path, name)
    actions_folder = "Actions-{}".format(name)

    info_path = os.path.join(new_path, constants.INFO_FILE)
    if not os.path.exists(info_path):
        CRITICAL("Can't find: {}".format(info_path))
        emergency_exit()

    with open_file(info_path) as info_file:
        info_json = json_load(info_file, critical=True)

    attrs = info_json["attrs"]
    if attrs is not None and 'ID' in attrs:
        id = attrs['ID']
        if id in OBJS:
            ERROR("Encountered duplicate GUID: {duplicate} duplicates {origin}: Ignoring {duplicate}".format(
                duplicate=name, origin=OBJS[id]
            ))
            return
        else:
            OBJS[id] = name

    write_xml("Object", attrs=attrs, indent=indent)
    write_actions(os.path.join(new_path, actions_folder), indent+2)
    write_xml("Objects", indent=indent+2)


    childs_order_path = os.path.join(new_path, constants.CHILDS_ORDER)

    if os.path.exists(childs_order_path):
        with open(childs_order_path) as f:
            names = json_load(f, default=[], critical=False)
            names = map(lambda s: s.lower(), names)
            childs_order = dict(zip(names, xrange(len(names))))
    else:
        childs_order = {}

    max_value = len(childs_order) + 1


    def key_func(name):
        key = name.lower()
        if key.endswith('.json'): 
            key = key[:-5]

        return [childs_order.get(key, max_value), name]

    nodes = list(set(os.listdir(new_path)) - set(constants.RESERVED_NAMES) - {actions_folder})
    nodes = [node for node in nodes if not constants.RESERVED_NAMES_REGEXP.match(node)]
    ordered_nodes = sorted(nodes, key=key_func)

    for name in ordered_nodes:
        if os.path.isdir(os.path.join(new_path, name)):
            walk(new_path, name, indent+4)

        else:
            write_object(new_path, name, indent+4)

    write_xml("Objects", indent=indent+2, closing=True)
    write_attributes(info_json["attributes"], indent+2)
    write_xml("Object", indent=indent, closing=True)
예제 #10
0
def copy_pages(config):
    """Copy pages and change resources and objects GUIDs
    """

    if "Pages" not in config:
        INFO("No information about pages")
        return

    target_path = os.path.join(config["target"]["path"],
                               constants.PAGES_FOLDER)

    pages = config["Pages"]
    params = {
        # "rename": None,
        "exclude": None,
        "include": None
    }

    if not isinstance(pages, (list, tuple)):
        pages = (pages, )

    new_pages = []
    for page in pages:
        if isinstance(page, (str, unicode)):
            _page = {"path": normalize_path(page, config)}

        if not _page["path"].rstrip("\/").lower().endswith("pages"):
            new_pages.append(page)
        else:
            if isinstance(page, dict):
                _page["path"] = normalize_path(page["path"], config)

                for param in params:
                    params[param] = val = page.get(param, None)
                    if val and not isinstance(val, (list, tuple, dict)):
                        params[param] = (val, )

                if params["exclude"]:
                    params["exclude"] = convert_to_regexp(params["exclude"])

                if params["include"]:
                    params["include"] = convert_to_regexp(params["include"])

            if not os.path.exists(_page["path"]):
                ERROR("No such directory: '%s'", _page["path"])
                continue

            for folder in os.listdir(_page["path"]):

                # if folder in exclude list - continue
                if params["exclude"] and check_by_regexps(
                        folder, params["exclude"]):
                    continue

                # if folder not in include list - continue
                if params["include"] and not check_by_regexps(
                        folder, params["include"]):
                    continue

                folder_path = os.path.join(_page["path"], folder)

                if not os.path.isdir(folder_path):
                    ERROR("Page can't be file: %s", folder_path)
                    continue

                new_page = _page.copy()
                new_page["path"] = folder_path
                new_pages.append(new_page)

    for page in new_pages:

        if isinstance(page, (str, unicode)):
            page = {"path": page}

        # normalize path - replace alias with real path
        page["path"] = normalize_path(page["path"], config)
        if not os.path.exists(page["path"]):
            ERROR("No such directory: '{}'".format(page["path"]))
            continue

        # if name not defined - got it from folder name
        if not page.get("name", ""):
            page["name"] = os.path.split(page["path"].rstrip("\/"))[1]
            page["rename"] = False

        copy_path = os.path.join(target_path, page["name"])
        if os.path.exists(copy_path):
            ERROR("Directory already exists: '{}'".format(copy_path))
            continue

        if page.get("mode", "") not in ("move", "copy"):
            page["mode"] = "move"

        # copy page to new folder
        DEBUG("Copy '{}' to '{}'".format(page["path"], copy_path))
        shutil.copytree(page["path"], copy_path)

        info_path = os.path.join(copy_path, constants.INFO_FILE)
        with fopen(info_path, "rb") as hdlr:
            info = json_load(hdlr, critical=True)

        if page.get("rename", True):
            info["attrs"]["Name"] = page["name"]

        with fopen(info_path, "wb") as hdlr:
            json_dump(info, hdlr, critical=True)

        # if page not copied continue, else need to change all guids to new
        if page["mode"] == "move":
            continue

        new_guid = gen_guid()
        old_guid = info["attrs"]["ID"]

        GUIDS_TO_REPLACE[old_guid] = new_guid
        GUIDS_TO_REPLACE[old_guid.replace("-",
                                          "_")] = new_guid.replace("-", "_")

    INFO("Pages were copied successfully")
예제 #11
0
def copy_files(target, sources, config):
    """Copy files
    """

    if not isinstance(sources, (list, tuple)):
        sources = (sources, )

    copied_files = []

    for source in sources:

        path = ""
        params = {"rename": None, "exclude": None, "include": None}

        # it can be single file or folder
        # with additional params like rename,
        # exclude, include
        if isinstance(source, dict):
            path = normalize_path(source["path"], config)

            for param in params:
                params[param] = val = source.get(param, None)
                if val and not isinstance(val, (list, tuple, dict)):
                    params[param] = (val, )

            if params["exclude"]:
                params["exclude"] = convert_to_regexp(params["exclude"])

            if params["include"]:
                params["include"] = convert_to_regexp(params["include"])

        # it can be single file or folder
        # without additional params
        else:
            path = normalize_path(source, config)

        # fetch all files if @path is directory
        if os.path.isdir(path):
            files = os.listdir(path)

        # else split @path to parent path and file name
        else:
            path, name = os.path.split(path.rstrip("\/"))
            files = (name, )

        for name in files:

            source_path = os.path.join(path, name)
            if os.path.isdir(source_path):
                DEBUG("Directories are not supported: {}".format(source_path))
                continue

            # if file in exclude list - continue
            if params["exclude"] and check_by_regexps(name, params["exclude"]):
                continue

            # if file not in include list - continue
            if params["include"] and not check_by_regexps(
                    name, params["include"]):
                continue

            # if file in rename list - rename it, else use source name
            if params["rename"] and \
               (name in params["rename"] or len(files) == 1):

                if isinstance(params["rename"], (tuple, list)):
                    new_name = params["rename"][0]

                else:
                    new_name = params["rename"].get(name, name)

            else:
                new_name = name

            target_path = os.path.join(target, new_name)

            if os.path.exists(source_path):
                DEBUG("Copy '%s' to '%s'", source_path, target_path)
                shutil.copy2(source_path, target_path)
                copied_files.append(new_name)

            else:
                ERROR("No such file or directory: '{}'".format(source_path))

    return copied_files