<s> from optparse import make_option , OptionParser <EOL> import os . path <EOL> import sys <EOL> from django . core . management . base import ( BaseCommand , LabelCommand , CommandError ) <EOL> from django . db . models import get_model <EOL> from django . conf import settings <EOL> class Command ( LabelCommand ) : <EOL> """<STR_LIT>""" <EOL> help = "<STR_LIT>" "<STR_LIT>" <EOL> args = "<STR_LIT>" <EOL> can_import_settings = True <EOL> def handle ( self , * args , ** options ) : <EOL> Resource = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Translation = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Language = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> RLStats = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Team = get_model ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> verbosity = int ( options . get ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> if not args : <EOL> resources = Resource . objects . all ( ) <EOL> else : <EOL> resources = [ ] <EOL> for arg in args : <EOL> try : <EOL> prj , res = arg . split ( '<STR_LIT:.>' ) <EOL> resources . extend ( Resource . objects . filter ( project__slug = prj , <EOL> slug = res ) or None ) <EOL> except TypeError , e : <EOL> raise Exception ( "<STR_LIT>" % ( prj , res ) ) <EOL> except ValueError , e : <EOL> raise Exception ( "<STR_LIT>" <EOL> % arg ) <EOL> num = len ( resources ) <EOL> if num == <NUM_LIT:0> : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( ) <EOL> if verbosity : <EOL> sys . stdout . write ( "<STR_LIT>" % num ) <EOL> for seq , r in enumerate ( resources ) : <EOL> if verbosity : <EOL> sys . stdout . write ( ( u"<STR_LIT>" % <EOL> ( r . project . slug , r . slug , seq + <NUM_LIT:1> , num ) ) . encode ( '<STR_LIT>' ) ) <EOL> r . update_total_entities ( ) <EOL> r . update_wordcount ( ) <EOL> langs = list ( Translation . objects . filter ( <EOL> resource = r ) . order_by ( '<STR_LIT>' ) . values_list ( <EOL> '<STR_LIT>' , flat = True ) . distinct ( ) ) <EOL> for lang in langs : <EOL> lang = Language . objects . get ( id = lang ) <EOL> if verbosity : <EOL> sys . stdout . write ( "<STR_LIT>" % lang ) <EOL> rl , created = RLStats . objects . get_or_create ( resource = r , language = lang ) <EOL> rl . update ( ) <EOL> if r . project . outsource : <EOL> teams = Team . objects . filter ( project = r . project . outsource ) <EOL> else : <EOL> teams = Team . objects . filter ( project = r . project ) <EOL> teams = teams . exclude ( language__id__in = langs ) <EOL> for team in teams : <EOL> lang = team . language <EOL> langs . append ( lang . id ) <EOL> if verbosity : <EOL> sys . stdout . write ( "<STR_LIT>" % lang ) <EOL> rl , created = RLStats . objects . get_or_create ( resource = r , language = lang ) <EOL> rl . update ( ) <EOL> langs . append ( r . source_language . id ) <EOL> rlstats = RLStats . objects . filter ( resource = r ) <EOL> for stat in rlstats : <EOL> if not stat . language . id in langs : <EOL> stat . delete ( ) </s>
