<s> import re <EOL> from unicodedata import normalize <EOL> from os import listdir , walk , makedirs , unlink , chdir , getcwd <EOL> from os . path import join , dirname , isdir <EOL> from tempfile import mkdtemp <EOL> from shutil import copy2 <EOL> from contextlib import contextmanager <EOL> from git import Repo <EOL> _punct_re = re . compile ( r'<STR_LIT>' ) <EOL> def slugify ( text , delim = u'<STR_LIT:->' ) : <EOL> """<STR_LIT>""" <EOL> result = [ ] <EOL> for word in _punct_re . split ( unicode ( text ) . lower ( ) ) : <EOL> nword = normalize ( '<STR_LIT>' , word ) . encode ( '<STR_LIT:ascii>' , '<STR_LIT:ignore>' ) <EOL> if nword : <EOL> pword = '<STR_LIT>' . join ( [ i for i in nword if ord ( i ) > <NUM_LIT> ] ) <EOL> result . append ( pword ) <EOL> return unicode ( delim . join ( filter ( bool , result ) ) ) <EOL> def indent ( payload , by = <NUM_LIT:4> , character = u'<STR_LIT:U+0020>' ) : <EOL> """<STR_LIT>""" <EOL> return_first = False <EOL> if isinstance ( payload , ( basestring ) ) : <EOL> payload = [ payload ] <EOL> return_first = True <EOL> indented = [ ] <EOL> for line in payload : <EOL> indented . append ( '<STR_LIT>' . join ( [ unicode ( character ) ] * by ) + unicode ( line ) ) <EOL> if return_first : <EOL> return indented [ <NUM_LIT:0> ] <EOL> return indented <EOL> @ contextmanager <EOL> def cwd_bounce ( dir ) : <EOL> """<STR_LIT>""" <EOL> original_dir = getcwd ( ) <EOL> try : <EOL> chdir ( dir ) <EOL> yield original_dir <EOL> finally : <EOL> chdir ( original_dir ) <EOL> class NumberedDirectoriesToGit ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , numdir ) : <EOL> if not isdir ( numdir ) : <EOL> raise ValueError ( '<STR_LIT>' . format ( numdir ) ) <EOL> self . numdir = numdir <EOL> self . target = mkdtemp ( ) <EOL> self . _repo = None <EOL> @ property <EOL> def repo ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . _repo : <EOL> self . _repo = Repo . init ( self . target ) <EOL> for d in sorted ( listdir ( self . numdir ) ) : <EOL> self . _commit ( self . _repo , join ( self . numdir , d ) ) <EOL> return self . _repo <EOL> def diffs ( self ) : <EOL> """<STR_LIT>""" <EOL> repo = self . repo <EOL> diffs = [ ] <EOL> for commit in repo . iter_commits ( ) : <EOL> try : <EOL> diffs . append ( commit . parents [ <NUM_LIT:0> ] . diff ( commit ) ) <EOL> except IndexError : <EOL> pass <EOL> diffs . reverse ( ) <EOL> return diffs <EOL> def _commit ( self , repo , d ) : <EOL> """<STR_LIT>""" <EOL> paths_current = set ( self . _flattendirectory ( <EOL> repo . working_dir , strip = self . target ) ) <EOL> paths_new = set ( self . _flattendirectory ( <EOL> d , strip = d ) ) <EOL> paths_to_add = paths_new - paths_current <EOL> paths_to_del = paths_current - paths_new <EOL> paths_same = paths_new . intersection ( paths_current ) <EOL> for path in paths_to_add . union ( paths_same ) : <EOL> directory = join ( self . target , dirname ( path ) ) <EOL> if not isdir ( directory ) : <EOL> makedirs ( directory ) <EOL> copy2 ( <EOL> join ( d , path ) , <EOL> join ( self . target , path ) ) <EOL> repo . index . add ( [ path ] ) <EOL> for path in paths_to_del : <EOL> repo . index . remove ( [ join ( self . target , path ) ] ) <EOL> unlink ( join ( self . target , path ) ) <EOL> repo . index . commit ( '<STR_LIT>' . format ( d ) ) <EOL> def _flattendirectory ( self , d , strip = None ) : <EOL> """<STR_LIT>""" <EOL> for ( dirpath , dirname , filenames ) in walk ( d ) : <EOL> for fn in filenames : <EOL> sdir = dirpath . replace ( '<STR_LIT>' . format ( strip ) , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) <EOL> if sdir . startswith ( '<STR_LIT>' ) : <EOL> continue <EOL> yield join ( sdir , fn ) </s>
