示例#1
0
def copy_databases(config):
    """Copy databases
    """

    if "Databases" not in config:
        INFO("No information about databases")
        return

    DEBUG("Collect databases info")

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

    DEBUG("Copy databases")

    sources = config["Databases"]
    if not isinstance(sources, (list, tuple)):
        sources = (sources, )

    for source in sources:

        files = copy_files(target_path, source, config)

        change_guids = False
        if isinstance(source, dict):
            change_guids = bool(source.get("generateGUIDs", False))

        if not change_guids:
            continue

        for old_name in files:

            raw_name = old_name.split("_", 1)
            res_type = "sqlite"
            res_guid = res_name = ""

            try:
                res_guid = str(UUID(raw_name[0])).lower()

            except ValueError:
                res_guid = gen_guid()
                res_name = old_name.rsplit(".", 1)[0]

            else:
                res_name = raw_name[1].rsplit(".", 1)[0]

            new_guid = GUIDS_TO_REPLACE[res_guid] = gen_guid()
            new_name = "{}_{}.{}".format(new_guid, res_name, res_type)

            old_path = os.path.join(target_path, old_name)
            new_path = os.path.join(target_path, new_name)

            DEBUG("Move '%s' to '%s'", old_path, new_path)
            shutil.move(old_path, new_path)

    # copy_files(target_path, config["Databases"], config)

    INFO("Databases were copied successfully")
示例#2
0
def create_application_info_file(config):
    """Create __info__.json in root directory
    """
    DEBUG("Collect application info")

    app_info = config.get("ApplicationInfo", {})

    # Load existing config file or create empty
    if "BaseConfigFile" in app_info:
        with open_file(app_info.pop("BaseConfigFile"), config) as hdlr:
            info = json_load(hdlr)

    else:
        info = dict(ID=gen_guid(),
                    Name="Application",
                    Description="",
                    Owner="-",
                    Active="1",
                    Serverversion="",
                    ScriptingLanguage="python",
                    Icon="")

    # Update values from config
    for key, value in app_info.items():
        info[key] = value

    # Generate new GUID if it isn't exiisting
    if not info.get("ID", ""):
        info["ID"] = gen_guid()

    # Write data to file
    path = os.path.join(config["target"]["path"], constants.INFO_FILE)

    DEBUG("Writing application info to '%s'", path)

    with fopen(path, "wb") as hdlr:
        json_dump(info, hdlr)

    INFO("Application info successfully written to '%s'", path)
示例#3
0
def write_resources(config):

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

    resources_path = os.path.join(config["source"], constants.RESOURCES_FOLDER)
    if not os.path.exists(resources_path):
        CRITICAL("Can't find: {}".format(resources_path))
        emergency_exit()

    write_xml("Resources", indent=2)

    files = list(set(os.listdir(resources_path)) - set(constants.RESERVED_NAMES))
    for res_name in sorted(files):
        res_path = os.path.join(resources_path, res_name)

        if not os.path.isfile(res_path):
            continue

        raw_name = res_name.split("_", 2)

        try:
            res_guid = UUID(raw_name[0])

        except ValueError:
            res_guid = gen_guid()
            res_type = res_name.rsplit(".", 1)
            res_type = res_type[1] if len(res_type) == 2 else "res"

        else:
            res_type = raw_name[1]
            res_name = raw_name[2]

        attrs = {
            "ID": res_guid,
            "Name": res_name,
            "Type": res_type
        }

        DEBUG("Open file: %s", res_path)
        with open_file(res_path) as res_f:
            write_xml(
                tagname="Resource",
                attrs=attrs,
                indent=4,
                data=base64.b64encode(res_f.read()),
                close=True
            )

    write_xml("Resources", indent=2, closing=True)
    INFO("Resources Data: Done!")
示例#4
0
def write_databases(config):
    INFO("Databases Data: Processing...")

    dbs_path = os.path.join(config["source"], constants.DATABASES_FOLDER)
    if not os.path.exists(dbs_path):
        DEBUG("Can't find: {}".format(dbs_path))
        return

    write_xml("Databases", indent=2)

    files = list(set(os.listdir(dbs_path)) - set(constants.RESERVED_NAMES))
    for db_name in sorted(files):
        db_path = os.path.join(dbs_path, db_name)

        if not os.path.isfile(db_path):
            continue

        raw_name = db_name.split("_", 1)

        try:
            db_guid = UUID(raw_name[0])

        except ValueError:
            db_guid = gen_guid()

        raw_name = raw_name[-1].split(".", 1)
        db_name = raw_name[0]
        db_type = raw_name[1] if len(raw_name) == 2 else "sqlite"

        attrs = {
            "ID": db_guid,
            "Name": db_name,
            "Type": db_type
        }

        DEBUG("Open file: %s", db_path)
        with open_file(db_path) as db_f:
            write_xml(
                tagname="Database",
                attrs=attrs,
                indent=4,
                data=base64.b64encode(db_f.read()),
                close=True
            )

    write_xml("Databases", indent=2, closing=True)
    INFO("Databases Data: Done!")
示例#5
0
    def func(match, create_new=False, *args, **kwargs):
        """Processing match objects. Replacing objects GUIDs.
        """

        data = match.group(0)

        if create_new:
            dash = data.replace("_", "-") if "_" in data else data

            if dash not in objects:
                new = gen_guid()
                objects[dash] = new
                objects[dash.replace("-", "_")] = new.replace("-", "_")

        if data in objects:
            return (objects[data], True)

        return (data, False)
示例#6
0
def write_actions(path, indent):
    actions_map_path = os.path.join(path, constants.MAP_FILE)

    if not os.path.exists(actions_map_path):
        INFO("Can't find: %s; skipping Actions", actions_map_path)
        write_xml("Actions", indent=indent)
        write_xml("Actions", indent=indent, closing=True)
        return

    with open_file(actions_map_path) as actions_map_file:
        actions_map = json_load(actions_map_file, critical=True)

    write_xml("Actions", indent=indent)

    for action_name in sorted(os.listdir(path)):
        action_path = os.path.join(path, action_name)
        if not os.path.isfile(action_path) or \
                action_name in constants.RESERVED_NAMES:

            continue

        attrs = actions_map.get(action_name, None)
        if not attrs:
            attrs = {
                "Top": "",
                "State": "",
                "Left": "",
                "ID": str(gen_guid()),
                "Name": action_name.split(".", 1)[0],
            }

        with open_file(action_path) as action_f:
            write_xml(
                tagname="Action",
                attrs=attrs,
                indent=indent+2,
                data=action_f.read(),
                close=True,
                force_cdata=True
            )

    write_xml("Actions", indent=indent, closing=True)
示例#7
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")