def test_html_report(self):
        'Create HTML Report'
        pool = Pool()
        ActionReport = pool.get('ir.action.report')
        Template = pool.get('html.template')
        HTMLTemplateTranslation = pool.get('html.template.translation')
        Model = pool.get('ir.model')

        model, = Model.search([('model', '=', 'ir.model')], limit=1)

        with file_open('html_report/tests/base.html') as f:
            tpl_base, = Template.create([{
                'name': 'Base',
                'type': 'base',
                'content': f.read(),
            }])

        with file_open('html_report/tests/models.html') as f:
            tpl_models, = Template.create([{
                'name': 'Modules',
                'type': 'extension',
                'content': f.read(),
                'parent': tpl_base,
            }])

        report, = ActionReport.create([{
            'name': 'Models',
            'model': 'ir.model',
            'report_name': 'ir.model.report',
            'template_extension': 'jinja',
            'extension': 'html',
            'html_template': tpl_models,
        }])

        models = Model.search([('model', 'like', 'ir.model%')])

        self.assertTrue(report.id)
        self.assertTrue('block body' in report.html_content, True)

        HTMLTemplateTranslation.create([{
            'lang': 'es',
            'src': 'Name',
            'value': 'Nombre',
            'report': report.id,
        }, {
            'lang': 'es',
            'src': 'Model',
            'value': 'Modelo',
            'report': report.id,
        }])

        with Transaction().set_context(language='es'):
            ModelReport = Pool().get('ir.model.report', type='report')
            ext, content, _, _ = ModelReport.execute([m.id for m in models],
                                                     {})
            self.assertTrue(ext, 'html')
            self.assertTrue('ir.model' in content, True)
            self.assertTrue('Nombre' in content, True)
            self.assertTrue('Modelo' in content, True)
def set_afip_certs(company=None, config=None):
    "Set AFIP certificates"
    if not company:
        company = get_company()
    with file_open('account_invoice_ar/tests/gcoop.crt', mode='rb') as fp:
        crt = fp.read()
        company.pyafipws_certificate = crt.decode('utf8')
    with file_open('account_invoice_ar/tests/gcoop.key', mode='rb') as fp:
        key = fp.read()
        company.pyafipws_private_key = key.decode('utf8')
    company.pyafipws_mode_cert = 'homologacion'
    company.save()
    return company
 def get_module_info(name):
     "Return the content of the __tryton__.py"
     try:
         if name in ['ir', 'res', 'webdav']:
             file_p = tools.file_open(os.path.join(name, '__tryton__.py'))
         else:
             file_p = tools.file_open(os.path.join(name, '__tryton__.py'))
         with file_p:
             data = file_p.read()
         info = tools.safe_eval(data)
     except Exception:
         return {}
     return info
Example #4
0
    def _get_installed_module_directories(cls):
        """
        A generator that yields tuples of the format (module_name, directory)
        for every installed module in the current database
        """
        from trytond.modules import create_graph, get_module_list, \
            MODULES_PATH, EGG_MODULES

        IrModule = Pool().get('ir.module.module')

        packages = list(create_graph(get_module_list())[0])[::-1]
        installed_module_list = map(
            lambda module: module.name,
            IrModule.search([('state', '=', 'installed')]))

        for package in packages:
            if package.name not in installed_module_list:
                # this package is not installed as a module in this
                # database and hence the tranlation is not relevant
                continue
            if package.name in EGG_MODULES:
                # trytond.tools has a good helper which allows resources to
                # be loaded from the installed site packages. Just use it
                # to load the tryton.cfg file which is guaranteed to exist
                # and from it lookup the directory. From here, its just
                # another searchpath for the loader.
                f = file_open(os.path.join(package.name, 'tryton.cfg'))
                module_dir = os.path.dirname(f.name)
            else:
                module_dir = os.path.join(MODULES_PATH, package.name)

            yield package.name, module_dir
Example #5
0
 def base64(name):
     module, path = name.split('/', 1)
     with file_open(os.path.join(module, path), 'rb') as f:
         value = binascii.b2a_base64(f.read())
         value = value.decode('ascii')
         mimetype = mimetypes.guess_type(f.name)[0]
         return ('data:%s;base64,%s' % (mimetype, value)).strip()
Example #6
0
    def jinja_loader_func(cls, name):
        """
        Return the template from the module directories or ID from other template.

        The name is expected to be in the format:

            <module_name>/path/to/template

        for example, if the account_invoice_html_report module had a base
        template in its reports folder, then you should be able to use:

            {% extends 'html_report/report/base.html' %}
        """
        Template = Pool().get('html.template')

        if '/' in name:
            module, path = name.split('/', 1)
            try:
                with file_open(os.path.join(module, path)) as f:
                    return f.read()
            except IOError:
                return None
        else:
            template, = Template.search([('id', '=', name)], limit=1)
            return template.all_content
Example #7
0
    def _get_installed_module_directories(cls):
        """
        A generator that yields tuples of the format (module_name, directory)
        for every installed module in the current database
        """
        from trytond.modules import create_graph, get_module_list, \
            MODULES_PATH, EGG_MODULES

        IrModule = Pool().get('ir.module.module')

        packages = list(create_graph(get_module_list())[0])[::-1]
        installed_module_list = map(
            lambda module: module.name,
            IrModule.search([('state', '=', 'installed')])
        )

        for package in packages:
            if package.name not in installed_module_list:
                # this package is not installed as a module in this
                # database and hence the tranlation is not relevant
                continue
            if package.name in EGG_MODULES:
                # trytond.tools has a good helper which allows resources to
                # be loaded from the installed site packages. Just use it
                # to load the tryton.cfg file which is guaranteed to exist
                # and from it lookup the directory. From here, its just
                # another searchpath for the loader.
                f = file_open(os.path.join(package.name, 'tryton.cfg'))
                module_dir = os.path.dirname(f.name)
            else:
                module_dir = os.path.join(MODULES_PATH, package.name)

            yield package.name, module_dir
Example #8
0
 def get_icon(self, ids, name):
     result = {}
     for icon in self.browse(ids):
         path = os.path.join(icon.module, icon.path.replace('/', os.sep))
         with file_open(path, subdir='modules') as fp:
             result[icon.id] = fp.read()
     return result
Example #9
0
 def get_icon(self, name):
     path = os.path.join(self.module, self.path.replace('/', os.sep))
     with file_open(
             path,
             subdir='modules' if self.module not in {'ir', 'res'} else '',
             mode='r',
             encoding='utf-8') as fp:
         return fp.read()
Example #10
0
    def loaders(self):
        '''
        Lazy load the loaders
        '''
        if self._loaders is None:
            self._loaders = []

            if not Transaction().connection.cursor():
                contextmanager = Transaction().start(self.database_name, 0)
            else:
                contextmanager = contextlib.nested(
                    Transaction().set_user(0),
                    Transaction().reset_context()
                )
            with contextmanager:
                cursor = Transaction().connection.cursor()
                cursor.execute(
                    "SELECT name FROM ir_module "
                    "WHERE state = 'installed'"
                )
                installed_module_list = [name for (name,) in cursor.fetchall()]

            if self.searchpath is not None:
                self._loaders.append(FileSystemLoader(self.searchpath))

            # Look into the module graph and check if they have template
            # folders and if they do add them too
            from trytond.modules import create_graph, get_module_list, \
                MODULES_PATH, EGG_MODULES

            packages = list(create_graph(get_module_list())[0])[::-1]
            for package in packages:
                if package.name not in installed_module_list:
                    # If the module is not installed in the current database
                    # then don't load the templates either to be consistent
                    # with Tryton's modularity
                    continue
                if package.name in EGG_MODULES:
                    # trytond.tools has a good helper which allows resources to
                    # be loaded from the installed site packages. Just use it
                    # to load the tryton.cfg file which is guaranteed to exist
                    # and from it lookup the directory. From here, its just
                    # another searchpath for the loader.
                    f = tools.file_open(
                        os.path.join(package.name, 'tryton.cfg')
                    )
                    template_dir = os.path.join(
                        os.path.dirname(f.name), 'templates'
                    )
                else:
                    template_dir = os.path.join(
                        MODULES_PATH, package.name, 'templates'
                    )
                if os.path.isdir(template_dir):
                    # Add to FS Loader only if the folder exists
                    self._loaders.append(FileSystemLoader(template_dir))

        return self._loaders
def create_graph(module_list):
    graph = Graph()
    packages = []

    for module in module_list:
        tryton_file = OPJ(MODULES_PATH, module, '__tryton__.py')
        mod_path = OPJ(MODULES_PATH, module)
        if module in ('ir', 'res', 'webdav', 'test'):
            root_path = os.path.abspath(os.path.dirname(
                    os.path.dirname(__file__)))
            tryton_file = OPJ(root_path, module, '__tryton__.py')
            mod_path = OPJ(root_path, module)
        elif module in EGG_MODULES:
            ep = EGG_MODULES[module]
            tryton_file = OPJ(ep.dist.location, 'trytond', 'modules', module,
                    '__tryton__.py')
            mod_path = OPJ(ep.dist.location, 'trytond', 'modules', module)
            if not os.path.isfile(tryton_file) or not os.path.isdir(mod_path):
                # When testing modules from setuptools location is the module
                # directory
                tryton_file = OPJ(ep.dist.location, '__tryton__.py')
                mod_path = os.path.dirname(ep.dist.location)
        if os.path.isfile(tryton_file):
            with tools.file_open(tryton_file, subdir='') as fp:
                info = tools.safe_eval(fp.read())
            packages.append((module, info.get('depends', []),
                    info.get('extras_depend', []), info))
        elif module != 'all':
            raise Exception('Module %s not found' % module)

    current, later = set([x[0] for x in packages]), set()
    all_packages = set(current)
    while packages and current > later:
        package, deps, xdep, info = packages[0]

        # if all dependencies of 'package' are already in the graph,
        # add 'package' in the graph
        all_deps = deps + [x for x in xdep if x in all_packages]
        if reduce(lambda x, y: x and y in graph, all_deps, True):
            if not package in current:
                packages.pop(0)
                continue
            later.clear()
            current.remove(package)
            graph.add_node(package, all_deps)
            node = Node(package, graph)
            node.info = info
        else:
            later.add(package)
            packages.append((package, deps, xdep, info))
        packages.pop(0)

    for package, deps, xdep, info in packages:
        if package not in later:
            continue
        missings = [x for x in deps if x not in graph]
        raise Exception('%s unmet dependencies: %s' % (package, missings))
    return graph, packages, later
Example #12
0
    def loaders(self):
        '''
        Lazy load the loaders
        '''
        if self._loaders is None:
            self._loaders = []

            if not Transaction().cursor:
                contextmanager = Transaction().start(self.database_name, 0)
            else:
                contextmanager = contextlib.nested(
                    Transaction().set_user(0),
                    Transaction().reset_context()
                )
            with contextmanager:
                cursor = Transaction().cursor
                cursor.execute(
                    "SELECT name FROM ir_module_module "
                    "WHERE state = 'installed'"
                )
                installed_module_list = [name for (name,) in cursor.fetchall()]

            if self.searchpath is not None:
                self._loaders.append(FileSystemLoader(self.searchpath))

            # Look into the module graph and check if they have template
            # folders and if they do add them too
            from trytond.modules import create_graph, get_module_list, \
                MODULES_PATH, EGG_MODULES

            packages = list(create_graph(get_module_list())[0])[::-1]
            for package in packages:
                if package.name not in installed_module_list:
                    # If the module is not installed in the current database
                    # then don't load the templates either to be consistent
                    # with Tryton's modularity
                    continue
                if package.name in EGG_MODULES:
                    # trytond.tools has a good helper which allows resources to
                    # be loaded from the installed site packages. Just use it
                    # to load the tryton.cfg file which is guaranteed to exist
                    # and from it lookup the directory. From here, its just
                    # another searchpath for the loader.
                    f = tools.file_open(
                        os.path.join(package.name, 'tryton.cfg')
                    )
                    template_dir = os.path.join(
                        os.path.dirname(f.name), 'templates'
                    )
                else:
                    template_dir = os.path.join(
                        MODULES_PATH, package.name, 'templates'
                    )
                if os.path.isdir(template_dir):
                    # Add to FS Loader only if the folder exists
                    self._loaders.append(FileSystemLoader(template_dir))

        return self._loaders
Example #13
0
    def test_file_open(self):
        "Test file_open"
        with file_open('__init__.py', subdir=None) as fp:
            self.assertTrue(fp)

        with file_open('ir/__init__.py') as fp:
            self.assertTrue(fp)

        with self.assertRaisesRegex(IOError, "File not found :"):
            with file_open('ir/noexist'):
                pass

        with self.assertRaisesRegex(IOError, "Permission denied:"):
            with file_open('/etc/passwd'):
                pass

        with self.assertRaisesRegex(IOError, "Permission denied:"):
            with file_open('../../foo'):
                pass
Example #14
0
 def test_xml_files(self):
     "Test validity of the xml files of the module"
     config = ConfigParser()
     with file_open('%s/tryton.cfg' % self.module,
                    subdir='modules',
                    mode='r',
                    encoding='utf-8') as fp:
         config.read_file(fp)
     if not config.has_option('tryton', 'xml'):
         return
     with file_open('tryton.rng', subdir='', mode='rb') as fp:
         rng = etree.parse(fp)
     validator = etree.RelaxNG(etree=rng)
     for xml_file in filter(None, config.get('tryton', 'xml').splitlines()):
         with file_open('%s/%s' % (self.module, xml_file),
                        subdir='modules',
                        mode='rb') as fp:
             tree = etree.parse(fp)
         validator.assertValid(tree)
Example #15
0
 def get_content(self, name):
     if not self.filename:
         return self.data
     try:
         with file_open(self.filename,
                        subdir='modules',
                        mode='r',
                        encoding='utf-8') as fp:
             return fp.read()
     except IOError:
         return
Example #16
0
def get_module_info(name):
    "Return the content of the tryton.cfg"
    module_config = ConfigParser.ConfigParser()
    with tools.file_open(os.path.join(name, "tryton.cfg")) as fp:
        module_config.readfp(fp)
        directory = os.path.dirname(fp.name)
    info = dict(module_config.items("tryton"))
    info["directory"] = directory
    for key in ("depends", "extras_depend", "xml"):
        if key in info:
            info[key] = info[key].strip().splitlines()
    return info
Example #17
0
def get_module_info(name):
    "Return the content of the tryton.cfg"
    module_config = ConfigParser.ConfigParser()
    with tools.file_open(os.path.join(name, 'tryton.cfg')) as fp:
        module_config.readfp(fp)
        directory = os.path.dirname(fp.name)
    info = dict(module_config.items('tryton'))
    info['directory'] = directory
    for key in ('depends', 'extras_depend', 'xml'):
        if key in info:
            info[key] = info[key].strip().splitlines()
    return info
Example #18
0
def get_module_info(name):
    "Return the content of the tryton.cfg"
    config = ConfigParser.ConfigParser()
    with tools.file_open(os.path.join(name, 'tryton.cfg')) as fp:
        config.readfp(fp)
        directory = os.path.dirname(fp.name)
    info = dict(config.items('tryton'))
    info['directory'] = directory
    for key in ('depends', 'extras_depend', 'xml'):
        if key in info:
            info[key] = info[key].strip().splitlines()
    return info
Example #19
0
 def get_arch(self, name):
     value = None
     if self.name and self.module:
         path = os.path.join(self.module, 'view', self.name + '.xml')
         try:
             with file_open(path, subdir='modules') as fp:
                 value = fp.read()
         except IOError:
             pass
     if not value:
         value = self.data
     return value
Example #20
0
 def get_arch(self, name):
     value = None
     if self.name and self.module:
         path = os.path.join(self.module, 'view', self.name + '.xml')
         try:
             with file_open(path, subdir='modules') as fp:
                 value = fp.read()
         except IOError:
             pass
     if not value:
         value = self.data
     return value
Example #21
0
 def jinja_loader_func(cls, name):  # pragma: no cover
     """
     Return the template from the module directories using the logic
     below:
     The name is expected to be in the format:
         <module_name>/path/to/email/template
     for example, if the party module had a base email template in
     its emails folder, then you should be able to use:
         {% extends 'party/emails/base.html' %}
     """
     module, path = name.split("/", 1)
     try:
         return file_open(os.path.join(module, path)).read()
     except IOError:
         return None
Example #22
0
 def jinja_loader_func(cls, name):  # pragma: no cover
     """
     Return the template from the module directories using the logic
     below:
     The name is expected to be in the format:
         <module_name>/path/to/email/template
     for example, if the party module had a base email template in
     its emails folder, then you should be able to use:
         {% extends 'party/emails/base.html' %}
     """
     module, path = name.split('/', 1)
     try:
         return file_open(os.path.join(module, path)).read()
     except IOError:
         return None
    def jinja_loader_func(cls, name):
        """
        Return the template from the module directories using the logic below:

        The name is expected to be in the format:

            <module_name>/path/to/template

        for example, if the account_invoice_html_report module had a base
        template in its reports folder, then you should be able to use:

            {% extends 'html_report/report/base.html' %}
        """
        module, path = name.split('/', 1)
        try:
            with file_open(os.path.join(module, path)) as f:
                return f.read()
        except IOError:
            return None
    def jinja_loader_func(cls, name):
        """
        Return the template from the module directories using the logic
        below:

        The name is expected to be in the format:

            <module_name>/path/to/template

        for example, if the account_reports module had a base template in
        its reports folder, then you should be able to use:

            {% extends 'account_reports/report/base.html' %}
        """
        module, path = name.split('/', 1)
        try:
            return file_open(os.path.join(module, path)).read()
        except IOError:
            return None
Example #25
0
 def get_report_content(cls, reports, name):
     contents = {}
     converter = fields.Binary.cast
     default = None
     format_ = Transaction().context.get('%s.%s' % (cls.__name__, name), '')
     if format_ == 'size':
         converter = len
         default = 0
     for report in reports:
         data = getattr(report, name + '_custom')
         if not data and getattr(report, name[:-8]):
             try:
                 with file_open(getattr(report,
                                        name[:-8]).replace('/', os.sep),
                                mode='rb') as fp:
                     data = fp.read()
             except Exception:
                 data = None
         contents[report.id] = converter(data) if data else default
     return contents
    def make_pkpass(self):
        """
        Builds a pass by calling the origin model for it and then adds a serial
        number and signs it and builds the pkpass file.
        """
        passfile = self.origin.get_passfile()

        # Add information to be provided by this API
        passfile.serialNumber = str(self.id)
        passfile.webServiceURL = url_for('nereid.passbook.pass.webservice_url',
                                         _external=True,
                                         _secure=True)
        passfile.authenticationToken = self.authentication_token

        # TODO: What to do if barcode is not there ?

        return passfile.create(
            config.get('nereid_passbook', 'certificate'),
            config.get('nereid_passbook', 'key'),
            file_open('nereid_passbook/wwdr.pem').name,
            '',  # Password for pem file ?
        )
 def module_path(name):
     module, path = name.split('/', 1)
     with file_open(os.path.join(module, path)) as f:
         return 'file://' + f.name
Example #28
0
def empty(request):
    fp = file_open('marketing_automation/empty.gif', mode='rb')
    return Response(fp, 200, content_type='image/gif', direct_passthrough=True)
Example #29
0
    def migrate_modules(cursor):
        modules_in_dir = get_module_list()
        modules_to_migrate = {}
        for module_dir in modules_in_dir:
            try:
                with tools.file_open(OPJ(module_dir,
                                         '__migrated_modules')) as f:
                    for line in f.readlines():
                        line = line.replace(' ', '').strip('\n')
                        if not line:
                            continue
                        action, old_module = line.split(':')
                        modules_to_migrate[old_module] = (action, module_dir)
            except IOError:
                continue

        cursor.execute(*ir_module.select(ir_module.name))
        for module_in_db, in cursor.fetchall():
            if (module_in_db in modules_in_dir
                    or module_in_db in modules_to_migrate):
                continue
            else:
                modules_to_migrate[module_in_db] = ('to_drop', None)

        # PKUNK 9502 add logs and control before uninstall modules
        if (not AUTO_UNINSTALL):
            dropped = False
            for module in modules_to_migrate:
                if modules_to_migrate[module][0] == 'to_drop':
                    logger.critical(
                        'To uninstall %s you should set'
                        ' COOG_AUTO_UNINSTALL environnement variable' % module)
                    dropped = True
            if dropped:
                sys.exit(1)
        else:
            for module in modules_to_migrate:
                if modules_to_migrate[module][0] == 'to_drop':
                    logger.warning('%s is about to be uninstalled' % (module))
        # PKUNK 9502 end

        def rename(cursor, table_name, old_name, new_name, var_name):
            table = Table(table_name)
            fields = None
            # If the view already exists in destination module
            if table_name == 'ir_model_data':
                fields = ['fs_id', 'model']
            if table_name == 'ir_ui_view':
                fields = ['model', 'name']
            if fields:
                query = ('DELETE from %(table)s where '
                         '(%(fields)s) in ('
                         'SELECT %(fields)s FROM %(table)s WHERE '
                         '"module" IN (\'%(old_name)s\', \'%(new_name)s\') '
                         'GROUP BY %(fields)s '
                         'HAVING COUNT("module") > 1) '
                         'and "module" = \'%(old_name)s\';' % {
                             'table': table_name,
                             'old_name': old_name,
                             'new_name': new_name,
                             'fields':
                             (', '.join('"' + f + '"' for f in fields))
                         })
                cursor.execute(query)

            query = table.update([getattr(table, var_name)], [new_name],
                                 where=(getattr(table, var_name) == old_name))
            cursor.execute(*query)

        def delete(cursor, table_name, old_name, var_name):
            table = Table(table_name)
            cursor.execute(*table.delete(
                where=(getattr(table, var_name) == old_name)))

        for old_name, (action, new_name) in modules_to_migrate.items():
            cursor.execute(*ir_module.select(Count(ir_module.id),
                                             where=ir_module.name == old_name))
            count, = cursor.fetchone()
            if not count:
                continue

            if action == 'to_drop':
                logger.info('%s directory has been removed from filesystem,'
                            ' deleting entries from database...' % old_name)
            else:
                logger.info('%s has been %s %s, updating database...' %
                            (old_name, {
                                'to_rename': 'renamed into',
                                'to_merge': 'merged with'
                            }[action], new_name))
            if new_name:
                rename(cursor, 'ir_model', old_name, new_name, 'module')
                rename(cursor, 'ir_action_report', old_name, new_name,
                       'module')
                rename(cursor, 'ir_model_field', old_name, new_name, 'module')
                rename(cursor, 'ir_model_data', old_name, new_name, 'module')
                rename(cursor, 'ir_translation', old_name, new_name, 'module')
                rename(cursor, 'ir_translation', old_name, new_name,
                       'overriding_module')
                rename(cursor, 'ir_ui_icon', old_name, new_name, 'module')
                rename(cursor, 'ir_ui_view', old_name, new_name, 'module')

            if action == 'to_rename':
                rename(cursor, 'ir_module_dependency', old_name, new_name,
                       'name')
                rename(cursor, 'ir_module', old_name, new_name, 'name')
            elif action == 'to_merge':
                delete(cursor, 'ir_module_dependency', old_name, 'name')
                delete(cursor, 'ir_module', old_name, 'name')
            elif action == 'to_drop':
                delete(cursor, 'ir_model', old_name, 'module')
                delete(cursor, 'ir_action_report', old_name, 'module')
                delete(cursor, 'ir_model_field', old_name, 'module')
                delete(cursor, 'ir_model_data', old_name, 'module')
                delete(cursor, 'ir_translation', old_name, 'module')
                delete(cursor, 'ir_translation', old_name, 'overriding_module')
                delete(cursor, 'ir_ui_icon', old_name, 'module')
                delete(cursor, 'ir_ui_view', old_name, 'module')
                delete(cursor, 'ir_module_dependency', old_name, 'name')
                delete(cursor, 'ir_module', old_name, 'name')
Example #30
0
def load_module_graph(graph, pool, lang=None):
    if lang is None:
        lang = [CONFIG['language']]
    modules_todo = []
    models_to_update_history = set()
    logger = logging.getLogger('modules')
    cursor = Transaction().cursor

    modules = [x.name for x in graph]
    cursor.execute('SELECT name, state FROM ir_module_module '
        'WHERE name in (' + ','.join(('%s',) * len(modules)) + ')',
        modules)
    module2state = dict(cursor.fetchall())

    for package in graph:
        module = package.name
        if module not in MODULES:
            continue
        logger.info(module)
        sys.stdout.flush()
        classes = pool.setup(module)
        package_state = module2state.get(module, 'uninstalled')
        if (is_module_to_install(module)
                or package_state in ('to install', 'to upgrade')):
            if package_state not in ('to install', 'to upgrade'):
                package_state = 'to install'
            for child in package.childs:
                module2state[child.name] = package_state
            for type in classes.keys():
                for cls in classes[type]:
                    logger.info('%s:register %s' % (module, cls.__name__))
                    cls.__register__(module)
            for model in classes['model']:
                if hasattr(model, '_history'):
                    models_to_update_history.add(model.__name__)

            #Instanciate a new parser for the package:
            tryton_parser = convert.TrytondXmlHandler(pool=pool, module=module)

            for filename in package.info.get('xml', []):
                filename = filename.replace('/', os.sep)
                logger.info('%s:loading %s' % (module, filename))
                # Feed the parser with xml content:
                with tools.file_open(OPJ(module, filename)) as fp:
                    tryton_parser.parse_xmlstream(fp)

            modules_todo.append((module, list(tryton_parser.to_delete)))

            for filename in iglob('%s/%s/*.po'
                    % (package.info['directory'], 'locale')):
                filename = filename.replace('/', os.sep)
                lang2 = os.path.splitext(os.path.basename(filename))[0]
                if lang2 not in lang:
                    continue
                logger.info('%s:loading %s' % (module,
                        filename[len(package.info['directory']) + 1:]))
                Translation = pool.get('ir.translation')
                Translation.translation_import(lang2, module, filename)

            cursor.execute('SELECT id FROM ir_module_module '
                'WHERE name = %s', (package.name,))
            try:
                module_id, = cursor.fetchone()
                cursor.execute('UPDATE ir_module_module SET state = %s '
                    'WHERE id = %s', ('installed', module_id))
            except TypeError:
                cursor.execute('INSERT INTO ir_module_module '
                    '(create_uid, create_date, name, state) '
                    'VALUES (%s, %s, %s, %s)', (0, datetime.datetime.now(),
                        package.name, 'installed'))
            module2state[package.name] = 'installed'

        cursor.commit()

    for model_name in models_to_update_history:
        model = pool.get(model_name)
        if model._history:
            logger.info('history:update %s' % model.__name__)
            model._update_history_table()

    # Vacuum :
    while modules_todo:
        (module, to_delete) = modules_todo.pop()
        convert.post_import(pool, module, to_delete)

    cursor.commit()
Example #31
0
File: icon.py Project: Sisouvan/ogh
 def get_icon(self, name):
     path = os.path.join(self.module, self.path.replace('/', os.sep))
     with file_open(path, subdir='modules') as fp:
         return fp.read()
Example #32
0
    def migrate_modules(cursor):
        modules_in_dir = get_module_list()
        modules_to_migrate = {}
        for module_dir in modules_in_dir:
            try:
                with tools.file_open(OPJ(module_dir,
                                         '__migrated_modules')) as f:
                    for line in f.readlines():
                        line = line.replace(' ', '').strip('\n')
                        if not line:
                            continue
                        action, old_module = line.split(':')
                        modules_to_migrate[old_module] = (action, module_dir)
            except IOError:
                continue

        cursor.execute(*ir_module.select(ir_module.name))
        for module_in_db, in cursor.fetchall():
            if (module_in_db in modules_in_dir
                    or module_in_db in modules_to_migrate):
                continue
            else:
                modules_to_migrate[module_in_db] = ('to_drop', None)

        def rename(cursor, table_name, old_name, new_name, var_name):
            table = Table(table_name)
            cursor.execute(
                *table.update([getattr(table, var_name)], [new_name],
                              where=(getattr(table, var_name) == old_name)))

        def delete(cursor, table_name, old_name, var_name):
            table = Table(table_name)
            cursor.execute(*table.delete(
                where=(getattr(table, var_name) == old_name)))

        for old_name, (action, new_name) in modules_to_migrate.iteritems():
            cursor.execute(*ir_module.select(Count(ir_module.id),
                                             where=ir_module.name == old_name))
            count, = cursor.fetchone()
            if not count:
                continue

            if action == 'to_drop':
                logger.info('%s directory has been removed from filesystem,'
                            ' deleting entries from database...' % old_name)
            else:
                logger.info('%s has been %s %s, updating database...' %
                            (old_name, {
                                'to_rename': 'renamed into',
                                'to_merge': 'merged with'
                            }[action], new_name))
            if new_name:
                rename(cursor, 'ir_model', old_name, new_name, 'module')
                rename(cursor, 'ir_action_report', old_name, new_name,
                       'module')
                rename(cursor, 'ir_model_field', old_name, new_name, 'module')
                rename(cursor, 'ir_model_data', old_name, new_name, 'module')
                rename(cursor, 'ir_translation', old_name, new_name, 'module')
                rename(cursor, 'ir_translation', old_name, new_name,
                       'overriding_module')
                rename(cursor, 'ir_ui_icon', old_name, new_name, 'module')
                rename(cursor, 'ir_ui_view', old_name, new_name, 'module')

            if action == 'to_rename':
                rename(cursor, 'ir_module_dependency', old_name, new_name,
                       'name')
                rename(cursor, 'ir_module', old_name, new_name, 'name')
            elif action == 'to_merge':
                delete(cursor, 'ir_module_dependency', old_name, 'name')
                delete(cursor, 'ir_module', old_name, 'name')
            elif action == 'to_drop':
                delete(cursor, 'ir_model', old_name, 'module')
                delete(cursor, 'ir_action_report', old_name, 'module')
                delete(cursor, 'ir_model_field', old_name, 'module')
                delete(cursor, 'ir_model_data', old_name, 'module')
                delete(cursor, 'ir_translation', old_name, 'module')
                delete(cursor, 'ir_translation', old_name, 'overriding_module')
                delete(cursor, 'ir_ui_icon', old_name, 'module')
                delete(cursor, 'ir_ui_view', old_name, 'module')
                delete(cursor, 'ir_module_dependency', old_name, 'name')
                delete(cursor, 'ir_module', old_name, 'name')
def load_module_graph(graph, pool, lang=None):
    if lang is None:
        lang = [CONFIG['language']]
    modules_todo = []
    models_to_update_history = set()
    logger = logging.getLogger('modules')
    cursor = Transaction().cursor

    modules = [x.name for x in graph]
    cursor.execute('SELECT name, state FROM ir_module_module '
        'WHERE name in (' + ','.join(('%s',) * len(modules)) + ')',
        modules)
    module2state = dict(cursor.fetchall())

    for package in graph:
        module = package.name
        if module not in MODULES:
            continue
        logger.info(module)
        sys.stdout.flush()
        objects = pool.instanciate(module)
        package_state = module2state.get(module, 'uninstalled')
        if (is_module_to_install(module)
                or package_state in ('to install', 'to upgrade')):
            for type in objects.keys():
                for obj in objects[type]:
                    logger.info('%s:init %s' % (module, obj._name))
                    obj.init(module)
            for model in objects['model']:
                if hasattr(model, '_history'):
                    models_to_update_history.add(model._name)

            #Instanciate a new parser for the package:
            tryton_parser = convert.TrytondXmlHandler(pool=pool, module=module)

            for filename in package.info.get('xml', []):
                filename = filename.replace('/', os.sep)
                logger.info('%s:loading %s' % (module, filename))
                # Feed the parser with xml content:
                with tools.file_open(OPJ(module, filename)) as fp:
                    tryton_parser.parse_xmlstream(fp)

            modules_todo.append((module, list(tryton_parser.to_delete)))

            for filename in package.info.get('translation', []):
                filename = filename.replace('/', os.sep)
                lang2 = os.path.splitext(os.path.basename(filename))[0]
                if lang2 not in lang:
                    continue
                logger.info('%s:loading %s' % (module, filename))
                with tools.file_open(OPJ(module, filename)) as trans_file:
                    po_path = trans_file.name
                translation_obj = pool.get('ir.translation')
                translation_obj.translation_import(lang2, module, po_path)

            cursor.execute("UPDATE ir_module_module SET state = 'installed' " \
                    "WHERE name = %s", (package.name,))
            module2state[package.name] = 'installed'

        cursor.commit()

    # Create missing reports
    from trytond.report import Report
    report_obj = pool.get('ir.action.report')
    report_ids = report_obj.search([
        ('module', '=', module),
        ])
    report_names = pool.object_name_list(type='report')
    for report in report_obj.browse(report_ids):
        report_name = report.report_name
        if report_name not in report_names:
            report = object.__new__(Report)
            report._name = report_name
            pool.add(report, type='report')
            report.__init__()

    for model_name in models_to_update_history:
        model = pool.get(model_name)
        if model._history:
            logger.info('history:update %s' % model._name)
            model._update_history_table()

    # Vacuum :
    while modules_todo:
        (module, to_delete) = modules_todo.pop()
        convert.post_import(pool, module, to_delete)

    cursor.commit()
Example #34
0
    def transition_import_icd10(self):
        pool = Pool()
        Group = pool.get('galeno.disease.group')
        Category = pool.get('galeno.disease.category')
        Disease = pool.get('galeno.disease')
        Procedure = pool.get('galeno.procedure')
        cursor = Transaction().connection.cursor()
        lang = self.start.language.code
        path = 'galeno_base_data/data/diseases/groups_' + lang + '.csv'
        with file_open(path, mode='r', encoding='utf-8') as group_file:
            fieldnames = ['code', 'name', 'description', 'information']
            reader = csv.DictReader(group_file,
                                    delimiter='|',
                                    fieldnames=fieldnames)
            next(reader)  # Skip header
            for row in reader:
                group = Group()
                for field in row:
                    if row[field] == '':
                        value = None
                    else:
                        value = row[field]
                    setattr(group, field, value)
                group.core = True
                group.save()

        path = 'galeno_base_data/data/diseases/categories_' + lang + '.csv'
        with file_open(path, mode='r', encoding='utf-8') as category_file:
            fieldnames = ['code', 'name', 'parent']
            reader = csv.DictReader(category_file,
                                    delimiter='|',
                                    fieldnames=fieldnames)
            next(reader)  # Skip header
            for row in reader:
                if row['parent'] != '':
                    parent, = Category.search([('code', '=', row['parent'])])
                else:
                    parent = None
                category = Category()
                category.code = row['code']
                category.name = row['name']
                category.parent = parent
                category.core = True
                category.save()

        groups = {}
        categories = {}
        for group in Group.search([]):
            groups[group.code] = group
        for category in Category.search([]):
            categories[category.code] = category

        to_save = []
        path = 'galeno_base_data/data/diseases/diseases_' + lang + '.csv'
        with file_open(path, mode='r', encoding='utf-8') as disease_file:
            fieldnames = [
                'code', 'name', 'category', 'groups', 'chromosome', 'protein',
                'gene', 'information', 'active'
            ]
            reader = csv.DictReader(disease_file,
                                    delimiter='|',
                                    fieldnames=fieldnames)
            next(reader)  # Skip header
            for row in reader:
                disease = Disease()
                for field in row:
                    if row[field] == '':
                        value = None
                    else:
                        if field == 'active':
                            value = bool(row[field])
                        elif field == 'category':
                            value = categories[row[field]]
                        elif field == 'groups':
                            value = []
                            for group_code in row[field].split(','):
                                value.append(groups[group_code].id)
                        else:
                            value = row[field]
                    setattr(disease, field, value)
                disease.core = True
                to_save.append(disease)
            Disease.save(to_save)

        table = Procedure.__table__()
        columns = [table.code, table.name, table.core]
        values = []
        path = 'galeno_base_data/data/procedures/procedures_' + lang + '.csv'
        with file_open(path, mode='r', encoding='utf-8') as procedure_file:
            fieldnames = ['code', 'name']
            reader = csv.DictReader(procedure_file,
                                    delimiter='|',
                                    fieldnames=fieldnames)
            next(reader)  # Skip header
            for row in reader:
                record = []
                for field in row:
                    if row[field] == '':
                        value = None
                    else:
                        value = row[field]
                    record.append(value)
                record.append(True)
                values.append(record)

            cursor.execute(*table.insert(columns, values))
            Procedure.save(to_save)

        return 'succeed'
Example #35
0
 def test_file_open_suffix(self):
     "Test file_open from same root name but with a suffix"
     with self.assertRaisesRegex(IOError, "Permission denied:"):
         file_open('../trytond_suffix', subdir=None)
Example #36
0
def load_module_graph(graph, pool, update=None, lang=None):
    if lang is None:
        lang = [config.get('database', 'language')]
    if update is None:
        update = []
    modules_todo = []
    models_to_update_history = set()
    cursor = Transaction().cursor

    modules = [x.name for x in graph]
    cursor.execute(*ir_module.select(ir_module.name, ir_module.state,
            where=ir_module.name.in_(modules)))
    module2state = dict(cursor.fetchall())

    for package in graph:
        module = package.name
        if module not in MODULES:
            continue
        logger.info(module)
        classes = pool.setup(module)
        package_state = module2state.get(module, 'uninstalled')
        if (is_module_to_install(module, update)
                or package_state in ('to install', 'to upgrade')):
            if package_state not in ('to install', 'to upgrade'):
                if package_state == 'installed':
                    package_state = 'to upgrade'
                elif package_state != 'to remove':
                    package_state = 'to install'
            for child in package.childs:
                module2state[child.name] = package_state
            for type in classes.keys():
                for cls in classes[type]:
                    logger.info('%s:register %s', module, cls.__name__)
                    cls.__register__(module)
            for model in classes['model']:
                if hasattr(model, '_history'):
                    models_to_update_history.add(model.__name__)

            # Instanciate a new parser for the package:
            tryton_parser = convert.TrytondXmlHandler(pool=pool, module=module,
                module_state=package_state)

            for filename in package.info.get('xml', []):
                filename = filename.replace('/', os.sep)
                logger.info('%s:loading %s', module, filename)
                # Feed the parser with xml content:
                with tools.file_open(OPJ(module, filename)) as fp:
                    tryton_parser.parse_xmlstream(fp)

            modules_todo.append((module, list(tryton_parser.to_delete)))

            localedir = '%s/%s' % (package.info['directory'], 'locale')
            for filename in itertools.chain(
                    iglob('%s/*.po' % localedir),
                    iglob('%s/override/*.po' % localedir)):
                filename = filename.replace('/', os.sep)
                lang2 = os.path.splitext(os.path.basename(filename))[0]
                if lang2 not in lang:
                    continue
                logger.info('%s:loading %s', module,
                    filename[len(package.info['directory']) + 1:])
                Translation = pool.get('ir.translation')
                Translation.translation_import(lang2, module, filename)

            if package_state == 'to remove':
                continue
            cursor.execute(*ir_module.select(ir_module.id,
                    where=(ir_module.name == package.name)))
            try:
                module_id, = cursor.fetchone()
                cursor.execute(*ir_module.update([ir_module.state],
                        ['installed'], where=(ir_module.id == module_id)))
            except TypeError:
                cursor.execute(*ir_module.insert(
                        [ir_module.create_uid, ir_module.create_date,
                            ir_module.name, ir_module.state],
                        [[0, CurrentTimestamp(), package.name, 'installed']]))
            module2state[package.name] = 'installed'

        cursor.commit()

    for model_name in models_to_update_history:
        model = pool.get(model_name)
        if model._history:
            logger.info('history:update %s', model.__name__)
            model._update_history_table()

    # Vacuum :
    while modules_todo:
        (module, to_delete) = modules_todo.pop()
        convert.post_import(pool, module, to_delete)

    cursor.commit()
Example #37
0
 def module_path(name):
     module, path = name.split('/', 1)
     with file_open(os.path.join(module, path)) as f:
         return 'file://' + f.name
Example #38
0
 def get_icon(self, name):
     path = os.path.join(self.module, self.path.replace('/', os.sep))
     with file_open(path, subdir='modules', mode='rb') as fp:
         return fp.read()
Example #39
0
def load_module_graph(graph, pool, update=None, lang=None):
    # Prevent to import backend when importing module
    from trytond.cache import Cache
    from trytond.ir.lang import get_parent_language

    if lang is None:
        lang = [config.get('database', 'language')]
    if update is None:
        update = []
    modules_todo = []
    models_to_update_history = set()

    # Load also parent languages
    lang = set(lang)
    for code in list(lang):
        while code:
            lang.add(code)
            code = get_parent_language(code)

    transaction = Transaction()
    with transaction.connection.cursor() as cursor:
        modules = [x.name for x in graph]
        module2state = dict()
        for sub_modules in tools.grouped_slice(modules):
            cursor.execute(
                *ir_module.select(ir_module.name,
                                  ir_module.state,
                                  where=ir_module.name.in_(list(sub_modules))))
            module2state.update(cursor)
        modules = set(modules)

        for node in graph:
            module = node.name
            if module not in MODULES:
                continue
            logger.info(module)
            classes = pool.fill(module, modules)
            if update:
                pool.setup(classes)
            package_state = module2state.get(module, 'not activated')
            if (is_module_to_install(module, update) or
                (update and package_state in ('to activate', 'to upgrade'))):
                if package_state not in ('to activate', 'to upgrade'):
                    if package_state == 'activated':
                        package_state = 'to upgrade'
                    elif package_state != 'to remove':
                        package_state = 'to activate'
                for child in node:
                    module2state[child.name] = package_state
                for type in list(classes.keys()):
                    for cls in classes[type]:
                        logger.info('%s:register %s', module, cls.__name__)
                        cls.__register__(module)
                for model in classes['model']:
                    if hasattr(model, '_history'):
                        models_to_update_history.add(model.__name__)

                # Instanciate a new parser for the module
                tryton_parser = convert.TrytondXmlHandler(
                    pool, module, package_state, modules, lang)

                for filename in node.info.get('xml', []):
                    filename = filename.replace('/', os.sep)
                    logger.info('%s:loading %s', module, filename)
                    # Feed the parser with xml content:
                    with tools.file_open(OPJ(module, filename), 'rb') as fp:
                        tryton_parser.parse_xmlstream(fp)

                modules_todo.append((module, list(tryton_parser.to_delete)))

                load_translations(pool, node, lang)

                if package_state == 'to remove':
                    continue
                cursor.execute(*ir_module.select(
                    ir_module.id, where=(ir_module.name == module)))
                try:
                    module_id, = cursor.fetchone()
                    cursor.execute(
                        *ir_module.update([ir_module.state], ['activated'],
                                          where=(ir_module.id == module_id)))
                except TypeError:
                    cursor.execute(*ir_module.insert([
                        ir_module.create_uid, ir_module.create_date,
                        ir_module.name, ir_module.state
                    ], [
                        [0, CurrentTimestamp(), module, 'activated'],
                    ]))
                module2state[module] = 'activated'

            # Avoid clearing cache to prevent dead lock on ir.cache table
            Cache.rollback(transaction)
            transaction.commit()
            # Clear transaction cache to update default_factory
            transaction.cache.clear()

        if not update:
            pool.setup()
        else:
            # Remove unknown models and fields
            Model = pool.get('ir.model')
            Model.clean()
            ModelField = pool.get('ir.model.field')
            ModelField.clean()
            transaction.commit()

        pool.setup_mixin(modules)

        for model_name in models_to_update_history:
            model = pool.get(model_name)
            if model._history:
                logger.info('history:update %s', model.__name__)
                model._update_history_table()

        # Vacuum :
        while modules_todo:
            (module, to_delete) = modules_todo.pop()
            convert.post_import(pool, module, to_delete)

        # Ensure cache is clear for other instances
        Cache.clear_all()
        Cache.refresh_pool(transaction)
    logger.info('all modules loaded')
Example #40
0
def load_module_graph(graph, pool, update=None, lang=None):
    from trytond.ir.lang import get_parent_language

    if lang is None:
        lang = [config.get('database', 'language')]
    if update is None:
        update = []
    modules_todo = []
    models_to_update_history = set()

    # Load also parent languages
    lang = set(lang)
    for code in list(lang):
        while code:
            lang.add(code)
            code = get_parent_language(code)

    transaction = Transaction()
    with transaction.connection.cursor() as cursor:
        modules = [x.name for x in graph]
        cursor.execute(*ir_module.select(ir_module.name,
                                         ir_module.state,
                                         where=ir_module.name.in_(modules)))
        module2state = dict(cursor.fetchall())
        modules = set(modules)

        for node in graph:
            module = node.name
            if module not in MODULES:
                continue
            logger.info(module)
            classes = pool.fill(module, modules)
            if update:
                pool.setup(classes)
            package_state = module2state.get(module, 'not activated')
            if (is_module_to_install(module, update) or
                (update and package_state in ('to activate', 'to upgrade'))):
                if package_state not in ('to activate', 'to upgrade'):
                    if package_state == 'activated':
                        package_state = 'to upgrade'
                    elif package_state != 'to remove':
                        package_state = 'to activate'
                for child in node:
                    module2state[child.name] = package_state
                for type in list(classes.keys()):
                    for cls in classes[type]:
                        logger.info('%s:register %s', module, cls.__name__)
                        cls.__register__(module)
                for model in classes['model']:
                    if hasattr(model, '_history'):
                        models_to_update_history.add(model.__name__)

                # Instanciate a new parser for the module
                tryton_parser = convert.TrytondXmlHandler(
                    pool, module, package_state, modules)

                for filename in node.info.get('xml', []):
                    filename = filename.replace('/', os.sep)
                    logger.info('%s:loading %s', module, filename)
                    # Feed the parser with xml content:
                    with tools.file_open(OPJ(module, filename), 'rb') as fp:
                        tryton_parser.parse_xmlstream(fp)

                modules_todo.append((module, list(tryton_parser.to_delete)))

                localedir = '%s/%s' % (node.info['directory'], 'locale')
                lang2filenames = defaultdict(list)
                for filename in itertools.chain(
                        iglob('%s/*.po' % localedir),
                        iglob('%s/override/*.po' % localedir)):
                    filename = filename.replace('/', os.sep)
                    lang2 = os.path.splitext(os.path.basename(filename))[0]
                    if lang2 not in lang:
                        continue
                    lang2filenames[lang2].append(filename)
                base_path_position = len(node.info['directory']) + 1
                for language, files in lang2filenames.items():
                    filenames = [f[base_path_position:] for f in files]
                    logger.info('%s:loading %s', module, ','.join(filenames))
                    Translation = pool.get('ir.translation')
                    Translation.translation_import(language, module, files)

                if package_state == 'to remove':
                    continue
                cursor.execute(*ir_module.select(
                    ir_module.id, where=(ir_module.name == module)))
                try:
                    module_id, = cursor.fetchone()
                    cursor.execute(
                        *ir_module.update([ir_module.state], ['activated'],
                                          where=(ir_module.id == module_id)))
                except TypeError:
                    cursor.execute(*ir_module.insert([
                        ir_module.create_uid, ir_module.create_date,
                        ir_module.name, ir_module.state
                    ], [
                        [0, CurrentTimestamp(), module, 'activated'],
                    ]))
                module2state[module] = 'activated'

            transaction.commit()

        if not update:
            pool.setup()
        else:
            # Remove unknown models and fields
            Model = pool.get('ir.model')
            Model.clean()
            ModelField = pool.get('ir.model.field')
            ModelField.clean()
            transaction.commit()

        pool.setup_mixin(modules)

        for model_name in models_to_update_history:
            model = pool.get(model_name)
            if model._history:
                logger.info('history:update %s', model.__name__)
                model._update_history_table()

        # Vacuum :
        while modules_todo:
            (module, to_delete) = modules_todo.pop()
            convert.post_import(pool, module, to_delete)
    logger.info('all modules loaded')
Example #41
0
def load_module_graph(graph, pool, update=None, lang=None):
    if lang is None:
        lang = [config.get("database", "language")]
    if update is None:
        update = []
    modules_todo = []
    models_to_update_history = set()

    with Transaction().connection.cursor() as cursor:
        modules = [x.name for x in graph]
        cursor.execute(*ir_module.select(ir_module.name, ir_module.state, where=ir_module.name.in_(modules)))
        module2state = dict(cursor.fetchall())

        for package in graph:
            module = package.name
            if module not in MODULES:
                continue
            logger.info(module)
            classes = pool.fill(module)
            if update:
                pool.setup(classes)
            package_state = module2state.get(module, "uninstalled")
            if is_module_to_install(module, update) or (update and package_state in ("to install", "to upgrade")):
                if package_state not in ("to install", "to upgrade"):
                    if package_state == "installed":
                        package_state = "to upgrade"
                    elif package_state != "to remove":
                        package_state = "to install"
                for child in package.childs:
                    module2state[child.name] = package_state
                for type in classes.keys():
                    for cls in classes[type]:
                        logger.info("%s:register %s", module, cls.__name__)
                        cls.__register__(module)
                for model in classes["model"]:
                    if hasattr(model, "_history"):
                        models_to_update_history.add(model.__name__)

                # Instanciate a new parser for the package:
                tryton_parser = convert.TrytondXmlHandler(pool=pool, module=module, module_state=package_state)

                for filename in package.info.get("xml", []):
                    filename = filename.replace("/", os.sep)
                    logger.info("%s:loading %s", module, filename)
                    # Feed the parser with xml content:
                    with tools.file_open(OPJ(module, filename), "rb") as fp:
                        tryton_parser.parse_xmlstream(fp)

                modules_todo.append((module, list(tryton_parser.to_delete)))

                localedir = "%s/%s" % (package.info["directory"], "locale")
                for filename in itertools.chain(iglob("%s/*.po" % localedir), iglob("%s/override/*.po" % localedir)):
                    filename = filename.replace("/", os.sep)
                    lang2 = os.path.splitext(os.path.basename(filename))[0]
                    if lang2 not in lang:
                        continue
                    logger.info("%s:loading %s", module, filename[len(package.info["directory"]) + 1 :])
                    Translation = pool.get("ir.translation")
                    Translation.translation_import(lang2, module, filename)

                if package_state == "to remove":
                    continue
                cursor.execute(*ir_module.select(ir_module.id, where=(ir_module.name == package.name)))
                try:
                    module_id, = cursor.fetchone()
                    cursor.execute(
                        *ir_module.update([ir_module.state], ["installed"], where=(ir_module.id == module_id))
                    )
                except TypeError:
                    cursor.execute(
                        *ir_module.insert(
                            [ir_module.create_uid, ir_module.create_date, ir_module.name, ir_module.state],
                            [[0, CurrentTimestamp(), package.name, "installed"]],
                        )
                    )
                module2state[package.name] = "installed"

            Transaction().connection.commit()

        if not update:
            pool.setup()

        for model_name in models_to_update_history:
            model = pool.get(model_name)
            if model._history:
                logger.info("history:update %s", model.__name__)
                model._update_history_table()

        # Vacuum :
        while modules_todo:
            (module, to_delete) = modules_todo.pop()
            convert.post_import(pool, module, to_delete)
    logger.info("all modules loaded")
Example #42
0
    def migrate_modules(cursor):
        modules_in_dir = get_module_list()
        modules_to_migrate = {}
        for module_dir in modules_in_dir:
            try:
                with tools.file_open(OPJ(module_dir, "__migrated_modules")) as f:
                    for line in f.readlines():
                        line = line.replace(" ", "").strip("\n")
                        if not line:
                            continue
                        action, old_module = line.split(":")
                        modules_to_migrate[old_module] = (action, module_dir)
            except IOError:
                continue

        cursor.execute(*ir_module.select(ir_module.name))
        for (module_in_db,) in cursor.fetchall():
            if module_in_db in modules_in_dir or module_in_db in modules_to_migrate:
                continue
            else:
                modules_to_migrate[module_in_db] = ("to_drop", None)

        def rename(cursor, table_name, old_name, new_name, var_name):
            table = Table(table_name)
            cursor.execute(
                *table.update([getattr(table, var_name)], [new_name], where=(getattr(table, var_name) == old_name))
            )

        def delete(cursor, table_name, old_name, var_name):
            table = Table(table_name)
            cursor.execute(*table.delete(where=(getattr(table, var_name) == old_name)))

        for old_name, (action, new_name) in modules_to_migrate.iteritems():
            cursor.execute(*ir_module.select(Count(ir_module.id), where=ir_module.name == old_name))
            count, = cursor.fetchone()
            if not count:
                continue

            if action == "to_drop":
                logger.info(
                    "%s directory has been removed from filesystem," " deleting entries from database..." % old_name
                )
            else:
                logger.info(
                    "%s has been %s %s, updating database..."
                    % (old_name, {"to_rename": "renamed into", "to_merge": "merged with"}[action], new_name)
                )
            if new_name:
                rename(cursor, "ir_model", old_name, new_name, "module")
                rename(cursor, "ir_action_report", old_name, new_name, "module")
                rename(cursor, "ir_model_field", old_name, new_name, "module")
                rename(cursor, "ir_model_data", old_name, new_name, "module")
                rename(cursor, "ir_translation", old_name, new_name, "module")
                rename(cursor, "ir_translation", old_name, new_name, "overriding_module")
                rename(cursor, "ir_ui_icon", old_name, new_name, "module")
                rename(cursor, "ir_ui_view", old_name, new_name, "module")

            if action == "to_rename":
                rename(cursor, "ir_module_dependency", old_name, new_name, "name")
                rename(cursor, "ir_module", old_name, new_name, "name")
            elif action == "to_merge":
                delete(cursor, "ir_module_dependency", old_name, "name")
                delete(cursor, "ir_module", old_name, "name")
            elif action == "to_drop":
                delete(cursor, "ir_model", old_name, "module")
                delete(cursor, "ir_action_report", old_name, "module")
                delete(cursor, "ir_model_field", old_name, "module")
                delete(cursor, "ir_model_data", old_name, "module")
                delete(cursor, "ir_translation", old_name, "module")
                delete(cursor, "ir_translation", old_name, "overriding_module")
                delete(cursor, "ir_ui_icon", old_name, "module")
                delete(cursor, "ir_ui_view", old_name, "module")
                delete(cursor, "ir_module_dependency", old_name, "name")
                delete(cursor, "ir_module", old_name, "name")
Example #43
0
def load_module_graph(graph, pool, update=None, lang=None):
    if lang is None:
        lang = [config.get('database', 'language')]
    if update is None:
        update = []
    modules_todo = []
    models_to_update_history = set()

    with Transaction().connection.cursor() as cursor:
        modules = [x.name for x in graph]
        cursor.execute(*ir_module.select(ir_module.name,
                                         ir_module.state,
                                         where=ir_module.name.in_(modules)))
        module2state = dict(cursor.fetchall())

        for package in graph:
            module = package.name
            if module not in MODULES:
                continue
            logger.info(module)
            classes = pool.fill(module)
            if update:
                pool.setup(classes)
            package_state = module2state.get(module, 'uninstalled')
            if (is_module_to_install(module, update) or
                (update and package_state in ('to install', 'to upgrade'))):
                if package_state not in ('to install', 'to upgrade'):
                    if package_state == 'installed':
                        package_state = 'to upgrade'
                    elif package_state != 'to remove':
                        package_state = 'to install'
                for child in package.childs:
                    module2state[child.name] = package_state
                for type in classes.keys():
                    for cls in classes[type]:
                        logger.info('%s:register %s', module, cls.__name__)
                        cls.__register__(module)
                for model in classes['model']:
                    if hasattr(model, '_history'):
                        models_to_update_history.add(model.__name__)

                # Instanciate a new parser for the package:
                tryton_parser = convert.TrytondXmlHandler(
                    pool=pool, module=module, module_state=package_state)

                for filename in package.info.get('xml', []):
                    filename = filename.replace('/', os.sep)
                    logger.info('%s:loading %s', module, filename)
                    # Feed the parser with xml content:
                    with tools.file_open(OPJ(module, filename), 'rb') as fp:
                        tryton_parser.parse_xmlstream(fp)

                modules_todo.append((module, list(tryton_parser.to_delete)))

                localedir = '%s/%s' % (package.info['directory'], 'locale')
                for filename in itertools.chain(
                        iglob('%s/*.po' % localedir),
                        iglob('%s/override/*.po' % localedir)):
                    filename = filename.replace('/', os.sep)
                    lang2 = os.path.splitext(os.path.basename(filename))[0]
                    if lang2 not in lang:
                        continue
                    logger.info('%s:loading %s', module,
                                filename[len(package.info['directory']) + 1:])
                    Translation = pool.get('ir.translation')
                    Translation.translation_import(lang2, module, filename)

                if package_state == 'to remove':
                    continue
                cursor.execute(*ir_module.select(
                    ir_module.id, where=(ir_module.name == package.name)))
                try:
                    module_id, = cursor.fetchone()
                    cursor.execute(
                        *ir_module.update([ir_module.state], ['installed'],
                                          where=(ir_module.id == module_id)))
                except TypeError:
                    cursor.execute(*ir_module.insert([
                        ir_module.create_uid, ir_module.create_date,
                        ir_module.name, ir_module.state
                    ], [
                        [0, CurrentTimestamp(), package.name, 'installed'],
                    ]))
                module2state[package.name] = 'installed'

            Transaction().connection.commit()

        if not update:
            pool.setup()

        for model_name in models_to_update_history:
            model = pool.get(model_name)
            if model._history:
                logger.info('history:update %s', model.__name__)
                model._update_history_table()

        # Vacuum :
        while modules_todo:
            (module, to_delete) = modules_todo.pop()
            convert.post_import(pool, module, to_delete)
    logger.info('all modules loaded')
Example #44
0
def fetch():
    sys.stderr.write('Fetching')
    with file_open('bank_ar/scripts/bank_ar.csv', mode='rb') as fp:
        data = fp.read()
    return data