Esempio n. 1
0
def create_idml_package_from_dir(dir_path, package_path=None):
    if os.path.exists(package_path):
        print "%s already exists." % package_path
        return None

    package = IDMLPackage(package_path, mode="w")

    for root, dirs, filenames in os.walk(dir_path):
        for filename in filenames:
            package.write(os.path.join(root, filename),
                          os.path.join(root.replace(dir_path, "."), filename))
    return package
Esempio n. 2
0
def use_working_copy(view_func):
    def new_func(idml_package, *args, **kwargs):
        if idml_package.working_copy_path is not None:
            return view_func(idml_package, *args, **kwargs)

        tmp_filename = NamedTemporaryFile().name
        idml_package.extractall(tmp_filename)
        idml_package.working_copy_path = tmp_filename
        idml_package.init_lazy_references()

        if idml_package.debug:
            # In debug it is useful to have the original trace.
            idml_package = view_func(idml_package, *args, **kwargs)
        else:
            # Catch any exception to reset working_copy_path.
            try:
                idml_package = view_func(idml_package, *args, **kwargs)
            except BaseException, err:
                idml_package.working_copy_path = None
                raise err

        from simple_idml.idml import IDMLPackage

        # Create a new archive from the extracted one.
        tmp_package = IDMLPackage("%s.idml" % tmp_filename, mode="w")
        for root, dirs, filenames in os.walk(tmp_filename):
            for filename in filenames:
                filename = os.path.join(root, filename)
                arcname = filename.replace(tmp_filename, "")
                tmp_package.write(filename, arcname)
        tmp_package.close()

        # swap working_copy with initial IDML Package.
        new_filename = idml_package.filename
        idml_package.close()
        os.unlink(idml_package.filename)
        os.rename(tmp_package.filename, new_filename)
        shutil.rmtree(tmp_filename)
        idml_package.working_copy_path = None

        return IDMLPackage(new_filename)
Esempio n. 3
0
def use_working_copy(view_func):
    def new_func(idml_package, *args, **kwargs):
        if idml_package.working_copy_path is not None:
            return view_func(idml_package, *args, **kwargs)

        tmp_filename = NamedTemporaryFile().name
        idml_package.extractall(tmp_filename)
        idml_package.working_copy_path = tmp_filename
        idml_package.init_lazy_references()

        if idml_package.debug:
            # In debug it is useful to have the original trace.
            idml_package = view_func(idml_package, *args, **kwargs)
        else:
            # Catch any exception to reset working_copy_path.
            try:
                idml_package = view_func(idml_package, *args, **kwargs)
            except BaseException, err:
                idml_package.working_copy_path = None
                raise err

        from simple_idml.idml import IDMLPackage
        # Create a new archive from the extracted one.
        tmp_package = IDMLPackage("%s.idml" % tmp_filename, mode="w")
        for root, dirs, filenames in os.walk(tmp_filename):
            for filename in filenames:
                filename = os.path.join(root, filename)
                arcname = filename.replace(tmp_filename, "")
                tmp_package.write(filename, arcname)
        tmp_package.close()

        # swap working_copy with initial IDML Package.
        new_filename = idml_package.filename
        idml_package.close()
        os.unlink(idml_package.filename)
        os.rename(tmp_package.filename, new_filename)
        shutil.rmtree(tmp_filename)
        idml_package.working_copy_path = None

        return IDMLPackage(new_filename)