<s> """<STR_LIT>""" <EOL> __revision__ = "<STR_LIT>" <EOL> import os , sys <EOL> from types import * <EOL> from distutils . errors import DistutilsFileError , DistutilsInternalError <EOL> from distutils import log <EOL> _path_created = { } <EOL> def mkpath ( name , mode = <NUM_LIT:0> <NUM_LIT> , verbose = <NUM_LIT:0> , dry_run = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> global _path_created <EOL> if not isinstance ( name , StringTypes ) : <EOL> raise DistutilsInternalError , "<STR_LIT>" % ( name , ) <EOL> name = os . path . normpath ( name ) <EOL> created_dirs = [ ] <EOL> if os . path . isdir ( name ) or name == '<STR_LIT>' : <EOL> return created_dirs <EOL> if _path_created . get ( os . path . abspath ( name ) ) : <EOL> return created_dirs <EOL> ( head , tail ) = os . path . split ( name ) <EOL> tails = [ tail ] <EOL> while head and tail and not os . path . isdir ( head ) : <EOL> ( head , tail ) = os . path . split ( head ) <EOL> tails . insert ( <NUM_LIT:0> , tail ) <EOL> for d in tails : <EOL> head = os . path . join ( head , d ) <EOL> abs_head = os . path . abspath ( head ) <EOL> if _path_created . get ( abs_head ) : <EOL> continue <EOL> log . info ( "<STR_LIT>" , head ) <EOL> if not dry_run : <EOL> try : <EOL> os . mkdir ( head ) <EOL> created_dirs . append ( head ) <EOL> except OSError , exc : <EOL> raise DistutilsFileError , "<STR_LIT>" % ( head , exc [ - <NUM_LIT:1> ] ) <EOL> _path_created [ abs_head ] = <NUM_LIT:1> <EOL> return created_dirs <EOL> def create_tree ( base_dir , files , mode = <NUM_LIT:0> <NUM_LIT> , verbose = <NUM_LIT:0> , dry_run = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> need_dir = { } <EOL> for file in files : <EOL> need_dir [ os . path . join ( base_dir , os . path . dirname ( file ) ) ] = <NUM_LIT:1> <EOL> need_dirs = need_dir . keys ( ) <EOL> need_dirs . sort ( ) <EOL> for dir in need_dirs : <EOL> mkpath ( dir , mode , dry_run = dry_run ) <EOL> def copy_tree ( src , dst , <EOL> preserve_mode = <NUM_LIT:1> , <EOL> preserve_times = <NUM_LIT:1> , <EOL> preserve_symlinks = <NUM_LIT:0> , <EOL> update = <NUM_LIT:0> , <EOL> verbose = <NUM_LIT:0> , <EOL> dry_run = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> from distutils . file_util import copy_file <EOL> if not dry_run and not os . path . isdir ( src ) : <EOL> raise DistutilsFileError , "<STR_LIT>" % src <EOL> try : <EOL> names = os . listdir ( src ) <EOL> except os . error , ( errno , errstr ) : <EOL> if dry_run : <EOL> names = [ ] <EOL> else : <EOL> raise DistutilsFileError , "<STR_LIT>" % ( src , errstr ) <EOL> if not dry_run : <EOL> mkpath ( dst ) <EOL> outputs = [ ] <EOL> for n in names : <EOL> src_name = os . path . join ( src , n ) <EOL> dst_name = os . path . join ( dst , n ) <EOL> if preserve_symlinks and os . path . islink ( src_name ) : <EOL> link_dest = os . readlink ( src_name ) <EOL> log . info ( "<STR_LIT>" , dst_name , link_dest ) <EOL> if not dry_run : <EOL> os . symlink ( link_dest , dst_name ) <EOL> outputs . append ( dst_name ) <EOL> elif os . path . isdir ( src_name ) : <EOL> outputs . extend ( <EOL> copy_tree ( src_name , dst_name , preserve_mode , <EOL> preserve_times , preserve_symlinks , update , <EOL> dry_run = dry_run ) ) <EOL> else : <EOL> copy_file ( src_name , dst_name , preserve_mode , <EOL> preserve_times , update , dry_run = dry_run ) <EOL> outputs . append ( dst_name ) <EOL> return outputs <EOL> def _build_cmdtuple ( path , cmdtuples ) : <EOL> for f in os . listdir ( path ) : <EOL> real_f = os . path . join ( path , f ) <EOL> if os . path . isdir ( real_f ) and not os . path . islink ( real_f ) : <EOL> _build_cmdtuple ( real_f , cmdtuples ) <EOL> else : <EOL> cmdtuples . append ( ( os . remove , real_f ) ) <EOL> cmdtuples . append ( ( os . rmdir , path ) ) <EOL> def remove_tree ( directory , verbose = <NUM_LIT:0> , dry_run = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> from distutils . util import grok_environment_error <EOL> global _path_created <EOL> log . info ( "<STR_LIT>" , directory ) <EOL> if dry_run : <EOL> return <EOL> cmdtuples = [ ] <EOL> _build_cmdtuple ( directory , cmdtuples ) <EOL> for cmd in cmdtuples : <EOL> try : <EOL> apply ( cmd [ <NUM_LIT:0> ] , ( cmd [ <NUM_LIT:1> ] , ) ) <EOL> abspath = os . path . abspath ( cmd [ <NUM_LIT:1> ] ) <EOL> if _path_created . has_key ( abspath ) : <EOL> del _path_created [ abspath ] <EOL> except ( IOError , OSError ) , exc : <EOL> log . warn ( grok_environment_error ( <EOL> exc , "<STR_LIT>" % directory ) ) <EOL> def ensure_relative ( path ) : <EOL> """<STR_LIT>""" <EOL> drive , path = os . path . splitdrive ( path ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> return os . sep + path <EOL> else : <EOL> if path [ <NUM_LIT:0> : <NUM_LIT:1> ] == os . sep : <EOL> path = drive + path [ <NUM_LIT:1> : ] <EOL> return path </s>
