<s> """<STR_LIT>""" <EOL> import os , sys , json , subprocess , re , json <EOL> import pipes , collections <EOL> import shutil <EOL> from sh import git <EOL> from dgitcore . plugins . repomanager import RepoManagerBase , Repo <EOL> from dgitcore . helper import cd <EOL> from dgitcore . exceptions import * <EOL> class GitRepoManager ( RepoManagerBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . username = None <EOL> self . workspace = None <EOL> self . metadatadir = '<STR_LIT>' <EOL> self . repos = { } <EOL> self . enable = True <EOL> super ( GitRepoManager , self ) . __init__ ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> "<STR_LIT>" ) <EOL> def _run ( self , cmd ) : <EOL> """<STR_LIT>""" <EOL> environ = os . environ . copy ( ) <EOL> environ [ '<STR_LIT>' ] = self . fullname <EOL> environ [ '<STR_LIT>' ] = self . email <EOL> environ [ '<STR_LIT>' ] = self . fullname <EOL> environ [ '<STR_LIT>' ] = self . email <EOL> cmd = [ pipes . quote ( c ) for c in cmd ] <EOL> cmd = "<STR_LIT:U+0020>" . join ( [ '<STR_LIT>' ] + cmd ) <EOL> cmd += "<STR_LIT>" <EOL> try : <EOL> output = subprocess . check_output ( cmd , <EOL> stderr = subprocess . STDOUT , <EOL> shell = True , <EOL> env = environ ) <EOL> except subprocess . CalledProcessError as e : <EOL> output = e . output <EOL> output = output . decode ( '<STR_LIT:utf-8>' ) <EOL> output = output . strip ( ) <EOL> return output <EOL> def _run_generic_command ( self , repo , cmd ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> with cd ( repo . rootdir ) : <EOL> output = self . _run ( cmd ) <EOL> try : <EOL> result = { <EOL> '<STR_LIT>' : cmd , <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT:message>' : output , <EOL> } <EOL> except Exception as e : <EOL> result = { <EOL> '<STR_LIT>' : cmd , <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : str ( e ) <EOL> } <EOL> return result <EOL> def notes ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def push ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def remote ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def pull ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def status ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT:status>" ] + args ) <EOL> def stash ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def diff ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def log ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def commit ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def show ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> return self . _run_generic_command ( repo , [ "<STR_LIT>" ] + args ) <EOL> def init ( self , username , reponame , force , backend = None ) : <EOL> """<STR_LIT>""" <EOL> key = self . key ( username , reponame ) <EOL> server_repodir = self . server_rootdir ( username , <EOL> reponame , <EOL> create = False ) <EOL> if os . path . exists ( server_repodir ) and not force : <EOL> raise RepositoryExists ( ) <EOL> if os . path . exists ( server_repodir ) : <EOL> shutil . rmtree ( server_repodir ) <EOL> os . makedirs ( server_repodir ) <EOL> with cd ( server_repodir ) : <EOL> git . init ( "<STR_LIT:.>" , "<STR_LIT>" ) <EOL> if backend is not None : <EOL> backend . init_repo ( server_repodir ) <EOL> repodir = self . rootdir ( username , reponame , create = False ) <EOL> if os . path . exists ( repodir ) and not force : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if os . path . exists ( repodir ) : <EOL> shutil . rmtree ( repodir ) <EOL> os . makedirs ( repodir ) <EOL> with cd ( os . path . dirname ( repodir ) ) : <EOL> git . clone ( server_repodir , '<STR_LIT>' ) <EOL> url = server_repodir <EOL> if backend is not None : <EOL> url = backend . url ( username , reponame ) <EOL> repo = Repo ( username , reponame ) <EOL> repo . manager = self <EOL> repo . remoteurl = url <EOL> repo . rootdir = self . rootdir ( username , reponame ) <EOL> self . add ( repo ) <EOL> return repo <EOL> def clone ( self , url , backend = None ) : <EOL> """<STR_LIT>""" <EOL> username = self . username <EOL> reponame = url . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] <EOL> reponame = reponame . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> key = ( username , reponame ) <EOL> server_repodir = self . server_rootdir ( username , <EOL> reponame , <EOL> create = False ) <EOL> rootdir = self . rootdir ( username , reponame , create = False ) <EOL> if backend is None : <EOL> with cd ( os . path . dirname ( rootdir ) ) : <EOL> self . _run ( [ '<STR_LIT>' , '<STR_LIT>' , url ] ) <EOL> else : <EOL> if not os . path . exists ( server_repodir ) : <EOL> backend . clone_repo ( url , server_repodir ) <EOL> with cd ( os . path . dirname ( rootdir ) ) : <EOL> self . _run ( [ '<STR_LIT>' , '<STR_LIT>' , server_repodir ] ) <EOL> if True : <EOL> configfile = os . path . join ( rootdir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> content = open ( configfile ) . read ( ) <EOL> original = "<STR_LIT>" <EOL> replacement = """<STR_LIT>""" <EOL> if "<STR_LIT>" not in content : <EOL> content = content . replace ( original , replacement ) <EOL> with open ( configfile , '<STR_LIT:w>' ) as fd : <EOL> fd . write ( content ) <EOL> with cd ( rootdir ) : <EOL> self . _run ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> r = Repo ( username , reponame ) <EOL> r . rootdir = rootdir <EOL> r . remoteurl = url <EOL> r . manager = self <EOL> package = os . path . join ( r . rootdir , '<STR_LIT>' ) <EOL> packagedata = open ( package ) . read ( ) <EOL> r . package = json . JSONDecoder ( object_pairs_hook = collections . OrderedDict ) . decode ( packagedata ) <EOL> return self . add ( r ) <EOL> def delete ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> result = None <EOL> with cd ( repo . rootdir ) : <EOL> try : <EOL> cmd = [ '<STR_LIT>' ] + list ( args ) <EOL> result = { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT:message>' : self . _run ( cmd ) <EOL> } <EOL> except Exception as e : <EOL> result = { <EOL> '<STR_LIT:status>' : '<STR_LIT:error>' , <EOL> '<STR_LIT:message>' : str ( e ) <EOL> } <EOL> return result <EOL> def drop ( self , repo , args = [ ] ) : <EOL> """<STR_LIT>""" <EOL> rootdir = repo . rootdir <EOL> if os . path . exists ( rootdir ) : <EOL> print ( "<STR_LIT>" . format ( rootdir ) ) <EOL> shutil . rmtree ( rootdir ) <EOL> server_repodir = self . server_rootdir_from_repo ( repo , <EOL> create = False ) <EOL> if os . path . exists ( server_repodir ) : <EOL> print ( "<STR_LIT>" . format ( server_repodir ) ) <EOL> shutil . rmtree ( server_repodir ) <EOL> super ( GitRepoManager , self ) . drop ( repo ) <EOL> return { <EOL> '<STR_LIT:status>' : '<STR_LIT:success>' , <EOL> '<STR_LIT:message>' : "<STR_LIT>" <EOL> } <EOL> def permalink ( self , repo , path ) : <EOL> """<STR_LIT>""" <EOL> if not os . path . exists ( path ) : <EOL> return ( None , None ) <EOL> cwd = os . getcwd ( ) <EOL> if os . path . isfile ( path ) : <EOL> os . chdir ( os . path . dirname ( path ) ) <EOL> rootdir = self . _run ( [ "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> if "<STR_LIT>" in rootdir : <EOL> return ( None , None ) <EOL> os . chdir ( rootdir ) <EOL> relpath = os . path . relpath ( path , rootdir ) <EOL> sha1 = self . _run ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT:1>" , "<STR_LIT>" , relpath ] ) <EOL> remoteurl = self . _run ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) <EOL> os . chdir ( cwd ) <EOL> m = re . search ( '<STR_LIT>' , remoteurl ) <EOL> if m is None : <EOL> m = re . search ( '<STR_LIT>' , remoteurl ) <EOL> if m is not None : <EOL> domain = m . group ( <NUM_LIT:1> ) <EOL> username = m . group ( <NUM_LIT:2> ) <EOL> project = m . group ( <NUM_LIT:3> ) <EOL> if project . endswith ( "<STR_LIT>" ) : <EOL> project = project [ : - <NUM_LIT:4> ] <EOL> permalink = "<STR_LIT>" . format ( domain , username , project , <EOL> sha1 , relpath ) <EOL> return ( relpath , permalink ) <EOL> else : <EOL> return ( None , None ) <EOL> def add_raw ( self , repo , files ) : <EOL> result = None <EOL> with cd ( repo . rootdir ) : <EOL> try : <EOL> result = self . _run ( [ "<STR_LIT>" ] + files ) <EOL> except : <EOL> pass <EOL> def add_files ( self , repo , files ) : <EOL> """<STR_LIT>""" <EOL> rootdir = repo . rootdir <EOL> for f in files : <EOL> relativepath = f [ '<STR_LIT>' ] <EOL> sourcepath = f [ '<STR_LIT>' ] <EOL> if sourcepath is None : <EOL> continue <EOL> targetpath = os . path . join ( rootdir , relativepath ) <EOL> try : <EOL> os . makedirs ( os . path . dirname ( targetpath ) ) <EOL> except : <EOL> pass <EOL> print ( "<STR_LIT>" . format ( relativepath ) ) <EOL> shutil . copyfile ( sourcepath , targetpath ) <EOL> with cd ( repo . rootdir ) : <EOL> self . _run ( [ '<STR_LIT>' , relativepath ] ) <EOL> def config ( self , what = '<STR_LIT>' , params = None ) : <EOL> """<STR_LIT>""" <EOL> if what == '<STR_LIT>' : <EOL> return { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : [ ] , <EOL> } <EOL> elif what == '<STR_LIT>' : <EOL> self . workspace = params [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . workspace = os . path . abspath ( self . workspace ) <EOL> self . username = params [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . fullname = params [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> self . email = params [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> repodir = os . path . join ( self . workspace , '<STR_LIT>' ) <EOL> if not os . path . exists ( repodir ) : <EOL> return <EOL> for username in os . listdir ( repodir ) : <EOL> for reponame in os . listdir ( os . path . join ( repodir , username ) ) : <EOL> if self . is_my_repo ( username , reponame ) : <EOL> r = Repo ( username , reponame ) <EOL> r . rootdir = os . path . join ( repodir , username , reponame ) <EOL> package = os . path . join ( r . rootdir , '<STR_LIT>' ) <EOL> if not os . path . exists ( package ) : <EOL> print ( "<STR_LIT>" ) <EOL> print ( "<STR_LIT>" . format ( username , reponame ) ) <EOL> continue <EOL> packagedata = open ( package ) . read ( ) <EOL> r . package = json . JSONDecoder ( object_pairs_hook = collections . OrderedDict ) . decode ( packagedata ) <EOL> r . manager = self <EOL> self . add ( r ) <EOL> def setup ( mgr ) : <EOL> obj = GitRepoManager ( ) <EOL> mgr . register ( '<STR_LIT>' , obj ) </s>
