<s> import os <EOL> from optparse import OptionParser <EOL> class Dot ( ) : <EOL> def __init__ ( self , git_dir ) : <EOL> """<STR_LIT>""" <EOL> self . git_dir = git_dir <EOL> def get_files ( self , dotignore = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> ignore_file = dotignore <EOL> if self . git_dir != "<STR_LIT:.>" : <EOL> ignore_file = self . git_dir + "<STR_LIT:/>" + dotignore <EOL> IGNORE = [ ] <EOL> if os . path . exists ( ignore_file ) : <EOL> with open ( ignore_file ) as f : <EOL> IGNORE = f . read ( ) . split ( ) <EOL> IGNORE . append ( dotignore ) <EOL> self . git_links = [ ] <EOL> self . home_links = [ ] <EOL> for f in os . listdir ( self . git_dir ) : <EOL> if f in IGNORE : <EOL> continue <EOL> to = os . environ [ "<STR_LIT>" ] + "<STR_LIT:/>" <EOL> if not f . startswith ( "<STR_LIT:.>" ) : <EOL> to += "<STR_LIT:.>" <EOL> to += f <EOL> f = os . path . abspath ( self . git_dir ) + "<STR_LIT:/>" + f <EOL> self . git_links . append ( f ) <EOL> self . home_links . append ( to ) <EOL> def link ( self , ask = False ) : <EOL> """<STR_LIT>""" <EOL> for i , f in enumerate ( self . git_links ) : <EOL> if ask and not os . path . exists ( self . home_links [ i ] ) : <EOL> if raw_input ( "<STR_LIT>" % f ) . lower ( ) == "<STR_LIT:y>" : <EOL> os . symlink ( f , self . home_links [ i ] ) <EOL> elif not os . path . exists ( self . home_links [ i ] ) : <EOL> os . symlink ( f , self . home_links [ i ] ) <EOL> else : <EOL> print "<STR_LIT>" , f <EOL> def unlink ( self , ask = False ) : <EOL> """<STR_LIT>""" <EOL> for f in self . home_links : <EOL> if ask and os . path . exists ( f ) : <EOL> if raw_input ( "<STR_LIT>" % f ) . lower ( ) == "<STR_LIT:y>" : <EOL> os . unlink ( f ) <EOL> elif os . path . exists ( f ) : <EOL> os . unlink ( f ) <EOL> else : <EOL> print "<STR_LIT>" , f <EOL> def preview ( self , unlink_preview ) : <EOL> """<STR_LIT>""" <EOL> for i , f in enumerate ( self . home_links ) : <EOL> if unlink_preview : <EOL> print "<STR_LIT>" % ( f , self . git_links [ i ] ) <EOL> else : <EOL> print "<STR_LIT>" % ( self . git_links [ i ] , f ) <EOL> def generate_dotignore ( self , to_dir ) : <EOL> """<STR_LIT>""" <EOL> to_dir = os . path . abspath ( to_dir ) + "<STR_LIT>" <EOL> if os . path . exists ( to_dir ) and raw_input ( "<STR_LIT>" ) . lower ( ) != "<STR_LIT:y>" : <EOL> return <EOL> IGNORE_LIST = "<STR_LIT>" <EOL> with open ( to_dir , "<STR_LIT:w>" ) as f : <EOL> f . write ( IGNORE_LIST ) <EOL> def main ( ) : <EOL> parser = OptionParser ( ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = False , <EOL> action = "<STR_LIT:store_true>" , help = "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = False , <EOL> action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = False , <EOL> action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , default = False , <EOL> action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> ( options , args ) = parser . parse_args ( ) <EOL> dotignore = "<STR_LIT>" <EOL> if options . dot_ignore : <EOL> dotignore = options . dot_ignore <EOL> git_dir = "<STR_LIT:.>" <EOL> if len ( args ) == <NUM_LIT:1> and os . path . exists ( args [ <NUM_LIT:0> ] ) : <EOL> git_dir = args [ <NUM_LIT:0> ] <EOL> elif len ( args ) == <NUM_LIT:1> and not os . path . exists ( args [ <NUM_LIT:0> ] ) : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> if raw_input ( "<STR_LIT>" ) . lower ( ) != "<STR_LIT:y>" : <EOL> print "<STR_LIT>" <EOL> return <EOL> d = Dot ( git_dir ) <EOL> d . get_files ( dotignore ) <EOL> if options . generate : <EOL> d . generate_dotignore ( git_dir ) <EOL> return <EOL> elif options . preview : <EOL> d . preview ( options . unlink ) <EOL> elif options . unlink : <EOL> d . unlink ( options . ask ) <EOL> else : <EOL> d . link ( options . ask ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
