<s> from optparse import make_option <EOL> import os <EOL> import re <EOL> import shutil <EOL> import subprocess <EOL> import sys <EOL> from dojango . conf import settings <EOL> from dojango . version import version_tuple <EOL> try : <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> except ImportError : <EOL> BaseCommand = object <EOL> class CommandError ( Exception ) : <EOL> pass <EOL> class Command ( BaseCommand ) : <EOL> '''<STR_LIT>''' <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , dest = '<STR_LIT>' , action = "<STR_LIT:store_true>" , default = False , <EOL> help = '<STR_LIT>' ) , <EOL> ) <EOL> help = "<STR_LIT>" <EOL> args = '<STR_LIT>' <EOL> dojo_base_dir = None <EOL> dojo_release_dir = None <EOL> skip_files = None <EOL> def handle ( self , * args , ** options ) : <EOL> if len ( args ) == <NUM_LIT:0> : <EOL> profile_name = settings . DOJO_BUILD_PROFILE <EOL> else : <EOL> profile_name = args [ <NUM_LIT:0> ] <EOL> profile = self . _get_profile ( profile_name ) <EOL> used_src_version = profile [ '<STR_LIT>' ] % { '<STR_LIT>' : settings . DOJO_BUILD_VERSION } <EOL> self . skip_files = profile . get ( "<STR_LIT>" , ( ) ) <EOL> self . dojo_base_dir = "<STR_LIT>" % { '<STR_LIT>' : settings . BASE_DOJO_ROOT , <EOL> '<STR_LIT:version>' : used_src_version } <EOL> util_base_dir = "<STR_LIT>" % { '<STR_LIT>' : self . dojo_base_dir } <EOL> if not os . path . exists ( util_base_dir ) : <EOL> raise CommandError ( '<STR_LIT>' % { '<STR_LIT:version>' : used_src_version , <EOL> '<STR_LIT>' : settings . BASE_DOJO_ROOT } ) <EOL> try : <EOL> subprocess . call ( settings . DOJO_BUILD_JAVA_EXEC , stdout = subprocess . PIPE ) <EOL> except : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> buildscript_dir = os . path . abspath ( '<STR_LIT>' % util_base_dir ) <EOL> if version_tuple ( settings . DOJO_BUILD_USED_VERSION ) < ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) : <EOL> executable = '<STR_LIT>' % { '<STR_LIT>' : settings . DOJO_BUILD_JAVA_EXEC } <EOL> else : <EOL> if ( os . path . sep == "<STR_LIT:\\>" ) : <EOL> executable = '<STR_LIT>' <EOL> else : <EOL> executable = '<STR_LIT>' <EOL> os . chmod ( os . path . join ( buildscript_dir , '<STR_LIT>' ) , <NUM_LIT:0> <NUM_LIT> ) <EOL> version = options . get ( '<STR_LIT>' , None ) <EOL> if not version : <EOL> version = profile [ '<STR_LIT>' ] % { '<STR_LIT>' : settings . DOJO_BUILD_VERSION } <EOL> self . dojo_release_dir = '<STR_LIT>' % { <EOL> '<STR_LIT>' : profile [ '<STR_LIT>' ] % { '<STR_LIT>' : settings . BASE_MEDIA_ROOT } , <EOL> '<STR_LIT:version>' : version } <EOL> release_dir = os . path . abspath ( os . path . join ( self . dojo_release_dir , "<STR_LIT>" ) ) <EOL> if version_tuple ( settings . DOJO_BUILD_USED_VERSION ) < ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) : <EOL> release_dir = release_dir + os . path . sep <EOL> build_addons = "<STR_LIT>" <EOL> if version_tuple ( settings . DOJO_BUILD_USED_VERSION ) >= ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) : <EOL> build_addons = "<STR_LIT>" <EOL> exe_command = '<STR_LIT>' % { '<STR_LIT>' : buildscript_dir , <EOL> '<STR_LIT>' : executable , <EOL> '<STR_LIT:version>' : version , <EOL> '<STR_LIT>' : release_dir , <EOL> '<STR_LIT>' : profile [ '<STR_LIT>' ] % { '<STR_LIT>' : settings . BASE_MEDIA_ROOT } , <EOL> '<STR_LIT>' : build_addons } <EOL> minify = options [ '<STR_LIT>' ] <EOL> minify_extreme = options [ '<STR_LIT>' ] <EOL> prepare_zipserve = options [ '<STR_LIT>' ] <EOL> if ( version_tuple ( settings . DOJO_BUILD_USED_VERSION ) < ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) ) and ( minify or minify_extreme ) : <EOL> self . _dojo_mini_before_build ( ) <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> exe_command = os . path . splitdrive ( buildscript_dir ) [ <NUM_LIT:0> ] + '<STR_LIT>' + exe_command <EOL> exit_code = os . system ( exe_command ) <EOL> if exit_code : <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if version_tuple ( settings . DOJO_BUILD_USED_VERSION ) < ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:0> ) : <EOL> if minify or minify_extreme : <EOL> self . _dojo_mini_after_build ( ) <EOL> if minify_extreme : <EOL> self . _dojo_mini_extreme ( ) <EOL> if prepare_zipserve : <EOL> self . _dojo_prepare_zipserve ( ) <EOL> def _get_profile ( self , name ) : <EOL> default_profile_settings = settings . DOJO_BUILD_PROFILES_DEFAULT <EOL> try : <EOL> profile = settings . DOJO_BUILD_PROFILES [ name ] <EOL> default_profile_settings . update ( profile ) <EOL> return default_profile_settings <EOL> except KeyError : <EOL> raise CommandError ( '<STR_LIT>' % name ) <EOL> def _dojo_mini_before_build ( self ) : <EOL> shutil . move ( "<STR_LIT>" % self . dojo_base_dir , "<STR_LIT>" % self . dojo_base_dir ) <EOL> if not os . path . exists ( "<STR_LIT>" % self . dojo_base_dir ) : <EOL> f = open ( "<STR_LIT>" % self . dojo_base_dir , '<STR_LIT:w>' ) <EOL> f . write ( '''<STR_LIT>''' ) <EOL> f . close ( ) <EOL> else : <EOL> shutil . copyfile ( "<STR_LIT>" % self . dojo_base_dir , "<STR_LIT>" % self . dojo_base_dir ) <EOL> shutil . move ( "<STR_LIT>" % self . dojo_base_dir , "<STR_LIT>" % self . dojo_base_dir ) <EOL> f = open ( "<STR_LIT>" % self . dojo_base_dir , '<STR_LIT:w>' ) <EOL> f . close ( ) <EOL> def _dojo_mini_after_build ( self ) : <EOL> try : <EOL> '''<STR_LIT>''' <EOL> if not os . path . exists ( self . dojo_release_dir ) : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> else : <EOL> self . _remove_files ( '<STR_LIT>' % self . dojo_release_dir , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> dijit_tests = ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ) <EOL> self . _remove_folders ( dijit_tests ) <EOL> noir_theme_path = ( "<STR_LIT>" % self . dojo_release_dir , ) <EOL> self . _remove_folders ( noir_theme_path ) <EOL> self . _remove_files ( '<STR_LIT>' % self . dojo_release_dir , ( '<STR_LIT>' , ) ) <EOL> self . _remove_files ( self . dojo_release_dir , ( '<STR_LIT>' , ) ) <EOL> self . _remove_files ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) <EOL> self . _remove_files ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) <EOL> self . _remove_files ( "<STR_LIT>" , ( "<STR_LIT>" , ) ) <EOL> self . _remove_files ( '<STR_LIT>' % self . dojo_release_dir , ( '<STR_LIT>' , ) ) <EOL> dojo_folders = ( "<STR_LIT>" , ) <EOL> self . _remove_folders ( dojo_folders ) <EOL> os . remove ( "<STR_LIT>" % self . dojo_release_dir ) <EOL> os . remove ( "<STR_LIT>" % self . dojo_release_dir ) <EOL> os . remove ( "<STR_LIT>" % self . dojo_release_dir ) <EOL> except Exception , e : <EOL> print e <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> shutil . move ( "<STR_LIT>" % self . dojo_base_dir , "<STR_LIT>" % self . dojo_base_dir ) <EOL> shutil . move ( "<STR_LIT>" % self . dojo_base_dir , "<STR_LIT>" % self . dojo_base_dir ) <EOL> def _remove_folders ( self , folders ) : <EOL> for folder in folders : <EOL> if os . path . exists ( "<STR_LIT>" % ( self . dojo_release_dir , folder ) ) : <EOL> shutil . rmtree ( "<STR_LIT>" % ( self . dojo_release_dir , folder ) ) <EOL> def _remove_files ( self , base_folder , regexp_list ) : <EOL> for root , dirs , files in os . walk ( base_folder ) : <EOL> for file in files : <EOL> for regexp in regexp_list : <EOL> my_re = re . compile ( regexp ) <EOL> if my_re . match ( file ) : <EOL> os . remove ( os . path . join ( root , file ) ) <EOL> for dir in dirs : <EOL> for regexp in regexp_list : <EOL> my_re = re . compile ( regexp ) <EOL> if my_re . match ( dir ) : <EOL> shutil . rmtree ( os . path . join ( root , dir ) ) <EOL> SKIP_FILES = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> def _dojo_mini_extreme ( self ) : <EOL> """<STR_LIT>""" <EOL> skip_files = self . SKIP_FILES + self . skip_files <EOL> my_re = re . compile ( '<STR_LIT>' % "<STR_LIT:|>" . join ( skip_files ) ) <EOL> try : <EOL> '''<STR_LIT>''' <EOL> if not os . path . exists ( self . dojo_release_dir ) : <EOL> raise CommandError ( '<STR_LIT>' ) <EOL> else : <EOL> for root , dirs , files in os . walk ( self . dojo_release_dir ) : <EOL> for file in files : <EOL> my_file = os . path . abspath ( os . path . join ( root , file ) ) <EOL> if not my_re . match ( my_file ) : <EOL> os . remove ( my_file ) <EOL> for root , dirs , files in os . walk ( self . dojo_release_dir ) : <EOL> for dir in dirs : <EOL> try : <EOL> os . removedirs ( os . path . join ( root , dir ) ) <EOL> except OSError : <EOL> pass <EOL> except Exception , e : <EOL> print e <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> DOJO_ZIP_SPECIAL = { '<STR_LIT>' : [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] } <EOL> def _dojo_prepare_zipserve ( self ) : <EOL> """<STR_LIT>""" <EOL> for folder in os . listdir ( self . dojo_release_dir ) : <EOL> module_dir = '<STR_LIT>' % ( self . dojo_release_dir , folder ) <EOL> if os . path . isdir ( module_dir ) : <EOL> if folder in self . DOJO_ZIP_SPECIAL . keys ( ) : <EOL> for special_module in self . DOJO_ZIP_SPECIAL [ folder ] : <EOL> special_module_dir = os . path . join ( module_dir , special_module ) <EOL> create_zip ( special_module_dir , <EOL> '<STR_LIT>' % { <EOL> '<STR_LIT>' : folder , <EOL> '<STR_LIT>' : special_module <EOL> } , <EOL> '<STR_LIT>' % { <EOL> '<STR_LIT>' : module_dir , <EOL> '<STR_LIT>' : special_module <EOL> } <EOL> ) <EOL> shutil . rmtree ( special_module_dir ) <EOL> create_zip ( module_dir , folder , module_dir + "<STR_LIT>" ) <EOL> shutil . rmtree ( module_dir ) <EOL> def zipfolder ( path , relname , archive ) : <EOL> paths = os . listdir ( path ) <EOL> for p in paths : <EOL> p1 = os . path . join ( path , p ) <EOL> p2 = os . path . join ( relname , p ) <EOL> if os . path . isdir ( p1 ) : <EOL> zipfolder ( p1 , p2 , archive ) <EOL> else : <EOL> archive . write ( p1 , p2 ) <EOL> def create_zip ( path , relname , archname ) : <EOL> import zipfile <EOL> archive = zipfile . ZipFile ( archname , "<STR_LIT:w>" , zipfile . ZIP_DEFLATED ) <EOL> if os . path . isdir ( path ) : <EOL> zipfolder ( path , relname , archive ) <EOL> else : <EOL> archive . write ( path , relname ) <EOL> archive . close ( ) </s>
