<s> import fnmatch <EOL> import glob <EOL> import os <EOL> import re <EOL> import sys <EOL> from itertools import dropwhile <EOL> from optparse import make_option <EOL> from subprocess import PIPE , Popen <EOL> from django . core . management . base import CommandError , NoArgsCommand <EOL> from django . utils . text import get_text_list <EOL> pythonize_re = re . compile ( r'<STR_LIT>' ) <EOL> plural_forms_re = re . compile ( r'<STR_LIT>' , re . MULTILINE | re . DOTALL ) <EOL> def handle_extensions ( extensions = ( '<STR_LIT:html>' , ) ) : <EOL> """<STR_LIT>""" <EOL> ext_list = [ ] <EOL> for ext in extensions : <EOL> ext_list . extend ( ext . replace ( '<STR_LIT:U+0020>' , '<STR_LIT>' ) . split ( '<STR_LIT:U+002C>' ) ) <EOL> for i , ext in enumerate ( ext_list ) : <EOL> if not ext . startswith ( '<STR_LIT:.>' ) : <EOL> ext_list [ i ] = '<STR_LIT>' % ext_list [ i ] <EOL> return set ( [ x for x in ext_list if x != '<STR_LIT>' ] ) <EOL> def _popen ( cmd ) : <EOL> """<STR_LIT>""" <EOL> p = Popen ( cmd , shell = True , stdout = PIPE , stderr = PIPE , close_fds = os . name != '<STR_LIT>' , universal_newlines = True ) <EOL> return p . communicate ( ) <EOL> def walk ( root , topdown = True , onerror = None , followlinks = False ) : <EOL> """<STR_LIT>""" <EOL> for dirpath , dirnames , filenames in os . walk ( root , topdown , onerror ) : <EOL> yield ( dirpath , dirnames , filenames ) <EOL> if followlinks : <EOL> for d in dirnames : <EOL> p = os . path . join ( dirpath , d ) <EOL> if os . path . islink ( p ) : <EOL> for link_dirpath , link_dirnames , link_filenames in walk ( p ) : <EOL> yield ( link_dirpath , link_dirnames , link_filenames ) <EOL> def is_ignored ( path , ignore_patterns ) : <EOL> """<STR_LIT>""" <EOL> for pattern in ignore_patterns : <EOL> if fnmatch . fnmatchcase ( path , pattern ) : <EOL> return True <EOL> return False <EOL> def find_files ( root , ignore_patterns , verbosity , symlinks = False ) : <EOL> """<STR_LIT>""" <EOL> all_files = [ ] <EOL> for ( dirpath , dirnames , filenames ) in walk ( "<STR_LIT:.>" , followlinks = symlinks ) : <EOL> for f in filenames : <EOL> norm_filepath = os . path . normpath ( os . path . join ( dirpath , f ) ) <EOL> if is_ignored ( norm_filepath , ignore_patterns ) : <EOL> if verbosity > <NUM_LIT:1> : <EOL> sys . stdout . write ( '<STR_LIT>' % ( f , dirpath ) ) <EOL> else : <EOL> all_files . extend ( [ ( dirpath , f ) ] ) <EOL> all_files . sort ( ) <EOL> return all_files <EOL> def copy_plural_forms ( msgs , locale , domain , verbosity ) : <EOL> """<STR_LIT>""" <EOL> import django <EOL> django_dir = os . path . normpath ( os . path . join ( os . path . dirname ( django . __file__ ) ) ) <EOL> if domain == '<STR_LIT>' : <EOL> domains = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> domains = ( '<STR_LIT>' , ) <EOL> for domain in domains : <EOL> django_po = os . path . join ( django_dir , '<STR_LIT>' , '<STR_LIT>' , locale , '<STR_LIT>' , '<STR_LIT>' % domain ) <EOL> if os . path . exists ( django_po ) : <EOL> m = plural_forms_re . search ( open ( django_po , '<STR_LIT>' ) . read ( ) ) <EOL> if m : <EOL> if verbosity > <NUM_LIT:1> : <EOL> sys . stderr . write ( "<STR_LIT>" % m . group ( '<STR_LIT:value>' ) ) <EOL> lines = [ ] <EOL> seen = False <EOL> for line in msgs . split ( '<STR_LIT:\n>' ) : <EOL> if not line and not seen : <EOL> line = '<STR_LIT>' % m . group ( '<STR_LIT:value>' ) <EOL> seen = True <EOL> lines . append ( line ) <EOL> msgs = '<STR_LIT:\n>' . join ( lines ) <EOL> break <EOL> return msgs <EOL> def make_messages ( locale = None , domain = '<STR_LIT>' , verbosity = '<STR_LIT:1>' , all = False , <EOL> extensions = None , symlinks = False , ignore_patterns = [ ] , no_wrap = False , <EOL> no_obsolete = False ) : <EOL> """<STR_LIT>""" <EOL> from django . conf import settings <EOL> if settings . configured : <EOL> settings . USE_I18N = True <EOL> else : <EOL> settings . configure ( USE_I18N = True ) <EOL> from django . utils . translation import templatize <EOL> invoked_for_django = False <EOL> if os . path . isdir ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> localedir = os . path . abspath ( os . path . join ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> invoked_for_django = True <EOL> ignore_patterns += [ '<STR_LIT>' ] <EOL> elif os . path . isdir ( '<STR_LIT>' ) : <EOL> localedir = os . path . abspath ( '<STR_LIT>' ) <EOL> else : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> if domain not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> raise CommandError ( "<STR_LIT>" ) <EOL> if ( locale is None and not all ) or domain is None : <EOL> message = "<STR_LIT>" % ( os . path . basename ( sys . argv [ <NUM_LIT:0> ] ) , sys . argv [ <NUM_LIT:1> ] ) <EOL> raise CommandError ( message ) <EOL> output = _popen ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> match = re . search ( r'<STR_LIT>' , output ) <EOL> if match : <EOL> xversion = ( int ( match . group ( '<STR_LIT>' ) ) , int ( match . group ( '<STR_LIT>' ) ) ) <EOL> if xversion < ( <NUM_LIT:0> , <NUM_LIT:15> ) : <EOL> raise CommandError ( "<STR_LIT>" % match . group ( ) ) <EOL> languages = [ ] <EOL> if locale is not None : <EOL> languages . append ( locale ) <EOL> elif all : <EOL> locale_dirs = filter ( os . path . isdir , glob . glob ( '<STR_LIT>' % localedir ) ) <EOL> languages = [ os . path . basename ( l ) for l in locale_dirs ] <EOL> wrap = no_wrap and '<STR_LIT>' or '<STR_LIT>' <EOL> for locale in languages : <EOL> if verbosity > <NUM_LIT:0> : <EOL> print "<STR_LIT>" , locale <EOL> basedir = os . path . join ( localedir , locale , '<STR_LIT>' ) <EOL> if not os . path . isdir ( basedir ) : <EOL> os . makedirs ( basedir ) <EOL> pofile = os . path . join ( basedir , '<STR_LIT>' % domain ) <EOL> potfile = os . path . join ( basedir , '<STR_LIT>' % domain ) <EOL> if os . path . exists ( potfile ) : <EOL> os . unlink ( potfile ) <EOL> for dirpath , file in find_files ( "<STR_LIT:.>" , ignore_patterns , verbosity , symlinks = symlinks ) : <EOL> file_base , file_ext = os . path . splitext ( file ) <EOL> if domain == '<STR_LIT>' and file_ext in extensions : <EOL> if verbosity > <NUM_LIT:1> : <EOL> sys . stdout . write ( '<STR_LIT>' % ( file , dirpath ) ) <EOL> src = open ( os . path . join ( dirpath , file ) , "<STR_LIT>" ) . read ( ) <EOL> src = pythonize_re . sub ( '<STR_LIT>' , src ) <EOL> thefile = '<STR_LIT>' % file <EOL> f = open ( os . path . join ( dirpath , thefile ) , "<STR_LIT:w>" ) <EOL> try : <EOL> f . write ( src ) <EOL> finally : <EOL> f . close ( ) <EOL> cmd = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> domain , wrap , os . path . join ( dirpath , thefile ) <EOL> ) <EOL> ) <EOL> msgs , errors = _popen ( cmd ) <EOL> if errors : <EOL> os . unlink ( os . path . join ( dirpath , thefile ) ) <EOL> if os . path . exists ( potfile ) : <EOL> os . unlink ( potfile ) <EOL> raise CommandError ( <EOL> "<STR_LIT>" % <EOL> ( file , errors ) ) <EOL> if msgs : <EOL> old = '<STR_LIT>' + os . path . join ( dirpath , thefile ) [ <NUM_LIT:2> : ] <EOL> new = '<STR_LIT>' + os . path . join ( dirpath , file ) [ <NUM_LIT:2> : ] <EOL> msgs = msgs . replace ( old , new ) <EOL> if os . path . exists ( potfile ) : <EOL> msgs = '<STR_LIT:\n>' . join ( dropwhile ( len , msgs . split ( '<STR_LIT:\n>' ) ) ) <EOL> else : <EOL> msgs = msgs . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> f = open ( potfile , '<STR_LIT>' ) <EOL> try : <EOL> f . write ( msgs ) <EOL> finally : <EOL> f . close ( ) <EOL> os . unlink ( os . path . join ( dirpath , thefile ) ) <EOL> elif domain == '<STR_LIT>' and ( file_ext == '<STR_LIT>' or file_ext in extensions ) : <EOL> thefile = file <EOL> orig_file = os . path . join ( dirpath , file ) <EOL> if file_ext in extensions : <EOL> src = open ( orig_file , "<STR_LIT>" ) . read ( ) <EOL> thefile = '<STR_LIT>' % file <EOL> f = open ( os . path . join ( dirpath , thefile ) , "<STR_LIT:w>" ) <EOL> try : <EOL> f . write ( templatize ( src , orig_file [ <NUM_LIT:2> : ] ) ) <EOL> finally : <EOL> f . close ( ) <EOL> if verbosity > <NUM_LIT:1> : <EOL> sys . stdout . write ( '<STR_LIT>' % ( file , dirpath ) ) <EOL> cmd = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( <EOL> domain , wrap , os . path . join ( dirpath , thefile ) ) <EOL> ) <EOL> msgs , errors = _popen ( cmd ) <EOL> if errors : <EOL> if thefile != file : <EOL> os . unlink ( os . path . join ( dirpath , thefile ) ) <EOL> if os . path . exists ( potfile ) : <EOL> os . unlink ( potfile ) <EOL> raise CommandError ( <EOL> "<STR_LIT>" % <EOL> ( file , errors ) ) <EOL> if msgs : <EOL> if thefile != file : <EOL> old = '<STR_LIT>' + os . path . join ( dirpath , thefile ) [ <NUM_LIT:2> : ] <EOL> new = '<STR_LIT>' + orig_file [ <NUM_LIT:2> : ] <EOL> msgs = msgs . replace ( old , new ) <EOL> if os . path . exists ( potfile ) : <EOL> msgs = '<STR_LIT:\n>' . join ( dropwhile ( len , msgs . split ( '<STR_LIT:\n>' ) ) ) <EOL> else : <EOL> msgs = msgs . replace ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> f = open ( potfile , '<STR_LIT>' ) <EOL> try : <EOL> f . write ( msgs ) <EOL> finally : <EOL> f . close ( ) <EOL> if thefile != file : <EOL> os . unlink ( os . path . join ( dirpath , thefile ) ) <EOL> if os . path . exists ( potfile ) : <EOL> msgs , errors = _popen ( '<STR_LIT>' % <EOL> ( wrap , potfile ) ) <EOL> if errors : <EOL> os . unlink ( potfile ) <EOL> raise CommandError ( <EOL> "<STR_LIT>" % errors ) <EOL> if os . path . exists ( pofile ) : <EOL> f = open ( potfile , '<STR_LIT:w>' ) <EOL> try : <EOL> f . write ( msgs ) <EOL> finally : <EOL> f . close ( ) <EOL> msgs , errors = _popen ( '<STR_LIT>' % <EOL> ( wrap , pofile , potfile ) ) <EOL> if errors : <EOL> os . unlink ( potfile ) <EOL> raise CommandError ( <EOL> "<STR_LIT>" % errors ) <EOL> elif not invoked_for_django : <EOL> msgs = copy_plural_forms ( msgs , locale , domain , verbosity ) <EOL> msgs = msgs . replace ( <EOL> "<STR_LIT>" % domain , "<STR_LIT>" ) <EOL> f = open ( pofile , '<STR_LIT:wb>' ) <EOL> try : <EOL> f . write ( msgs ) <EOL> finally : <EOL> f . close ( ) <EOL> os . unlink ( potfile ) <EOL> if no_obsolete : <EOL> msgs , errors = _popen ( '<STR_LIT>' % <EOL> ( wrap , pofile , pofile ) ) <EOL> if errors : <EOL> raise CommandError ( <EOL> "<STR_LIT>" % errors ) <EOL> class Command ( NoArgsCommand ) : <EOL> option_list = NoArgsCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , default = None , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , default = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT:all>' , <EOL> default = False , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> action = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = [ ] , metavar = '<STR_LIT>' , help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = True , help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = "<STR_LIT>" ) , <EOL> make_option ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> default = False , help = "<STR_LIT>" ) , <EOL> ) <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> requires_model_validation = False <EOL> can_import_settings = False <EOL> def handle_noargs ( self , * args , ** options ) : <EOL> locale = options . get ( '<STR_LIT>' ) <EOL> domain = options . get ( '<STR_LIT>' ) <EOL> verbosity = int ( options . get ( '<STR_LIT>' ) ) <EOL> process_all = options . get ( '<STR_LIT:all>' ) <EOL> extensions = options . get ( '<STR_LIT>' ) <EOL> symlinks = options . get ( '<STR_LIT>' ) <EOL> ignore_patterns = options . get ( '<STR_LIT>' ) <EOL> if options . get ( '<STR_LIT>' ) : <EOL> ignore_patterns += [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> ignore_patterns = list ( set ( ignore_patterns ) ) <EOL> no_wrap = options . get ( '<STR_LIT>' ) <EOL> no_obsolete = options . get ( '<STR_LIT>' ) <EOL> if domain == '<STR_LIT>' : <EOL> extensions = handle_extensions ( extensions or [ '<STR_LIT>' ] ) <EOL> else : <EOL> extensions = handle_extensions ( extensions or [ '<STR_LIT:html>' ] ) <EOL> if verbosity > <NUM_LIT:1> : <EOL> sys . stdout . write ( '<STR_LIT>' <EOL> % get_text_list ( list ( extensions ) , '<STR_LIT>' ) ) <EOL> make_messages ( locale , domain , verbosity , process_all , extensions , symlinks , ignore_patterns , no_wrap , no_obsolete ) </s>
