o 3aU5@sddlZddlZddlZddlZddlZddlZddlZddlmZddl m Z ddl Z ddl m Z ddlmZmZddlmZddlmZmZddlmZdd lmZGd d d eZdS) N) import_module) urlretrieve)settings) BaseCommand CommandError)handle_extensions)ContextEngine)archive)get_docs_versionc@sleZdZdZgZgdZdZddZdddZd d Z dd d Z ddZ ddZ ddZ ddZddZdS)TemplateCommanda Copy either a Django application layout template or a Django project layout template into the specified directory. :param style: A color style object (see django.core.management.color). :param app_or_project: The string 'app' or 'project'. :param name: The name of the application or project. :param directory: The directory to which the template should be copied. :param options: The additional variables passed to project or app templates )httphttpsftp))z.py-tplz.pycCs^|jddd|jdddd|jdd d|jd d d d dgdd|jdddd gdddS)Nnamez#Name of the application or project.)help directory?zOptional destination directory)nargsrz --templatez*The path or URL to load the template from.z --extensionz-e extensionsappendpyztThe file extension(s) to render (default: "py"). Separate multiple extensions with commas, or use -e multiple times.)destactiondefaultrz--namez-nfilesz_The file name(s) to render. Separate multiple file names with commas, or use -n multiple times.) add_argument)selfparserrB/usr/lib/python3/dist-packages/django/core/management/templates.py add_arguments(s zTemplateCommand.add_argumentsNc# Ksv||_|dkr dnd|_g|_|d|_|||durJtjt|}zt |Wn@t y:t d|t yI}zt |d}~ww|dkrX|tj |dtjtj|}tj|snt d|tt|d}g}|d D]} |td d | d q||jd kr|jd|d|f|jd|d|fd|} d|} d|} d|} ddd|D}ti|| || || |dtdtjidd}tjstt |!|d| }t"|d}t#|D]\}}}||d}|$| |}|rtj||}tj |dd|ddD]}|%d s1|d!kr6|&|q$|D]}|'d"rDq:tj||}tj|||$| |}|j(D]\}}|'|rq|dt"| |}nqZtj|rt d#||j|f|'|s||vrt)|d$d% }|*}Wdn 1swYt+,|} | -|}t)|d&d$d% }!|!|Wdn 1swYnt./|||jd kr|jd'|zt.0|||1|Wq:t y |j2d(||j3j4Yq:wq|jr7|jd kr|jd)|jD]}"tj5|"r0t&|"q t.6|"q dSdS)*Nappana verbosityz'%s' already existsrzBDestination directory '%s' does not exist, please create it first.rrcSs|SN)strip)xrrr Vsz(TemplateCommand.handle..,z/Rendering %s template files with extensions: %sz, z.Rendering %s template files with filenames: %sz%s_namez %s_templatez %s_directoryzcamel_case_%s_namecss|] }|dkr|VqdS)_Nr).0r(rrr dsz)TemplateCommand.handle.. docs_versiondjango_versionF) autoescapetemplateT)exist_ok. __pycache__)z.pyoz.pycz .py.classz_%s already exists. Overlaying %s %s into an existing directory won't replace conflicting files.zutf-8)encodingwz Creating %szkNotice: Couldn't set permission bits on %s. You're probably using an uncommon filesystem setup. No problem.zCleaning up temporary files.)7app_or_projecta_or_anpaths_to_remover% validate_nameospathjoingetcwdmakedirsFileExistsErrorrOSErrorbasenameabspath expanduserexiststuplerextendmapsplitstdoutwritetitlerr django __version__r configured configuresetuphandle_templatelenwalkreplace startswithremoveendswithrewrite_template_suffixesopenreadr from_stringrendershutilcopyfilecopymodemake_writeablestderrstyleNOTICEisfilermtree)#rr:rtargetoptionstop_direr extra_filesfile base_name base_subdirbase_directorycamel_case_namecamel_case_valuecontext template_dir prefix_lengthrootdirsr path_rest relative_dir target_dirdirnamefilenameold_pathnew_path old_suffix new_suffix template_filecontentr3new_filepath_to_removerrr handle:s                          ,   zTemplateCommand.handlecCs|durtjtjdd|S|dr|dd}tj|}tj|}tj|r.|S| |r9| |}ntj |}tj |rJ| |Std|j|f)z Determine where the app or project templates are. Use django.__path__[0] as the default because the Django install directory isn't known. Nrconfzfile://zcouldn't handle %s template %s.)r>r?r@rP__path__rYrGnormpathisdiris_urldownloadrFrHextractrr:)rr3subdirexpanded_template absolute_pathrrr rUs           zTemplateCommand.handle_templatercCsx|durtdj|j|jd|stdj||j|dzt|Wn ty.YdSwtdj||j|j|d)Nz you must provide {an} {app} name)r#r"zX'{name}' is not a valid {app} {type}. Please make sure the {type} is a valid identifier.)rr"typez'{name}' conflicts with the name of an existing Python module and cannot be used as {an} {app} {type}. Please try another {type}.)rr#r"r)rformatr;r: isidentifierr ImportError)rr name_or_dirrrr r=s8  zTemplateCommand.validate_namec Cs8dd}d|j}tj|dd}|j|||\}}|jdkr)|jd|zt|t j ||\}}Wnt yL} z t d||| fd } ~ ww|d d } |d } | rjt| \} } | d ph| }n| }||d}|d}|s|rt|}|r||7}| |krt j ||}t|||S|S)zB Download the given URL and return the file name. cSs>|d}|dd}|dr|d}||fS|}||fS)N/)rstriprLr[)urltmpr~ display_urlrrr cleanup_urls  z-TemplateCommand.download..cleanup_urldjango_%s_template_ _downloadprefixsuffixr+zDownloading %sz"couldn't download URL %s to %s: %sNrrzcontent-dispositionr~r4z content-type)r:tempfilemkdtempr<rr%rMrNrr>r?r@rDrrLgetcgi parse_headersplitext mimetypesguess_extensionramove)rrrrtempdirr~rthe_pathinform used_namecontent_dispositionr-paramsguessed_filenameext content_type guessed_pathrrr rs@        zTemplateCommand.downloadcCs@t|\}}|dr|dd|}|dd}||fS)z@ Like os.path.splitext, but takes off .tar, too z.tarN) posixpathrlowerr[)rrbaserrrr r*s  zTemplateCommand.splitextc Csd|j}tj|dd}|j||jdkr|jd|z t |||WStj t fy@}z t d|||fd}~ww)z Extract the given file to a temporary directory and return the path of the directory with the extracted content. r_extractrr+z Extracting %sz"couldn't extract file %s to %s: %sN) r:rrr<rr%rMrNr rArchiveExceptionrDr)rr~rrrmrrr r4s    zTemplateCommand.extractcCs*d|vrdS|ddd}||jvS)z)Return True if the name looks like a URL.:Fr4r)rLr url_schemes)rr3schemerrr rEs zTemplateCommand.is_urlcCs>t|tjst|}t|jtjB}t||dSdS)zb Make sure that the file is writeable. Useful if our source is read-only. N)r>accessW_OKstatS_IMODEst_modeS_IWUSRchmod)rr~stnew_permissionsrrr rdLs  zTemplateCommand.make_writeabler&)r)__name__ __module__ __qualname____doc__requires_system_checksrr\r!rrUr=rrrrrdrrrr r s   !6  r )rrr>rrarr importlibrurllib.requestrrP django.confrdjango.core.management.baserrdjango.core.management.utilsrdjango.templaterr django.utilsr django.utils.versionr r rrrr s"