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 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 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 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 else: response.raise_for_status() 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 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 else: response.raise_for_status() 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 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 'provider' in token and token['provider'] and 'accessToken' in token: r[token['provider']] = 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: #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 'provider' in token and token['provider'] and 'accessToken' in token: r[token['provider']] = token['accessToken'] break logger.debug('parsed auth tokens %s' % r) return r
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 _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