def service(self, service: str, data: bytes) -> IO: proc = Runner(service)\ .arg('--stateless-rpc')\ .arg(self.path)\ .popen(stdin=PIPE, stdout=PIPE) try: data, _ = proc.communicate(data) finally: proc.wait() return io.BytesIO(data)
def init(path: str) -> 'Git': Runner('git')\ .arg('init')\ .arg('--bare')\ .arg(path)\ .run(check=True) return Git(path)
async def ipfsinforefs(qmhash: str, service: Service): qmhash = f'Qm{qmhash}' path = Path(TEMPDIR.name, qmhash) repo = Git(path) Runner('ipfs')\ .arg('get', qmhash)\ .arg('--output', path)\ .run(check=True) data = repo.inforefs(service.value) media = f'application/x-{service.value}-advertisement' return StreamingResponse(data, media_type=media)
def inforefs(self, service: str) -> IO: result = Runner(service)\ .arg('--stateless-rpc')\ .arg('--advertise-refs')\ .arg(self.path)\ .run(check=True, capture_output=True) # Adapted from: # https://github.com/schacon/grack/blob/master/lib/grack.rb data = b'# service=' + service.encode() datalen = len(data) + 4 datalen = b'%04x' % datalen data = datalen + data + b'0000' + result.stdout return io.BytesIO(data)
def test_init(self): """ Adds command to args """ runner = Runner('ls') self.assertListEqual(runner._args, ['ls'])
def test_open(self, Popen): """ Opens process """ Runner('ls').popen(bufsize=0) Popen.assert_called_once_with(['ls'], env=None, bufsize=0)
def test_run(self, run): """ Runs process """ Runner('ls').run(check=True) run.assert_called_once_with(['ls'], env=None, check=True)
def test_env(self): """ Adds kwargs to environment """ runner = Runner('ls')\ .env(KEY='key') self.assertDictEqual(runner._env, {'KEY': 'key'})
def test_arg(self): """ Adds arguments to args list """ runner = Runner('ls')\ .arg('-h')\ .arg('-l') self.assertListEqual(runner._args, ['ls', '-h', '-l'])