def find_dependencies(module_name): p = subprocess.Popen(['sfood', '--follow', module_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = p.communicate() found_dependencies = set() missing_dependencies = set() warning_re = re.compile('^WARNING\\s*:\\s*Line [0-9]+:.*') missing_re = re.compile('^WARNING\\s*: \\s*(.*)$') for line in py3k_unicode(stderr).splitlines(): if warning_re.match(line): continue missing_match = missing_re.match(line) if missing_match: missing_dependencies.add(missing_match.group(1)) python_root = os.path.dirname(abc.__file__) def get_module_root(base_modulename): (file_, pathname, description) = imp.find_module(base_modulename) base_module = imp.load_module(base_modulename, file_, pathname, description) return os.path.abspath( os.path.dirname(os.path.dirname(base_module.__file__))) module_root = get_module_root(module_name) def add_dependence(python_root, module_root, found_dependencies, dirname, filename): if dirname is None or filename is None: return if dirname.find(python_root) == 0 or dirname.find(module_root) == 0: return name = filename.partition(os.path.sep)[0] basename, ext = os.path.splitext(name) if ext.find('.py') == 0: found_dependencies.add(basename) else: found_dependencies.add(name) for line in py3k_unicode(stdout).splitlines(): (src, dst) = json.loads( line.replace("(", "[").replace(")", "]").replace("'", '"').replace( 'None', 'null')) add_dependence(python_root, module_root, found_dependencies, *src) add_dependence(python_root, module_root, found_dependencies, *dst) return found_dependencies, missing_dependencies
def translate_string(src_str): """ Transform a ASCII string into a larger string with non-ASCII characters. >>> translate_string(py3k_unicode('ab')) == py3k_unicode('[ƒ——!ab!—–]') True """ dst_str = src_str return py3k_unicode('[ƒ——!{0}!—–]').format(dst_str)
def run(self): if self.input: try: # noinspection PyUnresolvedReferences import pstats except ImportError: logging.error('Module pstats not found.') return if not os.path.isfile(self.input): logging.error(py3k_unicode('File %s not found' % self.input)) return stats = pstats.Stats(self.input) stats.print_stats() elif not self.call: logging.error( 'Please provide a function to profile with --call \'module.function\'' ) 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: logging.info('Load module %s' % module_name) self.call = 'import %s ; %s' % (module_name, self.call) logging.info("running profiling on %(call)s" % {'call': self.call}) if self.debug: pdb.run(self.call) else: profile.run(self.call, self.output)
def run(self): if self.input: try: # noinspection PyUnresolvedReferences import pstats except ImportError: logging.error("Module pstats not found.") return if not os.path.isfile(self.input): logging.error(py3k_unicode("File %s not found" % self.input)) return stats = pstats.Stats(self.input) stats.print_stats() elif not self.call: logging.error("Please provide a function to profile with --call 'module.function'") 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: logging.info("Load module %s" % module_name) self.call = "import %s ; %s" % (module_name, self.call) logging.info("running profiling on %(call)s" % {"call": self.call}) if self.debug: pdb.run(self.call) else: profile.run(self.call, self.output)
def update_global_context(self, context, filters): project_name = RegexpInput(_('Project name'), regexp=r'[A-Za-z]\w*', default='Project').input() module_name = RegexpInput(_('Python module name'), regexp=r'[A-Za-z]\w*', default=project_name.lower()).input() company = CharInput(_('Company name'), max_length=255, blank=True, default=defaults.COMPANY).input() author = CharInput(_('Author name'), max_length=255, default=defaults.AUTHOR).input() email = RegexpInput(_('Author e-mail'), default='%s@%s' % (author, company), regexp=r'[\w_\-\.]+@[\w_\-\.]').input() license_ = ChoiceInput(_('License'), choices=licenses, blank=True, default='cecill b').input() pyversion = ChoiceInput(_('Minimum Python version'), choices=pyversions, default='2.7').input() use_2to3 = False use_six = False py3compat = 'source' if pyversion < 3.: use_six = BooleanInput( _('Use six tool for Python 3 compatibility'), default=True).input() if not use_six: use_2to3 = BooleanInput( _('Use 2to3 tool for Python 3 compatibility'), default=True).input() py3compat = '2to3' if use_2to3 else None else: py3compat = 'six' translation = BooleanInput(_('Include translation (i18n) stuff'), default=True).input() context['translation'] = translation if translation: filters['translate'] = py3k_unicode('_(\'{0}\')').format else: filters['translate'] = py3k_unicode('\'{0}\'').format module_version = RegexpInput(_('Initial version'), regexp=r'[\w\.\-]', default='0.1').input() context['project_name'] = project_name context['module_name'] = module_name context['pyversion'] = pyversion context['use_2to3'] = use_2to3 context['use_six'] = use_six context['py3compat'] = py3compat context['license'] = license_names[license_] if license_ != 'Other': licence_fd = pkg_resources.resource_stream( 'starterpyth.plugins.base', 'licenses/%s.txt' % license_) context['license_content'] = licence_fd.read().decode('utf-8') licence_fd.close() else: context['license_content'] = '' if py3compat == 'six': filters['unicode'] = lambda x: py3k_unicode('six.u("{0}")').format( x.replace("\"", "\\\"")) filters['binary'] = lambda x: py3k_unicode('six.b("{0}")').format( x.replace("\"", "\\\"")) context['unicode'] = 'six.text_type' context['binary'] = 'six.binary_type' context['install_requires'].append('six') context['setup_requires'].append('six') context['tests_requires'].append('six') elif py3compat == 'source': filters['unicode'] = lambda x: py3k_unicode('"{0}"').format( x.replace("\"", "\\\"")) filters['binary'] = lambda x: py3k_unicode('b"{0}"').format( x.replace("\"", "\\\"")) context['unicode'] = 'str' context['binary'] = 'bytes' else: # no compatibility or compatibility through 2to3 filters['unicode'] = lambda x: py3k_unicode('u"{0}"').format( x.replace("\"", "\\\"")) filters['binary'] = lambda x: py3k_unicode('"{0}"').format( x.replace("\"", "\\\"")) context['unicode'] = 'unicode' context['binary'] = 'str' context['copyright_full'] = _('Copyright %(year)d, %(comp)s') % { 'year': datetime.date.today().year, 'comp': company } context['company'] = company context['project_url'] = py3k_unicode('http://{0}/{1}.html').format( company, project_name) context['email'] = email context['author'] = author context['module_version'] = module_version context['install_requires'].append('distribute') context['setup_requires'].append('distribute') context['tests_requires'].append('distribute') context['classifiers'].append('Programming Language :: Python') if py3compat in ('source', 'six'): context['classifiers'].append( 'Programming Language :: Python :: 3') context['year'] = datetime.date.today().year context['doc_urls']['python'] = ('http://docs.python.org/%.1f/' % pyversion, 'externals/python_%.1f.inv' % pyversion) path = os.path.join(context['project_root'], project_name) if os.path.isdir(path): rm_choice = ChoiceInput( _('The folder %(f)s already exists. Remove it?') % { 'f': path }, default='yes', choices=(('yes', 'yes'), ('no', 'no'))).input() if rm_choice == 'yes': shutil.rmtree(path)
def update_global_context(self, context, filters): project_name = RegexpInput(_('Project name'), regexp=r'[A-Za-z]\w*', default='Project').input() module_name = RegexpInput(_('Python module name'), regexp=r'[A-Za-z]\w*', default=project_name.lower()).input() company = CharInput(_('Company name'), max_length=255, blank=True, default=defaults.COMPANY).input() author = CharInput(_('Author name'), max_length=255, default=defaults.AUTHOR).input() author_normalized = normalize_str(py3k_unicode(author)) company_normalized = normalize_str(py3k_unicode(company)) email = RegexpInput(_('Author e-mail'), default='%s@%s' % (author_normalized, company_normalized), regexp=r'[\w_\-\.]+@[\w_\-\.]').input() license_ = ChoiceInput(_('License'), choices=licenses, blank=True, default='cecill b').input() pyversion = ChoiceInput(_('Minimum Python version'), choices=pyversions, default='2.7').input() use_2to3 = False use_six = False py3compat = 'source' if pyversion < 3.: use_six = BooleanInput(_('Use six tool for Python 3 compatibility'), default=True).input() if not use_six: use_2to3 = BooleanInput(_('Use 2to3 tool for Python 3 compatibility'), default=True).input() py3compat = '2to3' if use_2to3 else None else: py3compat = 'six' translation = BooleanInput(_('Include translation (i18n) stuff'), default=True).input() context['translation'] = translation if translation: filters['translate'] = py3k_unicode('_(\'{0}\')').format else: filters['translate'] = py3k_unicode('\'{0}\'').format module_version = RegexpInput(_('Initial version'), regexp=r'[\w\.\-]', default='0.1').input() context['project_name'] = project_name context['module_name'] = module_name context['pyversion'] = pyversion context['use_2to3'] = use_2to3 context['use_six'] = use_six context['py3compat'] = py3compat context['license'] = license_names[license_] context['file_encoding'] = '' if author_normalized != author or company_normalized != company: context['file_encoding'] = "# -*- coding: utf-8 -*-\n" if license_ != 'Other': licence_fd = pkg_resources.resource_stream('starterpyth.plugins.base', 'licenses/%s.txt' % license_) context['license_content'] = licence_fd.read().decode('utf-8') licence_fd.close() else: context['license_content'] = '' if py3compat == 'six': filters['unicode'] = lambda x: py3k_unicode('six.u("{0}")').format(x.replace("\"", "\\\"")) filters['binary'] = lambda x: py3k_unicode('six.b("{0}")').format(x.replace("\"", "\\\"")) context['unicode'] = 'six.text_type' context['binary'] = 'six.binary_type' context['install_requires'].append('six') context['setup_requires'].append('six') context['tests_requires'].append('six') elif py3compat == 'source': filters['unicode'] = lambda x: py3k_unicode('"{0}"').format(x.replace("\"", "\\\"")) filters['binary'] = lambda x: py3k_unicode('b"{0}"').format(x.replace("\"", "\\\"")) context['unicode'] = 'str' context['binary'] = 'bytes' else: # no compatibility or compatibility through 2to3 filters['unicode'] = lambda x: py3k_unicode('u"{0}"').format(x.replace("\"", "\\\"")) filters['binary'] = lambda x: py3k_unicode('"{0}"').format(x.replace("\"", "\\\"")) context['unicode'] = 'unicode' context['binary'] = 'str' context['copyright_full'] = _('Copyright %(year)d, %(comp)s') % {'year': datetime.date.today().year, 'comp': company} context['company'] = company context['project_url'] = py3k_unicode('http://{0}/{1}.html').format(company, project_name) context['email'] = email context['author'] = author context['module_version'] = module_version context['install_requires'].append('distribute') context['setup_requires'].append('distribute') context['tests_requires'].append('distribute') context['classifiers'].append('Programming Language :: Python') if py3compat in ('source', 'six'): context['classifiers'].append('Programming Language :: Python :: 3') context['year'] = datetime.date.today().year context['doc_urls']['python'] = ('http://docs.python.org/%.1f/' % pyversion, 'externals/python_%.1f.inv' % pyversion) path = os.path.join(context['project_root'], project_name) if os.path.isdir(path): rm_choice = ChoiceInput(_('The folder %(f)s already exists. Remove it?') % {'f': path}, default='yes', choices=(('yes', 'yes'), ('no', 'no'))).input() if rm_choice == 'yes': shutil.rmtree(path)
def test_1(self): """Test the sample_function with two arguments.""" self.assertEqual(translate_string(py3k_unicode('ab')), py3k_unicode('[ƒ——!ab!—–]'))
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: 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} logging.info(msg) except UnicodeDecodeError: msg = _('Encoding of %(filename)s is not UTF-8.') % {'filename': filename} logging.error(msg) cmd = ['xgettext', '--language=Python', '--keyword=_', py3k_unicode('--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, py3k_unicode('--input=%s') % pot_filename, py3k_unicode('--output=%s') % po_filename, ] subprocess.check_call(cmd, stderr=subprocess.PIPE) msg = _('Please translate strings in %(filename)s') % {'filename': po_filename} logging.warning(msg) msg = _('Then run setup.py compilemessages -l %(lang)s') % {'lang': self.language} logging.warning(msg)