def run(self): module_names = find_packages() dst_rel_path = 'locale' if self.dest is None else self.dest # group by top-level packages and compute their directories: top_levels_modules = {} for module_name in module_names: tl, sep, bl = module_name.partition('.') if tl not in top_levels_modules: locale_dir = os.path.join( os.path.dirname(load_module(tl).__file__), dst_rel_path) top_levels_modules[tl] = locale_dir for module_name, locale_dir in top_levels_modules.items(): mo_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.mo' % module_name) po_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.po' % module_name) if not os.path.isdir(os.path.dirname(mo_file)): os.makedirs(os.path.dirname(mo_file)) if os.path.isfile(po_file): cmd = 'msgfmt --output-file %s %s' % (mo_file, po_file) display(_('Processing file %(filename)s.') % {'filename': po_file}, color=GREEN) subprocess.check_call(cmd, shell=True, stderr=subprocess.PIPE)
def run(self, interactive=True): project_root = self.global_context['project_root'] if os.path.exists(project_root): if self.global_context['overwrite']: if os.path.isdir(project_root): shutil.rmtree(project_root) else: os.remove(project_root) else: display(_('Destination path already exists!'), color=RED, bold=True) return context = self.get_context() self.global_context.update(context) extra_form = self.get_extraform(interactive=interactive) self.global_context.update(extra_form) extra_context = self.get_extracontext() self.global_context.update(extra_context) filters = self.get_template_filters() self.set_virtualenvs() for modname, dirname in self.template_roots: display('dirname %s' % dirname, color=CYAN) env = self.get_environment(modname, dirname, filters) self.write_files(modname, dirname, env)
def run(self): if sphinx is None: display(_('package sphinx is required.'), color=RED) return 1 if self.clean and os.path.isdir(self.build_dir): msg = _('removing %(dir)s') % {'dir': self.build_dir} display(msg, color=GREEN) shutil.rmtree(self.build_dir) sphinx_opts = shlex.split(self.ALLSPHINXOPTS % (self.build_dir, os.getenv('SPHINXOPTS') or '', self.doc_dir)) count = 0 for orig_fmt, txt in self.outputs.items(): fmt = 'latex' if orig_fmt == 'latexpdf' else orig_fmt if orig_fmt not in self.distribution.command_options['gen_doc']: continue count = 1 options = ['sphinx-build', '-b', fmt, ] + sphinx_opts + [os.path.join(self.build_dir, fmt), ] result = sphinx.main(options) if result == 0: msg = txt % self.build_dir display(msg, color=GREEN) if orig_fmt == 'latexpdf': subprocess.check_call('make -C %s/latex all-pdf' % self.build_dir, shell=True) msg = "pdflatex finished; the PDF files are in %s/latex." % self.build_dir display(msg, color=GREEN) if not count: display(_("please select at least one output format (e.g. gen_doc --html)"), color=YELLOW)
def run(self): if self.input: try: # noinspection PyUnresolvedReferences import pstats except ImportError: display('Module pstats not found.', color=RED) return if not os.path.isfile(self.input): display(u('File %s not found' % self.input), color=RED) return stats = pstats.Stats(self.input) stats.print_stats() elif not self.call: display( 'Please provide a function to profile with --call \'module.function\'', color=RED) return else: if '(' not in self.call: self.call += '()' tokens = tokenize.generate_tokens(BytesIO(self.call).readline) index = 0 simple_function_call = True for toknum, tokval, tokstart, tokend, tokline in tokens: if toknum == token.ENDMARKER: break elif index == 0 and toknum != token.NAME: simple_function_call = False break elif index == 1 and toknum == token.OP and tokval == '(': break elif index == 1 and (toknum != token.OP or tokval != '.'): simple_function_call = False break index = 1 - index if simple_function_call: module_name = self.call.partition('(')[0].rpartition('.')[0] if module_name: display('Load module %s' % module_name, color=GREEN) self.call = 'import %s ; %s' % (module_name, self.call) display("running profiling on %(call)s" % {'call': self.call}, color=GREEN) if self.debug: pdb.run(self.call) else: profile.run(self.call, self.output)
def read(self, initial=None): if initial is None: initial = self.initial raw_value = input_(self.widget(initial=initial)) if not raw_value and initial is not None: raw_value = self.to_str(initial) while True: try: valid_value = self.to_python(raw_value) break except InvalidValue as e: display(_('Invalid value: %(value)s (%(msg)s)') % {'value': raw_value, 'msg': str(e)}, color=RED, bold=True) raw_value = input_(self.widget()) if not raw_value and initial is not None: raw_value = self.to_str(initial) # noinspection PyUnboundLocalVariable return valid_value
def run(self): if self.input: try: # noinspection PyUnresolvedReferences import pstats except ImportError: display('Module pstats not found.', color=RED) return if not os.path.isfile(self.input): display(u('File %s not found' % self.input), color=RED) return stats = pstats.Stats(self.input) stats.print_stats() elif not self.call: display('Please provide a function to profile with --call \'module.function\'', color=RED) return else: if '(' not in self.call: self.call += '()' tokens = tokenize.generate_tokens(BytesIO(self.call).readline) index = 0 simple_function_call = True for toknum, tokval, tokstart, tokend, tokline in tokens: if toknum == token.ENDMARKER: break elif index == 0 and toknum != token.NAME: simple_function_call = False break elif index == 1 and toknum == token.OP and tokval == '(': break elif index == 1 and (toknum != token.OP or tokval != '.'): simple_function_call = False break index = 1 - index if simple_function_call: module_name = self.call.partition('(')[0].rpartition('.')[0] if module_name: display('Load module %s' % module_name, color=GREEN) self.call = 'import %s ; %s' % (module_name, self.call) display("running profiling on %(call)s" % {'call': self.call}, color=GREEN) if self.debug: pdb.run(self.call) else: profile.run(self.call, self.output)
def run(self): module_names = find_packages() dst_rel_path = 'locale' if self.dest is None else self.dest # group by top-level packages and compute their directories: top_levels_modules = {} for module_name in module_names: tl, sep, bl = module_name.partition('.') if tl not in top_levels_modules: locale_dir = os.path.join(os.path.dirname(load_module(tl).__file__), dst_rel_path) top_levels_modules[tl] = locale_dir for module_name, locale_dir in top_levels_modules.items(): mo_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.mo' % module_name) po_file = os.path.join(locale_dir, self.language, 'LC_MESSAGES', '%s.po' % module_name) if not os.path.isdir(os.path.dirname(mo_file)): os.makedirs(os.path.dirname(mo_file)) if os.path.isfile(po_file): cmd = 'msgfmt --output-file %s %s' % (mo_file, po_file) display(_('Processing file %(filename)s.') % {'filename': po_file}, color=GREEN) subprocess.check_call(cmd, shell=True, stderr=subprocess.PIPE)
def run(self): if sphinx is None: display(_('package sphinx is required.'), color=RED) return 1 if self.clean and os.path.isdir(self.build_dir): msg = _('removing %(dir)s') % {'dir': self.build_dir} display(msg, color=GREEN) shutil.rmtree(self.build_dir) sphinx_opts = shlex.split( self.ALLSPHINXOPTS % (self.build_dir, os.getenv('SPHINXOPTS') or '', self.doc_dir)) count = 0 for orig_fmt, txt in self.outputs.items(): fmt = 'latex' if orig_fmt == 'latexpdf' else orig_fmt if orig_fmt not in self.distribution.command_options['gen_doc']: continue count = 1 options = [ 'sphinx-build', '-b', fmt, ] + sphinx_opts + [ os.path.join(self.build_dir, fmt), ] result = sphinx.main(options) if result == 0: msg = txt % self.build_dir display(msg, color=GREEN) if orig_fmt == 'latexpdf': subprocess.check_call('make -C %s/latex all-pdf' % self.build_dir, shell=True) msg = "pdflatex finished; the PDF files are in %s/latex." % self.build_dir display(msg, color=GREEN) if not count: display(_( "please select at least one output format (e.g. gen_doc --html)" ), color=YELLOW)
def run(self): module_names = find_packages() for module_name in module_names: module = load_module(module_name) display('Processing %s.' % module_name, color=YELLOW) doctest.testmod(module) module_root = os.path.dirname(module.__file__) for filename in os.listdir(module_root): basename, sep, ext = filename.rpartition('.') if ext != 'py' or filename == '__init__.py': continue submodule_name = '%s.%s' % (module_name, basename) try: module = load_module(submodule_name) display('Processing %s.' % submodule_name, color=GREEN) doctest.testmod(module) except ImportError: display('Unable to import %s.' % submodule_name, color=YELLOW)
def run(self): if Environment is None: display(_('package jinja2 is required.'), color=RED) return 1 module_names = find_packages() dst_rel_path = 'locale' if self.dest is None else self.dest # group by top-level packages and compute their directories: all_modules = {} top_levels_modules = {} for module_name in module_names: top_module = module_name.partition('.')[0] all_modules.setdefault(top_module, []).append(module_name) if top_module not in top_levels_modules: top_levels_modules[top_module] = os.path.dirname(load_module(top_module).__file__) env = Environment(loader=PackageLoader('starterpyth.commands.makemessages', 'templates')) template = env.get_template('lang.po') context = { 'description': self.distribution.get_description(), 'copyright': self.distribution.get_author(), 'package': None, 'author': self.distribution.get_author(), 'version': self.distribution.get_version(), 'email': self.distribution.get_author_email(), 'year': datetime.datetime.now().year, } for tl_name in top_levels_modules.keys(): dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path) pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name) po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name) if not os.path.isdir(os.path.dirname(po_filename)): os.makedirs(os.path.dirname(po_filename)) for filename in (pot_filename, po_filename): if not os.path.isfile(filename): context['package'] = tl_name po_fd = codecs.open(filename, 'w', encoding='utf-8') po_fd.write(template.render(context)) po_fd.close() for tl_name, module_names in all_modules.items(): dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path) root_path = os.path.dirname(top_levels_modules[tl_name]) print(root_path) pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name) po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name) # build the list of files to examine, for each top-level module filenames = [] for module_name in module_names: init_filename = load_module(module_name).__file__ local_root = os.path.dirname(init_filename) for filename in os.listdir(local_root): filename = os.path.join(local_root, filename) basename, sepa, ext = filename.rpartition('.') if ext not in ('py', 'pyx', 'c'): continue try: po_fd = codecs.open(filename, 'r', encoding='utf-8') po_fd.read() po_fd.close() filenames.append(os.path.relpath(filename, root_path)) msg = _('%(filename)s added.') % {'filename': filename} display(msg, color=GREEN) except UnicodeDecodeError: msg = _('Encoding of %(filename)s is not UTF-8.') % {'filename': filename} display(msg, color=GREEN) cmd = ['xgettext', '--language=Python', '--keyword=_', u('--output=%s') % pot_filename, '--from-code=UTF-8', '--add-comments=Translators', ] + filenames subprocess.check_call(cmd, stdout=subprocess.PIPE) if os.path.isfile(po_filename): cmd = ['msgmerge', '--update', '--backup=off', po_filename, pot_filename, ] else: cmd = ['msginit', '--no-translator', '-l', self.language, u('--input=%s') % pot_filename, u('--output=%s') % po_filename, ] subprocess.check_call(cmd, stderr=subprocess.PIPE) msg = _('Please translate strings in %(filename)s') % {'filename': po_filename} display(msg, color=YELLOW) msg = _('Then run setup.py compilemessages -l %(lang)s') % {'lang': self.language} display(msg, color=YELLOW)
def run(self): install_requires = set(self.distribution.install_requires) module_name = self.distribution.get_name() msg = 'Looking for dependencies of %(module_name)s...' % {'module_name': module_name} display(msg, color=GREEN) found_dependencies, missing_dependencies = find_dependencies(module_name) if found_dependencies: display('Found dependencies: ' + ', '.join(found_dependencies), color=GREEN) marked_dependencies = filter(lambda x: x not in install_requires, found_dependencies) if marked_dependencies: display('You should add the following dependencies to your stdeb.cfg and setup.py.:', color=YELLOW) display(', '.join(marked_dependencies), color=YELLOW) else: display('All of them are correctly set in your setup.py.', color=GREEN) else: display('No dependencies', color=GREEN) if len(missing_dependencies) > 0: display('Missing dependencies: ' + ', '.join(missing_dependencies), color=YELLOW) display('They may be false positive dependencies.', color=GREEN)
def run(self): install_requires = set(self.distribution.install_requires) module_name = self.distribution.get_name() msg = 'Looking for dependencies of %(module_name)s...' % { 'module_name': module_name } display(msg, color=GREEN) found_dependencies, missing_dependencies = find_dependencies( module_name) if found_dependencies: display('Found dependencies: ' + ', '.join(found_dependencies), color=GREEN) marked_dependencies = filter(lambda x: x not in install_requires, found_dependencies) if marked_dependencies: display( 'You should add the following dependencies to your stdeb.cfg and setup.py.:', color=YELLOW) display(', '.join(marked_dependencies), color=YELLOW) else: display('All of them are correctly set in your setup.py.', color=GREEN) else: display('No dependencies', color=GREEN) if len(missing_dependencies) > 0: display('Missing dependencies: ' + ', '.join(missing_dependencies), color=YELLOW) display('They may be false positive dependencies.', color=GREEN)
def run(self): if Environment is None: display(_('package jinja2 is required.'), color=RED) return 1 module_names = find_packages() dst_rel_path = 'locale' if self.dest is None else self.dest # group by top-level packages and compute their directories: all_modules = {} top_levels_modules = {} for module_name in module_names: top_module = module_name.partition('.')[0] all_modules.setdefault(top_module, []).append(module_name) if top_module not in top_levels_modules: top_levels_modules[top_module] = os.path.dirname( load_module(top_module).__file__) env = Environment(loader=PackageLoader( 'starterpyth.commands.makemessages', 'templates')) template = env.get_template('lang.po') context = { 'description': self.distribution.get_description(), 'copyright': self.distribution.get_author(), 'package': None, 'author': self.distribution.get_author(), 'version': self.distribution.get_version(), 'email': self.distribution.get_author_email(), 'year': datetime.datetime.now().year, } for tl_name in top_levels_modules.keys(): dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path) pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name) po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name) if not os.path.isdir(os.path.dirname(po_filename)): os.makedirs(os.path.dirname(po_filename)) for filename in (pot_filename, po_filename): if not os.path.isfile(filename): context['package'] = tl_name po_fd = codecs.open(filename, 'w', encoding='utf-8') po_fd.write(template.render(context)) po_fd.close() for tl_name, module_names in all_modules.items(): dst_abs_path = os.path.join(top_levels_modules[tl_name], dst_rel_path) root_path = os.path.dirname(top_levels_modules[tl_name]) print(root_path) pot_filename = os.path.join(dst_abs_path, '%s.pot' % tl_name) po_filename = os.path.join(dst_abs_path, self.language, 'LC_MESSAGES', '%s.po' % tl_name) # build the list of files to examine, for each top-level module filenames = [] for module_name in module_names: init_filename = load_module(module_name).__file__ local_root = os.path.dirname(init_filename) for filename in os.listdir(local_root): filename = os.path.join(local_root, filename) basename, sepa, ext = filename.rpartition('.') if ext not in ('py', 'pyx', 'c'): continue try: po_fd = codecs.open(filename, 'r', encoding='utf-8') po_fd.read() po_fd.close() filenames.append(os.path.relpath(filename, root_path)) msg = _('%(filename)s added.') % {'filename': filename} display(msg, color=GREEN) except UnicodeDecodeError: msg = _('Encoding of %(filename)s is not UTF-8.') % { 'filename': filename } display(msg, color=GREEN) cmd = [ 'xgettext', '--language=Python', '--keyword=_', u('--output=%s') % pot_filename, '--from-code=UTF-8', '--add-comments=Translators', ] + filenames subprocess.check_call(cmd, stdout=subprocess.PIPE) if os.path.isfile(po_filename): cmd = [ 'msgmerge', '--update', '--backup=off', po_filename, pot_filename, ] else: cmd = [ 'msginit', '--no-translator', '-l', self.language, u('--input=%s') % pot_filename, u('--output=%s') % po_filename, ] subprocess.check_call(cmd, stderr=subprocess.PIPE) msg = _('Please translate strings in %(filename)s') % { 'filename': po_filename } display(msg, color=YELLOW) msg = _('Then run setup.py compilemessages -l %(lang)s') % { 'lang': self.language } display(msg, color=YELLOW)
def write_files(self, modname, dirname, env): """ Write all templated or raw files to the new project. All template are rendered twice. This behaviour allows to determine which functions must be imported at the beginning of Python files :param modname: module containing template files :param dirname: dirname containing template files in the module `modname` :param env: Jinja2 environment :return: """ from jinja2 import Template project_root = self.global_context['project_root'] # creation of the project directory if needed if not os.path.isdir(project_root): os.makedirs(project_root) display(_('Directory %(f)s created.') % {'f': project_root}, color=GREEN) # noinspection PyTypeChecker prefix_len = len(dirname) + 1 def get_path(root_, name): """return relative source path (to template dir) and absolute destination path""" src_path_ = (root_ + '/' + name)[prefix_len:] dst_path_ = src_path_ if os.sep != '/': dst_path_ = dst_path_.replace('/', os.sep) if dst_path_.find('{') > -1: # the name of the file is templated dst_path_ = Template(dst_path_).render(**self.global_context) if dst_path_[-len(self.template_suffix):] == self.template_suffix: dst_path_ = dst_path_[:-len(self.template_suffix)] return src_path_, os.path.join(project_root, dst_path_) # walk through all files (raw and templates) in modname/dirname and write them to destination for root, dirnames, filenames in walk(modname, dirname): for dirname in dirnames: src_path, dst_path = get_path(root, dirname) if not self.process_directory_or_file(src_path, dst_path, dirname, True): continue if not os.path.isdir(dst_path): os.makedirs(dst_path) display(_('Directory %(f)s created.') % {'f': dst_path}, color=GREEN) for filename in filenames: src_path, dst_path = get_path(root, filename) if not self.process_directory_or_file(src_path, dst_path, filename, False): continue if not os.path.isdir(os.path.dirname(dst_path)): continue if filename[-len(self.template_suffix):] == self.template_suffix: self.file_context = {'render_pass': 1} template = env.get_template(src_path) f_out = open(dst_path, 'wb') self.file_context.update(self.global_context) template.render(**self.file_context) self.file_context['render_pass'] = 2 template_content = template.render(**self.file_context).encode('utf-8') f_out.write(template_content) f_out.close() display(_('Template %(f)s written.') % {'f': dst_path}, color=GREEN) else: f_out = open(dst_path, 'wb') f_in = pkg_resources.resource_stream(modname, root + '/' + filename) data = f_in.read(10240) while data: f_out.write(data) data = f_in.read(10240) f_in.close() f_out.close() display(_('File %(f)s written.') % {'f': dst_path}, color=GREEN)
def run(self): if pylint is None: display(_('package pylint is required.'), color=RED) return 1 pylint.lint.Run((self.distribution.get_name(), ), exit=False)