def update_list(self, cr, uid, context={}): res = [0, 0] # [update, add] # iterate through installed modules and mark them as being so for mod_name in addons.get_modules(): ids = self.search(cr, uid, [('name','=',mod_name)]) terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) if ids: id = ids[0] mod = self.browse(cr, uid, id) if terp.get('installable', True) and mod.state == 'uninstallable': self.write(cr, uid, id, {'state': 'uninstalled'}) if parse_version(terp.get('version', '')) > parse_version(mod.latest_version or ''): self.write(cr, uid, id, {'url': ''}) res[0] += 1 self.write(cr, uid, id, values) cr.execute('DELETE FROM ir_module_module_dependency WHERE module_id = %s', (id,)) else: mod_path = addons.get_module_path(mod_name) if not mod_path: continue if not terp or not terp.get('installable', True): continue ids = self.search(cr, uid, [('name','=',mod_name)]) id = self.create(cr, uid, dict(name=mod_name, state='uninstalled', **values)) res[1] += 1 self._update_dependencies(cr, uid, id, terp.get('depends', [])) self._update_web_dependencies(cr, uid, id, terp.get('web_depends', [])) self._update_category(cr, uid, id, terp.get('category', 'Uncategorized')) return res
def download(self, cr, uid, ids, download=True, context=None): res = [] for mod in self.browse(cr, uid, ids, context=context): if not mod.url: continue match = re.search('-([a-zA-Z0-9\._-]+)(\.zip)', mod.url, re.I) version = '0' if match: version = match.group(1) if parse_version(mod.installed_version or '0') >= parse_version(version): continue res.append(mod.url) if not download: continue zipfile = urllib.urlopen(mod.url).read() fname = addons.get_module_path(str(mod.name)+'.zip', downloaded=True) try: fp = file(fname, 'wb') fp.write(zipfile) fp.close() except Exception, e: raise orm.except_orm(_('Error'), _('Can not create the module file:\n %s') % (fname,)) terp = self.get_module_info(mod.name) self.write(cr, uid, mod.id, self.get_values_from_terp(terp)) cr.execute('DELETE FROM ir_module_module_dependency ' \ 'WHERE module_id = %s', (mod.id,)) self._update_dependencies(cr, uid, mod.id, terp.get('depends', [])) self._update_category(cr, uid, mod.id, terp.get('category', 'Uncategorized')) # Import module zimp = zipimport.zipimporter(fname) zimp.load_module(mod.name)
def validate_template(self, cr, uid, ids, context=None): ir_model_data_obj = self.pool.get('ir.model.data') for run in self.browse(cr, uid, ids, context): ad_paths = addons.get_module_path('smile_import_data') newpath = ad_paths + '/runs/' + run.name + '/' # Create data folder if not os.path.exists(newpath): os.makedirs(newpath) os.makedirs(newpath + 'config/') os.makedirs(newpath + 'templates/') else: raise osv.except_osv(_('Error'), _("The folder %s must be removed or please set another name to your Run template") % (run.name,)) ir_model_data_obj.create(cr, uid, { 'name': 'run_%s' % (run.id), 'model': 'import.data.run', 'module':'smile_import_data', 'res_id':run.id, 'noupdate':True, }, context=context) self.write(cr, uid, ids, {'name':run.name, 'root_path':newpath, 'config_path':newpath + 'config/', 'templates_path':newpath + 'templates/', 'runs_path':newpath + 'runs/', 'template_ok':True, 'state':'template', 'ir_model_data_ref':'smile_import_data.run_%s' % (run.id), }, context=context) return True
def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=None, **kwargs): for package in graph: if package.state in ('to install', 'to upgrade'): mapping_dir = os.path.join(addons.get_module_path(package.name), 'autoload') exist = os.path.exists(mapping_dir) if exist: for mapping_module in osutil.listdir(mapping_dir): cr.execute( "select id from ir_module_module where name = '%s' and state in ('installed', 'to_update');" % mapping_module) res = cr.fetchall() if res: mapping_module_dir = os.path.join( mapping_dir, mapping_module) files = osutil.listdir(mapping_module_dir, recursive=True) for file in files: if file[-4:] in ['.csv', '.xml']: package.data['update_xml'].append( 'autoload/%s/%s' % (mapping_module, file)) return addons.load_module_graph_ori(cr, graph, status, perform_checks, skip_modules, **kwargs)
def update_translations(self, cr, uid, ids, filter_lang=None): logger = netsvc.Logger() if not filter_lang: pool = pooler.get_pool(cr.dbname) lang_obj = pool.get('res.lang') lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)]) filter_lang = [lang.code for lang in lang_obj.browse(cr, uid, lang_ids)] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] for mod in self.browse(cr, uid, ids): if mod.state != 'installed': continue modpath = addons.get_module_path(mod.name) if not modpath: # unable to find the module. we skip continue for lang in filter_lang: if len(lang) > 5: raise osv.except_osv(_('Error'), _('You Can Not Load Translation For language Due To Invalid Language/Country Code')) iso_lang = tools.get_iso_codes(lang) f = os.path.join(modpath, 'i18n', iso_lang + '.po') if not os.path.exists(f) and iso_lang.find('_') != -1: f = os.path.join(modpath, 'i18n', iso_lang.split('_')[0] + '.po') iso_lang = iso_lang.split('_')[0] if os.path.exists(f): logger.notifyChannel("i18n", netsvc.LOG_INFO, 'module %s: loading translation file for language %s' % (mod.name, iso_lang)) tools.trans_load(cr.dbname, f, lang, verbose=False)
def update_translations(self, cr, uid, ids, filter_lang=None, context={}): logger = logging.getLogger('i18n') if not filter_lang: pool = pooler.get_pool(cr.dbname) lang_obj = pool.get('res.lang') lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)]) filter_lang = [ lang.code for lang in lang_obj.browse(cr, uid, lang_ids) ] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] for mod in self.browse(cr, uid, ids): if mod.state != 'installed': continue modpath = addons.get_module_path(mod.name) if not modpath: # unable to find the module. we skip continue for lang in filter_lang: iso_lang = tools.get_iso_codes(lang) f = addons.get_module_resource(mod.name, 'i18n', iso_lang + '.po') context2 = context and context.copy() or {} if f and '_' in iso_lang: iso_lang2 = iso_lang.split('_')[0] f2 = addons.get_module_resource(mod.name, 'i18n', iso_lang2 + '.po') if f2: logger.info( 'module %s: loading base translation file %s for language %s', mod.name, iso_lang2, lang) tools.trans_load(cr, f2, lang, verbose=False, context=context) context2['overwrite'] = True # Implementation notice: we must first search for the full name of # the language derivative, like "en_UK", and then the generic, # like "en". if (not f) and '_' in iso_lang: iso_lang = iso_lang.split('_')[0] f = addons.get_module_resource(mod.name, 'i18n', iso_lang + '.po') if f: logger.info( 'module %s: loading translation file (%s) for language %s', mod.name, iso_lang, lang) tools.trans_load(cr, f, lang, verbose=False, context=context2) elif iso_lang != 'en': logger.warning('module %s: no translation for language %s', mod.name, iso_lang) tools.trans_update_res_ids(cr)
def update_translations(self, cr, uid, ids, filter_lang=None, context=None): if context is None: context = {} logger = logging.getLogger('i18n') if not filter_lang: pool = pooler.get_pool(cr.dbname) lang_obj = pool.get('res.lang') lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)]) filter_lang = [lang.code for lang in lang_obj.browse(cr, uid, lang_ids)] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] msf_profile_id = self.pool.get('ir.module.module').search(cr, uid, [('name', '=', 'msf_profile')]) if msf_profile_id and msf_profile_id[0] in ids: ids.remove(msf_profile_id[0]) # load msf_profile file at the end (due to es.po file, terms are always overwritten) ids.append(msf_profile_id[0]) for mod in self.browse(cr, uid, ids): if mod.state != 'installed': continue modpath = addons.get_module_path(mod.name) if not modpath: # unable to find the module. we skip continue for lang in filter_lang: iso_lang = tools.get_iso_codes(lang) f = addons.get_module_resource(mod.name, 'i18n', iso_lang + '.po') context2 = context and context.copy() or {} if f and '_' in iso_lang: iso_lang2 = iso_lang.split('_')[0] f2 = addons.get_module_resource(mod.name, 'i18n', iso_lang2 + '.po') if f2: logger.info('module %s: loading base translation file %s for language %s', mod.name, iso_lang2, lang) tools.trans_load(cr, f2, lang, verbose=False, context=context) context2['overwrite'] = True # Implementation notice: we must first search for the full name of # the language derivative, like "en_UK", and then the generic, # like "en". if (not f) and '_' in iso_lang: iso_lang = iso_lang.split('_')[0] f = addons.get_module_resource(mod.name, 'i18n', iso_lang + '.po') if f: logger.info('module %s: loading translation file (%s) for language %s', mod.name, iso_lang, lang) tools.trans_load(cr, f, lang, verbose=False, context=context2) elif iso_lang != 'en': logger.warning('module %s: no translation for language %s', mod.name, iso_lang) tools.trans_update_res_ids(cr)
def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=None, **kwargs): for package in graph: if package.state in ('to install', 'to upgrade'): mapping_dir = os.path.join(addons.get_module_path(package.name), 'autoload') exist = os.path.exists(mapping_dir) if exist: for mapping_module in osutil.listdir(mapping_dir): cr.execute("select id from ir_module_module where name = '%s' and state in ('installed', 'to_update');"%mapping_module) res = cr.fetchall() if res: mapping_module_dir = os.path.join(mapping_dir, mapping_module) files = osutil.listdir(mapping_module_dir, recursive=True) for file in files: if file[-4:] in ['.csv', '.xml']: package.data['update_xml'].append('autoload/%s/%s'%(mapping_module,file)) return addons.load_module_graph_ori(cr, graph, status, perform_checks, skip_modules, **kwargs)
def update_list(self, cr, uid, context={}): res = [0, 0] # [update, add] known_mods = self.browse(cr, uid, self.search(cr, uid, [])) known_mods_names = dict([(m.name, m) for m in known_mods]) # iterate through detected modules and update/create them in db for mod_name in addons.get_modules(): mod = known_mods_names.get(mod_name) terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) if mod: updated_values = {} for key in values: old = getattr(mod, key) updated = isinstance(values[key], basestring) and tools.ustr( values[key]) or values[key] if not old == updated: updated_values[key] = values[key] if terp.get('installable', True) and mod.state == 'uninstallable': updated_values['state'] = 'uninstalled' if parse_version(terp.get('version', '')) > parse_version( mod.latest_version or ''): res[0] += 1 if updated_values: self.write(cr, uid, mod.id, updated_values) else: mod_path = addons.get_module_path(mod_name) if not mod_path: continue if not terp or not terp.get('installable', True): continue id = self.create( cr, uid, dict(name=mod_name, state='uninstalled', **values)) mod = self.browse(cr, uid, id) res[1] += 1 self._update_dependencies(cr, uid, mod, terp.get('depends', [])) self._update_category(cr, uid, mod, terp.get('category', 'Uncategorized')) return res
def update_list(self, cr, uid, context={}): res = [0, 0] # [update, add] # iterate through installed modules and mark them as being so for mod_name in addons.get_modules(): ids = self.search(cr, uid, [('name', '=', mod_name)]) terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) if ids: id = ids[0] mod = self.browse(cr, uid, id) if terp.get('installable', True) and mod.state == 'uninstallable': self.write(cr, uid, id, {'state': 'uninstalled'}) if parse_version(terp.get('version', '')) > parse_version( mod.latest_version or ''): self.write(cr, uid, id, {'url': ''}) res[0] += 1 self.write(cr, uid, id, values) cr.execute( 'DELETE FROM ir_module_module_dependency WHERE module_id = %s', (id, )) else: mod_path = addons.get_module_path(mod_name) if not mod_path: continue if not terp or not terp.get('installable', True): continue ids = self.search(cr, uid, [('name', '=', mod_name)]) id = self.create( cr, uid, dict(name=mod_name, state='uninstalled', **values)) res[1] += 1 self._update_dependencies(cr, uid, id, terp.get('depends', [])) self._update_web_dependencies(cr, uid, id, terp.get('web_depends', [])) self._update_category(cr, uid, id, terp.get('category', 'Uncategorized')) return res
def download(self, cr, uid, ids, download=True, context=None): res = [] for mod in self.browse(cr, uid, ids, context=context): if not mod.url: continue match = re.search('-([a-zA-Z0-9\._-]+)(\.zip)', mod.url, re.I) version = '0' if match: version = match.group(1) if parse_version(mod.installed_version or '0') >= parse_version(version): continue res.append(mod.url) if not download: continue zipfile = urllib.urlopen(mod.url).read() fname = addons.get_module_path(str(mod.name) + '.zip', downloaded=True) try: fp = file(fname, 'wb') fp.write(zipfile) fp.close() except Exception: self.__logger.exception( 'Error when trying to create module ' 'file %s', fname) raise orm.except_orm( _('Error'), _('Can not create the module file:\n %s') % (fname, )) terp = self.get_module_info(mod.name) self.write(cr, uid, mod.id, self.get_values_from_terp(terp)) cr.execute('DELETE FROM ir_module_module_dependency ' \ 'WHERE module_id = %s', (mod.id,)) self._update_dependencies(cr, uid, mod, terp.get('depends', [])) self._update_category(cr, uid, mod, terp.get('category', 'Uncategorized')) # Import module zimp = zipimport.zipimporter(fname) zimp.load_module(mod.name) return res
def update_translations(self, cr, uid, ids, filter_lang=None, context=None): logger = logging.getLogger('i18n') if not filter_lang: pool = pooler.get_pool(cr.dbname) lang_obj = pool.get('res.lang') lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)]) filter_lang = [lang.code for lang in lang_obj.browse(cr, uid, lang_ids)] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] for mod in self.browse(cr, uid, ids): if mod.state != 'installed': continue modpath = addons.get_module_path(mod.name) if not modpath: # unable to find the module. we skip continue for lang in filter_lang: iso_lang = tools.get_iso_codes(lang) # Implementation notice: We need to load both the base language, # like "en" and then the dialects (like "en_GB"). # With overwrite=False, en will be complemented with 'en_GB' terms. # with overwrite, we need to reverse the loading order to_load = [] f = addons.get_module_resource(mod.name, 'i18n', iso_lang + '.po') if f: to_load.append((iso_lang, f)) if '_' in iso_lang: iso_lang = iso_lang.split('_')[0] f = addons.get_module_resource(mod.name, 'i18n', iso_lang + '.po') if f: to_load.append((iso_lang, f)) if context and context.get('overwrite', False): to_load.reverse() for (iso_lang, f) in to_load: logger.info('module %s: loading translation file for language %s', mod.name, iso_lang) tools.trans_load(cr, f, lang, verbose=False, context=context) if to_load == [] and lang != 'en_US': logger.warning('module %s: no translation for language %s', mod.name, lang)
def update_list(self, cr, uid, context={}): res = [0, 0] # [update, add] known_mods = self.browse(cr, uid, self.search(cr, uid, [])) known_mods_names = dict([(m.name, m) for m in known_mods]) # iterate through detected modules and update/create them in db for mod_name in addons.get_modules(): mod = known_mods_names.get(mod_name) terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) if mod: updated_values = {} for key in values: old = getattr(mod, key) updated = isinstance(values[key], basestring) and tools.ustr(values[key]) or values[key] if not old == updated: updated_values[key] = values[key] if terp.get('installable', True) and mod.state == 'uninstallable': updated_values['state'] = 'uninstalled' if parse_version(terp.get('version', '')) > parse_version(mod.latest_version or ''): res[0] += 1 if updated_values: self.write(cr, uid, mod.id, updated_values) else: mod_path = addons.get_module_path(mod_name) if not mod_path: continue if not terp or not terp.get('installable', True): continue id = self.create(cr, uid, dict(name=mod_name, state='uninstalled', **values)) mod = self.browse(cr, uid, id) res[1] += 1 self._update_dependencies(cr, uid, mod, terp.get('depends', [])) self._update_category(cr, uid, mod, terp.get('category', 'Uncategorized')) return res
def update_translations(self, cr, uid, ids, filter_lang=None): logger = netsvc.Logger() if not filter_lang: pool = pooler.get_pool(cr.dbname) lang_obj = pool.get('res.lang') lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)]) filter_lang = [lang.code for lang in lang_obj.browse(cr, uid, lang_ids)] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] for mod in self.browse(cr, uid, ids): if mod.state != 'installed': continue modpath = addons.get_module_path(mod.name) if not modpath: # unable to find the module. we skip continue for lang in filter_lang: f = os.path.join(modpath, 'i18n', lang + '.po') if os.path.exists(f): logger.notifyChannel("i18n", netsvc.LOG_INFO, 'module %s: loading translation file for language %s' % (mod.name, lang)) tools.trans_load(cr.dbname, f, lang, verbose=False)
def update_translations(self, cr, uid, ids, filter_lang=None): logger = netsvc.Logger() if not filter_lang: pool = pooler.get_pool(cr.dbname) lang_obj = pool.get('res.lang') lang_ids = lang_obj.search(cr, uid, [('translatable', '=', True)]) filter_lang = [ lang.code for lang in lang_obj.browse(cr, uid, lang_ids) ] elif not isinstance(filter_lang, (list, tuple)): filter_lang = [filter_lang] for mod in self.browse(cr, uid, ids): if mod.state != 'installed': continue modpath = addons.get_module_path(mod.name) if not modpath: # unable to find the module. we skip continue for lang in filter_lang: if len(lang) > 5: raise osv.except_osv( _('Error'), _('You Can Not Load Translation For language Due To Invalid Language/Country Code' )) iso_lang = tools.get_iso_codes(lang) f = os.path.join(modpath, 'i18n', iso_lang + '.po') if not os.path.exists(f) and iso_lang.find('_') != -1: f = os.path.join(modpath, 'i18n', iso_lang.split('_')[0] + '.po') iso_lang = iso_lang.split('_')[0] if os.path.exists(f): logger.notifyChannel( "i18n", netsvc.LOG_INFO, 'module %s: loading translation file for language %s' % (mod.name, iso_lang)) tools.trans_load(cr.dbname, f, lang, verbose=False)
def exp_get_migration_scripts(self, contract_id, contract_password): l = netsvc.Logger() import tools.maintenance as tm try: rc = tm.remote_contract(contract_id, contract_password) if not rc.id: raise tm.RemoteContractException('This contract does not exist or is not active') if rc.status != 'full': raise tm.RemoteContractException('Can not get updates for a partial contract') l.notifyChannel('migration', netsvc.LOG_INFO, 'starting migration with contract %s' % (rc.name,)) zips = rc.retrieve_updates(rc.id, addons.get_modules_with_version()) from shutil import rmtree, copytree, copy backup_directory = os.path.join(tools.config['root_path'], 'backup', time.strftime('%Y-%m-%d-%H-%M')) if zips and not os.path.isdir(backup_directory): l.notifyChannel('migration', netsvc.LOG_INFO, 'create a new backup directory to \ store the old modules: %s' % (backup_directory,)) os.makedirs(backup_directory) for module in zips: l.notifyChannel('migration', netsvc.LOG_INFO, 'upgrade module %s' % (module,)) mp = addons.get_module_path(module) if mp: if os.path.isdir(mp): copytree(mp, os.path.join(backup_directory, module)) if os.path.islink(mp): os.unlink(mp) else: rmtree(mp) else: copy(mp + 'zip', backup_directory) os.unlink(mp + '.zip') try: try: base64_decoded = base64.decodestring(zips[module]) except Exception: l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to read the module %s' % (module,)) raise zip_contents = StringIO(base64_decoded) zip_contents.seek(0) try: try: tools.extract_zip_file(zip_contents, tools.config['addons_path'] ) except Exception: l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to extract the module %s' % (module, )) rmtree(module) raise finally: zip_contents.close() except Exception: l.notifyChannel('migration', netsvc.LOG_ERROR, 'restore the previous version of the module %s' % (module, )) nmp = os.path.join(backup_directory, module) if os.path.isdir(nmp): copytree(nmp, tools.config['addons_path']) else: copy(nmp+'.zip', tools.config['addons_path']) raise return True except tm.RemoteContractException, e: self.abortResponse(1, 'Migration Error', 'warning', str(e))
def _create_table(self, uid, ids, fields, fields_order, results, context, group_cad, filter_cad, title='', list_groups=[], have_group=False): pageSize = [297.0, 210.0] new_doc = etree.Element("report") config = etree.SubElement(new_doc, 'config') groups_founds = [] def _append_node(name, text): n = etree.SubElement(config, name) n.text = text #_append_node('date', time.strftime('%d/%m/%Y')) _append_node('date', time.strftime(str(locale.nl_langinfo(locale.D_FMT).replace('%y', '%Y')))) _append_node('PageSize', '%.2fmm,%.2fmm' % tuple(pageSize)) _append_node('PageWidth', '%.2f' % (pageSize[0] * 2.8346,)) _append_node('PageHeight', '%.2f' % (pageSize[1] * 2.8346,)) _append_node('report-header', title) _append_node('filter_fields', _("Grouped by:")) _append_node('filter_content', group_cad) _append_node('group_fields', _("Filtered by:")) _append_node('group_content', filter_cad) _append_node('company', pooler.get_pool(self.cr.dbname).get('res.users').browse(self.cr, uid, uid).company_id.name) rpt_obj = pooler.get_pool(self.cr.dbname).get('res.users') rml_obj = report_sxw.rml_parse(self.cr, uid, rpt_obj._name, context) _append_node('header-date', str(rml_obj.formatLang(time.strftime("%Y-%m-%d"), date=True)) + ' ' + str(time.strftime("%H:%M"))) l = [] t = 0 strmax = (pageSize[0] - 40) * 2.8346 temp = [] tsum = [] for i in range(0, len(fields_order)): temp.append(0) tsum.append(0) ince = -1; for f in fields_order: s = 0 ince += 1 if fields[f]['type'] in ('date', 'time', 'datetime', 'float', 'integer'): s = 60 strmax -= s if fields[f]['type'] in ('float', 'integer'): temp[ince] = 1 else: t += fields[f].get('size', 80) / 28 + 1 l.append(s) for pos in range(len(l)): if not l[pos]: s = fields[fields_order[pos]].get('size', 80) / 28 + 1 l[pos] = strmax * s / t _append_node('tableSize', ','.join(map(str, l))) header = etree.SubElement(new_doc, 'header') for f in fields_order: field = etree.SubElement(header, 'field') field.text = tools.ustr(fields[f]['string'] or '') lines = etree.SubElement(new_doc, 'lines') for line in results: node_line = etree.SubElement(lines, 'row') count = -1 for f in fields_order: float_flag = 0 count += 1 if fields[f]['type'] == 'many2one' and line[f]: if not line.get('__group'): line[f] = line[f][1] if fields[f]['type'] == 'selection' and line[f]: for key, value in fields[f]['selection']: if key == line[f]: line[f] = value break if fields[f]['type'] in ('one2many', 'many2many') and line[f]: line[f] = '( ' + tools.ustr(len(line[f])) + ' )' if fields[f]['type'] == 'float' and line[f]: precision = (('digits' in fields[f]) and fields[f]['digits'][1]) or 2 prec = '%.' + str(precision) + 'f' line[f] = prec % (line[f]) float_flag = 1 if fields[f]['type'] == 'date' and line[f]: new_d1 = line[f] if not line.get('__group'): format = str(locale.nl_langinfo(locale.D_FMT).replace('%y', '%Y')) d1 = datetime.strptime(line[f], '%Y-%m-%d') new_d1 = d1.strftime(format) line[f] = new_d1 if fields[f]['type'] == 'time' and line[f]: new_d1 = line[f] if not line.get('__group'): format = str(locale.nl_langinfo(locale.T_FMT)) d1 = datetime.strptime(line[f], '%H:%M:%S') new_d1 = d1.strftime(format) line[f] = new_d1 if fields[f]['type'] == 'datetime' and line[f]: new_d1 = line[f] if not line.get('__group'): format = str(locale.nl_langinfo(locale.D_FMT).replace('%y', '%Y')) + ' ' + str(locale.nl_langinfo(locale.T_FMT)) d1 = datetime.strptime(line[f], '%Y-%m-%d %H:%M:%S') new_d1 = d1.strftime(format) line[f] = new_d1 if fields[f]['type'] == 'boolean' and line[f]: line[f] = (_('Yes') if line[f] else _('No')) if line.get('__group'): col = etree.SubElement(node_line, 'col', para='group', tree='no') else: col = etree.SubElement(node_line, 'col', para='yes', tree='no') # Prevent empty labels in groups if f == line.get('__grouped_by') and line.get('__group') and not line[f] and not float_flag and not temp[count]: col.text = line[f] = _('Undefined') col.set('tree', _('undefined')) if (line[f] != None) and not have_group: col.text = tools.ustr(line[f] or '') #if not tools.ustr(line[f] or '') in groups_founds: # groups_founds.append(tools.ustr(line[f] or '')) # col.text = tools.ustr(line[f] or '') if float_flag: col.set('tree', 'float') if line.get('__no_leaf') and temp[count] == 1 and f != 'id' and not line['__context']['group_by']: tsum[count] = float(tsum[count]) + float(line[f]) if not line.get('__group') and f != 'id' and temp[count] == 1: tsum[count] = float(tsum[count]) + float(line[f]); if (line[f] != None) and have_group: if line[f] and (f in list_groups): if not tools.ustr(line[f] or '') in groups_founds: groups_founds.append(tools.ustr(line[f] or '')) col.text = tools.ustr(line[f] or '') if line[f] and (not f in list_groups): col.text = tools.ustr(line[f] or '') if float_flag: col.set('tree', 'float') if line.get('__no_leaf') and temp[count] == 1 and f != 'id' and not line['__context']['group_by']: tsum[count] = float(tsum[count]) + float(line[f]) if not line.get('__group') and f != 'id' and temp[count] == 1: tsum[count] = float(tsum[count]) + float(line[f]); if line[f] == None: col.text = '/' #else: # col.text = '/' node_line = etree.SubElement(lines, 'row') for f in range(0, len(fields_order)): col = etree.SubElement(node_line, 'col', para='group', tree='no') col.set('tree', 'float') if tsum[f] != None: if tsum[f] != 0.0: digits = fields[fields_order[f]].get('digits', (16, 2)) prec = '%%.%sf' % (digits[1],) total = prec % (tsum[f],) txt = str(total or '') else: txt = str(tsum[f] or '') else: txt = '/' if f == 0: txt = 'Total' col.set('tree', 'no') col.text = tools.ustr(txt or '') transform = etree.XSLT(etree.parse(addons.get_module_path('df_report_base') + "/report/custom_new.xsl")) rml = etree.tostring(transform(new_doc)) self.obj = render.rml(rml, title=self.title) self.obj.render() return True
def init_db(cr): import addons f = addons.get_module_resource('base', 'base.sql') for line in file_open(f).read().split(';'): if (len(line)>0) and (not line.isspace()): cr.execute(line) cr.commit() for i in addons.get_modules(): mod_path = addons.get_module_path(i) if not mod_path: continue info = addons.load_information_from_description_file(i) if not info: continue categs = info.get('category', 'Uncategorized').split('/') p_id = None while categs: if p_id is not None: cr.execute('select id \ from ir_module_category \ where name=%s and parent_id=%s', (categs[0], p_id)) else: cr.execute('select id \ from ir_module_category \ where name=%s and parent_id is NULL', (categs[0],)) c_id = cr.fetchone() if not c_id: cr.execute('select nextval(\'ir_module_category_id_seq\')') c_id = cr.fetchone()[0] cr.execute('insert into ir_module_category \ (id, name, parent_id) \ values (%s, %s, %s)', (c_id, categs[0], p_id)) else: c_id = c_id[0] p_id = c_id categs = categs[1:] active = info.get('active', False) installable = info.get('installable', True) if installable: if active: state = 'to install' else: state = 'uninstalled' else: state = 'uninstallable' cr.execute('select nextval(\'ir_module_module_id_seq\')') id = cr.fetchone()[0] cr.execute('insert into ir_module_module \ (id, author, website, name, shortdesc, description, \ category_id, state, certificate) \ values (%s, %s, %s, %s, %s, %s, %s, %s, %s)', ( id, info.get('author', ''), info.get('website', ''), i, info.get('name', False), info.get('description', ''), p_id, state, info.get('certificate') or None)) cr.execute('insert into ir_model_data \ (name,model,module, res_id, noupdate) values (%s,%s,%s,%s,%s)', ( 'module_meta_information', 'ir.module.module', i, id, True)) dependencies = info.get('depends', []) for d in dependencies: cr.execute('insert into ir_module_module_dependency \ (module_id,name) values (%s, %s)', (id, d)) cr.commit()
def check_quality(self, cr, uid, module_name, module_state=None): ''' This function will calculate score of openerp module It will return data in below format: Format: {'final_score':'80.50', 'name': 'sale', 'check_detail_ids': [(0,0,{'name':'workflow_test', 'score':'100', 'ponderation':'0', 'summary': text_wiki format data, 'detail': html format data, 'state':'done', 'note':'XXXX'}), ((0,0,{'name':'terp_test', 'score':'60', 'ponderation':'1', 'summary': text_wiki format data, 'detail': html format data, 'state':'done', 'note':'terp desctioption'}), ..........]} So here the detail result is in html format and summary will be in text_wiki format. ''' pool = pooler.get_pool(cr.dbname) log = logging.getLogger('module.quality') obj_module = pool.get('ir.module.module') if not module_state: module_id = obj_module.search(cr, uid, [('name', '=', module_name)]) if module_id: module_state = obj_module.browse(cr, uid, module_id[0]).state abstract_obj = abstract_quality_check() score_sum = 0.0 ponderation_sum = 0.0 create_ids = [] module_path = addons.get_module_path(module_name) log.info('Performing quality tests for %s', module_name) for test in abstract_obj.tests: val = test.quality_test() if not val.active: log.info('Skipping inactive step %s for %s', val.name, module_name) continue log.info('Performing step %s for %s', val.name, module_name) # Get a separate cursor per test, so that an SQL error in one # will not block the others. cr2 = pooler.get_db(cr.dbname).cursor() try: if not val.bool_installed_only or module_state == "installed": val.run_test(cr2, uid, str(module_path)) if not val.error: data = { 'name': val.name, 'score': val.score * 100, 'ponderation': val.ponderation, 'summary': val.result, 'detail': val.result_details, 'state': 'done', 'note': val.note, 'message': val.message } if val.bool_count_score: score_sum += val.score * val.ponderation ponderation_sum += val.ponderation else: data = { 'name': val.name, 'score': 0, 'summary': val.result, 'state': 'skipped', 'note': val.note, } else: data = { 'name': val.name, 'note': val.note, 'score': 0, 'state': 'skipped', 'summary': _("The module has to be installed before running this test.") } create_ids.append((0, 0, data)) log.info('Finished quality test step') except Exception, e: log.exception("Could not finish test step %s due to %s", val.name, e) finally:
def __init__(self): ''' this method should initialize the variables ''' #This float have to store the rating of the module. #Used to compute the final score (average of all scores). #0 <= self.score <= 1 self.score = 0.0 #This char have to store the name of the test. self.name = "" #This char have to store the aim of the test and eventually a note. self.note = "" #This char have to store the result. #Used to display the result of the test. self.result = "" #This char have to store the result with more details. #Used to provide more details if necessary. self.result_details = "" # This boolean variable defines that if you do not want to calculate score and just only need detail # or summary report for some test then you will make it False. self.bool_count_score = True #This bool defines if the test can be run only if the module #is installed. #True => the module have to be installed. #False => the module can be uninstalled. self.bool_installed_only = True #This variable is used to give result of test more weight, #because some tests are more critical than others. self.ponderation = 1.0 #Specify test got an error on module self.error = False #Specify the minimal score for the test (in percentage(%)) self.min_score = 50 #Specify whether test should be consider for Quality checking of the module self.active = True #This variable used to give message if test result is good or not self.message = '' self.log = logging.getLogger('module.quality') #The tests have to subscribe itselfs in this list, that contains #all the test that have to be performed. self.tests = [] module_path = addons.get_module_path('base_module_quality') for item in os.listdir(module_path): path = module_path + '/' + item if os.path.isdir(path) and os.path.exists(path + '/' + item + '.py') and item not in ['report', 'wizard', 'security']: item2 = 'base_module_quality.' + item +'.' + item x_module = __import__(item2) x_file = getattr(x_module, item) x_obj = getattr(x_file, item) self.tests.append(x_obj)
def update_list(self, cr, uid, context={}): robj = self.pool.get('ir.module.repository') res = [0, 0] # [update, add] # iterate through installed modules and mark them as being so for mod_name in addons.get_modules(): ids = self.search(cr, uid, [('name','=',mod_name)]) if ids: id = ids[0] mod = self.browse(cr, uid, id) terp = self.get_module_info(mod_name) if terp.get('installable', True) and mod.state == 'uninstallable': self.write(cr, uid, id, {'state': 'uninstalled'}) if parse_version(terp.get('version', '')) > parse_version(mod.latest_version or ''): self.write(cr, uid, id, { 'url': ''}) res[0] += 1 self.write(cr, uid, id, { 'description': terp.get('description', ''), 'shortdesc': terp.get('name', ''), 'author': terp.get('author', 'Unknown'), 'website': terp.get('website', ''), 'license': terp.get('license', 'GPL-2'), 'certificate': terp.get('certificate') or None, }) cr.execute('DELETE FROM ir_module_module_dependency WHERE module_id = %s', (id,)) self._update_dependencies(cr, uid, ids[0], terp.get('depends', [])) self._update_category(cr, uid, ids[0], terp.get('category', 'Uncategorized')) continue mod_path = addons.get_module_path(mod_name) if mod_path: terp = self.get_module_info(mod_name) if not terp or not terp.get('installable', True): continue id = self.create(cr, uid, { 'name': mod_name, 'state': 'uninstalled', 'description': terp.get('description', ''), 'shortdesc': terp.get('name', ''), 'author': terp.get('author', 'Unknown'), 'website': terp.get('website', ''), 'license': terp.get('license', 'GPL-2'), 'certificate': terp.get('certificate') or None, }) res[1] += 1 self._update_dependencies(cr, uid, id, terp.get('depends', [])) self._update_category(cr, uid, id, terp.get('category', 'Uncategorized')) for repository in robj.browse(cr, uid, robj.search(cr, uid, [])): try: index_page = urllib.urlopen(repository.url).read() except IOError, e: if e.errno == 21: raise orm.except_orm(_('Error'), _("This url '%s' must provide an html file with links to zip modules") % (repository.url)) else: raise modules = re.findall(repository.filter, index_page, re.I+re.M) mod_sort = {} for m in modules: name, version, extension = m[0], m[1], m[-1] if not version or version == 'x': # 'x' version was a mistake version = '0' if name in mod_sort: if parse_version(version) <= parse_version(mod_sort[name][0]): continue mod_sort[name] = [version, extension] for name in mod_sort.keys(): version, extension = mod_sort[name] url = repository.url+'/'+name+'-'+version+extension ids = self.search(cr, uid, [('name','=',name)]) if not ids: self.create(cr, uid, { 'name': name, 'published_version': version, 'url': url, 'state': 'uninstalled', }) res[1] += 1 else: id = ids[0] installed_version = self.read(cr, uid, id, ['latest_version'])['latest_version'] if not installed_version or installed_version == 'x': # 'x' version was a mistake installed_version = '0' if parse_version(version) > parse_version(installed_version): self.write(cr, uid, id, { 'url': url }) res[0] += 1 published_version = self.read(cr, uid, id, ['published_version'])['published_version'] if published_version == 'x' or not published_version: published_version = '0' if parse_version(version) > parse_version(published_version): self.write(cr, uid, id, {'published_version': version})
def exp_get_migration_scripts(self, contract_id, contract_password): l = netsvc.Logger() import tools.maintenance as tm try: rc = tm.remote_contract(contract_id, contract_password) if not rc.id: raise tm.RemoteContractException('This contract does not exist or is not active') if rc.status != 'full': raise tm.RemoteContractException('Can not get updates for a partial contract') l.notifyChannel('migration', netsvc.LOG_INFO, 'starting migration with contract %s' % (rc.name,)) zips = rc.retrieve_updates(rc.id, addons.get_modules_with_version()) from shutil import rmtree, copytree, copy backup_directory = os.path.join(tools.config['root_path'], 'backup', time.strftime('%Y-%m-%d-%H-%M')) if zips and not os.path.isdir(backup_directory): l.notifyChannel('migration', netsvc.LOG_INFO, 'create a new backup directory to \ store the old modules: %s' % (backup_directory,)) os.makedirs(backup_directory) for module in zips: l.notifyChannel('migration', netsvc.LOG_INFO, 'upgrade module %s' % (module,)) mp = addons.get_module_path(module) if mp: if os.path.isdir(mp): copytree(mp, os.path.join(backup_directory, module)) if os.path.islink(mp): os.unlink(mp) else: rmtree(mp) else: copy(mp + 'zip', backup_directory) os.unlink(mp + '.zip') try: try: base64_decoded = base64.decodestring(zips[module]) except: l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to read the module %s' % (module,)) raise zip_contents = StringIO(base64_decoded) zip_contents.seek(0) try: try: tools.extract_zip_file(zip_contents, tools.config['addons_path'] ) except: l.notifyChannel('migration', netsvc.LOG_ERROR, 'unable to extract the module %s' % (module, )) rmtree(module) raise finally: zip_contents.close() except: l.notifyChannel('migration', netsvc.LOG_ERROR, 'restore the previous version of the module %s' % (module, )) nmp = os.path.join(backup_directory, module) if os.path.isdir(nmp): copytree(nmp, tools.config['addons_path']) else: copy(nmp+'.zip', tools.config['addons_path']) raise return True except tm.RemoteContractException, e: self.abortResponse(1, 'Migration Error', 'warning', str(e))
def check_quality(self, cr, uid, module_name, module_state=None): ''' This function will calculate score of openerp module It will return data in below format: Format: {'final_score':'80.50', 'name': 'sale', 'check_detail_ids': [(0,0,{'name':'workflow_test', 'score':'100', 'ponderation':'0', 'summary': text_wiki format data, 'detail': html format data, 'state':'done', 'note':'XXXX'}), ((0,0,{'name':'terp_test', 'score':'60', 'ponderation':'1', 'summary': text_wiki format data, 'detail': html format data, 'state':'done', 'note':'terp desctioption'}), ..........]} So here the detail result is in html format and summary will be in text_wiki format. ''' pool = pooler.get_pool(cr.dbname) log = logging.getLogger('module.quality') obj_module = pool.get('ir.module.module') if not module_state: module_id = obj_module.search(cr, uid, [('name', '=', module_name)]) if module_id: module_state = obj_module.browse(cr, uid, module_id[0]).state abstract_obj = abstract_quality_check() score_sum = 0.0 ponderation_sum = 0.0 create_ids = [] module_path = addons.get_module_path(module_name) log.info('Performing quality tests for %s', module_name) for test in abstract_obj.tests: val = test.quality_test() if not val.active: log.info('Skipping inactive step %s for %s', val.name, module_name) continue log.info('Performing step %s for %s', val.name, module_name) # Get a separate cursor per test, so that an SQL error in one # will not block the others. cr2 = pooler.get_db(cr.dbname).cursor() try: if not val.bool_installed_only or module_state == "installed": val.run_test(cr2, uid, str(module_path)) if not val.error: data = { 'name': val.name, 'score': val.score * 100, 'ponderation': val.ponderation, 'summary': val.result, 'detail': val.result_details, 'state': 'done', 'note': val.note, 'message': val.message } if val.bool_count_score: score_sum += val.score * val.ponderation ponderation_sum += val.ponderation else: data = { 'name': val.name, 'score': 0, 'summary': val.result, 'state': 'skipped', 'note': val.note, } else: data = { 'name': val.name, 'note': val.note, 'score': 0, 'state': 'skipped', 'summary': _("The module has to be installed before running this test." ) } create_ids.append((0, 0, data)) log.info('Finished quality test step') except Exception, e: log.exception("Could not finish test step %s due to %s", val.name, e) finally:
def update_list(self, cr, uid, context=None): if context is None: context = {} res = [0, 0] # [update, add] all_mod_ids = self.search(cr, uid, [], context=context) known_module_names = addons.get_modules() def ifustr(var): """ Auto-convert all strings to unicode""" if isinstance(var, basestring): return tools.ustr(var) else: return var for old_mod in self.browse(cr, uid, all_mod_ids, context=context): if old_mod.name in known_module_names: known_module_names.remove(old_mod.name) terp = self.get_module_info(old_mod.name) if not terp or not terp.get('installable', True): if old_mod.state != 'uninstallable': self.write(cr, uid, old_mod.id, {'state': 'uninstallable'}) continue values = self.get_values_from_terp(terp) new_values = { } for key in values: if getattr(old_mod, key) != ifustr(values[key]): new_values[key] = ifustr(values[key]) if new_values: self.write(cr, uid, old_mod.id, new_values) old_depends = [ x.name for x in old_mod.dependencies_id ] old_depends.sort() new_depends = terp.get('depends', []) new_depends.sort() if old_depends != new_depends: cr.execute('DELETE FROM ir_module_module_dependency ' 'WHERE module_id = %s', (old_mod.id,), debug=self._debug) self._update_dependencies(cr, uid, old_mod.id, new_depends) self._update_category(cr, uid, old_mod.id, terp.get('category', 'Uncategorized'), old_cat=old_mod.category_id) else: # This module is no longer in the file tree if old_mod.state != 'uninstallable': self.write(cr, uid, old_mod.id, {'state': 'uninstallable'}) # TODO: clear dependencies or even module data, RFC # Now, we are left with names of modules that are not in the db, # the new modules: for mod_name in known_module_names: terp = self.get_module_info(mod_name) values = self.get_values_from_terp(terp) mod_path = addons.get_module_path(mod_name) # Addons shouldn't ever tell us names of non-existing modules assert mod_path, "No module path for %s" % mod_name if not terp or not terp.get('installable', True): continue values['state'] = 'uninstalled' values['name'] = mod_name id = self.create(cr, uid, values, context=context) res[1] += 1 self._update_dependencies(cr, uid, id, terp.get('depends', [])) self._update_category(cr, uid, id, terp.get('category', 'Uncategorized'), old_cat=False) return res
def __init__(self): ''' this method should initialize the variables ''' #This float have to store the rating of the module. #Used to compute the final score (average of all scores). #0 <= self.score <= 1 self.score = 0.0 #This char have to store the name of the test. self.name = "" #This char have to store the aim of the test and eventually a note. self.note = "" #This char have to store the result. #Used to display the result of the test. self.result = "" #This char have to store the result with more details. #Used to provide more details if necessary. self.result_details = "" # This boolean variable defines that if you do not want to calculate score and just only need detail # or summary report for some test then you will make it False. self.bool_count_score = True #This bool defines if the test can be run only if the module #is installed. #True => the module have to be installed. #False => the module can be uninstalled. self.bool_installed_only = True #This variable is used to give result of test more weight, #because some tests are more critical than others. self.ponderation = 1.0 #Specify test got an error on module self.error = False #Specify the minimal score for the test (in percentage(%)) self.min_score = 50 #Specify whether test should be consider for Quality checking of the module self.active = True #This variable used to give message if test result is good or not self.message = '' self.log = logging.getLogger('module.quality') #The tests have to subscribe itselfs in this list, that contains #all the test that have to be performed. self.tests = [] module_path = addons.get_module_path('base_module_quality') for item in os.listdir(module_path): path = module_path + '/' + item if os.path.isdir(path) and os.path.exists(path + '/' + item + '.py') and item not in [ 'report', 'wizard', 'security' ]: item2 = 'base_module_quality.' + item + '.' + item x_module = __import__(item2) x_file = getattr(x_module, item) x_obj = getattr(x_file, item) self.tests.append(x_obj)