<s> from __future__ import with_statement <EOL> import cgi <EOL> import errno <EOL> import mimetypes <EOL> import os <EOL> import posixpath <EOL> import re <EOL> import shutil <EOL> import stat <EOL> import sys <EOL> import tempfile <EOL> import urllib <EOL> from optparse import make_option <EOL> from os import path <EOL> import django <EOL> from django . template import Template , Context <EOL> from django . utils import archive <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . _os import rmtree_errorhandler <EOL> from django . core . management . base import BaseCommand , CommandError <EOL> from django . core . management . commands . makemessages import handle_extensions <EOL> _drive_re = re . compile ( '<STR_LIT>' , re . I ) <EOL> _url_drive_re = re . compile ( '<STR_LIT>' , re . I ) <EOL> class TemplateCommand ( BaseCommand ) : <EOL> """<STR_LIT>""" <EOL> args = "<STR_LIT>" <EOL> option_list = BaseCommand . option_list + ( <EOL> make_option ( '<STR_LIT>' , <EOL> action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = [ '<STR_LIT>' ] , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> make_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> action = '<STR_LIT>' , default = [ ] , <EOL> help = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ) <EOL> requires_model_validation = False <EOL> can_import_settings = False <EOL> url_schemes = [ '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def handle ( self , app_or_project , name , target = None , ** options ) : <EOL> self . app_or_project = app_or_project <EOL> self . paths_to_remove = [ ] <EOL> self . verbosity = int ( options . get ( '<STR_LIT>' ) ) <EOL> if not re . search ( r'<STR_LIT>' , name ) : <EOL> if not re . search ( r'<STR_LIT>' , name ) : <EOL> message = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> else : <EOL> message = '<STR_LIT>' <EOL> raise CommandError ( "<STR_LIT>" % <EOL> ( name , app_or_project , message ) ) <EOL> if target is None : <EOL> top_dir = path . join ( os . getcwd ( ) , name ) <EOL> try : <EOL> os . makedirs ( top_dir ) <EOL> except OSError , e : <EOL> if e . errno == errno . EEXIST : <EOL> message = "<STR_LIT>" % top_dir <EOL> else : <EOL> message = e <EOL> raise CommandError ( message ) <EOL> else : <EOL> top_dir = os . path . abspath ( path . expanduser ( target ) ) <EOL> if not os . path . exists ( top_dir ) : <EOL> raise CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" % top_dir ) <EOL> extensions = tuple ( <EOL> handle_extensions ( options . get ( '<STR_LIT>' ) , ignored = ( ) ) ) <EOL> extra_files = [ ] <EOL> for file in options . get ( '<STR_LIT>' ) : <EOL> extra_files . extend ( map ( lambda x : x . strip ( ) , file . split ( '<STR_LIT:U+002C>' ) ) ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . stdout . write ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( app_or_project , '<STR_LIT:U+002CU+0020>' . join ( extensions ) ) ) <EOL> self . stdout . write ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> ( app_or_project , '<STR_LIT:U+002CU+0020>' . join ( extra_files ) ) ) <EOL> base_name = '<STR_LIT>' % app_or_project <EOL> base_subdir = '<STR_LIT>' % app_or_project <EOL> base_directory = '<STR_LIT>' % app_or_project <EOL> context = Context ( dict ( options , ** { <EOL> base_name : name , <EOL> base_directory : top_dir , <EOL> } ) ) <EOL> from django . conf import settings <EOL> if not settings . configured : <EOL> settings . configure ( ) <EOL> template_dir = self . handle_template ( options . get ( '<STR_LIT>' ) , <EOL> base_subdir ) <EOL> prefix_length = len ( template_dir ) + <NUM_LIT:1> <EOL> for root , dirs , files in os . walk ( template_dir ) : <EOL> path_rest = root [ prefix_length : ] <EOL> relative_dir = path_rest . replace ( base_name , name ) <EOL> if relative_dir : <EOL> target_dir = path . join ( top_dir , relative_dir ) <EOL> if not path . exists ( target_dir ) : <EOL> os . mkdir ( target_dir ) <EOL> for dirname in dirs [ : ] : <EOL> if dirname . startswith ( '<STR_LIT:.>' ) : <EOL> dirs . remove ( dirname ) <EOL> for filename in files : <EOL> if filename . endswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> continue <EOL> old_path = path . join ( root , filename ) <EOL> new_path = path . join ( top_dir , relative_dir , <EOL> filename . replace ( base_name , name ) ) <EOL> if path . exists ( new_path ) : <EOL> raise CommandError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % new_path ) <EOL> with open ( old_path , '<STR_LIT:r>' ) as template_file : <EOL> content = template_file . read ( ) <EOL> if filename . endswith ( extensions ) or filename in extra_files : <EOL> template = Template ( content ) <EOL> content = template . render ( context ) <EOL> with open ( new_path , '<STR_LIT:w>' ) as new_file : <EOL> new_file . write ( content ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . stdout . write ( "<STR_LIT>" % new_path ) <EOL> try : <EOL> shutil . copymode ( old_path , new_path ) <EOL> self . make_writeable ( new_path ) <EOL> except OSError : <EOL> notice = self . style . NOTICE ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % new_path ) <EOL> sys . stderr . write ( smart_str ( notice ) ) <EOL> if self . paths_to_remove : <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . stdout . write ( "<STR_LIT>" ) <EOL> for path_to_remove in self . paths_to_remove : <EOL> if path . isfile ( path_to_remove ) : <EOL> os . remove ( path_to_remove ) <EOL> else : <EOL> shutil . rmtree ( path_to_remove , <EOL> onerror = rmtree_errorhandler ) <EOL> def handle_template ( self , template , subdir ) : <EOL> """<STR_LIT>""" <EOL> if template is None : <EOL> return path . join ( django . __path__ [ <NUM_LIT:0> ] , '<STR_LIT>' , subdir ) <EOL> else : <EOL> if template . startswith ( '<STR_LIT>' ) : <EOL> template = template [ <NUM_LIT:7> : ] <EOL> expanded_template = path . expanduser ( template ) <EOL> expanded_template = path . normpath ( expanded_template ) <EOL> if path . isdir ( expanded_template ) : <EOL> return expanded_template <EOL> if self . is_url ( template ) : <EOL> absolute_path = self . download ( template ) <EOL> else : <EOL> absolute_path = path . abspath ( expanded_template ) <EOL> if path . exists ( absolute_path ) : <EOL> return self . extract ( absolute_path ) <EOL> raise CommandError ( "<STR_LIT>" % <EOL> ( self . app_or_project , template ) ) <EOL> def download ( self , url ) : <EOL> """<STR_LIT>""" <EOL> def cleanup_url ( url ) : <EOL> tmp = url . rstrip ( '<STR_LIT:/>' ) <EOL> filename = tmp . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> if url . endswith ( '<STR_LIT:/>' ) : <EOL> display_url = tmp + '<STR_LIT:/>' <EOL> else : <EOL> display_url = url <EOL> return filename , display_url <EOL> prefix = '<STR_LIT>' % self . app_or_project <EOL> tempdir = tempfile . mkdtemp ( prefix = prefix , suffix = '<STR_LIT>' ) <EOL> self . paths_to_remove . append ( tempdir ) <EOL> filename , display_url = cleanup_url ( url ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . stdout . write ( "<STR_LIT>" % display_url ) <EOL> try : <EOL> the_path , info = urllib . urlretrieve ( url , <EOL> path . join ( tempdir , filename ) ) <EOL> except IOError , e : <EOL> raise CommandError ( "<STR_LIT>" % <EOL> ( url , filename , e ) ) <EOL> used_name = the_path . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] <EOL> content_disposition = info . get ( '<STR_LIT>' ) <EOL> if content_disposition : <EOL> _ , params = cgi . parse_header ( content_disposition ) <EOL> guessed_filename = params . get ( '<STR_LIT:filename>' ) or used_name <EOL> else : <EOL> guessed_filename = used_name <EOL> ext = self . splitext ( guessed_filename ) [ <NUM_LIT:1> ] <EOL> content_type = info . get ( '<STR_LIT>' ) <EOL> if not ext and content_type : <EOL> ext = mimetypes . guess_extension ( content_type ) <EOL> if ext : <EOL> guessed_filename += ext <EOL> if used_name != guessed_filename : <EOL> guessed_path = path . join ( tempdir , guessed_filename ) <EOL> shutil . move ( the_path , guessed_path ) <EOL> return guessed_path <EOL> return the_path <EOL> def splitext ( self , the_path ) : <EOL> """<STR_LIT>""" <EOL> base , ext = posixpath . splitext ( the_path ) <EOL> if base . lower ( ) . endswith ( '<STR_LIT>' ) : <EOL> ext = base [ - <NUM_LIT:4> : ] + ext <EOL> base = base [ : - <NUM_LIT:4> ] <EOL> return base , ext <EOL> def extract ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> prefix = '<STR_LIT>' % self . app_or_project <EOL> tempdir = tempfile . mkdtemp ( prefix = prefix , suffix = '<STR_LIT>' ) <EOL> self . paths_to_remove . append ( tempdir ) <EOL> if self . verbosity >= <NUM_LIT:2> : <EOL> self . stdout . write ( "<STR_LIT>" % filename ) <EOL> try : <EOL> archive . extract ( filename , tempdir ) <EOL> return tempdir <EOL> except ( archive . ArchiveException , IOError ) , e : <EOL> raise CommandError ( "<STR_LIT>" % <EOL> ( filename , tempdir , e ) ) <EOL> def is_url ( self , template ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT::>' not in template : <EOL> return False <EOL> scheme = template . split ( '<STR_LIT::>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] . lower ( ) <EOL> return scheme in self . url_schemes <EOL> def make_writeable ( self , filename ) : <EOL> """<STR_LIT>""" <EOL> if sys . platform . startswith ( '<STR_LIT>' ) : <EOL> return <EOL> if not os . access ( filename , os . W_OK ) : <EOL> st = os . stat ( filename ) <EOL> new_permissions = stat . S_IMODE ( st . st_mode ) | stat . S_IWUSR <EOL> os . chmod ( filename , new_permissions ) </s>
