def loadAdditionalConfig(config_path): ''' returns (error, config) ''' error = None config = {} if not config_path: return (error, config) if os.path.isfile(config_path): try: config = ordered_json.load(config_path) except Exception as e: error = "Invalid syntax in file %s: %s" % (config_path, e) else: # try to interpret the argument as literal JSON try: config = ordered_json.loads(config_path) except Exception as e: # if this fails too, guess whether it was intended to be JSON or # not, and display an appropriate error message if '{' in config_path or '}' in config_path: error = "Invalid syntax in literal JSON: %s" % e else: error = "File \"%s\" does not exist" % config_path logger.debug('read additional config: %s', config) return (error, config)
def listOwners(namespace, name, registry=None): ''' List the owners of a module or target (owners are the people with permission to publish versions and add/remove the owners). ''' registry = registry or Registry_Base_URL url = '%s/%s/%s/owners' % ( registry, namespace, name ) request_headers = _headersForRegistry(registry) response = requests.get(url, headers=request_headers) if response.status_code == 404: logger.error('no such %s, "%s"' % (namespace[:-1], name)) return None # raise exceptions for other errors - the auth decorators handle these and # re-try if appropriate response.raise_for_status() return ordered_json.loads(response.text)
def getAuthData(): ''' Poll the registry to get the result of a completed authentication (which, depending on the authentication the user chose or was directed to, will include a github or other access token) ''' url = '%s/tokens' % ( Registry_Base_URL ) headers = { } auth = _registryAuthFilter() resource = Resource(url, pool=connection_pool.getPool(), filters=[auth]) try: logger.debug('poll for tokens...') response = resource.get( headers = headers ) except restkit_errors.Unauthorized as e: logger.debug(str(e)) return None except restkit_errors.ResourceNotFound as e: logger.debug(str(e)) return None except restkit_errors.RequestFailed as e: logger.debug(str(e)) return None body = response.body_string() logger.debug('auth data response: %s' % body); r = {} for token in ordered_json.loads(body): if token['provider'] == 'github': r['github'] = token['accessToken'] break logger.debug('parsed auth tokens %s' % r); return r
def search(query='', keywords=[], registry=None): ''' generator of objects returned by the search endpoint (both modules and targets). Query is a full-text search (description, name, keywords), keywords search only the module/target description keywords lists. If both parameters are specified the search is the intersection of the two queries. ''' registry = registry or Registry_Base_URL url = '%s/search' % registry headers = _headersForRegistry(registry) params = { 'skip': 0, 'limit': 50 } if len(query): params['query'] = query if len(keywords): params['keywords[]'] = keywords while True: response = requests.get(url, headers=headers, params=params) response.raise_for_status() objects = ordered_json.loads(response.text) if len(objects): for o in objects: yield o params['skip'] += params['limit'] else: break
def search(query='', keywords=[], registry=None): ''' generator of objects returned by the search endpoint (both modules and targets). Query is a full-text search (description, name, keywords), keywords search only the module/target description keywords lists. If both parameters are specified the search is the intersection of the two queries. ''' registry = registry or Registry_Base_URL url = '%s/search' % registry headers = _headersForRegistry(registry) params = {'skip': 0, 'limit': 50} if len(query): params['query'] = query if len(keywords): params['keywords[]'] = keywords while True: response = requests.get(url, headers=headers, params=params) response.raise_for_status() objects = ordered_json.loads(response.text) if len(objects): for o in objects: yield o params['skip'] += params['limit'] else: break
def whoami(registry=None): registry = registry or Registry_Base_URL url = '%s/users/me' % (registry) request_headers = _headersForRegistry(registry) logger.debug('test login...') response = requests.get(url, headers=request_headers) if response.status_code == 401: # not logged in return None elif response.status_code != 200: logger.error('error getting user information: %s', response.error) return None return ', '.join( ordered_json.loads(response.text).get('primary_emails', {}).values())
def whoami(registry=None): registry = registry or Registry_Base_URL url = '%s/users/me' % ( registry ) request_headers = _headersForRegistry(registry) logger.debug('test login...') response = requests.get(url, headers=request_headers) if response.status_code == 401: # not logged in return None elif response.status_code != 200: logger.error('error getting user information: %s', response.error) return None return ', '.join(ordered_json.loads(response.text).get('primary_emails', {}).values())
def listOwners(namespace, name): ''' List the owners of a module or target (owners are the people with permission to publish versions and add/remove the owners). ''' url = '%s/%s/%s/owners' % ( Registry_Base_URL, namespace, name ) auth = _registryAuthFilter() resource = Resource(url, pool=connection_pool.getPool(), filters=[auth]) try: response = resource.get() except restkit_errors.ResourceNotFound as e: logger.error('no such %s, "%s"' % (namespace, name)) return None return ordered_json.loads(response.body_string())
def getAuthData(): ''' Poll the registry to get the result of a completed authentication (which, depending on the authentication the user chose or was directed to, will include a github or other access token) ''' url = '%s/tokens' % (Registry_Base_URL) headers = {} auth_token = generate_jwt_token(_getPrivateKeyObject()) request_headers = {'Authorization': 'Bearer %s' % auth_token} logger.debug('poll for tokens...') try: response = requests.get(url, headers=request_headers) except requests.RequestException as e: logger.debug(str(e)) return None if response.status_code == requests.codes.unauthorized: logger.debug('Unauthorised') return None elif response.status_code == requests.codes.not_found: logger.debug('Not Found') return None body = response.text logger.debug('auth data response: %s' % body) r = {} json_body = ordered_json.loads(body) if 'error' in json_body: raise Exception(json_body['error']) for token in json_body: if token['provider'] == 'github': r['github'] = token['accessToken'] break logger.debug('parsed auth tokens %s' % r) return r
def getAuthData(registry=None): ''' Poll the registry to get the result of a completed authentication (which, depending on the authentication the user chose or was directed to, will include a github or other access token) ''' registry = registry or Registry_Base_URL url = '%s/tokens' % ( registry ) request_headers = _headersForRegistry(registry) logger.debug('poll for tokens... %s', request_headers) try: response = requests.get(url, headers=request_headers) except requests.RequestException as e: logger.debug(str(e)) return None if response.status_code == requests.codes.unauthorized: #pylint: disable=no-member logger.debug('Unauthorised') return None elif response.status_code == requests.codes.not_found: #pylint: disable=no-member logger.debug('Not Found') return None body = response.text logger.debug('auth data response: %s' % body); r = {} parsed_response = ordered_json.loads(body) if 'error' in parsed_response: raise AuthError(parsed_response['error']) for token in parsed_response: if token['provider'] == 'github': r['github'] = token['accessToken'] break logger.debug('parsed auth tokens %s' % r); return r
def getAuthData(registry=None): """ Poll the registry to get the result of a completed authentication (which, depending on the authentication the user chose or was directed to, will include a github or other access token) """ registry = registry or Registry_Base_URL url = "%s/tokens" % (registry) request_headers = _headersForRegistry(registry) logger.debug("poll for tokens... %s", request_headers) try: response = requests.get(url, headers=request_headers) except requests.RequestException as e: logger.debug(str(e)) return None if response.status_code == requests.codes.unauthorized: logger.debug("Unauthorised") return None elif response.status_code == requests.codes.not_found: logger.debug("Not Found") return None body = response.text logger.debug("auth data response: %s" % body) r = {} parsed_response = ordered_json.loads(body) if "error" in parsed_response: raise AuthError(parsed_response["error"]) for token in parsed_response: if token["provider"] == "github": r["github"] = token["accessToken"] break logger.debug("parsed auth tokens %s" % r) return r
def _listVersions(namespace, name): sources = _getSources() registry_urls = [s['url'] for s in sources if 'type' in s and s['type'] == 'registry'] # look in the public registry last registry_urls.append(Registry_Base_URL) versions = [] for registry in registry_urls: # list versions of the package: url = '%s/%s/%s/versions' % ( registry, namespace, name ) request_headers = _headersForRegistry(registry) logger.debug("GET %s, %s", url, request_headers) response = requests.get(url, headers=request_headers) if response.status_code == 404: continue # raise any other HTTP errors response.raise_for_status() for x in ordered_json.loads(response.text): rtv = RegistryThingVersion(x, namespace, name, registry=registry) if not rtv in versions: versions.append(rtv) if not len(versions): raise access_common.Unavailable( ('%s does not exist in the %s registry. '+ 'Check that the name is correct, and that it has been published.') % (name, namespace) ) return versions
def getAuthData(registry=None): ''' Poll the registry to get the result of a completed authentication (which, depending on the authentication the user chose or was directed to, will include a github or other access token) ''' registry = registry or Registry_Base_URL url = '%s/tokens' % (registry) request_headers = _headersForRegistry(registry) logger.debug('poll for tokens... %s', request_headers) try: response = requests.get(url, headers=request_headers) except requests.RequestException as e: logger.debug(str(e)) return None if response.status_code == requests.codes.unauthorized: logger.debug('Unauthorised') return None elif response.status_code == requests.codes.not_found: logger.debug('Not Found') return None body = response.text logger.debug('auth data response: %s' % body) r = {} parsed_response = ordered_json.loads(body) if 'error' in parsed_response: raise AuthError(parsed_response['error']) for token in parsed_response: if token['provider'] == 'github': r['github'] = token['accessToken'] break logger.debug('parsed auth tokens %s' % r) return r
def listOwners(namespace, name): ''' List the owners of a module or target (owners are the people with permission to publish versions and add/remove the owners). ''' url = '%s/%s/%s/owners' % (Registry_Base_URL, namespace, name) auth_token = generate_jwt_token(_getPrivateKeyObject()) request_headers = {'Authorization': 'Bearer %s' % auth_token} response = requests.get(url, headers=request_headers) if response.status_code == 404: logger.error('no such %s, "%s"' % (namespace[:-1], name)) return None # raise exceptions for other errors - the auth decorators handle these and # re-try if appropriate response.raise_for_status() return ordered_json.loads(response.text)
def _listVersions(namespace, name): # list versions of the package: url = '%s/%s/%s/versions' % ( Registry_Base_URL, namespace, name ) headers = { } auth = _registryAuthFilter() resource = Resource(url, pool=connection_pool.getPool(), filters=[auth]) try: logger.info('get versions for ' + name) response = resource.get( headers = headers ) except restkit_errors.ResourceNotFound as e: raise access_common.ComponentUnavailable( '%s does not exist in the %s registry' % (name, namespace) ) body_s = response.body_string() return [RegistryThingVersion(x, namespace, name) for x in ordered_json.loads(body_s)]
def listOwners(namespace, name, registry=None): ''' List the owners of a module or target (owners are the people with permission to publish versions and add/remove the owners). ''' registry = registry or Registry_Base_URL url = '%s/%s/%s/owners' % (registry, namespace, name) request_headers = _headersForRegistry(registry) response = requests.get(url, headers=request_headers) if response.status_code == 404: logger.error('no such %s, "%s"' % (namespace[:-1], name)) return [] # raise exceptions for other errors - the auth decorators handle these and # re-try if appropriate response.raise_for_status() return ordered_json.loads(response.text)
def _listVersions(namespace, name): sources = _getSources() registry_urls = [ s['url'] for s in sources if 'type' in s and s['type'] == 'registry' ] # look in the public registry last registry_urls.append(Registry_Base_URL) versions = [] for registry in registry_urls: # list versions of the package: url = '%s/%s/%s/versions' % (registry, namespace, name) request_headers = _headersForRegistry(registry) logger.debug("GET %s, %s", url, request_headers) response = requests.get(url, headers=request_headers) if response.status_code == 404: continue # raise any other HTTP errors response.raise_for_status() for x in ordered_json.loads(response.text): rtv = RegistryThingVersion(x, namespace, name, registry=registry) if not rtv in versions: versions.append(rtv) if not len(versions): raise access_common.Unavailable( ('%s does not exist in the %s registry. ' + 'Check that the name is correct, and that it has been published.') % (name, namespace)) return versions
def _listVersions(namespace, name): # list versions of the package: url = '%s/%s/%s/versions' % (Registry_Base_URL, namespace, name) auth_token = generate_jwt_token(_getPrivateKeyObject()) request_headers = {'Authorization': 'Bearer %s' % auth_token} response = requests.get(url, headers=request_headers) if response.status_code == 404: raise access_common.ComponentUnavailable( ('%s does not exist in the %s registry. ' + 'Check that the name is correct, and that it has been published.') % (name, namespace)) # raise any other HTTP errors response.raise_for_status() return [ RegistryThingVersion(x, namespace, name) for x in ordered_json.loads(response.text) ]