def test_urlopen_netrc(self): mockopen = Mock() mock_result = Mock() backopen = vcstools.common.urlopen backget = vcstools.common._netrc_open try: #monkey-patch with mocks vcstools.common.urlopen = mockopen vcstools.common._netrc_open = Mock() vcstools.common._netrc_open.return_value = mock_result ioe = IOError('MockError') mockopen.side_effect = ioe self.assertRaises(IOError, urlopen_netrc, 'foo') ioe.code = 401 result = urlopen_netrc('foo') self.assertEqual(mock_result, result) finally: vcstools.common.urlopen = backopen vcstools.common._netrc_open = backget
def get_yaml_from_uri(uri): """reads and parses yaml from a local file or remote uri""" stream = None try: try: if os.path.isfile(uri): try: stream = open(uri, 'r') except IOError as ioe: raise MultiProjectException("Unable open file [%s]: %s" % (uri, ioe)) else: try: stream = urlopen_netrc(uri) except IOError as ioe2: raise MultiProjectException( "Unable to download URL [%s]: %s" % (uri, ioe2)) except ValueError as vae: raise MultiProjectException( "Is not a local file, nor a valid URL [%s] : %s" % (uri, vae)) if not stream: raise MultiProjectException("couldn't load config uri %s" % uri) try: yamldata = yaml.load(stream) except yaml.YAMLError as yame: raise MultiProjectException( "Invalid multiproject yaml format in [%s]: %s" % (uri, yame)) # we want a list or a dict, but pyyaml parses xml as string if type(yamldata) == 'str': raise MultiProjectException( "Invalid multiproject yaml format in [%s]: %s" % (uri, yamldata)) finally: if stream is not None: stream.close() return yamldata
def get_yaml_from_uri(uri): """reads and parses yaml from a local file or remote uri""" stream = None try: try: if os.path.isfile(uri): try: stream = open(uri, 'r') except IOError as ioe: raise MultiProjectException( "Unable open file [%s]: %s" % (uri, ioe)) else: try: stream = urlopen_netrc(uri) except IOError as ioe2: raise MultiProjectException( "Unable to download URL [%s]: %s" % (uri, ioe2)) except ValueError as vae: raise MultiProjectException( "Is not a local file, nor a valid URL [%s] : %s" % (uri, vae)) if not stream: raise MultiProjectException("couldn't load config uri %s" % uri) try: yamldata = yaml.load(stream) except yaml.YAMLError as yame: raise MultiProjectException( "Invalid multiproject yaml format in [%s]: %s" % (uri, yame)) # we want a list or a dict, but pyyaml parses xml as string if type(yamldata) == 'str': raise MultiProjectException( "Invalid multiproject yaml format in [%s]: %s" % (uri, yamldata)) finally: if stream is not None: stream.close() return yamldata