def project_from_files(self, files, func_wrapper=astng_wrapper, project_name=None, black_list=None): """return a Project from a list of files or modules""" # build the project representation project_name = project_name or self.config.project black_list = black_list or self.config.black_list project = Project(project_name) for something in files: if not exists(something): fpath = file_from_modpath(something.split('.')) elif isdir(something): fpath = join(something, '__init__.py') else: fpath = something astng = func_wrapper(self.astng_from_file, fpath) if astng is None: continue # XXX why is first file defining the project.path ? project.path = project.path or astng.file project.add_module(astng) base_name = astng.name # recurse in package except if __init__ was explicitly given if astng.package and something.find('__init__') == -1: # recurse on others packages / modules if this is a package for fpath in get_module_files(dirname(astng.file), black_list): astng = func_wrapper(self.astng_from_file, fpath) if astng is None or astng.name == base_name: continue project.add_module(astng) return project
def project_from_files(self, files, func_wrapper=astroid_wrapper, project_name=None, black_list=None): """return a Project from a list of files or modules""" # build the project representation project_name = project_name or self.config.project black_list = black_list or self.config.black_list project = Project(project_name) for something in files: if not exists(something): fpath = file_from_modpath(something.split('.')) elif isdir(something): fpath = join(something, '__init__.py') else: fpath = something astroid = func_wrapper(self.ast_from_file, fpath) if astroid is None: continue # XXX why is first file defining the project.path ? project.path = project.path or astroid.file project.add_module(astroid) base_name = astroid.name # recurse in package except if __init__ was explicitly given if astroid.package and something.find('__init__') == -1: # recurse on others packages / modules if this is a package for fpath in get_module_files(dirname(astroid.file), black_list): astroid = func_wrapper(self.ast_from_file, fpath) if astroid is None or astroid.name == base_name: continue project.add_module(astroid) return project
def test_knownValues_get_module_files_1(self): # XXXFIXME: TOWRITE """given a directory return a list of all available python module's files, even in subdirectories """ import data modules = sorted(modutils.get_module_files(path.join(DATADIR, 'find_test'), data.__path__[0])) self.assertEqual(modules, [path.join(DATADIR, 'find_test', x) for x in ['__init__.py', 'module.py', 'module2.py', 'noendingnewline.py', 'nonregr.py']])
def expand_modules(files_or_modules, black_list): """take a list of files/modules/packages and return the list of tuple (file, module name) which have to be actually checked """ real_stuff = [] for file_or_module_name in files_or_modules: if not any([ re.search(pattern, file_or_module_name) for pattern in black_list ]): real_stuff.append(file_or_module_name) result = [] errors = [] for something in real_stuff: if exists(something): # this is a file or a directory try: modname = '.'.join(modpath_from_file(something)) except ImportError: modname = splitext(basename(something))[0] if isdir(something): filepath = join(something, '__init__.py') else: filepath = something else: # suppose it's a module or package modname = something try: filepath = file_from_modpath(modname.split('.')) if filepath is None: errors.append({'key': 'F0003', 'mod': modname}) continue except (ImportError, SyntaxError), ex: # FIXME p3k : the SyntaxError is a Python bug and should be # removed as soon as possible http://bugs.python.org/issue10588 errors.append({'key': 'F0001', 'mod': modname, 'ex': ex}) continue filepath = normpath(filepath) result.append({ 'path': filepath, 'name': modname, 'basepath': filepath, 'basename': modname }) if not (modname.endswith('.__init__') or modname == '__init__') \ and '__init__.py' in filepath: for subfilepath in get_module_files(dirname(filepath), black_list): if filepath == subfilepath: continue submodname = '.'.join(modpath_from_file(subfilepath)) result.append({ 'path': subfilepath, 'name': submodname, 'basepath': filepath, 'basename': modname })
def expand_modules(files_or_modules, black_list): """take a list of files/modules/packages and return the list of tuple (file, module name) which have to be actually checked """ real_stuff = [] for file_or_module_name in files_or_modules: if not any([re.search(pattern, file_or_module_name) for pattern in black_list]): real_stuff.append(file_or_module_name) result = [] errors = [] for something in real_stuff: if exists(something): # this is a file or a directory try: modname = '.'.join(modpath_from_file(something)) except ImportError: modname = splitext(basename(something))[0] if isdir(something): filepath = join(something, '__init__.py') else: filepath = something else: # suppose it's a module or package modname = something try: filepath = file_from_modpath(modname.split('.')) if filepath is None: errors.append( {'key' : 'F0003', 'mod': modname} ) continue except (ImportError, SyntaxError), ex: # FIXME p3k : the SyntaxError is a Python bug and should be # removed as soon as possible http://bugs.python.org/issue10588 errors.append( {'key': 'F0001', 'mod': modname, 'ex': ex} ) continue filepath = normpath(filepath) result.append( {'path': filepath, 'name': modname, 'basepath': filepath, 'basename': modname} ) if not (modname.endswith('.__init__') or modname == '__init__') \ and '__init__.py' in filepath: for subfilepath in get_module_files(dirname(filepath), black_list): if filepath == subfilepath: continue submodname = '.'.join(modpath_from_file(subfilepath)) result.append( {'path': subfilepath, 'name': submodname, 'basepath': filepath, 'basename': modname} )
def expand_modules(files_or_modules, black_list): """take a list of files/modules/packages and return the list of tuple (file, module name) which have to be actually checked """ result = [] errors = [] for something in files_or_modules: if exists(something): # this is a file or a directory try: modname = ".".join(modpath_from_file(something)) except ImportError: modname = splitext(basename(something))[0] if isdir(something): filepath = join(something, "__init__.py") else: filepath = something else: # suppose it's a module or package modname = something try: filepath = file_from_modpath(modname.split(".")) if filepath is None: errors.append({"key": "ignored-builtin-module", "mod": modname}) continue except (ImportError, SyntaxError), ex: # FIXME p3k : the SyntaxError is a Python bug and should be # removed as soon as possible http://bugs.python.org/issue10588 errors.append({"key": "fatal", "mod": modname, "ex": ex}) continue filepath = normpath(filepath) result.append({"path": filepath, "name": modname, "isarg": True, "basepath": filepath, "basename": modname}) if not (modname.endswith(".__init__") or modname == "__init__") and "__init__.py" in filepath: for subfilepath in get_module_files(dirname(filepath), black_list): if filepath == subfilepath: continue submodname = ".".join(modpath_from_file(subfilepath)) result.append( {"path": subfilepath, "name": submodname, "isarg": False, "basepath": filepath, "basename": modname} )
def project_from_files(self, files, func_wrapper=astng_wrapper, project_name=None, black_list=None): """return a Project from a list of files or modules""" # insert current working directory to the python path to have a correct # behaviour sys.path.insert(0, os.getcwd()) try: # build the project representation project_name = project_name or self.config.project black_list = black_list or self.config.black_list project = Project(project_name) for something in files: if not exists(something): fpath = file_from_modpath(something.split('.')) elif isdir(something): fpath = join(something, '__init__.py') else: fpath = something astng = func_wrapper(self.astng_from_file, fpath) if astng is None: continue project.path = project.path or astng.file project.add_module(astng) base_name = astng.name # recurse in package except if __init__ was explicitly given if astng.package and something.find('__init__') == -1: # recurse on others packages / modules if this is a package for fpath in get_module_files(dirname(astng.file), black_list): astng = func_wrapper(self.astng_from_file, fpath) if astng is None or astng.name == base_name: continue project.add_module(astng) return project finally: sys.path.pop(0)
def expand_modules(files_or_modules, black_list): """take a list of files/modules/packages and return the list of tuple (file, module name) which have to be actually checked """ result = [] errors = [] for something in files_or_modules: if exists(something): # this is a file or a directory try: modname = '.'.join(modpath_from_file(something)) except ImportError: modname = splitext(basename(something))[0] if isdir(something): filepath = join(something, '__init__.py') else: filepath = something else: # suppose it's a module or package modname = something try: filepath = file_from_modpath(modname.split('.')) if filepath is None: errors.append( {'key' : 'F0003', 'mod': modname} ) continue except ImportError, ex: errors.append( {'key': 'F0001', 'mod': modname, 'ex': ex} ) continue filepath = normpath(filepath) result.append( {'path': filepath, 'name': modname, 'basepath': filepath, 'basename': modname} ) if not (modname.endswith('.__init__') or modname == '__init__') \ and '__init__.py' in filepath: for subfilepath in get_module_files(dirname(filepath), black_list): if filepath == subfilepath: continue submodname = '.'.join(modpath_from_file(subfilepath)) result.append( {'path': subfilepath, 'name': submodname, 'basepath': filepath, 'basename': modname} )
def expand_modules(files_or_modules, black_list): """take a list of files/modules/packages and return the list of tuple (file, module name) which have to be actually checked """ result = [] errors = [] for something in files_or_modules: if exists(something): # this is a file or a directory try: modname = ".".join(modpath_from_file(something)) except ImportError: modname = splitext(basename(something))[0] if isdir(something): filepath = join(something, "__init__.py") else: filepath = something else: # suppose it's a module or package modname = something try: filepath = file_from_modpath(modname.split(".")) if filepath is None: errors.append({"key": "F0003", "mod": modname}) continue except ImportError, ex: errors.append({"key": "F0001", "mod": modname, "ex": ex}) continue filepath = normpath(filepath) result.append({"path": filepath, "name": modname, "basepath": filepath, "basename": modname}) if not (modname.endswith(".__init__") or modname == "__init__") and "__init__.py" in filepath: for subfilepath in get_module_files(dirname(filepath), black_list): if filepath == subfilepath: continue submodname = ".".join(modpath_from_file(subfilepath)) result.append({"path": subfilepath, "name": submodname, "basepath": filepath, "basename": modname})
def run(self, args): """run the command with its specific arguments""" import shutil import tempfile import yams from logilab.common.fileutils import ensure_fs_mode from logilab.common.shellutils import globfind, find, rm from logilab.common.modutils import get_module_files from cubicweb.i18n import extract_from_tal, execute2 tempdir = tempfile.mkdtemp(prefix='cw-') cwi18ndir = WebConfiguration.i18n_lib_dir() print('-> extract messages:', end=' ') print('schema', end=' ') schemapot = osp.join(tempdir, 'schema.pot') potfiles = [schemapot] potfiles.append(schemapot) # explicit close necessary else the file may not be yet flushed when # we'll using it below schemapotstream = open(schemapot, 'w') generate_schema_pot(schemapotstream.write, cubedir=None) schemapotstream.close() print('TAL', end=' ') tali18nfile = osp.join(tempdir, 'tali18n.py') extract_from_tal(find(osp.join(BASEDIR, 'web'), ('.py', '.pt')), tali18nfile) print('-> generate .pot files.') pyfiles = get_module_files(BASEDIR) pyfiles += globfind(osp.join(BASEDIR, 'misc', 'migration'), '*.py') schemafiles = globfind(osp.join(BASEDIR, 'schemas'), '*.py') jsfiles = globfind(osp.join(BASEDIR, 'web'), 'cub*.js') for id, files, lang in [ ('pycubicweb', pyfiles, None), ('schemadescr', schemafiles, None), ('yams', get_module_files(yams.__path__[0]), None), ('tal', [tali18nfile], None), ('js', jsfiles, 'java'), ]: potfile = osp.join(tempdir, '%s.pot' % id) cmd = ['xgettext', '--no-location', '--omit-header', '-k_'] if lang is not None: cmd.extend(['-L', lang]) cmd.extend(['-o', potfile]) cmd.extend(files) execute2(cmd) if osp.exists(potfile): potfiles.append(potfile) else: print('-> WARNING: %s file was not generated' % potfile) print('-> merging %i .pot files' % len(potfiles)) cubicwebpot = osp.join(tempdir, 'cubicweb.pot') cmd = ['msgcat', '-o', cubicwebpot] + potfiles execute2(cmd) print('-> merging main pot file with existing translations.') chdir(cwi18ndir) toedit = [] for lang in CubicWebNoAppConfiguration.cw_languages(): target = '%s.po' % lang cmd = [ 'msgmerge', '-N', '--sort-output', '-o', target + 'new', target, cubicwebpot ] execute2(cmd) ensure_fs_mode(target) shutil.move('%snew' % target, target) toedit.append(osp.abspath(target)) # cleanup rm(tempdir) # instructions pour la suite print('-> regenerated CubicWeb\'s .po catalogs.') print('\nYou can now edit the following files:') print('* ' + '\n* '.join(toedit)) print('when you are done, run "cubicweb-ctl i18ncube yourcube".')