Esempio n. 1
0
def install_package(app, module, init_func, raise_exc=False, log_exc=True):
    """Installs a Python package like a product."""
    from App.ProductContext import ProductContext
    try:
        do_install = doInstall()
        name = module.__name__
        if do_install:
            product = App.Product.initializeProduct(module, name,
                                                    module.__path__[0], app)
        else:
            product = FactoryDispatcher.Product(name)
            app = None

        product.package_name = name

        if init_func is not None:
            newContext = ProductContext(product, app, module)
            init_func(newContext)

        package_initialized(module, init_func)

        if do_install:
            transaction.get().note('Installed package %s' % module.__name__)
            transaction.commit()
    except Exception:
        if log_exc:
            LOG.error("Couldn't install %s" % module.__name__, exc_info=True)
        transaction.abort()
        if raise_exc:
            raise
Esempio n. 2
0
def _registerPackage(module_, init_func=None):
    """Registers the given python package as a Zope 2 style product
    """

    if not hasattr(module_, '__path__'):
        raise ValueError("Must be a package and the " \
                         "package must be filesystem based")
    
    app = Zope2.app()
    try:
        product = initializeProduct(module_, 
                                    module_.__name__, 
                                    module_.__path__[0],
                                    app)

        product.package_name = module_.__name__

        if init_func is not None:
            newContext = ProductContext(product, app, module_)
            init_func(newContext)
    finally:
        try:
            import transaction
            transaction.commit()
        finally:
            app._p_jar.close()
def install_product(app, product_dir, product_name, meta_types,
                    folder_permissions, raise_exc=None):
    if not _is_package(product_dir, product_name):
        return

    __traceback_info__ = product_name
    global_dict = globals()
    product = __import__("Products.%s" % product_name,
                         global_dict, global_dict, ('__doc__', ))

    # Install items into the misc_ namespace, used by products
    # and the framework itself to store common static resources
    # like icon images.
    misc_ = pgetattr(product, 'misc_', {})
    if misc_:
        if isinstance(misc_, dict):
            misc_ = Misc_(product_name, misc_)
        setattr(Application.misc_, product_name, misc_)

    productObject = FactoryDispatcher.Product(product_name)
    context = ProductContext(productObject, app, product)

    # Look for an 'initialize' method in the product.
    initmethod = pgetattr(product, 'initialize', None)
    if initmethod is not None:
        initmethod(context)
def install_package(app, module, init_func, raise_exc=None):
    """Installs a Python package like a product."""
    name = module.__name__
    product = FactoryDispatcher.Product(name)
    product.package_name = name

    if init_func is not None:
        newContext = ProductContext(product, app, module)
        init_func(newContext)

    package_initialized(module, init_func)
Esempio n. 5
0
def _registerPackage(module_, init_func=None):
    """Registers the given python package as a Zope 2 style product
    """

    if not hasattr(module_, '__path__'):
        raise ValueError("Must be a package and the " \
                         "package must be filesystem based")

    registered_packages = getattr(Products, '_registered_packages', None)
    if registered_packages is None:
        registered_packages = Products._registered_packages = []
    registered_packages.append(module_)

    # Delay the actual setup until the usual product loading time in
    # OFS.Application on versions of Zope that support this.
    #
    # Without this processing, we may get database write errors in
    # ZEO, when there's no connection with which to write an entry to
    # Control_Panel. We would also get multiple calls to initialize().
    #
    # For older versions of Zope not aware of this variable, initialize
    # immediately as before
    to_initialize = getattr(Products, '_packages_to_initialize', None)
    if to_initialize is None:
        app = Zope2.app()
        try:
            try:
                product = initializeProduct(module_, module_.__name__,
                                            module_.__path__[0], app)

                product.package_name = module_.__name__

                if init_func is not None:
                    newContext = ProductContext(product, app, module_)
                    init_func(newContext)
            except:
                raise
            else:
                transaction.commit()
        finally:
            app._p_jar.close()
    else:
        to_initialize.append((
            module_,
            init_func,
        ))
Esempio n. 6
0
def install_product(app,
                    product_dir,
                    product_name,
                    meta_types,
                    folder_permissions,
                    raise_exc=None):

    from App.ProductContext import ProductContext
    path_join = os.path.join
    isdir = os.path.isdir
    exists = os.path.exists
    global_dict = globals()

    package_dir = path_join(product_dir, product_name)
    __traceback_info__ = product_name
    if not isdir(package_dir): return
    if not exists(path_join(package_dir, '__init__.py')):
        if not exists(path_join(package_dir, '__init__.pyc')):
            if not exists(path_join(package_dir, '__init__.pyo')):
                return

    product = __import__("Products.%s" % product_name, global_dict,
                         global_dict, ('__doc__', ))

    # Install items into the misc_ namespace, used by products
    # and the framework itself to store common static resources
    # like icon images.
    misc_ = pgetattr(product, 'misc_', {})
    if misc_:
        if isinstance(misc_, dict):
            misc_ = Misc_(product_name, misc_)
        Application.misc_.__dict__[product_name] = misc_

    productObject = FactoryDispatcher.Product(product_name)
    context = ProductContext(productObject, None, product)

    # Look for an 'initialize' method in the product.
    initmethod = pgetattr(product, 'initialize', None)
    if initmethod is not None:
        initmethod(context)
Esempio n. 7
0
def install_package(app, module, init_func, raise_exc=False, log_exc=True):
    """Installs a Python package like a product."""
    try:
        product = App.Product.initializeProduct(module, module.__name__,
                                                module.__path__[0], app)
        product.package_name = module.__name__
        if init_func is not None:
            newContext = ProductContext(product, app, module)
            init_func(newContext)

        if not doInstall():
            transaction.abort()
        else:
            transaction.get().note('Installed package %s' % module.__name__)
            transaction.commit()

    except:
        if log_exc:
            LOG.error("Couldn't install %s" % module.__name__, exc_info=True)
        transaction.abort()
        if raise_exc:
            raise
Esempio n. 8
0
def install_product(app,
                    product_dir,
                    product_name,
                    meta_types,
                    folder_permissions,
                    raise_exc=0,
                    log_exc=1):

    from App.ProductContext import ProductContext
    path_join = os.path.join
    isdir = os.path.isdir
    exists = os.path.exists
    global_dict = globals()
    silly = ('__doc__', )

    if 1:  # Preserve indentation for diff :-)
        package_dir = path_join(product_dir, product_name)
        __traceback_info__ = product_name
        if not isdir(package_dir): return
        if not exists(path_join(package_dir, '__init__.py')):
            if not exists(path_join(package_dir, '__init__.pyc')):
                if not exists(path_join(package_dir, '__init__.pyo')):
                    return
        try:
            product = __import__("Products.%s" % product_name, global_dict,
                                 global_dict, silly)

            # Install items into the misc_ namespace, used by products
            # and the framework itself to store common static resources
            # like icon images.
            misc_ = pgetattr(product, 'misc_', {})
            if misc_:
                if isinstance(misc_, dict):
                    misc_ = Misc_(product_name, misc_)
                Application.misc_.__dict__[product_name] = misc_

            # Here we create a ProductContext object which contains
            # information about the product and provides an interface
            # for registering things like classes and help topics that
            # should be associated with that product. Products are
            # expected to implement a method named 'initialize' in
            # their __init__.py that takes the ProductContext as an
            # argument.
            do_install = doInstall()
            if do_install:
                productObject = App.Product.initializeProduct(
                    product, product_name, package_dir, app)
                context = ProductContext(productObject, app, product)
            else:
                # avoid any persistent connection
                productObject = FactoryDispatcher.Product(product_name)
                context = ProductContext(productObject, None, product)

            # Look for an 'initialize' method in the product.
            initmethod = pgetattr(product, 'initialize', None)
            if initmethod is not None:
                initmethod(context)

            if do_install:
                transaction.get().note('Installed product ' + product_name)
                transaction.commit()

        except Exception:
            if log_exc:
                LOG.error('Couldn\'t install %s' % product_name,
                          exc_info=sys.exc_info())
            transaction.abort()
            if raise_exc:
                raise
Esempio n. 9
0
def install_product(app,
                    product_dir,
                    product_name,
                    meta_types,
                    folder_permissions,
                    raise_exc=0,
                    log_exc=1):

    path_join = os.path.join
    isdir = os.path.isdir
    exists = os.path.exists
    global_dict = globals()
    silly = ('__doc__', )

    if 1:  # Preserve indentation for diff :-)
        package_dir = path_join(product_dir, product_name)
        __traceback_info__ = product_name
        if not isdir(package_dir): return
        if not exists(path_join(package_dir, '__init__.py')):
            if not exists(path_join(package_dir, '__init__.pyc')):
                if not exists(path_join(package_dir, '__init__.pyo')):
                    return
        try:
            product = __import__("Products.%s" % product_name, global_dict,
                                 global_dict, silly)

            # Install items into the misc_ namespace, used by products
            # and the framework itself to store common static resources
            # like icon images.
            misc_ = pgetattr(product, 'misc_', {})
            if misc_:
                if isinstance(misc_, dict):
                    misc_ = Misc_(product_name, misc_)
                Application.misc_.__dict__[product_name] = misc_

            # Here we create a ProductContext object which contains
            # information about the product and provides an interface
            # for registering things like classes and help topics that
            # should be associated with that product. Products are
            # expected to implement a method named 'initialize' in
            # their __init__.py that takes the ProductContext as an
            # argument.
            productObject = App.Product.initializeProduct(
                product, product_name, package_dir, app)
            context = ProductContext(productObject, app, product)

            # Look for an 'initialize' method in the product. If it does
            # not exist, then this is an old product that has never been
            # updated. In that case, we will analyze the product and
            # build up enough information to do initialization manually.
            initmethod = pgetattr(product, 'initialize', None)
            if initmethod is not None:
                initmethod(context)

            # Support old-style product metadata. Older products may
            # define attributes to name their permissions, meta_types,
            # constructors, etc.
            permissions = {}
            new_permissions = {}
            if pgetattr(product, '__ac_permissions__', None) is not None:
                warn(
                    '__init__.py of %s has a long deprecated '
                    '\'__ac_permissions__\' attribute. '
                    '\'__ac_permissions__\' will be ignored by '
                    'install_product in Zope 2.11. Please use registerClass '
                    'instead.' % product.__name__, DeprecationWarning)
            for p in pgetattr(product, '__ac_permissions__', ()):
                permission, names, default = (tuple(p) + ('Manager', ))[:3]
                if names:
                    for name in names:
                        permissions[name] = permission
                elif not folder_permissions.has_key(permission):
                    new_permissions[permission] = ()

            if pgetattr(product, 'meta_types', None) is not None:
                warn(
                    '__init__.py of %s has a long deprecated \'meta_types\' '
                    'attribute. \'meta_types\' will be ignored by '
                    'install_product in Zope 2.11. Please use registerClass '
                    'instead.' % product.__name__, DeprecationWarning)
            for meta_type in pgetattr(product, 'meta_types', ()):
                # Modern product initialization via a ProductContext
                # adds 'product' and 'permission' keys to the meta_type
                # mapping. We have to add these here for old products.
                pname = permissions.get(meta_type['action'], None)
                if pname is not None:
                    meta_type['permission'] = pname
                meta_type['product'] = productObject.id
                meta_type['visibility'] = 'Global'
                meta_types.append(meta_type)

            if pgetattr(product, 'methods', None) is not None:
                warn(
                    "__init__.py of %s has a long deprecated 'methods' "
                    "attribute. 'methods' support might be removed in Zope "
                    "2.11 or a later feature release. Please use the "
                    "'legacy' argument of registerClass instead if the "
                    "methods are constructors. Or refactor the product "
                    "using adapters." % product.__name__, DeprecationWarning)
            for name, method in pgetattr(product, 'methods', {}).items():
                if not hasattr(Folder.Folder, name):
                    setattr(Folder.Folder, name, method)
                    if name[-9:] != '__roles__':  # not Just setting roles
                        if (permissions.has_key(name)
                                and not folder_permissions.has_key(
                                    permissions[name])):
                            permission = permissions[name]
                            if new_permissions.has_key(permission):
                                new_permissions[permission].append(name)
                            else:
                                new_permissions[permission] = [name]

            if new_permissions:
                new_permissions = new_permissions.items()
                for permission, names in new_permissions:
                    folder_permissions[permission] = names
                new_permissions.sort()
                Folder.Folder.__ac_permissions__ = tuple(
                    list(Folder.Folder.__ac_permissions__) + new_permissions)

            if not doInstall():
                transaction.abort()
            else:
                transaction.get().note('Installed product ' + product_name)
                transaction.commit()

        except:
            if log_exc:
                LOG.error('Couldn\'t install %s' % product_name,
                          exc_info=sys.exc_info())
            transaction.abort()
            if raise_exc:
                raise