<s> import datetime <EOL> import itertools <EOL> from vcs . utils import author_name , author_email <EOL> from vcs . utils . lazy import LazyProperty <EOL> from vcs . utils . helpers import get_dict_for_attrs <EOL> from vcs . conf import settings <EOL> from vcs . exceptions import ( <EOL> ChangesetError , EmptyRepositoryError , NodeAlreadyAddedError , <EOL> NodeAlreadyChangedError , NodeAlreadyExistsError , NodeAlreadyRemovedError , <EOL> NodeDoesNotExistError , NodeNotChangedError , RepositoryError <EOL> ) <EOL> class BaseRepository ( object ) : <EOL> """<STR_LIT>""" <EOL> scm = None <EOL> DEFAULT_BRANCH_NAME = None <EOL> EMPTY_CHANGESET = '<STR_LIT:0>' * <NUM_LIT> <EOL> def __init__ ( self , repo_path , create = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . path ) <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def __len__ ( self ) : <EOL> return self . count ( ) <EOL> def __eq__ ( self , other ) : <EOL> same_instance = isinstance ( other , self . __class__ ) <EOL> return same_instance and getattr ( other , '<STR_LIT:path>' , None ) == self . path <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> @ LazyProperty <EOL> def alias ( self ) : <EOL> for k , v in settings . BACKENDS . items ( ) : <EOL> if v . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] == str ( self . __class__ . __name__ ) : <EOL> return k <EOL> @ LazyProperty <EOL> def name ( self ) : <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def owner ( self ) : <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def description ( self ) : <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> size = <NUM_LIT:0> <EOL> try : <EOL> tip = self . get_changeset ( ) <EOL> for topnode , dirs , files in tip . walk ( '<STR_LIT:/>' ) : <EOL> for f in files : <EOL> size += tip . get_file_size ( f . path ) <EOL> for dir in dirs : <EOL> for f in files : <EOL> size += tip . get_file_size ( f . path ) <EOL> except RepositoryError : <EOL> pass <EOL> return size <EOL> def is_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_last_change ( self ) : <EOL> self . get_changesets ( ) <EOL> def get_changeset ( self , revision = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> for revision in self . revisions : <EOL> yield self . get_changeset ( revision ) <EOL> def get_changesets ( self , start = None , end = None , start_date = None , <EOL> end_date = None , branch_name = None , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def __getslice__ ( self , i , j ) : <EOL> """<STR_LIT>""" <EOL> for rev in self . revisions [ i : j ] : <EOL> yield self . get_changeset ( rev ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . get_changeset ( key ) <EOL> def count ( self ) : <EOL> return len ( self . revisions ) <EOL> def tag ( self , name , user , revision = None , message = None , date = None , ** opts ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def remove_tag ( self , name , user , message = None , date = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_diff ( self , rev1 , rev2 , path = None , ignore_whitespace = False , <EOL> context = <NUM_LIT:3> ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def in_memory_changeset ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def add ( self , filenode , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def remove ( self , filenode , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def commit ( self , message , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_state ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_config_value ( self , section , name , config_file = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_user_name ( self , config_file = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_user_email ( self , config_file = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def workdir ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class BaseChangeset ( object ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , self . revision , <EOL> self . short_id ) <EOL> def __repr__ ( self ) : <EOL> return self . __str__ ( ) <EOL> def __unicode__ ( self ) : <EOL> return u'<STR_LIT>' % ( self . revision , self . short_id ) <EOL> def __eq__ ( self , other ) : <EOL> return self . raw_id == other . raw_id <EOL> @ LazyProperty <EOL> def last ( self ) : <EOL> if self . repository is None : <EOL> raise ChangesetError ( "<STR_LIT>" ) <EOL> return self . raw_id == self . repository . revisions [ - <NUM_LIT:1> ] <EOL> @ LazyProperty <EOL> def parents ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def children ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def id ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def raw_id ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def short_id ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def revision ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def committer ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def committer_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return author_name ( self . committer ) <EOL> @ LazyProperty <EOL> def committer_email ( self ) : <EOL> """<STR_LIT>""" <EOL> return author_email ( self . committer ) <EOL> @ LazyProperty <EOL> def author ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def author_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return author_name ( self . author ) <EOL> @ LazyProperty <EOL> def author_email ( self ) : <EOL> """<STR_LIT>""" <EOL> return author_email ( self . author ) <EOL> def get_file_mode ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_file_content ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_file_size ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_file_changeset ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_file_history ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_nodes ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_node ( self , path ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def fill_archive ( self , stream = None , kind = '<STR_LIT>' , prefix = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_chunked_archive ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> chunk_size = kwargs . pop ( '<STR_LIT>' , <NUM_LIT> ) <EOL> stream = kwargs . get ( '<STR_LIT>' ) <EOL> self . fill_archive ( ** kwargs ) <EOL> while True : <EOL> data = stream . read ( chunk_size ) <EOL> if not data : <EOL> break <EOL> yield data <EOL> @ LazyProperty <EOL> def root ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get_node ( '<STR_LIT>' ) <EOL> def next ( self , branch = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def prev ( self , branch = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def added ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def changed ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def removed ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> @ LazyProperty <EOL> def size ( self ) : <EOL> """<STR_LIT>""" <EOL> return sum ( ( node . size for node in self . get_filenodes_generator ( ) ) ) <EOL> def walk ( self , topurl = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> topnode = self . get_node ( topurl ) <EOL> yield ( topnode , topnode . dirs , topnode . files ) <EOL> for dirnode in topnode . dirs : <EOL> for tup in self . walk ( dirnode . path ) : <EOL> yield tup <EOL> def get_filenodes_generator ( self ) : <EOL> """<STR_LIT>""" <EOL> for topnode , dirs , files in self . walk ( ) : <EOL> for node in files : <EOL> yield node <EOL> def as_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> data = get_dict_for_attrs ( self , [ '<STR_LIT:id>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:date>' , '<STR_LIT:message>' ] ) <EOL> data [ '<STR_LIT>' ] = { '<STR_LIT:name>' : self . author_name , '<STR_LIT:email>' : self . author_email } <EOL> data [ '<STR_LIT>' ] = [ node . path for node in self . added ] <EOL> data [ '<STR_LIT>' ] = [ node . path for node in self . changed ] <EOL> data [ '<STR_LIT>' ] = [ node . path for node in self . removed ] <EOL> return data <EOL> class BaseWorkdir ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , repository ) : <EOL> self . repository = repository <EOL> def get_branch ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_changeset ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_added ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_removed ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_untracked ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def get_status ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def commit ( self , message , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def update ( self , revision = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> def checkout_branch ( self , branch = None ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class BaseInMemoryChangeset ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , repository ) : <EOL> self . repository = repository <EOL> self . added = [ ] <EOL> self . changed = [ ] <EOL> self . removed = [ ] <EOL> self . parents = [ ] <EOL> def add ( self , * filenodes ) : <EOL> """<STR_LIT>""" <EOL> for node in filenodes : <EOL> if node . path in ( n . path for n in self . added ) : <EOL> raise NodeAlreadyAddedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % node . path ) <EOL> for node in filenodes : <EOL> self . added . append ( node ) <EOL> def change ( self , * filenodes ) : <EOL> """<STR_LIT>""" <EOL> for node in filenodes : <EOL> if node . path in ( n . path for n in self . removed ) : <EOL> raise NodeAlreadyRemovedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % node . path ) <EOL> try : <EOL> self . repository . get_changeset ( ) <EOL> except EmptyRepositoryError : <EOL> raise EmptyRepositoryError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for node in filenodes : <EOL> if node . path in ( n . path for n in self . changed ) : <EOL> raise NodeAlreadyChangedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % node . path ) <EOL> self . changed . append ( node ) <EOL> def remove ( self , * filenodes ) : <EOL> """<STR_LIT>""" <EOL> for node in filenodes : <EOL> if node . path in ( n . path for n in self . removed ) : <EOL> raise NodeAlreadyRemovedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % node . path ) <EOL> if node . path in ( n . path for n in self . changed ) : <EOL> raise NodeAlreadyChangedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % node . path ) <EOL> self . removed . append ( node ) <EOL> def reset ( self ) : <EOL> """<STR_LIT>""" <EOL> self . added = [ ] <EOL> self . changed = [ ] <EOL> self . removed = [ ] <EOL> self . parents = [ ] <EOL> def get_ipaths ( self ) : <EOL> """<STR_LIT>""" <EOL> for node in itertools . chain ( self . added , self . changed , self . removed ) : <EOL> yield node . path <EOL> def get_paths ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self . get_ipaths ( ) ) <EOL> def check_integrity ( self , parents = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . parents : <EOL> parents = parents or [ ] <EOL> if len ( parents ) == <NUM_LIT:0> : <EOL> try : <EOL> parents = [ self . repository . get_changeset ( ) , None ] <EOL> except EmptyRepositoryError : <EOL> parents = [ None , None ] <EOL> elif len ( parents ) == <NUM_LIT:1> : <EOL> parents += [ None ] <EOL> self . parents = parents <EOL> parents = [ p for p in self . parents if p ] <EOL> for p in parents : <EOL> for node in self . added : <EOL> try : <EOL> p . get_node ( node . path ) <EOL> except NodeDoesNotExistError : <EOL> pass <EOL> else : <EOL> raise NodeAlreadyExistsError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( node . path , p ) ) <EOL> missing = set ( self . changed ) <EOL> not_changed = set ( self . changed ) <EOL> if self . changed and not parents : <EOL> raise NodeDoesNotExistError ( str ( self . changed [ <NUM_LIT:0> ] . path ) ) <EOL> for p in parents : <EOL> for node in self . changed : <EOL> try : <EOL> old = p . get_node ( node . path ) <EOL> missing . remove ( node ) <EOL> if old . content != node . content : <EOL> not_changed . remove ( node ) <EOL> except NodeDoesNotExistError : <EOL> pass <EOL> if self . changed and missing : <EOL> raise NodeDoesNotExistError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( node . path , parents ) ) <EOL> if self . changed and not_changed : <EOL> raise NodeNotChangedError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( not_changed . pop ( ) . path , <EOL> parents ) <EOL> ) <EOL> if self . removed and not parents : <EOL> raise NodeDoesNotExistError ( "<STR_LIT>" <EOL> "<STR_LIT>" % self . removed [ <NUM_LIT:0> ] . path ) <EOL> really_removed = set ( ) <EOL> for p in parents : <EOL> for node in self . removed : <EOL> try : <EOL> p . get_node ( node . path ) <EOL> really_removed . add ( node ) <EOL> except ChangesetError : <EOL> pass <EOL> not_removed = set ( self . removed ) - really_removed <EOL> if not_removed : <EOL> raise NodeDoesNotExistError ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( not_removed [ <NUM_LIT:0> ] , parents ) ) <EOL> def commit ( self , message , author , parents = None , branch = None , date = None , <EOL> ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError <EOL> class EmptyChangeset ( BaseChangeset ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , cs = '<STR_LIT:0>' * <NUM_LIT> , repo = None , requested_revision = None , <EOL> alias = None , revision = - <NUM_LIT:1> , message = '<STR_LIT>' , author = '<STR_LIT>' , date = None ) : <EOL> self . _empty_cs = cs <EOL> self . revision = revision <EOL> self . message = message <EOL> self . author = author <EOL> self . date = date or datetime . datetime . fromtimestamp ( <NUM_LIT:0> ) <EOL> self . repository = repo <EOL> self . requested_revision = requested_revision <EOL> self . alias = alias <EOL> @ LazyProperty <EOL> def raw_id ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _empty_cs <EOL> @ LazyProperty <EOL> def branch ( self ) : <EOL> from vcs . backends import get_backend <EOL> return get_backend ( self . alias ) . DEFAULT_BRANCH_NAME <EOL> @ LazyProperty <EOL> def short_id ( self ) : <EOL> return self . raw_id [ : <NUM_LIT:12> ] <EOL> def get_file_changeset ( self , path ) : <EOL> return self <EOL> def get_file_content ( self , path ) : <EOL> return u'<STR_LIT>' <EOL> def get_file_size ( self , path ) : <EOL> return <NUM_LIT:0> <EOL> class CollectionGenerator ( object ) : <EOL> def __init__ ( self , repo , revs ) : <EOL> self . repo = repo <EOL> self . revs = revs <EOL> def __len__ ( self ) : <EOL> return len ( self . revs ) <EOL> def __iter__ ( self ) : <EOL> for rev in self . revs : <EOL> yield self . repo . get_changeset ( rev ) <EOL> def __getslice__ ( self , i , j ) : <EOL> """<STR_LIT>""" <EOL> sliced_revs = self . revs [ i : j ] <EOL> return CollectionGenerator ( self . repo , sliced_revs ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( len ( self ) ) </s>
