<s> import os <EOL> import sys <EOL> import coverage <EOL> from coverage . report import Reporter <EOL> from pulsar import new_event_loop <EOL> from pulsar . apps . http import HttpClient <EOL> from pulsar . utils . system import json <EOL> from pulsar . utils . version import gitrepo <EOL> COVERALLS_URL = '<STR_LIT>' <EOL> class CoverallsReporter ( Reporter ) : <EOL> def report ( self , morfs ) : <EOL> self . ret = [ ] <EOL> self . report_files ( self . _coverall , morfs ) <EOL> return self . ret <EOL> def _coverall ( self , fr , analysis ) : <EOL> try : <EOL> with open ( fr . filename ) as fp : <EOL> source = fp . readlines ( ) <EOL> except IOError : <EOL> if not self . config . ignore_errors : <EOL> raise <EOL> if self . config . strip_dirs : <EOL> filename = fr . filename <EOL> for dir in self . config . strip_dirs : <EOL> if filename . startswith ( dir ) : <EOL> filename = filename . replace ( dir , '<STR_LIT>' ) . lstrip ( '<STR_LIT:/>' ) <EOL> break <EOL> else : <EOL> filename = fr . relname <EOL> self . ret . append ( { <EOL> '<STR_LIT:name>' : filename , <EOL> '<STR_LIT:source>' : '<STR_LIT>' . join ( source ) . rstrip ( ) , <EOL> '<STR_LIT>' : list ( self . _coverage_list ( source , analysis ) ) , <EOL> } ) <EOL> def _coverage_list ( self , source , analysis ) : <EOL> for lineno , line in enumerate ( source , <NUM_LIT:1> ) : <EOL> if lineno in analysis . statements : <EOL> yield int ( lineno not in analysis . missing ) <EOL> else : <EOL> yield None <EOL> class Coverage ( coverage . Coverage ) : <EOL> def coveralls ( self , morfs = None , strip_dirs = None , ** kw ) : <EOL> self . config . from_args ( ** kw ) <EOL> self . config . strip_dirs = strip_dirs <EOL> reporter = CoverallsReporter ( self , self . config ) <EOL> return reporter . report ( morfs ) <EOL> def coveralls ( http = None , url = None , <EOL> data_file = None , repo_token = None , <EOL> git = None , service_name = None , <EOL> service_job_id = None , strip_dirs = None , <EOL> ignore_errors = False , stream = None ) : <EOL> '''<STR_LIT>''' <EOL> if repo_token is None and os . path . isfile ( '<STR_LIT>' ) : <EOL> with open ( '<STR_LIT>' ) as f : <EOL> repo_token = f . read ( ) . strip ( ) <EOL> if strip_dirs is None : <EOL> strip_dirs = [ os . getcwd ( ) ] <EOL> stream = stream or sys . stdout <EOL> coverage = Coverage ( data_file = data_file ) <EOL> coverage . load ( ) <EOL> if http is None : <EOL> http = HttpClient ( loop = new_event_loop ( ) ) <EOL> if not git : <EOL> try : <EOL> git = gitrepo ( ) <EOL> except Exception : <EOL> pass <EOL> data = { '<STR_LIT>' : coverage . coveralls ( strip_dirs = strip_dirs , <EOL> ignore_errors = ignore_errors ) } <EOL> if git : <EOL> data [ '<STR_LIT>' ] = git <EOL> if os . environ . get ( '<STR_LIT>' ) : <EOL> data [ '<STR_LIT>' ] = service_name or '<STR_LIT>' <EOL> data [ '<STR_LIT>' ] = os . environ . get ( '<STR_LIT>' ) <EOL> else : <EOL> assert repo_token , '<STR_LIT>' <EOL> if repo_token : <EOL> data [ '<STR_LIT>' ] = repo_token <EOL> url = url or COVERALLS_URL <EOL> stream . write ( '<STR_LIT>' % url ) <EOL> response = http . post ( url , files = { '<STR_LIT>' : json . dumps ( data ) } ) <EOL> stream . write ( '<STR_LIT>' % response . status_code ) <EOL> try : <EOL> info = response . json ( ) <EOL> code = <NUM_LIT:0> <EOL> if '<STR_LIT:error>' in info : <EOL> stream . write ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> code = <NUM_LIT:1> <EOL> stream . write ( '<STR_LIT>' % info [ '<STR_LIT:message>' ] ) <EOL> except Exception : <EOL> code = <NUM_LIT:1> <EOL> stream . write ( '<STR_LIT>' % response . status_code ) <EOL> return code </s>
