id
int32
0
252k
repo
stringlengths
7
55
path
stringlengths
4
127
func_name
stringlengths
1
88
original_string
stringlengths
75
19.8k
language
stringclasses
1 value
code
stringlengths
75
19.8k
code_tokens
list
docstring
stringlengths
3
17.3k
docstring_tokens
list
sha
stringlengths
40
40
url
stringlengths
87
242
225,900
RedHatInsights/insights-core
insights/client/support.py
InsightsSupport._support_diag_dump
def _support_diag_dump(self): ''' Collect log info for debug ''' # check insights config cfg_block = [] pconn = InsightsConnection(self.config) logger.info('Insights version: %s', get_nvr()) reg_check = registration_check(pconn) cfg_block.append('Registration check:') for key in reg_check: cfg_block.append(key + ': ' + str(reg_check[key])) lastupload = 'never' if os.path.isfile(constants.lastupload_file): with open(constants.lastupload_file) as upl_file: lastupload = upl_file.readline().strip() cfg_block.append('\nLast successful upload was ' + lastupload) cfg_block.append('auto_config: ' + str(self.config.auto_config)) if self.config.proxy: obfuscated_proxy = re.sub(r'(.*)(:)(.*)(@.*)', r'\1\2********\4', self.config.proxy) else: obfuscated_proxy = 'None' cfg_block.append('proxy: ' + obfuscated_proxy) logger.info('\n'.join(cfg_block)) logger.info('python-requests: %s', requests.__version__) succ = pconn.test_connection() if succ == 0: logger.info('Connection test: PASS\n') else: logger.info('Connection test: FAIL\n') # run commands commands = ['uname -a', 'cat /etc/redhat-release', 'env', 'sestatus', 'subscription-manager identity', 'systemctl cat insights-client.timer', 'systemctl cat insights-client.service', 'systemctl status insights-client.timer', 'systemctl status insights-client.service'] for cmd in commands: logger.info("Running command: %s", cmd) try: proc = Popen( shlex.split(cmd), shell=False, stdout=PIPE, stderr=STDOUT, close_fds=True) stdout, stderr = proc.communicate() except OSError as o: if 'systemctl' not in cmd: # suppress output for systemctl cmd failures logger.info('Error running command "%s": %s', cmd, o) except Exception as e: # unknown error logger.info("Process failed: %s", e) logger.info("Process output: \n%s", stdout) # check available disk space for /var/tmp tmp_dir = '/var/tmp' dest_dir_stat = os.statvfs(tmp_dir) dest_dir_size = (dest_dir_stat.f_bavail * dest_dir_stat.f_frsize) logger.info('Available space in %s:\t%s bytes\t%.1f 1K-blocks\t%.1f MB', tmp_dir, dest_dir_size, dest_dir_size / 1024.0, (dest_dir_size / 1024.0) / 1024.0)
python
def _support_diag_dump(self): ''' Collect log info for debug ''' # check insights config cfg_block = [] pconn = InsightsConnection(self.config) logger.info('Insights version: %s', get_nvr()) reg_check = registration_check(pconn) cfg_block.append('Registration check:') for key in reg_check: cfg_block.append(key + ': ' + str(reg_check[key])) lastupload = 'never' if os.path.isfile(constants.lastupload_file): with open(constants.lastupload_file) as upl_file: lastupload = upl_file.readline().strip() cfg_block.append('\nLast successful upload was ' + lastupload) cfg_block.append('auto_config: ' + str(self.config.auto_config)) if self.config.proxy: obfuscated_proxy = re.sub(r'(.*)(:)(.*)(@.*)', r'\1\2********\4', self.config.proxy) else: obfuscated_proxy = 'None' cfg_block.append('proxy: ' + obfuscated_proxy) logger.info('\n'.join(cfg_block)) logger.info('python-requests: %s', requests.__version__) succ = pconn.test_connection() if succ == 0: logger.info('Connection test: PASS\n') else: logger.info('Connection test: FAIL\n') # run commands commands = ['uname -a', 'cat /etc/redhat-release', 'env', 'sestatus', 'subscription-manager identity', 'systemctl cat insights-client.timer', 'systemctl cat insights-client.service', 'systemctl status insights-client.timer', 'systemctl status insights-client.service'] for cmd in commands: logger.info("Running command: %s", cmd) try: proc = Popen( shlex.split(cmd), shell=False, stdout=PIPE, stderr=STDOUT, close_fds=True) stdout, stderr = proc.communicate() except OSError as o: if 'systemctl' not in cmd: # suppress output for systemctl cmd failures logger.info('Error running command "%s": %s', cmd, o) except Exception as e: # unknown error logger.info("Process failed: %s", e) logger.info("Process output: \n%s", stdout) # check available disk space for /var/tmp tmp_dir = '/var/tmp' dest_dir_stat = os.statvfs(tmp_dir) dest_dir_size = (dest_dir_stat.f_bavail * dest_dir_stat.f_frsize) logger.info('Available space in %s:\t%s bytes\t%.1f 1K-blocks\t%.1f MB', tmp_dir, dest_dir_size, dest_dir_size / 1024.0, (dest_dir_size / 1024.0) / 1024.0)
[ "def", "_support_diag_dump", "(", "self", ")", ":", "# check insights config", "cfg_block", "=", "[", "]", "pconn", "=", "InsightsConnection", "(", "self", ".", "config", ")", "logger", ".", "info", "(", "'Insights version: %s'", ",", "get_nvr", "(", ")", ")",...
Collect log info for debug
[ "Collect", "log", "info", "for", "debug" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/support.py#L94-L165
225,901
RedHatInsights/insights-core
insights/core/filters.py
add_filter
def add_filter(ds, patterns): """ Add a filter or list of filters to a datasource. A filter is a simple string, and it matches if it is contained anywhere within a line. Args: ds (@datasource component): The datasource to filter patterns (str, [str]): A string, list of strings, or set of strings to add to the datasource's filters. """ if not plugins.is_datasource(ds): raise Exception("Filters are applicable only to datasources.") delegate = dr.get_delegate(ds) if delegate.raw: raise Exception("Filters aren't applicable to raw datasources.") if not delegate.filterable: raise Exception("Filters aren't applicable to %s." % dr.get_name(ds)) if ds in _CACHE: del _CACHE[ds] if isinstance(patterns, six.string_types): FILTERS[ds].add(patterns) elif isinstance(patterns, list): FILTERS[ds] |= set(patterns) elif isinstance(patterns, set): FILTERS[ds] |= patterns else: raise TypeError("patterns must be string, list, or set.")
python
def add_filter(ds, patterns): """ Add a filter or list of filters to a datasource. A filter is a simple string, and it matches if it is contained anywhere within a line. Args: ds (@datasource component): The datasource to filter patterns (str, [str]): A string, list of strings, or set of strings to add to the datasource's filters. """ if not plugins.is_datasource(ds): raise Exception("Filters are applicable only to datasources.") delegate = dr.get_delegate(ds) if delegate.raw: raise Exception("Filters aren't applicable to raw datasources.") if not delegate.filterable: raise Exception("Filters aren't applicable to %s." % dr.get_name(ds)) if ds in _CACHE: del _CACHE[ds] if isinstance(patterns, six.string_types): FILTERS[ds].add(patterns) elif isinstance(patterns, list): FILTERS[ds] |= set(patterns) elif isinstance(patterns, set): FILTERS[ds] |= patterns else: raise TypeError("patterns must be string, list, or set.")
[ "def", "add_filter", "(", "ds", ",", "patterns", ")", ":", "if", "not", "plugins", ".", "is_datasource", "(", "ds", ")", ":", "raise", "Exception", "(", "\"Filters are applicable only to datasources.\"", ")", "delegate", "=", "dr", ".", "get_delegate", "(", "d...
Add a filter or list of filters to a datasource. A filter is a simple string, and it matches if it is contained anywhere within a line. Args: ds (@datasource component): The datasource to filter patterns (str, [str]): A string, list of strings, or set of strings to add to the datasource's filters.
[ "Add", "a", "filter", "or", "list", "of", "filters", "to", "a", "datasource", ".", "A", "filter", "is", "a", "simple", "string", "and", "it", "matches", "if", "it", "is", "contained", "anywhere", "within", "a", "line", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L49-L79
225,902
RedHatInsights/insights-core
insights/core/filters.py
get_filters
def get_filters(component): """ Get the set of filters for the given datasource. Filters added to a ``RegistryPoint`` will be applied to all datasources that implement it. Filters added to a datasource implementation apply only to that implementation. For example, a filter added to ``Specs.ps_auxww`` will apply to ``DefaultSpecs.ps_auxww``, ``InsightsArchiveSpecs.ps_auxww``, ``SosSpecs.ps_auxww``, etc. But a filter added to ``DefaultSpecs.ps_auxww`` will only apply to ``DefaultSpecs.ps_auxww``. See the modules in ``insights.specs`` for those classes. Args: component (a datasource): The target datasource Returns: set: The set of filters defined for the datasource """ def inner(c, filters=None): filters = filters or set() if not ENABLED: return filters if not plugins.is_datasource(c): return filters if c in FILTERS: filters |= FILTERS[c] for d in dr.get_dependents(c): filters |= inner(d, filters) return filters if component not in _CACHE: _CACHE[component] = inner(component) return _CACHE[component]
python
def get_filters(component): """ Get the set of filters for the given datasource. Filters added to a ``RegistryPoint`` will be applied to all datasources that implement it. Filters added to a datasource implementation apply only to that implementation. For example, a filter added to ``Specs.ps_auxww`` will apply to ``DefaultSpecs.ps_auxww``, ``InsightsArchiveSpecs.ps_auxww``, ``SosSpecs.ps_auxww``, etc. But a filter added to ``DefaultSpecs.ps_auxww`` will only apply to ``DefaultSpecs.ps_auxww``. See the modules in ``insights.specs`` for those classes. Args: component (a datasource): The target datasource Returns: set: The set of filters defined for the datasource """ def inner(c, filters=None): filters = filters or set() if not ENABLED: return filters if not plugins.is_datasource(c): return filters if c in FILTERS: filters |= FILTERS[c] for d in dr.get_dependents(c): filters |= inner(d, filters) return filters if component not in _CACHE: _CACHE[component] = inner(component) return _CACHE[component]
[ "def", "get_filters", "(", "component", ")", ":", "def", "inner", "(", "c", ",", "filters", "=", "None", ")", ":", "filters", "=", "filters", "or", "set", "(", ")", "if", "not", "ENABLED", ":", "return", "filters", "if", "not", "plugins", ".", "is_da...
Get the set of filters for the given datasource. Filters added to a ``RegistryPoint`` will be applied to all datasources that implement it. Filters added to a datasource implementation apply only to that implementation. For example, a filter added to ``Specs.ps_auxww`` will apply to ``DefaultSpecs.ps_auxww``, ``InsightsArchiveSpecs.ps_auxww``, ``SosSpecs.ps_auxww``, etc. But a filter added to ``DefaultSpecs.ps_auxww`` will only apply to ``DefaultSpecs.ps_auxww``. See the modules in ``insights.specs`` for those classes. Args: component (a datasource): The target datasource Returns: set: The set of filters defined for the datasource
[ "Get", "the", "set", "of", "filters", "for", "the", "given", "datasource", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L82-L119
225,903
RedHatInsights/insights-core
insights/core/filters.py
apply_filters
def apply_filters(target, lines): """ Applys filters to the lines of a datasource. This function is used only in integration tests. Filters are applied in an equivalent but more performant way at run time. """ filters = get_filters(target) if filters: for l in lines: if any(f in l for f in filters): yield l else: for l in lines: yield l
python
def apply_filters(target, lines): """ Applys filters to the lines of a datasource. This function is used only in integration tests. Filters are applied in an equivalent but more performant way at run time. """ filters = get_filters(target) if filters: for l in lines: if any(f in l for f in filters): yield l else: for l in lines: yield l
[ "def", "apply_filters", "(", "target", ",", "lines", ")", ":", "filters", "=", "get_filters", "(", "target", ")", "if", "filters", ":", "for", "l", "in", "lines", ":", "if", "any", "(", "f", "in", "l", "for", "f", "in", "filters", ")", ":", "yield"...
Applys filters to the lines of a datasource. This function is used only in integration tests. Filters are applied in an equivalent but more performant way at run time.
[ "Applys", "filters", "to", "the", "lines", "of", "a", "datasource", ".", "This", "function", "is", "used", "only", "in", "integration", "tests", ".", "Filters", "are", "applied", "in", "an", "equivalent", "but", "more", "performant", "way", "at", "run", "t...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L122-L135
225,904
RedHatInsights/insights-core
insights/core/filters.py
loads
def loads(string): """Loads the filters dictionary given a string.""" d = _loads(string) for k, v in d.items(): FILTERS[dr.get_component(k) or k] = set(v)
python
def loads(string): """Loads the filters dictionary given a string.""" d = _loads(string) for k, v in d.items(): FILTERS[dr.get_component(k) or k] = set(v)
[ "def", "loads", "(", "string", ")", ":", "d", "=", "_loads", "(", "string", ")", "for", "k", ",", "v", "in", "d", ".", "items", "(", ")", ":", "FILTERS", "[", "dr", ".", "get_component", "(", "k", ")", "or", "k", "]", "=", "set", "(", "v", ...
Loads the filters dictionary given a string.
[ "Loads", "the", "filters", "dictionary", "given", "a", "string", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L143-L147
225,905
RedHatInsights/insights-core
insights/core/filters.py
load
def load(stream=None): """ Loads filters from a stream, normally an open file. If one is not passed, filters are loaded from a default location within the project. """ if stream: loads(stream.read()) else: data = pkgutil.get_data(insights.__name__, _filename) return loads(data) if data else None
python
def load(stream=None): """ Loads filters from a stream, normally an open file. If one is not passed, filters are loaded from a default location within the project. """ if stream: loads(stream.read()) else: data = pkgutil.get_data(insights.__name__, _filename) return loads(data) if data else None
[ "def", "load", "(", "stream", "=", "None", ")", ":", "if", "stream", ":", "loads", "(", "stream", ".", "read", "(", ")", ")", "else", ":", "data", "=", "pkgutil", ".", "get_data", "(", "insights", ".", "__name__", ",", "_filename", ")", "return", "...
Loads filters from a stream, normally an open file. If one is not passed, filters are loaded from a default location within the project.
[ "Loads", "filters", "from", "a", "stream", "normally", "an", "open", "file", ".", "If", "one", "is", "not", "passed", "filters", "are", "loaded", "from", "a", "default", "location", "within", "the", "project", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L150-L160
225,906
RedHatInsights/insights-core
insights/core/filters.py
dumps
def dumps(): """Returns a string representation of the FILTERS dictionary.""" d = {} for k, v in FILTERS.items(): d[dr.get_name(k)] = list(v) return _dumps(d)
python
def dumps(): """Returns a string representation of the FILTERS dictionary.""" d = {} for k, v in FILTERS.items(): d[dr.get_name(k)] = list(v) return _dumps(d)
[ "def", "dumps", "(", ")", ":", "d", "=", "{", "}", "for", "k", ",", "v", "in", "FILTERS", ".", "items", "(", ")", ":", "d", "[", "dr", ".", "get_name", "(", "k", ")", "]", "=", "list", "(", "v", ")", "return", "_dumps", "(", "d", ")" ]
Returns a string representation of the FILTERS dictionary.
[ "Returns", "a", "string", "representation", "of", "the", "FILTERS", "dictionary", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L163-L168
225,907
RedHatInsights/insights-core
insights/core/filters.py
dump
def dump(stream=None): """ Dumps a string representation of `FILTERS` to a stream, normally an open file. If none is passed, `FILTERS` is dumped to a default location within the project. """ if stream: stream.write(dumps()) else: path = os.path.join(os.path.dirname(insights.__file__), _filename) with open(path, "wu") as f: f.write(dumps())
python
def dump(stream=None): """ Dumps a string representation of `FILTERS` to a stream, normally an open file. If none is passed, `FILTERS` is dumped to a default location within the project. """ if stream: stream.write(dumps()) else: path = os.path.join(os.path.dirname(insights.__file__), _filename) with open(path, "wu") as f: f.write(dumps())
[ "def", "dump", "(", "stream", "=", "None", ")", ":", "if", "stream", ":", "stream", ".", "write", "(", "dumps", "(", ")", ")", "else", ":", "path", "=", "os", ".", "path", ".", "join", "(", "os", ".", "path", ".", "dirname", "(", "insights", "....
Dumps a string representation of `FILTERS` to a stream, normally an open file. If none is passed, `FILTERS` is dumped to a default location within the project.
[ "Dumps", "a", "string", "representation", "of", "FILTERS", "to", "a", "stream", "normally", "an", "open", "file", ".", "If", "none", "is", "passed", "FILTERS", "is", "dumped", "to", "a", "default", "location", "within", "the", "project", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/filters.py#L171-L182
225,908
RedHatInsights/insights-core
insights/parsers/grub_conf.py
_parse_script
def _parse_script(list, line, line_iter): """ Eliminate any bash script contained in the grub v2 configuration """ ifIdx = 0 while (True): line = next(line_iter) if line.startswith("fi"): if ifIdx == 0: return ifIdx -= 1 elif line.startswith("if"): ifIdx += 1
python
def _parse_script(list, line, line_iter): """ Eliminate any bash script contained in the grub v2 configuration """ ifIdx = 0 while (True): line = next(line_iter) if line.startswith("fi"): if ifIdx == 0: return ifIdx -= 1 elif line.startswith("if"): ifIdx += 1
[ "def", "_parse_script", "(", "list", ",", "line", ",", "line_iter", ")", ":", "ifIdx", "=", "0", "while", "(", "True", ")", ":", "line", "=", "next", "(", "line_iter", ")", "if", "line", ".", "startswith", "(", "\"fi\"", ")", ":", "if", "ifIdx", "=...
Eliminate any bash script contained in the grub v2 configuration
[ "Eliminate", "any", "bash", "script", "contained", "in", "the", "grub", "v2", "configuration" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/grub_conf.py#L397-L409
225,909
RedHatInsights/insights-core
insights/parsers/grub_conf.py
_parse_title
def _parse_title(line_iter, cur_line, conf): """ Parse "title" in grub v1 config """ title = [] conf['title'].append(title) title.append(('title_name', cur_line.split('title', 1)[1].strip())) while (True): line = next(line_iter) if line.startswith("title "): return line cmd, opt = _parse_cmd(line) title.append((cmd, opt))
python
def _parse_title(line_iter, cur_line, conf): """ Parse "title" in grub v1 config """ title = [] conf['title'].append(title) title.append(('title_name', cur_line.split('title', 1)[1].strip())) while (True): line = next(line_iter) if line.startswith("title "): return line cmd, opt = _parse_cmd(line) title.append((cmd, opt))
[ "def", "_parse_title", "(", "line_iter", ",", "cur_line", ",", "conf", ")", ":", "title", "=", "[", "]", "conf", "[", "'title'", "]", ".", "append", "(", "title", ")", "title", ".", "append", "(", "(", "'title_name'", ",", "cur_line", ".", "split", "...
Parse "title" in grub v1 config
[ "Parse", "title", "in", "grub", "v1", "config" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/grub_conf.py#L447-L460
225,910
RedHatInsights/insights-core
insights/parsers/grub_conf.py
GrubConfig.is_kdump_iommu_enabled
def is_kdump_iommu_enabled(self): """ Does any kernel have 'intel_iommu=on' set? Returns: (bool): ``True`` when 'intel_iommu=on' is set, otherwise returns ``False`` """ for line in self._boot_entries: if line.cmdline and IOMMU in line.cmdline: return True return False
python
def is_kdump_iommu_enabled(self): """ Does any kernel have 'intel_iommu=on' set? Returns: (bool): ``True`` when 'intel_iommu=on' is set, otherwise returns ``False`` """ for line in self._boot_entries: if line.cmdline and IOMMU in line.cmdline: return True return False
[ "def", "is_kdump_iommu_enabled", "(", "self", ")", ":", "for", "line", "in", "self", ".", "_boot_entries", ":", "if", "line", ".", "cmdline", "and", "IOMMU", "in", "line", ".", "cmdline", ":", "return", "True", "return", "False" ]
Does any kernel have 'intel_iommu=on' set? Returns: (bool): ``True`` when 'intel_iommu=on' is set, otherwise returns ``False``
[ "Does", "any", "kernel", "have", "intel_iommu", "=", "on", "set?" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/grub_conf.py#L177-L188
225,911
RedHatInsights/insights-core
insights/parsers/grub_conf.py
GrubConfig.kernel_initrds
def kernel_initrds(self): """ Get the `kernel` and `initrd` files referenced in GRUB configuration files Returns: (dict): Returns a dict of the `kernel` and `initrd` files referenced in GRUB configuration files """ kernels = [] initrds = [] name_values = [(k, v) for k, v in self.data.get('configs', [])] for value in self.data.get('title', []) + self.data.get('menuentry', []): name_values.extend(value) for name, value in name_values: if name.startswith('module'): if 'vmlinuz' in value: kernels.append(_parse_kernel_initrds_value(value)) elif 'initrd' in value or 'initramfs' in value: initrds.append(_parse_kernel_initrds_value(value)) elif (name.startswith(('kernel', 'linux'))): if 'ipxe.lkrn' in value: # Machine PXE boots the kernel, assume all is ok return {} elif 'xen.gz' not in value: kernels.append(_parse_kernel_initrds_value(value)) elif name.startswith('initrd') or name.startswith('initrd16'): initrds.append(_parse_kernel_initrds_value(value)) return {GRUB_KERNELS: kernels, GRUB_INITRDS: initrds}
python
def kernel_initrds(self): """ Get the `kernel` and `initrd` files referenced in GRUB configuration files Returns: (dict): Returns a dict of the `kernel` and `initrd` files referenced in GRUB configuration files """ kernels = [] initrds = [] name_values = [(k, v) for k, v in self.data.get('configs', [])] for value in self.data.get('title', []) + self.data.get('menuentry', []): name_values.extend(value) for name, value in name_values: if name.startswith('module'): if 'vmlinuz' in value: kernels.append(_parse_kernel_initrds_value(value)) elif 'initrd' in value or 'initramfs' in value: initrds.append(_parse_kernel_initrds_value(value)) elif (name.startswith(('kernel', 'linux'))): if 'ipxe.lkrn' in value: # Machine PXE boots the kernel, assume all is ok return {} elif 'xen.gz' not in value: kernels.append(_parse_kernel_initrds_value(value)) elif name.startswith('initrd') or name.startswith('initrd16'): initrds.append(_parse_kernel_initrds_value(value)) return {GRUB_KERNELS: kernels, GRUB_INITRDS: initrds}
[ "def", "kernel_initrds", "(", "self", ")", ":", "kernels", "=", "[", "]", "initrds", "=", "[", "]", "name_values", "=", "[", "(", "k", ",", "v", ")", "for", "k", ",", "v", "in", "self", ".", "data", ".", "get", "(", "'configs'", ",", "[", "]", ...
Get the `kernel` and `initrd` files referenced in GRUB configuration files Returns: (dict): Returns a dict of the `kernel` and `initrd` files referenced in GRUB configuration files
[ "Get", "the", "kernel", "and", "initrd", "files", "referenced", "in", "GRUB", "configuration", "files" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/grub_conf.py#L192-L222
225,912
RedHatInsights/insights-core
insights/core/serde.py
serializer
def serializer(_type): """ Decorator for serializers. A serializer should accept two parameters: An object and a path which is a directory on the filesystem where supplementary data can be stored. This is most often useful for datasources. It should return a dictionary version of the original object that contains only elements that can be serialized to json. """ def inner(func): name = dr.get_name(_type) if name in SERIALIZERS: msg = "%s already has a serializer registered: %s" raise Exception(msg % (name, dr.get_name(SERIALIZERS[name]))) SERIALIZERS[name] = func return func return inner
python
def serializer(_type): """ Decorator for serializers. A serializer should accept two parameters: An object and a path which is a directory on the filesystem where supplementary data can be stored. This is most often useful for datasources. It should return a dictionary version of the original object that contains only elements that can be serialized to json. """ def inner(func): name = dr.get_name(_type) if name in SERIALIZERS: msg = "%s already has a serializer registered: %s" raise Exception(msg % (name, dr.get_name(SERIALIZERS[name]))) SERIALIZERS[name] = func return func return inner
[ "def", "serializer", "(", "_type", ")", ":", "def", "inner", "(", "func", ")", ":", "name", "=", "dr", ".", "get_name", "(", "_type", ")", "if", "name", "in", "SERIALIZERS", ":", "msg", "=", "\"%s already has a serializer registered: %s\"", "raise", "Excepti...
Decorator for serializers. A serializer should accept two parameters: An object and a path which is a directory on the filesystem where supplementary data can be stored. This is most often useful for datasources. It should return a dictionary version of the original object that contains only elements that can be serialized to json.
[ "Decorator", "for", "serializers", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/serde.py#L26-L44
225,913
RedHatInsights/insights-core
insights/core/serde.py
deserializer
def deserializer(_type): """ Decorator for deserializers. A deserializer should accept three parameters: A type, a dictionary, and a path that may contain supplementary data stored by its paired serializer. If the serializer stores supplementary data, the relative path to it should be somewhere in the dict of the second parameter. """ def inner(func): name = dr.get_name(_type) if name in DESERIALIZERS: msg = "%s already has a deserializer registered: %s" raise Exception(msg % (dr.get_name(name), dr.get_name(DESERIALIZERS[name]))) DESERIALIZERS[name] = (_type, func) return func return inner
python
def deserializer(_type): """ Decorator for deserializers. A deserializer should accept three parameters: A type, a dictionary, and a path that may contain supplementary data stored by its paired serializer. If the serializer stores supplementary data, the relative path to it should be somewhere in the dict of the second parameter. """ def inner(func): name = dr.get_name(_type) if name in DESERIALIZERS: msg = "%s already has a deserializer registered: %s" raise Exception(msg % (dr.get_name(name), dr.get_name(DESERIALIZERS[name]))) DESERIALIZERS[name] = (_type, func) return func return inner
[ "def", "deserializer", "(", "_type", ")", ":", "def", "inner", "(", "func", ")", ":", "name", "=", "dr", ".", "get_name", "(", "_type", ")", "if", "name", "in", "DESERIALIZERS", ":", "msg", "=", "\"%s already has a deserializer registered: %s\"", "raise", "E...
Decorator for deserializers. A deserializer should accept three parameters: A type, a dictionary, and a path that may contain supplementary data stored by its paired serializer. If the serializer stores supplementary data, the relative path to it should be somewhere in the dict of the second parameter.
[ "Decorator", "for", "deserializers", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/serde.py#L47-L64
225,914
RedHatInsights/insights-core
insights/core/serde.py
Hydration.hydrate
def hydrate(self, broker=None): """ Loads a Broker from a previously saved one. A Broker is created if one isn't provided. """ broker = broker or dr.Broker() for path in glob(os.path.join(self.meta_data, "*")): try: with open(path) as f: doc = ser.load(f) res = self._hydrate_one(doc) comp, results, exec_time, ser_time = res if results: broker[comp] = results broker.exec_times[comp] = exec_time + ser_time except Exception as ex: log.warning(ex) return broker
python
def hydrate(self, broker=None): """ Loads a Broker from a previously saved one. A Broker is created if one isn't provided. """ broker = broker or dr.Broker() for path in glob(os.path.join(self.meta_data, "*")): try: with open(path) as f: doc = ser.load(f) res = self._hydrate_one(doc) comp, results, exec_time, ser_time = res if results: broker[comp] = results broker.exec_times[comp] = exec_time + ser_time except Exception as ex: log.warning(ex) return broker
[ "def", "hydrate", "(", "self", ",", "broker", "=", "None", ")", ":", "broker", "=", "broker", "or", "dr", ".", "Broker", "(", ")", "for", "path", "in", "glob", "(", "os", ".", "path", ".", "join", "(", "self", ".", "meta_data", ",", "\"*\"", ")",...
Loads a Broker from a previously saved one. A Broker is created if one isn't provided.
[ "Loads", "a", "Broker", "from", "a", "previously", "saved", "one", ".", "A", "Broker", "is", "created", "if", "one", "isn", "t", "provided", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/serde.py#L144-L161
225,915
RedHatInsights/insights-core
insights/core/serde.py
Hydration.dehydrate
def dehydrate(self, comp, broker): """ Saves a component in the given broker to the file system. """ if not self.meta_data: raise Exception("Hydration meta_path not set. Can't dehydrate.") if not self.created: fs.ensure_path(self.meta_data, mode=0o770) if self.data: fs.ensure_path(self.data, mode=0o770) self.created = True c = comp doc = None try: name = dr.get_name(c) value = broker.get(c) errors = [t for e in broker.exceptions.get(c, []) for t in broker.tracebacks[e]] doc = { "name": name, "exec_time": broker.exec_times.get(c), "errors": errors } try: start = time.time() doc["results"] = marshal(value, root=self.data, pool=self.pool) except Exception: errors.append(traceback.format_exc()) log.debug(traceback.format_exc()) doc["results"] = None finally: doc["ser_time"] = time.time() - start except Exception as ex: log.exception(ex) else: if doc is not None and (doc["results"] or doc["errors"]): try: path = os.path.join(self.meta_data, name + "." + self.ser_name) with open(path, "w") as f: ser.dump(doc, f) except Exception as boom: log.error("Could not serialize %s to %s: %r" % (name, self.ser_name, boom)) if path: fs.remove(path)
python
def dehydrate(self, comp, broker): """ Saves a component in the given broker to the file system. """ if not self.meta_data: raise Exception("Hydration meta_path not set. Can't dehydrate.") if not self.created: fs.ensure_path(self.meta_data, mode=0o770) if self.data: fs.ensure_path(self.data, mode=0o770) self.created = True c = comp doc = None try: name = dr.get_name(c) value = broker.get(c) errors = [t for e in broker.exceptions.get(c, []) for t in broker.tracebacks[e]] doc = { "name": name, "exec_time": broker.exec_times.get(c), "errors": errors } try: start = time.time() doc["results"] = marshal(value, root=self.data, pool=self.pool) except Exception: errors.append(traceback.format_exc()) log.debug(traceback.format_exc()) doc["results"] = None finally: doc["ser_time"] = time.time() - start except Exception as ex: log.exception(ex) else: if doc is not None and (doc["results"] or doc["errors"]): try: path = os.path.join(self.meta_data, name + "." + self.ser_name) with open(path, "w") as f: ser.dump(doc, f) except Exception as boom: log.error("Could not serialize %s to %s: %r" % (name, self.ser_name, boom)) if path: fs.remove(path)
[ "def", "dehydrate", "(", "self", ",", "comp", ",", "broker", ")", ":", "if", "not", "self", ".", "meta_data", ":", "raise", "Exception", "(", "\"Hydration meta_path not set. Can't dehydrate.\"", ")", "if", "not", "self", ".", "created", ":", "fs", ".", "ensu...
Saves a component in the given broker to the file system.
[ "Saves", "a", "component", "in", "the", "given", "broker", "to", "the", "file", "system", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/serde.py#L163-L209
225,916
RedHatInsights/insights-core
insights/core/serde.py
Hydration.make_persister
def make_persister(self, to_persist): """ Returns a function that hydrates components as they are evaluated. The function should be registered as an observer on a Broker just before execution. Args: to_persist (set): Set of components to persist. Skip everything else. """ if not self.meta_data: raise Exception("Root not set. Can't create persister.") def persister(c, broker): if c in to_persist: self.dehydrate(c, broker) return persister
python
def make_persister(self, to_persist): """ Returns a function that hydrates components as they are evaluated. The function should be registered as an observer on a Broker just before execution. Args: to_persist (set): Set of components to persist. Skip everything else. """ if not self.meta_data: raise Exception("Root not set. Can't create persister.") def persister(c, broker): if c in to_persist: self.dehydrate(c, broker) return persister
[ "def", "make_persister", "(", "self", ",", "to_persist", ")", ":", "if", "not", "self", ".", "meta_data", ":", "raise", "Exception", "(", "\"Root not set. Can't create persister.\"", ")", "def", "persister", "(", "c", ",", "broker", ")", ":", "if", "c", "in"...
Returns a function that hydrates components as they are evaluated. The function should be registered as an observer on a Broker just before execution. Args: to_persist (set): Set of components to persist. Skip everything else.
[ "Returns", "a", "function", "that", "hydrates", "components", "as", "they", "are", "evaluated", ".", "The", "function", "should", "be", "registered", "as", "an", "observer", "on", "a", "Broker", "just", "before", "execution", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/serde.py#L211-L228
225,917
RedHatInsights/insights-core
insights/parsers/lvm.py
map_keys
def map_keys(pvs, keys): """ Add human readable key names to dictionary while leaving any existing key names. """ rs = [] for pv in pvs: r = dict((v, None) for k, v in keys.items()) for k, v in pv.items(): if k in keys: r[keys[k]] = v r[k] = v rs.append(r) return rs
python
def map_keys(pvs, keys): """ Add human readable key names to dictionary while leaving any existing key names. """ rs = [] for pv in pvs: r = dict((v, None) for k, v in keys.items()) for k, v in pv.items(): if k in keys: r[keys[k]] = v r[k] = v rs.append(r) return rs
[ "def", "map_keys", "(", "pvs", ",", "keys", ")", ":", "rs", "=", "[", "]", "for", "pv", "in", "pvs", ":", "r", "=", "dict", "(", "(", "v", ",", "None", ")", "for", "k", ",", "v", "in", "keys", ".", "items", "(", ")", ")", "for", "k", ",",...
Add human readable key names to dictionary while leaving any existing key names.
[ "Add", "human", "readable", "key", "names", "to", "dictionary", "while", "leaving", "any", "existing", "key", "names", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/lvm.py#L43-L55
225,918
RedHatInsights/insights-core
insights/configtree/__init__.py
from_dict
def from_dict(dct): """ Convert a dictionary into a configtree. """ def inner(d): results = [] for name, v in d.items(): if isinstance(v, dict): results.append(Section(name=name, children=from_dict(v))) elif isinstance(v, list): if not any(isinstance(i, dict) for i in v): results.append(Directive(name=name, attrs=v)) else: for i in v: if isinstance(i, dict): results.append(Section(name=name, children=from_dict(i))) elif isinstance(i, list): results.append(Directive(name=name, attrs=i)) else: results.append(Directive(name=name, attrs=[i])) else: results.append(Directive(name, attrs=[v])) return results return Root(children=inner(dct))
python
def from_dict(dct): """ Convert a dictionary into a configtree. """ def inner(d): results = [] for name, v in d.items(): if isinstance(v, dict): results.append(Section(name=name, children=from_dict(v))) elif isinstance(v, list): if not any(isinstance(i, dict) for i in v): results.append(Directive(name=name, attrs=v)) else: for i in v: if isinstance(i, dict): results.append(Section(name=name, children=from_dict(i))) elif isinstance(i, list): results.append(Directive(name=name, attrs=i)) else: results.append(Directive(name=name, attrs=[i])) else: results.append(Directive(name, attrs=[v])) return results return Root(children=inner(dct))
[ "def", "from_dict", "(", "dct", ")", ":", "def", "inner", "(", "d", ")", ":", "results", "=", "[", "]", "for", "name", ",", "v", "in", "d", ".", "items", "(", ")", ":", "if", "isinstance", "(", "v", ",", "dict", ")", ":", "results", ".", "app...
Convert a dictionary into a configtree.
[ "Convert", "a", "dictionary", "into", "a", "configtree", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/configtree/__init__.py#L348-L369
225,919
RedHatInsights/insights-core
insights/configtree/__init__.py
__or
def __or(funcs, args): """ Support list sugar for "or" of two predicates. Used inside `select`. """ results = [] for f in funcs: result = f(args) if result: results.extend(result) return results
python
def __or(funcs, args): """ Support list sugar for "or" of two predicates. Used inside `select`. """ results = [] for f in funcs: result = f(args) if result: results.extend(result) return results
[ "def", "__or", "(", "funcs", ",", "args", ")", ":", "results", "=", "[", "]", "for", "f", "in", "funcs", ":", "result", "=", "f", "(", "args", ")", "if", "result", ":", "results", ".", "extend", "(", "result", ")", "return", "results" ]
Support list sugar for "or" of two predicates. Used inside `select`.
[ "Support", "list", "sugar", "for", "or", "of", "two", "predicates", ".", "Used", "inside", "select", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/configtree/__init__.py#L582-L589
225,920
RedHatInsights/insights-core
insights/configtree/__init__.py
BinaryBool
def BinaryBool(pred): """ Lifts predicates that take an argument into the DSL. """ class Predicate(Bool): def __init__(self, value, ignore_case=False): self.value = caseless(value) if ignore_case else value self.ignore_case = ignore_case def __call__(self, data): if not isinstance(data, list): data = [data] for d in data: try: if pred(caseless(d) if self.ignore_case else d, self.value): return True except: pass return False return Predicate
python
def BinaryBool(pred): """ Lifts predicates that take an argument into the DSL. """ class Predicate(Bool): def __init__(self, value, ignore_case=False): self.value = caseless(value) if ignore_case else value self.ignore_case = ignore_case def __call__(self, data): if not isinstance(data, list): data = [data] for d in data: try: if pred(caseless(d) if self.ignore_case else d, self.value): return True except: pass return False return Predicate
[ "def", "BinaryBool", "(", "pred", ")", ":", "class", "Predicate", "(", "Bool", ")", ":", "def", "__init__", "(", "self", ",", "value", ",", "ignore_case", "=", "False", ")", ":", "self", ".", "value", "=", "caseless", "(", "value", ")", "if", "ignore...
Lifts predicates that take an argument into the DSL.
[ "Lifts", "predicates", "that", "take", "an", "argument", "into", "the", "DSL", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/configtree/__init__.py#L652-L669
225,921
RedHatInsights/insights-core
insights/configtree/__init__.py
select
def select(*queries, **kwargs): """ Builds a function that will execute the specified queries against a list of Nodes. """ def make_query(*args): def simple_query(nodes): if len(args) == 0: return nodes pred = args[0] results = [] if isinstance(pred, list): funcs = [make_query(q) for q in pred] return __or(funcs, nodes) elif isinstance(pred, tuple): name, attrs = pred[0], pred[1:] name_pred = __make_name_pred(name) attrs_pred = __make_attrs_pred(attrs) for n in nodes: if name_pred(n.name) and attrs_pred(n.attrs): results.append(n) else: name_pred = __make_name_pred(pred) for n in nodes: if name_pred(n.name): results.append(n) return results if len(args) > 1: return __compose(make_query(*args[1:]), simple_query) return simple_query def deep_query(query, nodes): """ Slide the query down the branches. """ def inner(children): results = [] for c in children: if query([c]): results.append(c) results.extend(inner(c.children)) return results return inner(nodes) def unique(roots): seen = set() results = [] for r in roots: if r not in seen: seen.add(r) results.append(r) return results def compiled_query(nodes): """ This is the compiled query that can be run against a configuration. """ query = make_query(*queries) roots = kwargs.get("roots", True) if kwargs.get("deep", False): results = deep_query(query, nodes) if roots: results = unique([r.root for r in results]) elif roots: results = unique([n.root for n in query(nodes)]) else: results = query(nodes) one = kwargs.get("one") if one is None: return SearchResult(children=results) return results[one] if results else None return compiled_query
python
def select(*queries, **kwargs): """ Builds a function that will execute the specified queries against a list of Nodes. """ def make_query(*args): def simple_query(nodes): if len(args) == 0: return nodes pred = args[0] results = [] if isinstance(pred, list): funcs = [make_query(q) for q in pred] return __or(funcs, nodes) elif isinstance(pred, tuple): name, attrs = pred[0], pred[1:] name_pred = __make_name_pred(name) attrs_pred = __make_attrs_pred(attrs) for n in nodes: if name_pred(n.name) and attrs_pred(n.attrs): results.append(n) else: name_pred = __make_name_pred(pred) for n in nodes: if name_pred(n.name): results.append(n) return results if len(args) > 1: return __compose(make_query(*args[1:]), simple_query) return simple_query def deep_query(query, nodes): """ Slide the query down the branches. """ def inner(children): results = [] for c in children: if query([c]): results.append(c) results.extend(inner(c.children)) return results return inner(nodes) def unique(roots): seen = set() results = [] for r in roots: if r not in seen: seen.add(r) results.append(r) return results def compiled_query(nodes): """ This is the compiled query that can be run against a configuration. """ query = make_query(*queries) roots = kwargs.get("roots", True) if kwargs.get("deep", False): results = deep_query(query, nodes) if roots: results = unique([r.root for r in results]) elif roots: results = unique([n.root for n in query(nodes)]) else: results = query(nodes) one = kwargs.get("one") if one is None: return SearchResult(children=results) return results[one] if results else None return compiled_query
[ "def", "select", "(", "*", "queries", ",", "*", "*", "kwargs", ")", ":", "def", "make_query", "(", "*", "args", ")", ":", "def", "simple_query", "(", "nodes", ")", ":", "if", "len", "(", "args", ")", "==", "0", ":", "return", "nodes", "pred", "="...
Builds a function that will execute the specified queries against a list of Nodes.
[ "Builds", "a", "function", "that", "will", "execute", "the", "specified", "queries", "against", "a", "list", "of", "Nodes", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/configtree/__init__.py#L722-L793
225,922
RedHatInsights/insights-core
insights/configtree/__init__.py
Node.find
def find(self, *queries, **kwargs): """ Finds the first result found anywhere in the configuration. Pass `one=last` for the last result. Returns `None` if no results are found. """ kwargs["deep"] = True kwargs["roots"] = False if "one" not in kwargs: kwargs["one"] = first return self.select(*queries, **kwargs)
python
def find(self, *queries, **kwargs): """ Finds the first result found anywhere in the configuration. Pass `one=last` for the last result. Returns `None` if no results are found. """ kwargs["deep"] = True kwargs["roots"] = False if "one" not in kwargs: kwargs["one"] = first return self.select(*queries, **kwargs)
[ "def", "find", "(", "self", ",", "*", "queries", ",", "*", "*", "kwargs", ")", ":", "kwargs", "[", "\"deep\"", "]", "=", "True", "kwargs", "[", "\"roots\"", "]", "=", "False", "if", "\"one\"", "not", "in", "kwargs", ":", "kwargs", "[", "\"one\"", "...
Finds the first result found anywhere in the configuration. Pass `one=last` for the last result. Returns `None` if no results are found.
[ "Finds", "the", "first", "result", "found", "anywhere", "in", "the", "configuration", ".", "Pass", "one", "=", "last", "for", "the", "last", "result", ".", "Returns", "None", "if", "no", "results", "are", "found", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/configtree/__init__.py#L165-L174
225,923
RedHatInsights/insights-core
insights/configtree/__init__.py
Node.find_all
def find_all(self, *queries): """ Find all results matching the query anywhere in the configuration. Returns an empty `SearchResult` if no results are found. """ return self.select(*queries, deep=True, roots=False)
python
def find_all(self, *queries): """ Find all results matching the query anywhere in the configuration. Returns an empty `SearchResult` if no results are found. """ return self.select(*queries, deep=True, roots=False)
[ "def", "find_all", "(", "self", ",", "*", "queries", ")", ":", "return", "self", ".", "select", "(", "*", "queries", ",", "deep", "=", "True", ",", "roots", "=", "False", ")" ]
Find all results matching the query anywhere in the configuration. Returns an empty `SearchResult` if no results are found.
[ "Find", "all", "results", "matching", "the", "query", "anywhere", "in", "the", "configuration", ".", "Returns", "an", "empty", "SearchResult", "if", "no", "results", "are", "found", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/configtree/__init__.py#L176-L181
225,924
RedHatInsights/insights-core
insights/parsers/installed_rpms.py
pad_version
def pad_version(left, right): """Returns two sequences of the same length so that they can be compared. The shorter of the two arguments is lengthened by inserting extra zeros before non-integer components. The algorithm attempts to align character components.""" pair = vcmp(left), vcmp(right) mn, mx = min(pair, key=len), max(pair, key=len) for idx, c in enumerate(mx): try: a = mx[idx] b = mn[idx] if type(a) != type(b): mn.insert(idx, 0) except IndexError: if type(c) is int: mn.append(0) elif isinstance(c, six.string_types): mn.append('') else: raise Exception("pad_version failed (%s) (%s)" % (left, right)) return pair
python
def pad_version(left, right): """Returns two sequences of the same length so that they can be compared. The shorter of the two arguments is lengthened by inserting extra zeros before non-integer components. The algorithm attempts to align character components.""" pair = vcmp(left), vcmp(right) mn, mx = min(pair, key=len), max(pair, key=len) for idx, c in enumerate(mx): try: a = mx[idx] b = mn[idx] if type(a) != type(b): mn.insert(idx, 0) except IndexError: if type(c) is int: mn.append(0) elif isinstance(c, six.string_types): mn.append('') else: raise Exception("pad_version failed (%s) (%s)" % (left, right)) return pair
[ "def", "pad_version", "(", "left", ",", "right", ")", ":", "pair", "=", "vcmp", "(", "left", ")", ",", "vcmp", "(", "right", ")", "mn", ",", "mx", "=", "min", "(", "pair", ",", "key", "=", "len", ")", ",", "max", "(", "pair", ",", "key", "=",...
Returns two sequences of the same length so that they can be compared. The shorter of the two arguments is lengthened by inserting extra zeros before non-integer components. The algorithm attempts to align character components.
[ "Returns", "two", "sequences", "of", "the", "same", "length", "so", "that", "they", "can", "be", "compared", ".", "The", "shorter", "of", "the", "two", "arguments", "is", "lengthened", "by", "inserting", "extra", "zeros", "before", "non", "-", "integer", "...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/installed_rpms.py#L254-L278
225,925
RedHatInsights/insights-core
insights/parsers/installed_rpms.py
InstalledRpm._parse_package
def _parse_package(cls, package_string): """ Helper method for parsing package string. Args: package_string (str): dash separated package string such as 'bash-4.2.39-3.el7' Returns: dict: dictionary containing 'name', 'version', 'release' and 'arch' keys """ pkg, arch = rsplit(package_string, cls._arch_sep(package_string)) if arch not in KNOWN_ARCHITECTURES: pkg, arch = (package_string, None) pkg, release = rsplit(pkg, '-') name, version = rsplit(pkg, '-') epoch, version = version.split(':', 1) if ":" in version else ['0', version] # oracleasm packages have a dash in their version string, fix that if name.startswith('oracleasm') and name.endswith('.el5'): name, version2 = name.split('-', 1) version = version2 + '-' + version return { 'name': name, 'version': version, 'release': release, 'arch': arch, 'epoch': epoch }
python
def _parse_package(cls, package_string): """ Helper method for parsing package string. Args: package_string (str): dash separated package string such as 'bash-4.2.39-3.el7' Returns: dict: dictionary containing 'name', 'version', 'release' and 'arch' keys """ pkg, arch = rsplit(package_string, cls._arch_sep(package_string)) if arch not in KNOWN_ARCHITECTURES: pkg, arch = (package_string, None) pkg, release = rsplit(pkg, '-') name, version = rsplit(pkg, '-') epoch, version = version.split(':', 1) if ":" in version else ['0', version] # oracleasm packages have a dash in their version string, fix that if name.startswith('oracleasm') and name.endswith('.el5'): name, version2 = name.split('-', 1) version = version2 + '-' + version return { 'name': name, 'version': version, 'release': release, 'arch': arch, 'epoch': epoch }
[ "def", "_parse_package", "(", "cls", ",", "package_string", ")", ":", "pkg", ",", "arch", "=", "rsplit", "(", "package_string", ",", "cls", ".", "_arch_sep", "(", "package_string", ")", ")", "if", "arch", "not", "in", "KNOWN_ARCHITECTURES", ":", "pkg", ","...
Helper method for parsing package string. Args: package_string (str): dash separated package string such as 'bash-4.2.39-3.el7' Returns: dict: dictionary containing 'name', 'version', 'release' and 'arch' keys
[ "Helper", "method", "for", "parsing", "package", "string", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/installed_rpms.py#L431-L457
225,926
RedHatInsights/insights-core
insights/parsers/installed_rpms.py
InstalledRpm._parse_line
def _parse_line(cls, line): """ Helper method for parsing package line with or without SOS report information. Args: line (str): package line with or without SOS report information Returns: dict: dictionary containing 'name', 'version', 'release' and 'arch' keys plus additionally 'installtime', 'buildtime', 'vendor', 'buildserver', 'pgpsig', 'pgpsig_short' if these are present. """ try: pkg, rest = line.split(None, 1) except ValueError: rpm = cls._parse_package(line.strip()) return rpm rpm = cls._parse_package(pkg) rest = rest.split('\t') for i, value in enumerate(rest): rpm[cls.SOSREPORT_KEYS[i]] = value return rpm
python
def _parse_line(cls, line): """ Helper method for parsing package line with or without SOS report information. Args: line (str): package line with or without SOS report information Returns: dict: dictionary containing 'name', 'version', 'release' and 'arch' keys plus additionally 'installtime', 'buildtime', 'vendor', 'buildserver', 'pgpsig', 'pgpsig_short' if these are present. """ try: pkg, rest = line.split(None, 1) except ValueError: rpm = cls._parse_package(line.strip()) return rpm rpm = cls._parse_package(pkg) rest = rest.split('\t') for i, value in enumerate(rest): rpm[cls.SOSREPORT_KEYS[i]] = value return rpm
[ "def", "_parse_line", "(", "cls", ",", "line", ")", ":", "try", ":", "pkg", ",", "rest", "=", "line", ".", "split", "(", "None", ",", "1", ")", "except", "ValueError", ":", "rpm", "=", "cls", ".", "_parse_package", "(", "line", ".", "strip", "(", ...
Helper method for parsing package line with or without SOS report information. Args: line (str): package line with or without SOS report information Returns: dict: dictionary containing 'name', 'version', 'release' and 'arch' keys plus additionally 'installtime', 'buildtime', 'vendor', 'buildserver', 'pgpsig', 'pgpsig_short' if these are present.
[ "Helper", "method", "for", "parsing", "package", "line", "with", "or", "without", "SOS", "report", "information", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/installed_rpms.py#L460-L481
225,927
RedHatInsights/insights-core
insights/contrib/importlib.py
_resolve_name
def _resolve_name(name, package, level): """Return the absolute name of the module to be imported.""" if not hasattr(package, 'rindex'): raise ValueError("'package' not set to a string") dot = len(package) for x in xrange(level, 1, -1): try: dot = package.rindex('.', 0, dot) except ValueError: raise ValueError("attempted relative import beyond top-level " "package") return "%s.%s" % (package[:dot], name)
python
def _resolve_name(name, package, level): """Return the absolute name of the module to be imported.""" if not hasattr(package, 'rindex'): raise ValueError("'package' not set to a string") dot = len(package) for x in xrange(level, 1, -1): try: dot = package.rindex('.', 0, dot) except ValueError: raise ValueError("attempted relative import beyond top-level " "package") return "%s.%s" % (package[:dot], name)
[ "def", "_resolve_name", "(", "name", ",", "package", ",", "level", ")", ":", "if", "not", "hasattr", "(", "package", ",", "'rindex'", ")", ":", "raise", "ValueError", "(", "\"'package' not set to a string\"", ")", "dot", "=", "len", "(", "package", ")", "f...
Return the absolute name of the module to be imported.
[ "Return", "the", "absolute", "name", "of", "the", "module", "to", "be", "imported", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/contrib/importlib.py#L6-L17
225,928
RedHatInsights/insights-core
insights/parsers/krb5.py
_handle_key_value
def _handle_key_value(t_dict, key, value): """ Function to handle key has multi value, and return the values as list. """ if key in t_dict: val = t_dict[key] if isinstance(val, str): val = [val] val.append(value) return val return value
python
def _handle_key_value(t_dict, key, value): """ Function to handle key has multi value, and return the values as list. """ if key in t_dict: val = t_dict[key] if isinstance(val, str): val = [val] val.append(value) return val return value
[ "def", "_handle_key_value", "(", "t_dict", ",", "key", ",", "value", ")", ":", "if", "key", "in", "t_dict", ":", "val", "=", "t_dict", "[", "key", "]", "if", "isinstance", "(", "val", ",", "str", ")", ":", "val", "=", "[", "val", "]", "val", ".",...
Function to handle key has multi value, and return the values as list.
[ "Function", "to", "handle", "key", "has", "multi", "value", "and", "return", "the", "values", "as", "list", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/krb5.py#L63-L73
225,929
RedHatInsights/insights-core
insights/formats/__init__.py
get_formatter
def get_formatter(name): """ Looks up a formatter class given a prefix to it. The names are sorted, and the first matching class is returned. """ for k in sorted(_FORMATTERS): if k.startswith(name): return _FORMATTERS[k]
python
def get_formatter(name): """ Looks up a formatter class given a prefix to it. The names are sorted, and the first matching class is returned. """ for k in sorted(_FORMATTERS): if k.startswith(name): return _FORMATTERS[k]
[ "def", "get_formatter", "(", "name", ")", ":", "for", "k", "in", "sorted", "(", "_FORMATTERS", ")", ":", "if", "k", ".", "startswith", "(", "name", ")", ":", "return", "_FORMATTERS", "[", "k", "]" ]
Looks up a formatter class given a prefix to it. The names are sorted, and the first matching class is returned.
[ "Looks", "up", "a", "formatter", "class", "given", "a", "prefix", "to", "it", ".", "The", "names", "are", "sorted", "and", "the", "first", "matching", "class", "is", "returned", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/formats/__init__.py#L10-L17
225,930
RedHatInsights/insights-core
insights/parsers/nfs_exports.py
NFSExportsBase.all_options
def all_options(self): """Returns the set of all options used in all export entries""" items = chain.from_iterable(hosts.values() for hosts in self.data.values()) return set(chain.from_iterable(items))
python
def all_options(self): """Returns the set of all options used in all export entries""" items = chain.from_iterable(hosts.values() for hosts in self.data.values()) return set(chain.from_iterable(items))
[ "def", "all_options", "(", "self", ")", ":", "items", "=", "chain", ".", "from_iterable", "(", "hosts", ".", "values", "(", ")", "for", "hosts", "in", "self", ".", "data", ".", "values", "(", ")", ")", "return", "set", "(", "chain", ".", "from_iterab...
Returns the set of all options used in all export entries
[ "Returns", "the", "set", "of", "all", "options", "used", "in", "all", "export", "entries" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/nfs_exports.py#L133-L136
225,931
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection._init_session
def _init_session(self): """ Set up the session, auth is handled here """ session = requests.Session() session.headers = {'User-Agent': self.user_agent, 'Accept': 'application/json'} if self.systemid is not None: session.headers.update({'systemid': self.systemid}) if self.authmethod == "BASIC": session.auth = (self.username, self.password) elif self.authmethod == "CERT": cert = rhsmCertificate.certpath() key = rhsmCertificate.keypath() if rhsmCertificate.exists(): session.cert = (cert, key) else: logger.error('ERROR: Certificates not found.') session.verify = self.cert_verify session.proxies = self.proxies session.trust_env = False if self.proxy_auth: # HACKY try: # Need to make a request that will fail to get proxies set up net_logger.info("GET %s", self.base_url) session.request( "GET", self.base_url, timeout=self.config.http_timeout) except requests.ConnectionError: pass # Major hack, requests/urllib3 does not make access to # proxy_headers easy proxy_mgr = session.adapters['https://'].proxy_manager[self.proxies['https']] auth_map = {'Proxy-Authorization': self.proxy_auth} proxy_mgr.proxy_headers = auth_map proxy_mgr.connection_pool_kw['_proxy_headers'] = auth_map conns = proxy_mgr.pools._container for conn in conns: connection = conns[conn] connection.proxy_headers = auth_map return session
python
def _init_session(self): """ Set up the session, auth is handled here """ session = requests.Session() session.headers = {'User-Agent': self.user_agent, 'Accept': 'application/json'} if self.systemid is not None: session.headers.update({'systemid': self.systemid}) if self.authmethod == "BASIC": session.auth = (self.username, self.password) elif self.authmethod == "CERT": cert = rhsmCertificate.certpath() key = rhsmCertificate.keypath() if rhsmCertificate.exists(): session.cert = (cert, key) else: logger.error('ERROR: Certificates not found.') session.verify = self.cert_verify session.proxies = self.proxies session.trust_env = False if self.proxy_auth: # HACKY try: # Need to make a request that will fail to get proxies set up net_logger.info("GET %s", self.base_url) session.request( "GET", self.base_url, timeout=self.config.http_timeout) except requests.ConnectionError: pass # Major hack, requests/urllib3 does not make access to # proxy_headers easy proxy_mgr = session.adapters['https://'].proxy_manager[self.proxies['https']] auth_map = {'Proxy-Authorization': self.proxy_auth} proxy_mgr.proxy_headers = auth_map proxy_mgr.connection_pool_kw['_proxy_headers'] = auth_map conns = proxy_mgr.pools._container for conn in conns: connection = conns[conn] connection.proxy_headers = auth_map return session
[ "def", "_init_session", "(", "self", ")", ":", "session", "=", "requests", ".", "Session", "(", ")", "session", ".", "headers", "=", "{", "'User-Agent'", ":", "self", ".", "user_agent", ",", "'Accept'", ":", "'application/json'", "}", "if", "self", ".", ...
Set up the session, auth is handled here
[ "Set", "up", "the", "session", "auth", "is", "handled", "here" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L129-L169
225,932
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.handle_fail_rcs
def handle_fail_rcs(self, req): """ Bail out if we get a 401 and leave a message """ try: logger.debug("HTTP Status Code: %s", req.status_code) logger.debug("HTTP Response Text: %s", req.text) logger.debug("HTTP Response Reason: %s", req.reason) logger.debug("HTTP Response Content: %s", req.content) except: logger.error("Malformed HTTP Request.") # attempt to read the HTTP response JSON message try: logger.debug("HTTP Response Message: %s", req.json()["message"]) except: logger.debug("No HTTP Response message present.") # handle specific status codes if req.status_code >= 400: logger.info("Debug Information:\nHTTP Status Code: %s", req.status_code) logger.info("HTTP Status Text: %s", req.reason) if req.status_code == 401: logger.error("Authorization Required.") logger.error("Please ensure correct credentials " "in " + constants.default_conf_file) logger.debug("HTTP Response Text: %s", req.text) if req.status_code == 402: # failed registration because of entitlement limit hit logger.debug('Registration failed by 402 error.') try: logger.error(req.json()["message"]) except LookupError: logger.error("Got 402 but no message") logger.debug("HTTP Response Text: %s", req.text) except: logger.error("Got 402 but no message") logger.debug("HTTP Response Text: %s", req.text) if req.status_code == 403 and self.auto_config: # Insights disabled in satellite rhsm_hostname = urlparse(self.base_url).hostname if (rhsm_hostname != 'subscription.rhn.redhat.com' and rhsm_hostname != 'subscription.rhsm.redhat.com'): logger.error('Please enable Insights on Satellite server ' '%s to continue.', rhsm_hostname) if req.status_code == 412: try: unreg_date = req.json()["unregistered_at"] logger.error(req.json()["message"]) write_unregistered_file(unreg_date) except LookupError: unreg_date = "412, but no unreg_date or message" logger.debug("HTTP Response Text: %s", req.text) except: unreg_date = "412, but no unreg_date or message" logger.debug("HTTP Response Text: %s", req.text) return True return False
python
def handle_fail_rcs(self, req): """ Bail out if we get a 401 and leave a message """ try: logger.debug("HTTP Status Code: %s", req.status_code) logger.debug("HTTP Response Text: %s", req.text) logger.debug("HTTP Response Reason: %s", req.reason) logger.debug("HTTP Response Content: %s", req.content) except: logger.error("Malformed HTTP Request.") # attempt to read the HTTP response JSON message try: logger.debug("HTTP Response Message: %s", req.json()["message"]) except: logger.debug("No HTTP Response message present.") # handle specific status codes if req.status_code >= 400: logger.info("Debug Information:\nHTTP Status Code: %s", req.status_code) logger.info("HTTP Status Text: %s", req.reason) if req.status_code == 401: logger.error("Authorization Required.") logger.error("Please ensure correct credentials " "in " + constants.default_conf_file) logger.debug("HTTP Response Text: %s", req.text) if req.status_code == 402: # failed registration because of entitlement limit hit logger.debug('Registration failed by 402 error.') try: logger.error(req.json()["message"]) except LookupError: logger.error("Got 402 but no message") logger.debug("HTTP Response Text: %s", req.text) except: logger.error("Got 402 but no message") logger.debug("HTTP Response Text: %s", req.text) if req.status_code == 403 and self.auto_config: # Insights disabled in satellite rhsm_hostname = urlparse(self.base_url).hostname if (rhsm_hostname != 'subscription.rhn.redhat.com' and rhsm_hostname != 'subscription.rhsm.redhat.com'): logger.error('Please enable Insights on Satellite server ' '%s to continue.', rhsm_hostname) if req.status_code == 412: try: unreg_date = req.json()["unregistered_at"] logger.error(req.json()["message"]) write_unregistered_file(unreg_date) except LookupError: unreg_date = "412, but no unreg_date or message" logger.debug("HTTP Response Text: %s", req.text) except: unreg_date = "412, but no unreg_date or message" logger.debug("HTTP Response Text: %s", req.text) return True return False
[ "def", "handle_fail_rcs", "(", "self", ",", "req", ")", ":", "try", ":", "logger", ".", "debug", "(", "\"HTTP Status Code: %s\"", ",", "req", ".", "status_code", ")", "logger", ".", "debug", "(", "\"HTTP Response Text: %s\"", ",", "req", ".", "text", ")", ...
Bail out if we get a 401 and leave a message
[ "Bail", "out", "if", "we", "get", "a", "401", "and", "leave", "a", "message" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L352-L411
225,933
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.get_satellite5_info
def get_satellite5_info(self, branch_info): """ Get remote_leaf for Satellite 5 Managed box """ logger.debug( "Remote branch not -1 but remote leaf is -1, must be Satellite 5") if os.path.isfile('/etc/sysconfig/rhn/systemid'): logger.debug("Found systemid file") sat5_conf = ET.parse('/etc/sysconfig/rhn/systemid').getroot() leaf_id = None for member in sat5_conf.getiterator('member'): if member.find('name').text == 'system_id': logger.debug("Found member 'system_id'") leaf_id = member.find('value').find( 'string').text.split('ID-')[1] logger.debug("Found leaf id: %s", leaf_id) branch_info['remote_leaf'] = leaf_id if leaf_id is None: logger.error("Could not determine leaf_id! Exiting!") return False
python
def get_satellite5_info(self, branch_info): """ Get remote_leaf for Satellite 5 Managed box """ logger.debug( "Remote branch not -1 but remote leaf is -1, must be Satellite 5") if os.path.isfile('/etc/sysconfig/rhn/systemid'): logger.debug("Found systemid file") sat5_conf = ET.parse('/etc/sysconfig/rhn/systemid').getroot() leaf_id = None for member in sat5_conf.getiterator('member'): if member.find('name').text == 'system_id': logger.debug("Found member 'system_id'") leaf_id = member.find('value').find( 'string').text.split('ID-')[1] logger.debug("Found leaf id: %s", leaf_id) branch_info['remote_leaf'] = leaf_id if leaf_id is None: logger.error("Could not determine leaf_id! Exiting!") return False
[ "def", "get_satellite5_info", "(", "self", ",", "branch_info", ")", ":", "logger", ".", "debug", "(", "\"Remote branch not -1 but remote leaf is -1, must be Satellite 5\"", ")", "if", "os", ".", "path", ".", "isfile", "(", "'/etc/sysconfig/rhn/systemid'", ")", ":", "l...
Get remote_leaf for Satellite 5 Managed box
[ "Get", "remote_leaf", "for", "Satellite", "5", "Managed", "box" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L413-L432
225,934
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.get_branch_info
def get_branch_info(self): """ Retrieve branch_info from Satellite Server """ branch_info = None if os.path.exists(constants.cached_branch_info): # use cached branch info file if less than 10 minutes old # (failsafe, should be deleted at end of client run normally) logger.debug(u'Reading branch info from cached file.') ctime = datetime.utcfromtimestamp( os.path.getctime(constants.cached_branch_info)) if datetime.utcnow() < (ctime + timedelta(minutes=5)): with io.open(constants.cached_branch_info, encoding='utf8', mode='r') as f: branch_info = json.load(f) return branch_info else: logger.debug(u'Cached branch info is older than 5 minutes.') logger.debug(u'Obtaining branch information from %s', self.branch_info_url) net_logger.info(u'GET %s', self.branch_info_url) response = self.session.get(self.branch_info_url, timeout=self.config.http_timeout) logger.debug(u'GET branch_info status: %s', response.status_code) if response.status_code != 200: logger.debug("There was an error obtaining branch information.") logger.debug(u'Bad status from server: %s', response.status_code) logger.debug("Assuming default branch information %s" % self.branch_info) return False branch_info = response.json() logger.debug(u'Branch information: %s', json.dumps(branch_info)) # Determine if we are connected to Satellite 5 if ((branch_info[u'remote_branch'] is not -1 and branch_info[u'remote_leaf'] is -1)): self.get_satellite5_info(branch_info) logger.debug(u'Saving branch info to file.') with io.open(constants.cached_branch_info, encoding='utf8', mode='w') as f: # json.dump is broke in py2 so use dumps bi_str = json.dumps(branch_info, ensure_ascii=False) f.write(bi_str) self.branch_info = branch_info return branch_info
python
def get_branch_info(self): """ Retrieve branch_info from Satellite Server """ branch_info = None if os.path.exists(constants.cached_branch_info): # use cached branch info file if less than 10 minutes old # (failsafe, should be deleted at end of client run normally) logger.debug(u'Reading branch info from cached file.') ctime = datetime.utcfromtimestamp( os.path.getctime(constants.cached_branch_info)) if datetime.utcnow() < (ctime + timedelta(minutes=5)): with io.open(constants.cached_branch_info, encoding='utf8', mode='r') as f: branch_info = json.load(f) return branch_info else: logger.debug(u'Cached branch info is older than 5 minutes.') logger.debug(u'Obtaining branch information from %s', self.branch_info_url) net_logger.info(u'GET %s', self.branch_info_url) response = self.session.get(self.branch_info_url, timeout=self.config.http_timeout) logger.debug(u'GET branch_info status: %s', response.status_code) if response.status_code != 200: logger.debug("There was an error obtaining branch information.") logger.debug(u'Bad status from server: %s', response.status_code) logger.debug("Assuming default branch information %s" % self.branch_info) return False branch_info = response.json() logger.debug(u'Branch information: %s', json.dumps(branch_info)) # Determine if we are connected to Satellite 5 if ((branch_info[u'remote_branch'] is not -1 and branch_info[u'remote_leaf'] is -1)): self.get_satellite5_info(branch_info) logger.debug(u'Saving branch info to file.') with io.open(constants.cached_branch_info, encoding='utf8', mode='w') as f: # json.dump is broke in py2 so use dumps bi_str = json.dumps(branch_info, ensure_ascii=False) f.write(bi_str) self.branch_info = branch_info return branch_info
[ "def", "get_branch_info", "(", "self", ")", ":", "branch_info", "=", "None", "if", "os", ".", "path", ".", "exists", "(", "constants", ".", "cached_branch_info", ")", ":", "# use cached branch info file if less than 10 minutes old", "# (failsafe, should be deleted at end...
Retrieve branch_info from Satellite Server
[ "Retrieve", "branch_info", "from", "Satellite", "Server" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L434-L478
225,935
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.create_system
def create_system(self, new_machine_id=False): """ Create the machine via the API """ client_hostname = determine_hostname() machine_id = generate_machine_id(new_machine_id) branch_info = self.branch_info if not branch_info: return False remote_branch = branch_info['remote_branch'] remote_leaf = branch_info['remote_leaf'] data = {'machine_id': machine_id, 'remote_branch': remote_branch, 'remote_leaf': remote_leaf, 'hostname': client_hostname} if self.config.display_name is not None: data['display_name'] = self.config.display_name data = json.dumps(data) post_system_url = self.api_url + '/v1/systems' logger.debug("POST System: %s", post_system_url) logger.debug(data) net_logger.info("POST %s", post_system_url) return self.session.post(post_system_url, headers={'Content-Type': 'application/json'}, data=data)
python
def create_system(self, new_machine_id=False): """ Create the machine via the API """ client_hostname = determine_hostname() machine_id = generate_machine_id(new_machine_id) branch_info = self.branch_info if not branch_info: return False remote_branch = branch_info['remote_branch'] remote_leaf = branch_info['remote_leaf'] data = {'machine_id': machine_id, 'remote_branch': remote_branch, 'remote_leaf': remote_leaf, 'hostname': client_hostname} if self.config.display_name is not None: data['display_name'] = self.config.display_name data = json.dumps(data) post_system_url = self.api_url + '/v1/systems' logger.debug("POST System: %s", post_system_url) logger.debug(data) net_logger.info("POST %s", post_system_url) return self.session.post(post_system_url, headers={'Content-Type': 'application/json'}, data=data)
[ "def", "create_system", "(", "self", ",", "new_machine_id", "=", "False", ")", ":", "client_hostname", "=", "determine_hostname", "(", ")", "machine_id", "=", "generate_machine_id", "(", "new_machine_id", ")", "branch_info", "=", "self", ".", "branch_info", "if", ...
Create the machine via the API
[ "Create", "the", "machine", "via", "the", "API" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L481-L508
225,936
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.group_systems
def group_systems(self, group_name, systems): """ Adds an array of systems to specified group Args: group_name: Display name of group systems: Array of {'machine_id': machine_id} """ api_group_id = None headers = {'Content-Type': 'application/json'} group_path = self.api_url + '/v1/groups' group_get_path = group_path + ('?display_name=%s' % quote(group_name)) logger.debug("GET group: %s", group_get_path) net_logger.info("GET %s", group_get_path) get_group = self.session.get(group_get_path) logger.debug("GET group status: %s", get_group.status_code) if get_group.status_code == 200: api_group_id = get_group.json()['id'] if get_group.status_code == 404: # Group does not exist, POST to create logger.debug("POST group") data = json.dumps({'display_name': group_name}) net_logger.info("POST", group_path) post_group = self.session.post(group_path, headers=headers, data=data) logger.debug("POST group status: %s", post_group.status_code) logger.debug("POST Group: %s", post_group.json()) self.handle_fail_rcs(post_group) api_group_id = post_group.json()['id'] logger.debug("PUT group") data = json.dumps(systems) net_logger.info("PUT %s", group_path + ('/%s/systems' % api_group_id)) put_group = self.session.put(group_path + ('/%s/systems' % api_group_id), headers=headers, data=data) logger.debug("PUT group status: %d", put_group.status_code) logger.debug("PUT Group: %s", put_group.json())
python
def group_systems(self, group_name, systems): """ Adds an array of systems to specified group Args: group_name: Display name of group systems: Array of {'machine_id': machine_id} """ api_group_id = None headers = {'Content-Type': 'application/json'} group_path = self.api_url + '/v1/groups' group_get_path = group_path + ('?display_name=%s' % quote(group_name)) logger.debug("GET group: %s", group_get_path) net_logger.info("GET %s", group_get_path) get_group = self.session.get(group_get_path) logger.debug("GET group status: %s", get_group.status_code) if get_group.status_code == 200: api_group_id = get_group.json()['id'] if get_group.status_code == 404: # Group does not exist, POST to create logger.debug("POST group") data = json.dumps({'display_name': group_name}) net_logger.info("POST", group_path) post_group = self.session.post(group_path, headers=headers, data=data) logger.debug("POST group status: %s", post_group.status_code) logger.debug("POST Group: %s", post_group.json()) self.handle_fail_rcs(post_group) api_group_id = post_group.json()['id'] logger.debug("PUT group") data = json.dumps(systems) net_logger.info("PUT %s", group_path + ('/%s/systems' % api_group_id)) put_group = self.session.put(group_path + ('/%s/systems' % api_group_id), headers=headers, data=data) logger.debug("PUT group status: %d", put_group.status_code) logger.debug("PUT Group: %s", put_group.json())
[ "def", "group_systems", "(", "self", ",", "group_name", ",", "systems", ")", ":", "api_group_id", "=", "None", "headers", "=", "{", "'Content-Type'", ":", "'application/json'", "}", "group_path", "=", "self", ".", "api_url", "+", "'/v1/groups'", "group_get_path"...
Adds an array of systems to specified group Args: group_name: Display name of group systems: Array of {'machine_id': machine_id}
[ "Adds", "an", "array", "of", "systems", "to", "specified", "group" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L511-L552
225,937
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.do_group
def do_group(self): """ Do grouping on register """ group_id = self.config.group systems = {'machine_id': generate_machine_id()} self.group_systems(group_id, systems)
python
def do_group(self): """ Do grouping on register """ group_id = self.config.group systems = {'machine_id': generate_machine_id()} self.group_systems(group_id, systems)
[ "def", "do_group", "(", "self", ")", ":", "group_id", "=", "self", ".", "config", ".", "group", "systems", "=", "{", "'machine_id'", ":", "generate_machine_id", "(", ")", "}", "self", ".", "group_systems", "(", "group_id", ",", "systems", ")" ]
Do grouping on register
[ "Do", "grouping", "on", "register" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L556-L562
225,938
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection._legacy_api_registration_check
def _legacy_api_registration_check(self): ''' Check registration status through API ''' logger.debug('Checking registration status...') machine_id = generate_machine_id() try: url = self.api_url + '/v1/systems/' + machine_id net_logger.info("GET %s", url) res = self.session.get(url, timeout=self.config.http_timeout) except requests.ConnectionError: # can't connect, run connection test logger.error('Connection timed out. Running connection test...') self.test_connection() return False # had to do a quick bugfix changing this around, # which makes the None-False-True dichotomy seem weird # TODO: reconsider what gets returned, probably this: # True for registered # False for unregistered # None for system 404 try: # check the 'unregistered_at' key of the response unreg_status = json.loads(res.content).get('unregistered_at', 'undefined') # set the global account number self.config.account_number = json.loads(res.content).get('account_number', 'undefined') except ValueError: # bad response, no json object return False if unreg_status == 'undefined': # key not found, machine not yet registered return None elif unreg_status is None: # unregistered_at = null, means this machine IS registered return True else: # machine has been unregistered, this is a timestamp return unreg_status
python
def _legacy_api_registration_check(self): ''' Check registration status through API ''' logger.debug('Checking registration status...') machine_id = generate_machine_id() try: url = self.api_url + '/v1/systems/' + machine_id net_logger.info("GET %s", url) res = self.session.get(url, timeout=self.config.http_timeout) except requests.ConnectionError: # can't connect, run connection test logger.error('Connection timed out. Running connection test...') self.test_connection() return False # had to do a quick bugfix changing this around, # which makes the None-False-True dichotomy seem weird # TODO: reconsider what gets returned, probably this: # True for registered # False for unregistered # None for system 404 try: # check the 'unregistered_at' key of the response unreg_status = json.loads(res.content).get('unregistered_at', 'undefined') # set the global account number self.config.account_number = json.loads(res.content).get('account_number', 'undefined') except ValueError: # bad response, no json object return False if unreg_status == 'undefined': # key not found, machine not yet registered return None elif unreg_status is None: # unregistered_at = null, means this machine IS registered return True else: # machine has been unregistered, this is a timestamp return unreg_status
[ "def", "_legacy_api_registration_check", "(", "self", ")", ":", "logger", ".", "debug", "(", "'Checking registration status...'", ")", "machine_id", "=", "generate_machine_id", "(", ")", "try", ":", "url", "=", "self", ".", "api_url", "+", "'/v1/systems/'", "+", ...
Check registration status through API
[ "Check", "registration", "status", "through", "API" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L565-L602
225,939
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection._fetch_system_by_machine_id
def _fetch_system_by_machine_id(self): ''' Get a system by machine ID Returns dict system exists in inventory False system does not exist in inventory None error connection or parsing response ''' machine_id = generate_machine_id() try: url = self.api_url + '/inventory/v1/hosts?insights_id=' + machine_id net_logger.info("GET %s", url) res = self.session.get(url, timeout=self.config.http_timeout) except (requests.ConnectionError, requests.Timeout) as e: logger.error(e) logger.error('The Insights API could not be reached.') return None try: if (self.handle_fail_rcs(res)): return None res_json = json.loads(res.content) except ValueError as e: logger.error(e) logger.error('Could not parse response body.') return None if res_json['total'] == 0: logger.debug('No hosts found with machine ID: %s', machine_id) return False return res_json['results']
python
def _fetch_system_by_machine_id(self): ''' Get a system by machine ID Returns dict system exists in inventory False system does not exist in inventory None error connection or parsing response ''' machine_id = generate_machine_id() try: url = self.api_url + '/inventory/v1/hosts?insights_id=' + machine_id net_logger.info("GET %s", url) res = self.session.get(url, timeout=self.config.http_timeout) except (requests.ConnectionError, requests.Timeout) as e: logger.error(e) logger.error('The Insights API could not be reached.') return None try: if (self.handle_fail_rcs(res)): return None res_json = json.loads(res.content) except ValueError as e: logger.error(e) logger.error('Could not parse response body.') return None if res_json['total'] == 0: logger.debug('No hosts found with machine ID: %s', machine_id) return False return res_json['results']
[ "def", "_fetch_system_by_machine_id", "(", "self", ")", ":", "machine_id", "=", "generate_machine_id", "(", ")", "try", ":", "url", "=", "self", ".", "api_url", "+", "'/inventory/v1/hosts?insights_id='", "+", "machine_id", "net_logger", ".", "info", "(", "\"GET %s...
Get a system by machine ID Returns dict system exists in inventory False system does not exist in inventory None error connection or parsing response
[ "Get", "a", "system", "by", "machine", "ID", "Returns", "dict", "system", "exists", "in", "inventory", "False", "system", "does", "not", "exist", "in", "inventory", "None", "error", "connection", "or", "parsing", "response" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L604-L632
225,940
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.api_registration_check
def api_registration_check(self): ''' Reach out to the inventory API to check whether a machine exists. Returns True system exists in inventory False system does not exist in inventory None error connection or parsing response ''' if self.config.legacy_upload: return self._legacy_api_registration_check() logger.debug('Checking registration status...') results = self._fetch_system_by_machine_id() if not results: return results logger.debug('System found.') logger.debug('Machine ID: %s', results[0]['insights_id']) logger.debug('Inventory ID: %s', results[0]['id']) return True
python
def api_registration_check(self): ''' Reach out to the inventory API to check whether a machine exists. Returns True system exists in inventory False system does not exist in inventory None error connection or parsing response ''' if self.config.legacy_upload: return self._legacy_api_registration_check() logger.debug('Checking registration status...') results = self._fetch_system_by_machine_id() if not results: return results logger.debug('System found.') logger.debug('Machine ID: %s', results[0]['insights_id']) logger.debug('Inventory ID: %s', results[0]['id']) return True
[ "def", "api_registration_check", "(", "self", ")", ":", "if", "self", ".", "config", ".", "legacy_upload", ":", "return", "self", ".", "_legacy_api_registration_check", "(", ")", "logger", ".", "debug", "(", "'Checking registration status...'", ")", "results", "="...
Reach out to the inventory API to check whether a machine exists. Returns True system exists in inventory False system does not exist in inventory None error connection or parsing response
[ "Reach", "out", "to", "the", "inventory", "API", "to", "check", "whether", "a", "machine", "exists", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L634-L655
225,941
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.unregister
def unregister(self): """ Unregister this system from the insights service """ machine_id = generate_machine_id() try: logger.debug("Unregistering %s", machine_id) url = self.api_url + "/v1/systems/" + machine_id net_logger.info("DELETE %s", url) self.session.delete(url) logger.info( "Successfully unregistered from the Red Hat Insights Service") return True except requests.ConnectionError as e: logger.debug(e) logger.error("Could not unregister this system") return False
python
def unregister(self): """ Unregister this system from the insights service """ machine_id = generate_machine_id() try: logger.debug("Unregistering %s", machine_id) url = self.api_url + "/v1/systems/" + machine_id net_logger.info("DELETE %s", url) self.session.delete(url) logger.info( "Successfully unregistered from the Red Hat Insights Service") return True except requests.ConnectionError as e: logger.debug(e) logger.error("Could not unregister this system") return False
[ "def", "unregister", "(", "self", ")", ":", "machine_id", "=", "generate_machine_id", "(", ")", "try", ":", "logger", ".", "debug", "(", "\"Unregistering %s\"", ",", "machine_id", ")", "url", "=", "self", ".", "api_url", "+", "\"/v1/systems/\"", "+", "machin...
Unregister this system from the insights service
[ "Unregister", "this", "system", "from", "the", "insights", "service" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L658-L674
225,942
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.register
def register(self): """ Register this machine """ client_hostname = determine_hostname() # This will undo a blacklist logger.debug("API: Create system") system = self.create_system(new_machine_id=False) if system is False: return ('Could not reach the Insights service to register.', '', '', '') # If we get a 409, we know we need to generate a new machine-id if system.status_code == 409: system = self.create_system(new_machine_id=True) self.handle_fail_rcs(system) logger.debug("System: %s", system.json()) message = system.headers.get("x-rh-message", "") # Do grouping if self.config.group is not None: self.do_group() # Display registration success messasge to STDOUT and logs if system.status_code == 201: try: system_json = system.json() machine_id = system_json["machine_id"] account_number = system_json["account_number"] logger.info("You successfully registered %s to account %s." % (machine_id, account_number)) except: logger.debug('Received invalid JSON on system registration.') logger.debug('API still indicates valid registration with 201 status code.') logger.debug(system) logger.debug(system.json()) if self.config.group is not None: return (message, client_hostname, self.config.group, self.config.display_name) elif self.config.display_name is not None: return (message, client_hostname, "None", self.config.display_name) else: return (message, client_hostname, "None", "")
python
def register(self): """ Register this machine """ client_hostname = determine_hostname() # This will undo a blacklist logger.debug("API: Create system") system = self.create_system(new_machine_id=False) if system is False: return ('Could not reach the Insights service to register.', '', '', '') # If we get a 409, we know we need to generate a new machine-id if system.status_code == 409: system = self.create_system(new_machine_id=True) self.handle_fail_rcs(system) logger.debug("System: %s", system.json()) message = system.headers.get("x-rh-message", "") # Do grouping if self.config.group is not None: self.do_group() # Display registration success messasge to STDOUT and logs if system.status_code == 201: try: system_json = system.json() machine_id = system_json["machine_id"] account_number = system_json["account_number"] logger.info("You successfully registered %s to account %s." % (machine_id, account_number)) except: logger.debug('Received invalid JSON on system registration.') logger.debug('API still indicates valid registration with 201 status code.') logger.debug(system) logger.debug(system.json()) if self.config.group is not None: return (message, client_hostname, self.config.group, self.config.display_name) elif self.config.display_name is not None: return (message, client_hostname, "None", self.config.display_name) else: return (message, client_hostname, "None", "")
[ "def", "register", "(", "self", ")", ":", "client_hostname", "=", "determine_hostname", "(", ")", "# This will undo a blacklist", "logger", ".", "debug", "(", "\"API: Create system\"", ")", "system", "=", "self", ".", "create_system", "(", "new_machine_id", "=", "...
Register this machine
[ "Register", "this", "machine" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L677-L719
225,943
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection._legacy_upload_archive
def _legacy_upload_archive(self, data_collected, duration): ''' Do an HTTPS upload of the archive ''' file_name = os.path.basename(data_collected) try: from insights.contrib import magic m = magic.open(magic.MAGIC_MIME) m.load() mime_type = m.file(data_collected) except ImportError: magic = None logger.debug('python-magic not installed, using backup function...') from .utilities import magic_plan_b mime_type = magic_plan_b(data_collected) files = { 'file': (file_name, open(data_collected, 'rb'), mime_type)} if self.config.analyze_container: logger.debug('Uploading container, image, mountpoint or tarfile.') upload_url = self.upload_url else: logger.debug('Uploading a host.') upload_url = self.upload_url + '/' + generate_machine_id() logger.debug("Uploading %s to %s", data_collected, upload_url) headers = {'x-rh-collection-time': str(duration)} net_logger.info("POST %s", upload_url) upload = self.session.post(upload_url, files=files, headers=headers) logger.debug("Upload status: %s %s %s", upload.status_code, upload.reason, upload.text) if upload.status_code in (200, 201): the_json = json.loads(upload.text) else: logger.error("Upload archive failed with status code %s", upload.status_code) return upload try: self.config.account_number = the_json["upload"]["account_number"] except: self.config.account_number = None logger.debug("Upload duration: %s", upload.elapsed) return upload
python
def _legacy_upload_archive(self, data_collected, duration): ''' Do an HTTPS upload of the archive ''' file_name = os.path.basename(data_collected) try: from insights.contrib import magic m = magic.open(magic.MAGIC_MIME) m.load() mime_type = m.file(data_collected) except ImportError: magic = None logger.debug('python-magic not installed, using backup function...') from .utilities import magic_plan_b mime_type = magic_plan_b(data_collected) files = { 'file': (file_name, open(data_collected, 'rb'), mime_type)} if self.config.analyze_container: logger.debug('Uploading container, image, mountpoint or tarfile.') upload_url = self.upload_url else: logger.debug('Uploading a host.') upload_url = self.upload_url + '/' + generate_machine_id() logger.debug("Uploading %s to %s", data_collected, upload_url) headers = {'x-rh-collection-time': str(duration)} net_logger.info("POST %s", upload_url) upload = self.session.post(upload_url, files=files, headers=headers) logger.debug("Upload status: %s %s %s", upload.status_code, upload.reason, upload.text) if upload.status_code in (200, 201): the_json = json.loads(upload.text) else: logger.error("Upload archive failed with status code %s", upload.status_code) return upload try: self.config.account_number = the_json["upload"]["account_number"] except: self.config.account_number = None logger.debug("Upload duration: %s", upload.elapsed) return upload
[ "def", "_legacy_upload_archive", "(", "self", ",", "data_collected", ",", "duration", ")", ":", "file_name", "=", "os", ".", "path", ".", "basename", "(", "data_collected", ")", "try", ":", "from", "insights", ".", "contrib", "import", "magic", "m", "=", "...
Do an HTTPS upload of the archive
[ "Do", "an", "HTTPS", "upload", "of", "the", "archive" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L722-L766
225,944
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.upload_archive
def upload_archive(self, data_collected, content_type, duration): """ Do an HTTPS Upload of the archive """ if self.config.legacy_upload: return self._legacy_upload_archive(data_collected, duration) file_name = os.path.basename(data_collected) upload_url = self.upload_url c_facts = {} try: c_facts = get_canonical_facts() except Exception as e: logger.debug('Error getting canonical facts: %s', e) if self.config.display_name: # add display_name to canonical facts c_facts['display_name'] = self.config.display_name c_facts = json.dumps(c_facts) logger.debug('Canonical facts collected:\n%s', c_facts) files = { 'file': (file_name, open(data_collected, 'rb'), content_type), 'metadata': c_facts } logger.debug("Uploading %s to %s", data_collected, upload_url) net_logger.info("POST %s", upload_url) upload = self.session.post(upload_url, files=files, headers={}) logger.debug("Upload status: %s %s %s", upload.status_code, upload.reason, upload.text) logger.debug('Request ID: %s', upload.headers.get('x-rh-insights-request-id', None)) if upload.status_code == 202: # 202 from platform, no json response logger.debug(upload.text) # upload = registration on platform write_registered_file() else: logger.error( "Upload archive failed with status code %s", upload.status_code) return upload logger.debug("Upload duration: %s", upload.elapsed) return upload
python
def upload_archive(self, data_collected, content_type, duration): """ Do an HTTPS Upload of the archive """ if self.config.legacy_upload: return self._legacy_upload_archive(data_collected, duration) file_name = os.path.basename(data_collected) upload_url = self.upload_url c_facts = {} try: c_facts = get_canonical_facts() except Exception as e: logger.debug('Error getting canonical facts: %s', e) if self.config.display_name: # add display_name to canonical facts c_facts['display_name'] = self.config.display_name c_facts = json.dumps(c_facts) logger.debug('Canonical facts collected:\n%s', c_facts) files = { 'file': (file_name, open(data_collected, 'rb'), content_type), 'metadata': c_facts } logger.debug("Uploading %s to %s", data_collected, upload_url) net_logger.info("POST %s", upload_url) upload = self.session.post(upload_url, files=files, headers={}) logger.debug("Upload status: %s %s %s", upload.status_code, upload.reason, upload.text) logger.debug('Request ID: %s', upload.headers.get('x-rh-insights-request-id', None)) if upload.status_code == 202: # 202 from platform, no json response logger.debug(upload.text) # upload = registration on platform write_registered_file() else: logger.error( "Upload archive failed with status code %s", upload.status_code) return upload logger.debug("Upload duration: %s", upload.elapsed) return upload
[ "def", "upload_archive", "(", "self", ",", "data_collected", ",", "content_type", ",", "duration", ")", ":", "if", "self", ".", "config", ".", "legacy_upload", ":", "return", "self", ".", "_legacy_upload_archive", "(", "data_collected", ",", "duration", ")", "...
Do an HTTPS Upload of the archive
[ "Do", "an", "HTTPS", "Upload", "of", "the", "archive" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L768-L811
225,945
RedHatInsights/insights-core
insights/client/connection.py
InsightsConnection.set_display_name
def set_display_name(self, display_name): ''' Set display name of a system independently of upload. ''' if self.config.legacy_upload: return self._legacy_set_display_name(display_name) system = self._fetch_system_by_machine_id() if not system: return system inventory_id = system[0]['id'] req_url = self.base_url + '/inventory/v1/hosts/' + inventory_id try: net_logger.info("PATCH %s", req_url) res = self.session.patch(req_url, json={'display_name': display_name}) except (requests.ConnectionError, requests.Timeout) as e: logger.error(e) logger.error('The Insights API could not be reached.') return False if (self.handle_fail_rcs(res)): logger.error('Could not update display name.') return False logger.info('Display name updated to ' + display_name + '.') return True
python
def set_display_name(self, display_name): ''' Set display name of a system independently of upload. ''' if self.config.legacy_upload: return self._legacy_set_display_name(display_name) system = self._fetch_system_by_machine_id() if not system: return system inventory_id = system[0]['id'] req_url = self.base_url + '/inventory/v1/hosts/' + inventory_id try: net_logger.info("PATCH %s", req_url) res = self.session.patch(req_url, json={'display_name': display_name}) except (requests.ConnectionError, requests.Timeout) as e: logger.error(e) logger.error('The Insights API could not be reached.') return False if (self.handle_fail_rcs(res)): logger.error('Could not update display name.') return False logger.info('Display name updated to ' + display_name + '.') return True
[ "def", "set_display_name", "(", "self", ",", "display_name", ")", ":", "if", "self", ".", "config", ".", "legacy_upload", ":", "return", "self", ".", "_legacy_set_display_name", "(", "display_name", ")", "system", "=", "self", ".", "_fetch_system_by_machine_id", ...
Set display name of a system independently of upload.
[ "Set", "display", "name", "of", "a", "system", "independently", "of", "upload", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/connection.py#L851-L874
225,946
RedHatInsights/insights-core
insights/client/config.py
InsightsConfig._update_dict
def _update_dict(self, dict_): ''' Update without allowing undefined options or overwrite of class methods ''' dict_ = dict((k, v) for k, v in dict_.items() if ( k not in self._init_attrs)) # zzz if 'no_gpg' in dict_ and dict_['no_gpg']: dict_['gpg'] = False unknown_opts = set(dict_.keys()).difference(set(DEFAULT_OPTS.keys())) if unknown_opts and self._print_errors: # only print error once sys.stdout.write( 'WARNING: Unknown options: ' + ', '.join(list(unknown_opts)) + '\n') if 'no_schedule' in unknown_opts: sys.stdout.write('WARNING: Config option `no_schedule` has ' 'been deprecated. To disable automatic ' 'scheduling for Red Hat Insights, run ' '`insights-client --disable-schedule`\n') for u in unknown_opts: dict_.pop(u, None) self.__dict__.update(dict_)
python
def _update_dict(self, dict_): ''' Update without allowing undefined options or overwrite of class methods ''' dict_ = dict((k, v) for k, v in dict_.items() if ( k not in self._init_attrs)) # zzz if 'no_gpg' in dict_ and dict_['no_gpg']: dict_['gpg'] = False unknown_opts = set(dict_.keys()).difference(set(DEFAULT_OPTS.keys())) if unknown_opts and self._print_errors: # only print error once sys.stdout.write( 'WARNING: Unknown options: ' + ', '.join(list(unknown_opts)) + '\n') if 'no_schedule' in unknown_opts: sys.stdout.write('WARNING: Config option `no_schedule` has ' 'been deprecated. To disable automatic ' 'scheduling for Red Hat Insights, run ' '`insights-client --disable-schedule`\n') for u in unknown_opts: dict_.pop(u, None) self.__dict__.update(dict_)
[ "def", "_update_dict", "(", "self", ",", "dict_", ")", ":", "dict_", "=", "dict", "(", "(", "k", ",", "v", ")", "for", "k", ",", "v", "in", "dict_", ".", "items", "(", ")", "if", "(", "k", "not", "in", "self", ".", "_init_attrs", ")", ")", "#...
Update without allowing undefined options or overwrite of class methods
[ "Update", "without", "allowing", "undefined", "options", "or", "overwrite", "of", "class", "methods" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/config.py#L393-L417
225,947
RedHatInsights/insights-core
insights/client/config.py
InsightsConfig._load_config_file
def _load_config_file(self, fname=None): ''' Load config from config file. If fname is not specified, config is loaded from the file named by InsightsConfig.conf ''' parsedconfig = ConfigParser.RawConfigParser() try: parsedconfig.read(fname or self.conf) except ConfigParser.Error: if self._print_errors: sys.stdout.write( 'ERROR: Could not read configuration file, ' 'using defaults\n') return try: if parsedconfig.has_section(constants.app_name): d = dict(parsedconfig.items(constants.app_name)) elif parsedconfig.has_section('redhat-access-insights'): d = dict(parsedconfig.items('redhat-access-insights')) else: raise ConfigParser.Error except ConfigParser.Error: if self._print_errors: sys.stdout.write( 'ERROR: Could not read configuration file, ' 'using defaults\n') return for key in d: try: if key == 'retries' or key == 'cmd_timeout': d[key] = parsedconfig.getint(constants.app_name, key) if key == 'http_timeout': d[key] = parsedconfig.getfloat(constants.app_name, key) if key in DEFAULT_BOOLS and isinstance( d[key], six.string_types): d[key] = parsedconfig.getboolean(constants.app_name, key) except ValueError as e: if self._print_errors: sys.stdout.write( 'ERROR: {0}.\nCould not read configuration file, ' 'using defaults\n'.format(e)) return self._update_dict(d)
python
def _load_config_file(self, fname=None): ''' Load config from config file. If fname is not specified, config is loaded from the file named by InsightsConfig.conf ''' parsedconfig = ConfigParser.RawConfigParser() try: parsedconfig.read(fname or self.conf) except ConfigParser.Error: if self._print_errors: sys.stdout.write( 'ERROR: Could not read configuration file, ' 'using defaults\n') return try: if parsedconfig.has_section(constants.app_name): d = dict(parsedconfig.items(constants.app_name)) elif parsedconfig.has_section('redhat-access-insights'): d = dict(parsedconfig.items('redhat-access-insights')) else: raise ConfigParser.Error except ConfigParser.Error: if self._print_errors: sys.stdout.write( 'ERROR: Could not read configuration file, ' 'using defaults\n') return for key in d: try: if key == 'retries' or key == 'cmd_timeout': d[key] = parsedconfig.getint(constants.app_name, key) if key == 'http_timeout': d[key] = parsedconfig.getfloat(constants.app_name, key) if key in DEFAULT_BOOLS and isinstance( d[key], six.string_types): d[key] = parsedconfig.getboolean(constants.app_name, key) except ValueError as e: if self._print_errors: sys.stdout.write( 'ERROR: {0}.\nCould not read configuration file, ' 'using defaults\n'.format(e)) return self._update_dict(d)
[ "def", "_load_config_file", "(", "self", ",", "fname", "=", "None", ")", ":", "parsedconfig", "=", "ConfigParser", ".", "RawConfigParser", "(", ")", "try", ":", "parsedconfig", ".", "read", "(", "fname", "or", "self", ".", "conf", ")", "except", "ConfigPar...
Load config from config file. If fname is not specified, config is loaded from the file named by InsightsConfig.conf
[ "Load", "config", "from", "config", "file", ".", "If", "fname", "is", "not", "specified", "config", "is", "loaded", "from", "the", "file", "named", "by", "InsightsConfig", ".", "conf" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/config.py#L493-L535
225,948
RedHatInsights/insights-core
insights/client/config.py
InsightsConfig.load_all
def load_all(self): ''' Helper function for actual Insights client use ''' # check for custom conf file before loading conf self._load_command_line(conf_only=True) self._load_config_file() self._load_env() self._load_command_line() self._imply_options() self._validate_options() return self
python
def load_all(self): ''' Helper function for actual Insights client use ''' # check for custom conf file before loading conf self._load_command_line(conf_only=True) self._load_config_file() self._load_env() self._load_command_line() self._imply_options() self._validate_options() return self
[ "def", "load_all", "(", "self", ")", ":", "# check for custom conf file before loading conf", "self", ".", "_load_command_line", "(", "conf_only", "=", "True", ")", "self", ".", "_load_config_file", "(", ")", "self", ".", "_load_env", "(", ")", "self", ".", "_lo...
Helper function for actual Insights client use
[ "Helper", "function", "for", "actual", "Insights", "client", "use" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/config.py#L537-L548
225,949
RedHatInsights/insights-core
insights/client/config.py
InsightsConfig._validate_options
def _validate_options(self): ''' Make sure there are no conflicting or invalid options ''' if self.obfuscate_hostname and not self.obfuscate: raise ValueError( 'Option `obfuscate_hostname` requires `obfuscate`') if self.analyze_image_id is not None and len(self.analyze_image_id) < 12: raise ValueError( 'Image/Container ID must be at least twelve characters long.') if self.enable_schedule and self.disable_schedule: raise ValueError( 'Conflicting options: --enable-schedule and --disable-schedule') if self.analyze_container and (self.register or self.unregister): raise ValueError('Registration not supported with ' 'image or container analysis.') if self.to_json and self.to_stdout: raise ValueError( 'Conflicting options: --to-stdout and --to-json') if self.payload and not self.content_type: raise ValueError( '--payload requires --content-type') if not self.legacy_upload: if self.group: raise ValueError( '--group is not supported at this time.') if self.analyze_image_id: raise ValueError( '--analyze-image-id is not supported at this time.') if self.analyze_file: raise ValueError( '--analyze-file is not supported at this time.') if self.analyze_mountpoint: raise ValueError( '--analyze-mountpoint is not supported at this time.') if self.analyze_container: raise ValueError( '--analyze-container is not supported at this time.')
python
def _validate_options(self): ''' Make sure there are no conflicting or invalid options ''' if self.obfuscate_hostname and not self.obfuscate: raise ValueError( 'Option `obfuscate_hostname` requires `obfuscate`') if self.analyze_image_id is not None and len(self.analyze_image_id) < 12: raise ValueError( 'Image/Container ID must be at least twelve characters long.') if self.enable_schedule and self.disable_schedule: raise ValueError( 'Conflicting options: --enable-schedule and --disable-schedule') if self.analyze_container and (self.register or self.unregister): raise ValueError('Registration not supported with ' 'image or container analysis.') if self.to_json and self.to_stdout: raise ValueError( 'Conflicting options: --to-stdout and --to-json') if self.payload and not self.content_type: raise ValueError( '--payload requires --content-type') if not self.legacy_upload: if self.group: raise ValueError( '--group is not supported at this time.') if self.analyze_image_id: raise ValueError( '--analyze-image-id is not supported at this time.') if self.analyze_file: raise ValueError( '--analyze-file is not supported at this time.') if self.analyze_mountpoint: raise ValueError( '--analyze-mountpoint is not supported at this time.') if self.analyze_container: raise ValueError( '--analyze-container is not supported at this time.')
[ "def", "_validate_options", "(", "self", ")", ":", "if", "self", ".", "obfuscate_hostname", "and", "not", "self", ".", "obfuscate", ":", "raise", "ValueError", "(", "'Option `obfuscate_hostname` requires `obfuscate`'", ")", "if", "self", ".", "analyze_image_id", "is...
Make sure there are no conflicting or invalid options
[ "Make", "sure", "there", "are", "no", "conflicting", "or", "invalid", "options" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/config.py#L550-L587
225,950
RedHatInsights/insights-core
insights/client/config.py
InsightsConfig._imply_options
def _imply_options(self): ''' Some options enable others automatically ''' self.no_upload = self.no_upload or self.to_stdout or self.offline self.auto_update = self.auto_update and not self.offline if (self.analyze_container or self.analyze_file or self.analyze_mountpoint or self.analyze_image_id): self.analyze_container = True self.to_json = self.to_json or self.analyze_container self.register = (self.register or self.reregister) and not self.offline self.keep_archive = self.keep_archive or self.no_upload if self.payload: self.legacy_upload = False
python
def _imply_options(self): ''' Some options enable others automatically ''' self.no_upload = self.no_upload or self.to_stdout or self.offline self.auto_update = self.auto_update and not self.offline if (self.analyze_container or self.analyze_file or self.analyze_mountpoint or self.analyze_image_id): self.analyze_container = True self.to_json = self.to_json or self.analyze_container self.register = (self.register or self.reregister) and not self.offline self.keep_archive = self.keep_archive or self.no_upload if self.payload: self.legacy_upload = False
[ "def", "_imply_options", "(", "self", ")", ":", "self", ".", "no_upload", "=", "self", ".", "no_upload", "or", "self", ".", "to_stdout", "or", "self", ".", "offline", "self", ".", "auto_update", "=", "self", ".", "auto_update", "and", "not", "self", ".",...
Some options enable others automatically
[ "Some", "options", "enable", "others", "automatically" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/config.py#L589-L604
225,951
RedHatInsights/insights-core
insights/parsers/httpd_conf.py
dict_deep_merge
def dict_deep_merge(tgt, src): """ Utility function to merge the source dictionary `src` to the target dictionary recursively Note: The type of the values in the dictionary can only be `dict` or `list` Parameters: tgt (dict): The target dictionary src (dict): The source dictionary """ for k, v in src.items(): if k in tgt: if isinstance(tgt[k], dict) and isinstance(v, dict): dict_deep_merge(tgt[k], v) else: tgt[k].extend(deepcopy(v)) else: tgt[k] = deepcopy(v)
python
def dict_deep_merge(tgt, src): """ Utility function to merge the source dictionary `src` to the target dictionary recursively Note: The type of the values in the dictionary can only be `dict` or `list` Parameters: tgt (dict): The target dictionary src (dict): The source dictionary """ for k, v in src.items(): if k in tgt: if isinstance(tgt[k], dict) and isinstance(v, dict): dict_deep_merge(tgt[k], v) else: tgt[k].extend(deepcopy(v)) else: tgt[k] = deepcopy(v)
[ "def", "dict_deep_merge", "(", "tgt", ",", "src", ")", ":", "for", "k", ",", "v", "in", "src", ".", "items", "(", ")", ":", "if", "k", "in", "tgt", ":", "if", "isinstance", "(", "tgt", "[", "k", "]", ",", "dict", ")", "and", "isinstance", "(", ...
Utility function to merge the source dictionary `src` to the target dictionary recursively Note: The type of the values in the dictionary can only be `dict` or `list` Parameters: tgt (dict): The target dictionary src (dict): The source dictionary
[ "Utility", "function", "to", "merge", "the", "source", "dictionary", "src", "to", "the", "target", "dictionary", "recursively" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/httpd_conf.py#L195-L214
225,952
RedHatInsights/insights-core
insights/client/mount.py
Mount._activate_thin_device
def _activate_thin_device(name, dm_id, size, pool): """ Provisions an LVM device-mapper thin device reflecting, DM device id 'dm_id' in the docker pool. """ table = '0 %d thin /dev/mapper/%s %s' % (int(size) // 512, pool, dm_id) cmd = ['dmsetup', 'create', name, '--table', table] r = util.subp(cmd) if r.return_code != 0: raise MountError('Failed to create thin device: %s' % r.stderr.decode(sys.getdefaultencoding()))
python
def _activate_thin_device(name, dm_id, size, pool): """ Provisions an LVM device-mapper thin device reflecting, DM device id 'dm_id' in the docker pool. """ table = '0 %d thin /dev/mapper/%s %s' % (int(size) // 512, pool, dm_id) cmd = ['dmsetup', 'create', name, '--table', table] r = util.subp(cmd) if r.return_code != 0: raise MountError('Failed to create thin device: %s' % r.stderr.decode(sys.getdefaultencoding()))
[ "def", "_activate_thin_device", "(", "name", ",", "dm_id", ",", "size", ",", "pool", ")", ":", "table", "=", "'0 %d thin /dev/mapper/%s %s'", "%", "(", "int", "(", "size", ")", "//", "512", ",", "pool", ",", "dm_id", ")", "cmd", "=", "[", "'dmsetup'", ...
Provisions an LVM device-mapper thin device reflecting, DM device id 'dm_id' in the docker pool.
[ "Provisions", "an", "LVM", "device", "-", "mapper", "thin", "device", "reflecting", "DM", "device", "id", "dm_id", "in", "the", "docker", "pool", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L74-L84
225,953
RedHatInsights/insights-core
insights/client/mount.py
Mount.remove_thin_device
def remove_thin_device(name, force=False): """ Destroys a thin device via subprocess call. """ cmd = ['dmsetup', 'remove', '--retry', name] r = util.subp(cmd) if not force: if r.return_code != 0: raise MountError('Could not remove thin device:\n%s' % r.stderr.decode(sys.getdefaultencoding()).split("\n")[0])
python
def remove_thin_device(name, force=False): """ Destroys a thin device via subprocess call. """ cmd = ['dmsetup', 'remove', '--retry', name] r = util.subp(cmd) if not force: if r.return_code != 0: raise MountError('Could not remove thin device:\n%s' % r.stderr.decode(sys.getdefaultencoding()).split("\n")[0])
[ "def", "remove_thin_device", "(", "name", ",", "force", "=", "False", ")", ":", "cmd", "=", "[", "'dmsetup'", ",", "'remove'", ",", "'--retry'", ",", "name", "]", "r", "=", "util", ".", "subp", "(", "cmd", ")", "if", "not", "force", ":", "if", "r",...
Destroys a thin device via subprocess call.
[ "Destroys", "a", "thin", "device", "via", "subprocess", "call", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L87-L96
225,954
RedHatInsights/insights-core
insights/client/mount.py
Mount._is_device_active
def _is_device_active(device): """ Checks dmsetup to see if a device is already active """ cmd = ['dmsetup', 'info', device] dmsetup_info = util.subp(cmd) for dm_line in dmsetup_info.stdout.split("\n"): line = dm_line.split(':') if ('State' in line[0].strip()) and ('ACTIVE' in line[1].strip()): return True return False
python
def _is_device_active(device): """ Checks dmsetup to see if a device is already active """ cmd = ['dmsetup', 'info', device] dmsetup_info = util.subp(cmd) for dm_line in dmsetup_info.stdout.split("\n"): line = dm_line.split(':') if ('State' in line[0].strip()) and ('ACTIVE' in line[1].strip()): return True return False
[ "def", "_is_device_active", "(", "device", ")", ":", "cmd", "=", "[", "'dmsetup'", ",", "'info'", ",", "device", "]", "dmsetup_info", "=", "util", ".", "subp", "(", "cmd", ")", "for", "dm_line", "in", "dmsetup_info", ".", "stdout", ".", "split", "(", "...
Checks dmsetup to see if a device is already active
[ "Checks", "dmsetup", "to", "see", "if", "a", "device", "is", "already", "active" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L99-L109
225,955
RedHatInsights/insights-core
insights/client/mount.py
Mount.mount_path
def mount_path(source, target, bind=False): """ Subprocess call to mount dev at path. """ cmd = ['mount'] if bind: cmd.append('--bind') cmd.append(source) cmd.append(target) r = util.subp(cmd) if r.return_code != 0: raise MountError('Could not mount docker container:\n' + ' '.join(cmd) + '\n%s' % r.stderr.decode(sys.getdefaultencoding()))
python
def mount_path(source, target, bind=False): """ Subprocess call to mount dev at path. """ cmd = ['mount'] if bind: cmd.append('--bind') cmd.append(source) cmd.append(target) r = util.subp(cmd) if r.return_code != 0: raise MountError('Could not mount docker container:\n' + ' '.join(cmd) + '\n%s' % r.stderr.decode(sys.getdefaultencoding()))
[ "def", "mount_path", "(", "source", ",", "target", ",", "bind", "=", "False", ")", ":", "cmd", "=", "[", "'mount'", "]", "if", "bind", ":", "cmd", ".", "append", "(", "'--bind'", ")", "cmd", ".", "append", "(", "source", ")", "cmd", ".", "append", ...
Subprocess call to mount dev at path.
[ "Subprocess", "call", "to", "mount", "dev", "at", "path", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L121-L134
225,956
RedHatInsights/insights-core
insights/client/mount.py
Mount.get_dev_at_mountpoint
def get_dev_at_mountpoint(mntpoint): """ Retrieves the device mounted at mntpoint, or raises MountError if none. """ results = util.subp(['findmnt', '-o', 'SOURCE', mntpoint]) if results.return_code != 0: raise MountError('No device mounted at %s' % mntpoint) stdout = results.stdout.decode(sys.getdefaultencoding()) return stdout.replace('SOURCE\n', '').strip().split('\n')[-1]
python
def get_dev_at_mountpoint(mntpoint): """ Retrieves the device mounted at mntpoint, or raises MountError if none. """ results = util.subp(['findmnt', '-o', 'SOURCE', mntpoint]) if results.return_code != 0: raise MountError('No device mounted at %s' % mntpoint) stdout = results.stdout.decode(sys.getdefaultencoding()) return stdout.replace('SOURCE\n', '').strip().split('\n')[-1]
[ "def", "get_dev_at_mountpoint", "(", "mntpoint", ")", ":", "results", "=", "util", ".", "subp", "(", "[", "'findmnt'", ",", "'-o'", ",", "'SOURCE'", ",", "mntpoint", "]", ")", "if", "results", ".", "return_code", "!=", "0", ":", "raise", "MountError", "(...
Retrieves the device mounted at mntpoint, or raises MountError if none.
[ "Retrieves", "the", "device", "mounted", "at", "mntpoint", "or", "raises", "MountError", "if", "none", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L137-L147
225,957
RedHatInsights/insights-core
insights/client/mount.py
Mount.unmount_path
def unmount_path(path, force=False): """ Unmounts the directory specified by path. """ r = util.subp(['umount', path]) if not force: if r.return_code != 0: raise ValueError(r.stderr)
python
def unmount_path(path, force=False): """ Unmounts the directory specified by path. """ r = util.subp(['umount', path]) if not force: if r.return_code != 0: raise ValueError(r.stderr)
[ "def", "unmount_path", "(", "path", ",", "force", "=", "False", ")", ":", "r", "=", "util", ".", "subp", "(", "[", "'umount'", ",", "path", "]", ")", "if", "not", "force", ":", "if", "r", ".", "return_code", "!=", "0", ":", "raise", "ValueError", ...
Unmounts the directory specified by path.
[ "Unmounts", "the", "directory", "specified", "by", "path", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L150-L157
225,958
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._create_temp_container
def _create_temp_container(self, iid): """ Create a temporary container from a given iid. Temporary containers are marked with a sentinel environment variable so that they can be cleaned on unmount. """ try: return self.client.create_container( image=iid, command='/bin/true', environment=['_ATOMIC_TEMP_CONTAINER'], detach=True, network_disabled=True)['Id'] except docker.errors.APIError as ex: raise MountError('Error creating temporary container:\n%s' % str(ex))
python
def _create_temp_container(self, iid): """ Create a temporary container from a given iid. Temporary containers are marked with a sentinel environment variable so that they can be cleaned on unmount. """ try: return self.client.create_container( image=iid, command='/bin/true', environment=['_ATOMIC_TEMP_CONTAINER'], detach=True, network_disabled=True)['Id'] except docker.errors.APIError as ex: raise MountError('Error creating temporary container:\n%s' % str(ex))
[ "def", "_create_temp_container", "(", "self", ",", "iid", ")", ":", "try", ":", "return", "self", ".", "client", ".", "create_container", "(", "image", "=", "iid", ",", "command", "=", "'/bin/true'", ",", "environment", "=", "[", "'_ATOMIC_TEMP_CONTAINER'", ...
Create a temporary container from a given iid. Temporary containers are marked with a sentinel environment variable so that they can be cleaned on unmount.
[ "Create", "a", "temporary", "container", "from", "a", "given", "iid", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L176-L189
225,959
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._clone
def _clone(self, cid): """ Create a temporary image snapshot from a given cid. Temporary image snapshots are marked with a sentinel label so that they can be cleaned on unmount. """ try: iid = self.client.commit( container=cid, conf={ 'Labels': { 'io.projectatomic.Temporary': 'true' } } )['Id'] except docker.errors.APIError as ex: raise MountError(str(ex)) self.tmp_image = iid return self._create_temp_container(iid)
python
def _clone(self, cid): """ Create a temporary image snapshot from a given cid. Temporary image snapshots are marked with a sentinel label so that they can be cleaned on unmount. """ try: iid = self.client.commit( container=cid, conf={ 'Labels': { 'io.projectatomic.Temporary': 'true' } } )['Id'] except docker.errors.APIError as ex: raise MountError(str(ex)) self.tmp_image = iid return self._create_temp_container(iid)
[ "def", "_clone", "(", "self", ",", "cid", ")", ":", "try", ":", "iid", "=", "self", ".", "client", ".", "commit", "(", "container", "=", "cid", ",", "conf", "=", "{", "'Labels'", ":", "{", "'io.projectatomic.Temporary'", ":", "'true'", "}", "}", ")",...
Create a temporary image snapshot from a given cid. Temporary image snapshots are marked with a sentinel label so that they can be cleaned on unmount.
[ "Create", "a", "temporary", "image", "snapshot", "from", "a", "given", "cid", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L191-L210
225,960
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._identifier_as_cid
def _identifier_as_cid(self, identifier): """ Returns a container uuid for identifier. If identifier is an image UUID or image tag, create a temporary container and return its uuid. """ def __cname_matches(container, identifier): return any([n for n in (container['Names'] or []) if matches(n, '/' + identifier)]) # Determine if identifier is a container containers = [c['Id'] for c in self.client.containers(all=True) if (__cname_matches(c, identifier) or matches(c['Id'], identifier + '*'))] if len(containers) > 1: raise SelectionMatchError(identifier, containers) elif len(containers) == 1: c = containers[0] return self._clone(c) # Determine if identifier is an image UUID images = [i for i in set(self.client.images(all=True, quiet=True)) if i.startswith(identifier)] if len(images) > 1: raise SelectionMatchError(identifier, images) elif len(images) == 1: return self._create_temp_container(images[0]) # Match image tag. images = util.image_by_name(identifier) if len(images) > 1: tags = [t for i in images for t in i['RepoTags']] raise SelectionMatchError(identifier, tags) elif len(images) == 1: return self._create_temp_container(images[0]['Id'].replace("sha256:", "")) raise MountError('{} did not match any image or container.' ''.format(identifier))
python
def _identifier_as_cid(self, identifier): """ Returns a container uuid for identifier. If identifier is an image UUID or image tag, create a temporary container and return its uuid. """ def __cname_matches(container, identifier): return any([n for n in (container['Names'] or []) if matches(n, '/' + identifier)]) # Determine if identifier is a container containers = [c['Id'] for c in self.client.containers(all=True) if (__cname_matches(c, identifier) or matches(c['Id'], identifier + '*'))] if len(containers) > 1: raise SelectionMatchError(identifier, containers) elif len(containers) == 1: c = containers[0] return self._clone(c) # Determine if identifier is an image UUID images = [i for i in set(self.client.images(all=True, quiet=True)) if i.startswith(identifier)] if len(images) > 1: raise SelectionMatchError(identifier, images) elif len(images) == 1: return self._create_temp_container(images[0]) # Match image tag. images = util.image_by_name(identifier) if len(images) > 1: tags = [t for i in images for t in i['RepoTags']] raise SelectionMatchError(identifier, tags) elif len(images) == 1: return self._create_temp_container(images[0]['Id'].replace("sha256:", "")) raise MountError('{} did not match any image or container.' ''.format(identifier))
[ "def", "_identifier_as_cid", "(", "self", ",", "identifier", ")", ":", "def", "__cname_matches", "(", "container", ",", "identifier", ")", ":", "return", "any", "(", "[", "n", "for", "n", "in", "(", "container", "[", "'Names'", "]", "or", "[", "]", ")"...
Returns a container uuid for identifier. If identifier is an image UUID or image tag, create a temporary container and return its uuid.
[ "Returns", "a", "container", "uuid", "for", "identifier", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L216-L256
225,961
RedHatInsights/insights-core
insights/client/mount.py
DockerMount.mount
def mount(self, identifier): """ Mounts a container or image referred to by identifier to the host filesystem. """ driver = self.client.info()['Driver'] driver_mount_fn = getattr(self, "_mount_" + driver, self._unsupported_backend) cid = driver_mount_fn(identifier) # Return mount path so it can be later unmounted by path return self.mountpoint, cid
python
def mount(self, identifier): """ Mounts a container or image referred to by identifier to the host filesystem. """ driver = self.client.info()['Driver'] driver_mount_fn = getattr(self, "_mount_" + driver, self._unsupported_backend) cid = driver_mount_fn(identifier) # Return mount path so it can be later unmounted by path return self.mountpoint, cid
[ "def", "mount", "(", "self", ",", "identifier", ")", ":", "driver", "=", "self", ".", "client", ".", "info", "(", ")", "[", "'Driver'", "]", "driver_mount_fn", "=", "getattr", "(", "self", ",", "\"_mount_\"", "+", "driver", ",", "self", ".", "_unsuppor...
Mounts a container or image referred to by identifier to the host filesystem.
[ "Mounts", "a", "container", "or", "image", "referred", "to", "by", "identifier", "to", "the", "host", "filesystem", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L274-L286
225,962
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._mount_devicemapper
def _mount_devicemapper(self, identifier): """ Devicemapper mount backend. """ info = self.client.info() # cid is the contaienr_id of the temp container cid = self._identifier_as_cid(identifier) cinfo = self.client.inspect_container(cid) dm_dev_name, dm_dev_id, dm_dev_size = '', '', '' dm_pool = info['DriverStatus'][0][1] try: dm_dev_name = cinfo['GraphDriver']['Data']['DeviceName'] dm_dev_id = cinfo['GraphDriver']['Data']['DeviceId'] dm_dev_size = cinfo['GraphDriver']['Data']['DeviceSize'] except: # TODO: deprecated when GraphDriver patch makes it upstream dm_dev_id, dm_dev_size = DockerMount._no_gd_api_dm(cid) dm_dev_name = dm_pool.replace('pool', cid) # grab list of devces dmsetupLs = dmsetupWrap.getDmsetupLs() if dmsetupLs == -1: raise MountError('Error: dmsetup returned non zero error ') # ENSURE device exists! if dm_dev_name not in dmsetupLs: # IF device doesn't exist yet we create it! Mount._activate_thin_device(dm_dev_name, dm_dev_id, dm_dev_size, dm_pool) # check that device is shown in /dev/mapper, if not we can use the # major minor numbers in /dev/block mapperDir = os.path.join('/dev/mapper', dm_dev_name) if os.path.exists(mapperDir): dm_dev_path = mapperDir else: # get new dmsetupLs after device has been created! dmsetupLs = dmsetupWrap.getDmsetupLs() # test if device exists in dmsetupls, if so, get its majorminor found in /dev/block majorMinor = dmsetupWrap.getMajorMinor(dm_dev_name, dmsetupLs) blockDir = os.path.join('/dev/block', majorMinor) # FIXME, coudl be due to Virtual box, but occasionally the block device # will not be created by the time we check it exists below, so we # can wait a half a second to let it be created up import time time.sleep(0.1) if os.path.exists(blockDir): dm_dev_path = blockDir else: raise MountError('Error: Block device found in dmsetup ls ' 'but not in /dev/mapper/ or /dev/block') options = ['ro', 'nosuid', 'nodev'] # XFS should get nouuid fstype = Mount._get_fs(dm_dev_path).decode(sys.getdefaultencoding()) if fstype.upper() == 'XFS' and 'nouuid' not in options: if 'nouuid' not in options: options.append('nouuid') try: Mount.mount_path(dm_dev_path, self.mountpoint) except MountError as de: self._cleanup_container(cinfo) Mount.remove_thin_device(dm_dev_name) raise de # return the temp container ID so we can unmount later return cid
python
def _mount_devicemapper(self, identifier): """ Devicemapper mount backend. """ info = self.client.info() # cid is the contaienr_id of the temp container cid = self._identifier_as_cid(identifier) cinfo = self.client.inspect_container(cid) dm_dev_name, dm_dev_id, dm_dev_size = '', '', '' dm_pool = info['DriverStatus'][0][1] try: dm_dev_name = cinfo['GraphDriver']['Data']['DeviceName'] dm_dev_id = cinfo['GraphDriver']['Data']['DeviceId'] dm_dev_size = cinfo['GraphDriver']['Data']['DeviceSize'] except: # TODO: deprecated when GraphDriver patch makes it upstream dm_dev_id, dm_dev_size = DockerMount._no_gd_api_dm(cid) dm_dev_name = dm_pool.replace('pool', cid) # grab list of devces dmsetupLs = dmsetupWrap.getDmsetupLs() if dmsetupLs == -1: raise MountError('Error: dmsetup returned non zero error ') # ENSURE device exists! if dm_dev_name not in dmsetupLs: # IF device doesn't exist yet we create it! Mount._activate_thin_device(dm_dev_name, dm_dev_id, dm_dev_size, dm_pool) # check that device is shown in /dev/mapper, if not we can use the # major minor numbers in /dev/block mapperDir = os.path.join('/dev/mapper', dm_dev_name) if os.path.exists(mapperDir): dm_dev_path = mapperDir else: # get new dmsetupLs after device has been created! dmsetupLs = dmsetupWrap.getDmsetupLs() # test if device exists in dmsetupls, if so, get its majorminor found in /dev/block majorMinor = dmsetupWrap.getMajorMinor(dm_dev_name, dmsetupLs) blockDir = os.path.join('/dev/block', majorMinor) # FIXME, coudl be due to Virtual box, but occasionally the block device # will not be created by the time we check it exists below, so we # can wait a half a second to let it be created up import time time.sleep(0.1) if os.path.exists(blockDir): dm_dev_path = blockDir else: raise MountError('Error: Block device found in dmsetup ls ' 'but not in /dev/mapper/ or /dev/block') options = ['ro', 'nosuid', 'nodev'] # XFS should get nouuid fstype = Mount._get_fs(dm_dev_path).decode(sys.getdefaultencoding()) if fstype.upper() == 'XFS' and 'nouuid' not in options: if 'nouuid' not in options: options.append('nouuid') try: Mount.mount_path(dm_dev_path, self.mountpoint) except MountError as de: self._cleanup_container(cinfo) Mount.remove_thin_device(dm_dev_name) raise de # return the temp container ID so we can unmount later return cid
[ "def", "_mount_devicemapper", "(", "self", ",", "identifier", ")", ":", "info", "=", "self", ".", "client", ".", "info", "(", ")", "# cid is the contaienr_id of the temp container", "cid", "=", "self", ".", "_identifier_as_cid", "(", "identifier", ")", "cinfo", ...
Devicemapper mount backend.
[ "Devicemapper", "mount", "backend", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L293-L366
225,963
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._mount_overlay
def _mount_overlay(self, identifier): """ OverlayFS mount backend. """ cid = self._identifier_as_cid(identifier) cinfo = self.client.inspect_container(cid) ld, ud, wd = '', '', '' try: ld = cinfo['GraphDriver']['Data']['lowerDir'] ud = cinfo['GraphDriver']['Data']['upperDir'] wd = cinfo['GraphDriver']['Data']['workDir'] except: ld, ud, wd = DockerMount._no_gd_api_overlay(cid) options = ['ro', 'lowerdir=' + ld, 'upperdir=' + ud, 'workdir=' + wd] optstring = ','.join(options) cmd = ['mount', '-t', 'overlay', '-o', optstring, 'overlay', self.mountpoint] status = util.subp(cmd) if status.return_code != 0: self._cleanup_container(cinfo) raise MountError('Failed to mount OverlayFS device.\n%s' % status.stderr.decode(sys.getdefaultencoding())) return cid
python
def _mount_overlay(self, identifier): """ OverlayFS mount backend. """ cid = self._identifier_as_cid(identifier) cinfo = self.client.inspect_container(cid) ld, ud, wd = '', '', '' try: ld = cinfo['GraphDriver']['Data']['lowerDir'] ud = cinfo['GraphDriver']['Data']['upperDir'] wd = cinfo['GraphDriver']['Data']['workDir'] except: ld, ud, wd = DockerMount._no_gd_api_overlay(cid) options = ['ro', 'lowerdir=' + ld, 'upperdir=' + ud, 'workdir=' + wd] optstring = ','.join(options) cmd = ['mount', '-t', 'overlay', '-o', optstring, 'overlay', self.mountpoint] status = util.subp(cmd) if status.return_code != 0: self._cleanup_container(cinfo) raise MountError('Failed to mount OverlayFS device.\n%s' % status.stderr.decode(sys.getdefaultencoding())) return cid
[ "def", "_mount_overlay", "(", "self", ",", "identifier", ")", ":", "cid", "=", "self", ".", "_identifier_as_cid", "(", "identifier", ")", "cinfo", "=", "self", ".", "client", ".", "inspect_container", "(", "cid", ")", "ld", ",", "ud", ",", "wd", "=", "...
OverlayFS mount backend.
[ "OverlayFS", "mount", "backend", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L368-L394
225,964
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._cleanup_container
def _cleanup_container(self, cinfo): """ Remove a container and clean up its image if necessary. """ # I'm not a fan of doing this again here. env = cinfo['Config']['Env'] if (env and '_ATOMIC_TEMP_CONTAINER' not in env) or not env: return iid = cinfo['Image'] self.client.remove_container(cinfo['Id']) try: labels = self.client.inspect_image(iid)['Config']['Labels'] except TypeError: labels = {} if labels and 'io.projectatomic.Temporary' in labels: if labels['io.projectatomic.Temporary'] == 'true': self.client.remove_image(iid)
python
def _cleanup_container(self, cinfo): """ Remove a container and clean up its image if necessary. """ # I'm not a fan of doing this again here. env = cinfo['Config']['Env'] if (env and '_ATOMIC_TEMP_CONTAINER' not in env) or not env: return iid = cinfo['Image'] self.client.remove_container(cinfo['Id']) try: labels = self.client.inspect_image(iid)['Config']['Labels'] except TypeError: labels = {} if labels and 'io.projectatomic.Temporary' in labels: if labels['io.projectatomic.Temporary'] == 'true': self.client.remove_image(iid)
[ "def", "_cleanup_container", "(", "self", ",", "cinfo", ")", ":", "# I'm not a fan of doing this again here.", "env", "=", "cinfo", "[", "'Config'", "]", "[", "'Env'", "]", "if", "(", "env", "and", "'_ATOMIC_TEMP_CONTAINER'", "not", "in", "env", ")", "or", "no...
Remove a container and clean up its image if necessary.
[ "Remove", "a", "container", "and", "clean", "up", "its", "image", "if", "necessary", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L396-L413
225,965
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._unmount_devicemapper
def _unmount_devicemapper(self, cid): """ Devicemapper unmount backend. """ mountpoint = self.mountpoint Mount.unmount_path(mountpoint) cinfo = self.client.inspect_container(cid) dev_name = cinfo['GraphDriver']['Data']['DeviceName'] Mount.remove_thin_device(dev_name) self._cleanup_container(cinfo)
python
def _unmount_devicemapper(self, cid): """ Devicemapper unmount backend. """ mountpoint = self.mountpoint Mount.unmount_path(mountpoint) cinfo = self.client.inspect_container(cid) dev_name = cinfo['GraphDriver']['Data']['DeviceName'] Mount.remove_thin_device(dev_name) self._cleanup_container(cinfo)
[ "def", "_unmount_devicemapper", "(", "self", ",", "cid", ")", ":", "mountpoint", "=", "self", ".", "mountpoint", "Mount", ".", "unmount_path", "(", "mountpoint", ")", "cinfo", "=", "self", ".", "client", ".", "inspect_container", "(", "cid", ")", "dev_name",...
Devicemapper unmount backend.
[ "Devicemapper", "unmount", "backend", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L430-L441
225,966
RedHatInsights/insights-core
insights/client/mount.py
DockerMount._unmount_overlay
def _unmount_overlay(self, cid): """ OverlayFS unmount backend. """ mountpoint = self.mountpoint Mount.unmount_path(mountpoint) self._cleanup_container(self.client.inspect_container(cid))
python
def _unmount_overlay(self, cid): """ OverlayFS unmount backend. """ mountpoint = self.mountpoint Mount.unmount_path(mountpoint) self._cleanup_container(self.client.inspect_container(cid))
[ "def", "_unmount_overlay", "(", "self", ",", "cid", ")", ":", "mountpoint", "=", "self", ".", "mountpoint", "Mount", ".", "unmount_path", "(", "mountpoint", ")", "self", ".", "_cleanup_container", "(", "self", ".", "client", ".", "inspect_container", "(", "c...
OverlayFS unmount backend.
[ "OverlayFS", "unmount", "backend", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/mount.py#L443-L449
225,967
RedHatInsights/insights-core
insights/specs/jdr_archive.py
JDRSpecs.jboss_standalone_conf_file
def jboss_standalone_conf_file(broker): """Get which jboss standalone conf file is using from server log""" log_files = broker[JDRSpecs.jboss_standalone_server_log] if log_files: log_content = log_files[-1].content results = [] for line in log_content: if "sun.java.command =" in line and ".jdr" not in line and "-Djboss.server.base.dir" in line: results.append(line) if results: # default is standalone.xml config_xml = 'standalone.xml' java_command = results[-1] if '--server-config' in java_command: config_xml = java_command.split('--server-config=')[1].split()[0] elif '-c ' in java_command: config_xml = java_command.split('-c ')[1].split()[0] return [config_xml] return []
python
def jboss_standalone_conf_file(broker): """Get which jboss standalone conf file is using from server log""" log_files = broker[JDRSpecs.jboss_standalone_server_log] if log_files: log_content = log_files[-1].content results = [] for line in log_content: if "sun.java.command =" in line and ".jdr" not in line and "-Djboss.server.base.dir" in line: results.append(line) if results: # default is standalone.xml config_xml = 'standalone.xml' java_command = results[-1] if '--server-config' in java_command: config_xml = java_command.split('--server-config=')[1].split()[0] elif '-c ' in java_command: config_xml = java_command.split('-c ')[1].split()[0] return [config_xml] return []
[ "def", "jboss_standalone_conf_file", "(", "broker", ")", ":", "log_files", "=", "broker", "[", "JDRSpecs", ".", "jboss_standalone_server_log", "]", "if", "log_files", ":", "log_content", "=", "log_files", "[", "-", "1", "]", ".", "content", "results", "=", "["...
Get which jboss standalone conf file is using from server log
[ "Get", "which", "jboss", "standalone", "conf", "file", "is", "using", "from", "server", "log" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/specs/jdr_archive.py#L23-L41
225,968
RedHatInsights/insights-core
insights/util/__init__.py
parse_bool
def parse_bool(s, default=False): """ Return the boolean value of an English string or default if it can't be determined. """ if s is None: return default return TRUTH.get(s.lower(), default)
python
def parse_bool(s, default=False): """ Return the boolean value of an English string or default if it can't be determined. """ if s is None: return default return TRUTH.get(s.lower(), default)
[ "def", "parse_bool", "(", "s", ",", "default", "=", "False", ")", ":", "if", "s", "is", "None", ":", "return", "default", "return", "TRUTH", ".", "get", "(", "s", ".", "lower", "(", ")", ",", "default", ")" ]
Return the boolean value of an English string or default if it can't be determined.
[ "Return", "the", "boolean", "value", "of", "an", "English", "string", "or", "default", "if", "it", "can", "t", "be", "determined", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/__init__.py#L22-L29
225,969
RedHatInsights/insights-core
insights/util/__init__.py
defaults
def defaults(default=None): """ Catches any exception thrown by the wrapped function and returns `default` instead. Parameters ---------- default : object The default value to return if the wrapped function throws an exception """ def _f(func): @functools.wraps(func) def __f(self, *args, **kwargs): try: return func(self, *args, **kwargs) except Exception: return default return __f return _f
python
def defaults(default=None): """ Catches any exception thrown by the wrapped function and returns `default` instead. Parameters ---------- default : object The default value to return if the wrapped function throws an exception """ def _f(func): @functools.wraps(func) def __f(self, *args, **kwargs): try: return func(self, *args, **kwargs) except Exception: return default return __f return _f
[ "def", "defaults", "(", "default", "=", "None", ")", ":", "def", "_f", "(", "func", ")", ":", "@", "functools", ".", "wraps", "(", "func", ")", "def", "__f", "(", "self", ",", "*", "args", ",", "*", "*", "kwargs", ")", ":", "try", ":", "return"...
Catches any exception thrown by the wrapped function and returns `default` instead. Parameters ---------- default : object The default value to return if the wrapped function throws an exception
[ "Catches", "any", "exception", "thrown", "by", "the", "wrapped", "function", "and", "returns", "default", "instead", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/__init__.py#L66-L85
225,970
RedHatInsights/insights-core
insights/util/__init__.py
keys_in
def keys_in(items, *args): """ Use this utility function to ensure multiple keys are in one or more dicts. Returns `True` if all keys are present in at least one of the given dicts, otherwise returns `False`. :Parameters: - `items`: Iterable of required keys - Variable number of subsequent arguments, each one being a dict to check. """ found = dict((key, False) for key in items) for d in args: for item in items: if not found[item] and item in d: found[item] = True return all(found.values())
python
def keys_in(items, *args): """ Use this utility function to ensure multiple keys are in one or more dicts. Returns `True` if all keys are present in at least one of the given dicts, otherwise returns `False`. :Parameters: - `items`: Iterable of required keys - Variable number of subsequent arguments, each one being a dict to check. """ found = dict((key, False) for key in items) for d in args: for item in items: if not found[item] and item in d: found[item] = True return all(found.values())
[ "def", "keys_in", "(", "items", ",", "*", "args", ")", ":", "found", "=", "dict", "(", "(", "key", ",", "False", ")", "for", "key", "in", "items", ")", "for", "d", "in", "args", ":", "for", "item", "in", "items", ":", "if", "not", "found", "[",...
Use this utility function to ensure multiple keys are in one or more dicts. Returns `True` if all keys are present in at least one of the given dicts, otherwise returns `False`. :Parameters: - `items`: Iterable of required keys - Variable number of subsequent arguments, each one being a dict to check.
[ "Use", "this", "utility", "function", "to", "ensure", "multiple", "keys", "are", "in", "one", "or", "more", "dicts", ".", "Returns", "True", "if", "all", "keys", "are", "present", "in", "at", "least", "one", "of", "the", "given", "dicts", "otherwise", "r...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/__init__.py#L88-L104
225,971
RedHatInsights/insights-core
insights/util/__init__.py
deprecated
def deprecated(func, solution): """ Mark a parser or combiner as deprecated, and give a message of how to fix this. This will emit a warning in the logs when the function is used. When combined with modifications to conftest, this causes deprecations to become fatal errors when testing, so they get fixed. Arguments: func (function): the function or method being deprecated. solution (str): a string describing the replacement class, method or function that replaces the thing being deprecated. For example, "use the `fnord()` function" or "use the `search()` method with the parameter `name='(value)'`". """ def get_name_line(src): for line in src: if "@" not in line: return line.strip() path = inspect.getsourcefile(func) src, line_no = inspect.getsourcelines(func) name = get_name_line(src) or "Unknown" the_msg = "<{c}> at {p}:{l} is deprecated: {s}".format( c=name, p=path, l=line_no, s=solution ) warnings.warn(the_msg, DeprecationWarning)
python
def deprecated(func, solution): """ Mark a parser or combiner as deprecated, and give a message of how to fix this. This will emit a warning in the logs when the function is used. When combined with modifications to conftest, this causes deprecations to become fatal errors when testing, so they get fixed. Arguments: func (function): the function or method being deprecated. solution (str): a string describing the replacement class, method or function that replaces the thing being deprecated. For example, "use the `fnord()` function" or "use the `search()` method with the parameter `name='(value)'`". """ def get_name_line(src): for line in src: if "@" not in line: return line.strip() path = inspect.getsourcefile(func) src, line_no = inspect.getsourcelines(func) name = get_name_line(src) or "Unknown" the_msg = "<{c}> at {p}:{l} is deprecated: {s}".format( c=name, p=path, l=line_no, s=solution ) warnings.warn(the_msg, DeprecationWarning)
[ "def", "deprecated", "(", "func", ",", "solution", ")", ":", "def", "get_name_line", "(", "src", ")", ":", "for", "line", "in", "src", ":", "if", "\"@\"", "not", "in", "line", ":", "return", "line", ".", "strip", "(", ")", "path", "=", "inspect", "...
Mark a parser or combiner as deprecated, and give a message of how to fix this. This will emit a warning in the logs when the function is used. When combined with modifications to conftest, this causes deprecations to become fatal errors when testing, so they get fixed. Arguments: func (function): the function or method being deprecated. solution (str): a string describing the replacement class, method or function that replaces the thing being deprecated. For example, "use the `fnord()` function" or "use the `search()` method with the parameter `name='(value)'`".
[ "Mark", "a", "parser", "or", "combiner", "as", "deprecated", "and", "give", "a", "message", "of", "how", "to", "fix", "this", ".", "This", "will", "emit", "a", "warning", "in", "the", "logs", "when", "the", "function", "is", "used", ".", "When", "combi...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/__init__.py#L119-L146
225,972
RedHatInsights/insights-core
insights/util/__init__.py
parse_keypair_lines
def parse_keypair_lines(content, delim='|', kv_sep='='): """ Parses a set of entities, where each entity is a set of key-value pairs contained all on one line. Each entity is parsed into a dictionary and added to the list returned from this function. """ r = [] if content: for row in [line for line in content if line]: item_dict = {} for item in row.split(delim): key, value = [i.strip("'\"").strip() for i in item.strip().split(kv_sep)] item_dict[key] = value r.append(item_dict) return r
python
def parse_keypair_lines(content, delim='|', kv_sep='='): """ Parses a set of entities, where each entity is a set of key-value pairs contained all on one line. Each entity is parsed into a dictionary and added to the list returned from this function. """ r = [] if content: for row in [line for line in content if line]: item_dict = {} for item in row.split(delim): key, value = [i.strip("'\"").strip() for i in item.strip().split(kv_sep)] item_dict[key] = value r.append(item_dict) return r
[ "def", "parse_keypair_lines", "(", "content", ",", "delim", "=", "'|'", ",", "kv_sep", "=", "'='", ")", ":", "r", "=", "[", "]", "if", "content", ":", "for", "row", "in", "[", "line", "for", "line", "in", "content", "if", "line", "]", ":", "item_di...
Parses a set of entities, where each entity is a set of key-value pairs contained all on one line. Each entity is parsed into a dictionary and added to the list returned from this function.
[ "Parses", "a", "set", "of", "entities", "where", "each", "entity", "is", "a", "set", "of", "key", "-", "value", "pairs", "contained", "all", "on", "one", "line", ".", "Each", "entity", "is", "parsed", "into", "a", "dictionary", "and", "added", "to", "t...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/__init__.py#L182-L196
225,973
RedHatInsights/insights-core
insights/util/__init__.py
rsplit
def rsplit(_str, seps): """ Splits _str by the first sep in seps that is found from the right side. Returns a tuple without the separator. """ for idx, ch in enumerate(reversed(_str)): if ch in seps: return _str[0:-idx - 1], _str[-idx:]
python
def rsplit(_str, seps): """ Splits _str by the first sep in seps that is found from the right side. Returns a tuple without the separator. """ for idx, ch in enumerate(reversed(_str)): if ch in seps: return _str[0:-idx - 1], _str[-idx:]
[ "def", "rsplit", "(", "_str", ",", "seps", ")", ":", "for", "idx", ",", "ch", "in", "enumerate", "(", "reversed", "(", "_str", ")", ")", ":", "if", "ch", "in", "seps", ":", "return", "_str", "[", "0", ":", "-", "idx", "-", "1", "]", ",", "_st...
Splits _str by the first sep in seps that is found from the right side. Returns a tuple without the separator.
[ "Splits", "_str", "by", "the", "first", "sep", "in", "seps", "that", "is", "found", "from", "the", "right", "side", ".", "Returns", "a", "tuple", "without", "the", "separator", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/__init__.py#L199-L206
225,974
RedHatInsights/insights-core
insights/formats/text.py
HumanReadableFormat.progress_bar
def progress_bar(self, c, broker): """ Print the formated progress information for the processed return types """ v = broker.get(c) if v and isinstance(v, dict) and len(v) > 0 and 'type' in v: if v["type"] in self.responses: print(self.responses[v["type"]].color + self.responses[v["type"]].intl + Style.RESET_ALL, end="", file=self.stream) else: print(".", end="", file=self.stream) elif c in broker.exceptions: self.counts['exception'] += len(broker.exceptions[c]) print(Fore.RED + "E" + Style.RESET_ALL, end="", file=self.stream) return self
python
def progress_bar(self, c, broker): """ Print the formated progress information for the processed return types """ v = broker.get(c) if v and isinstance(v, dict) and len(v) > 0 and 'type' in v: if v["type"] in self.responses: print(self.responses[v["type"]].color + self.responses[v["type"]].intl + Style.RESET_ALL, end="", file=self.stream) else: print(".", end="", file=self.stream) elif c in broker.exceptions: self.counts['exception'] += len(broker.exceptions[c]) print(Fore.RED + "E" + Style.RESET_ALL, end="", file=self.stream) return self
[ "def", "progress_bar", "(", "self", ",", "c", ",", "broker", ")", ":", "v", "=", "broker", ".", "get", "(", "c", ")", "if", "v", "and", "isinstance", "(", "v", ",", "dict", ")", "and", "len", "(", "v", ")", ">", "0", "and", "'type'", "in", "v...
Print the formated progress information for the processed return types
[ "Print", "the", "formated", "progress", "information", "for", "the", "processed", "return", "types" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/formats/text.py#L94-L108
225,975
RedHatInsights/insights-core
insights/formats/text.py
HumanReadableFormat.show_dropped
def show_dropped(self): """ Show dropped files """ ctx = _find_context(self.broker) if ctx and ctx.all_files: ds = self.broker.get_by_type(datasource) vals = [] for v in ds.values(): if isinstance(v, list): vals.extend(d.path for d in v) else: vals.append(v.path) dropped = set(ctx.all_files) - set(vals) pprint("Dropped Files:", stream=self.stream) pprint(dropped, indent=4, stream=self.stream)
python
def show_dropped(self): """ Show dropped files """ ctx = _find_context(self.broker) if ctx and ctx.all_files: ds = self.broker.get_by_type(datasource) vals = [] for v in ds.values(): if isinstance(v, list): vals.extend(d.path for d in v) else: vals.append(v.path) dropped = set(ctx.all_files) - set(vals) pprint("Dropped Files:", stream=self.stream) pprint(dropped, indent=4, stream=self.stream)
[ "def", "show_dropped", "(", "self", ")", ":", "ctx", "=", "_find_context", "(", "self", ".", "broker", ")", "if", "ctx", "and", "ctx", ".", "all_files", ":", "ds", "=", "self", ".", "broker", ".", "get_by_type", "(", "datasource", ")", "vals", "=", "...
Show dropped files
[ "Show", "dropped", "files" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/formats/text.py#L118-L131
225,976
RedHatInsights/insights-core
insights/client/client.py
register
def register(config, pconn): """ Do registration using basic auth """ username = config.username password = config.password authmethod = config.authmethod auto_config = config.auto_config if not username and not password and not auto_config and authmethod == 'BASIC': logger.debug('Username and password must be defined in configuration file with BASIC authentication method.') return False return pconn.register()
python
def register(config, pconn): """ Do registration using basic auth """ username = config.username password = config.password authmethod = config.authmethod auto_config = config.auto_config if not username and not password and not auto_config and authmethod == 'BASIC': logger.debug('Username and password must be defined in configuration file with BASIC authentication method.') return False return pconn.register()
[ "def", "register", "(", "config", ",", "pconn", ")", ":", "username", "=", "config", ".", "username", "password", "=", "config", ".", "password", "authmethod", "=", "config", ".", "authmethod", "auto_config", "=", "config", ".", "auto_config", "if", "not", ...
Do registration using basic auth
[ "Do", "registration", "using", "basic", "auth" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/client.py#L93-L104
225,977
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.validate_gpg_sig
def validate_gpg_sig(self, path, sig=None): """ Validate the collection rules """ logger.debug("Verifying GPG signature of Insights configuration") if sig is None: sig = path + ".asc" command = ("/usr/bin/gpg --no-default-keyring " "--keyring " + constants.pub_gpg_path + " --verify " + sig + " " + path) if not six.PY3: command = command.encode('utf-8', 'ignore') args = shlex.split(command) logger.debug("Executing: %s", args) proc = Popen( args, shell=False, stdout=PIPE, stderr=STDOUT, close_fds=True) stdout, stderr = proc.communicate() logger.debug("STDOUT: %s", stdout) logger.debug("STDERR: %s", stderr) logger.debug("Status: %s", proc.returncode) if proc.returncode: logger.error("ERROR: Unable to validate GPG signature: %s", path) return False else: logger.debug("GPG signature verified") return True
python
def validate_gpg_sig(self, path, sig=None): """ Validate the collection rules """ logger.debug("Verifying GPG signature of Insights configuration") if sig is None: sig = path + ".asc" command = ("/usr/bin/gpg --no-default-keyring " "--keyring " + constants.pub_gpg_path + " --verify " + sig + " " + path) if not six.PY3: command = command.encode('utf-8', 'ignore') args = shlex.split(command) logger.debug("Executing: %s", args) proc = Popen( args, shell=False, stdout=PIPE, stderr=STDOUT, close_fds=True) stdout, stderr = proc.communicate() logger.debug("STDOUT: %s", stdout) logger.debug("STDERR: %s", stderr) logger.debug("Status: %s", proc.returncode) if proc.returncode: logger.error("ERROR: Unable to validate GPG signature: %s", path) return False else: logger.debug("GPG signature verified") return True
[ "def", "validate_gpg_sig", "(", "self", ",", "path", ",", "sig", "=", "None", ")", ":", "logger", ".", "debug", "(", "\"Verifying GPG signature of Insights configuration\"", ")", "if", "sig", "is", "None", ":", "sig", "=", "path", "+", "\".asc\"", "command", ...
Validate the collection rules
[ "Validate", "the", "collection", "rules" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L46-L71
225,978
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.try_disk
def try_disk(self, path, gpg=True): """ Try to load json off disk """ if not os.path.isfile(path): return if not gpg or self.validate_gpg_sig(path): stream = open(path, 'r') json_stream = stream.read() if len(json_stream): try: json_config = json.loads(json_stream) return json_config except ValueError: logger.error("ERROR: Invalid JSON in %s", path) return False else: logger.warn("WARNING: %s was an empty file", path) return
python
def try_disk(self, path, gpg=True): """ Try to load json off disk """ if not os.path.isfile(path): return if not gpg or self.validate_gpg_sig(path): stream = open(path, 'r') json_stream = stream.read() if len(json_stream): try: json_config = json.loads(json_stream) return json_config except ValueError: logger.error("ERROR: Invalid JSON in %s", path) return False else: logger.warn("WARNING: %s was an empty file", path) return
[ "def", "try_disk", "(", "self", ",", "path", ",", "gpg", "=", "True", ")", ":", "if", "not", "os", ".", "path", ".", "isfile", "(", "path", ")", ":", "return", "if", "not", "gpg", "or", "self", ".", "validate_gpg_sig", "(", "path", ")", ":", "str...
Try to load json off disk
[ "Try", "to", "load", "json", "off", "disk" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L73-L92
225,979
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.get_collection_rules
def get_collection_rules(self, raw=False): """ Download the collection rules """ logger.debug("Attemping to download collection rules from %s", self.collection_rules_url) net_logger.info("GET %s", self.collection_rules_url) try: req = self.conn.session.get( self.collection_rules_url, headers=({'accept': 'text/plain'})) if req.status_code == 200: logger.debug("Successfully downloaded collection rules") json_response = NamedTemporaryFile() json_response.write(req.text.encode('utf-8')) json_response.file.flush() else: logger.error("ERROR: Could not download dynamic configuration") logger.error("Debug Info: \nConf status: %s", req.status_code) logger.error("Debug Info: \nConf message: %s", req.text) return None except requests.ConnectionError as e: logger.error( "ERROR: Could not download dynamic configuration: %s", e) return None if self.gpg: self.get_collection_rules_gpg(json_response) self.write_collection_data(self.collection_rules_file, req.text) if raw: return req.text else: return json.loads(req.text)
python
def get_collection_rules(self, raw=False): """ Download the collection rules """ logger.debug("Attemping to download collection rules from %s", self.collection_rules_url) net_logger.info("GET %s", self.collection_rules_url) try: req = self.conn.session.get( self.collection_rules_url, headers=({'accept': 'text/plain'})) if req.status_code == 200: logger.debug("Successfully downloaded collection rules") json_response = NamedTemporaryFile() json_response.write(req.text.encode('utf-8')) json_response.file.flush() else: logger.error("ERROR: Could not download dynamic configuration") logger.error("Debug Info: \nConf status: %s", req.status_code) logger.error("Debug Info: \nConf message: %s", req.text) return None except requests.ConnectionError as e: logger.error( "ERROR: Could not download dynamic configuration: %s", e) return None if self.gpg: self.get_collection_rules_gpg(json_response) self.write_collection_data(self.collection_rules_file, req.text) if raw: return req.text else: return json.loads(req.text)
[ "def", "get_collection_rules", "(", "self", ",", "raw", "=", "False", ")", ":", "logger", ".", "debug", "(", "\"Attemping to download collection rules from %s\"", ",", "self", ".", "collection_rules_url", ")", "net_logger", ".", "info", "(", "\"GET %s\"", ",", "se...
Download the collection rules
[ "Download", "the", "collection", "rules" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L94-L130
225,980
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.get_collection_rules_gpg
def get_collection_rules_gpg(self, collection_rules): """ Download the collection rules gpg signature """ sig_text = self.fetch_gpg() sig_response = NamedTemporaryFile(suffix=".asc") sig_response.write(sig_text.encode('utf-8')) sig_response.file.flush() self.validate_gpg_sig(collection_rules.name, sig_response.name) self.write_collection_data(self.collection_rules_file + ".asc", sig_text)
python
def get_collection_rules_gpg(self, collection_rules): """ Download the collection rules gpg signature """ sig_text = self.fetch_gpg() sig_response = NamedTemporaryFile(suffix=".asc") sig_response.write(sig_text.encode('utf-8')) sig_response.file.flush() self.validate_gpg_sig(collection_rules.name, sig_response.name) self.write_collection_data(self.collection_rules_file + ".asc", sig_text)
[ "def", "get_collection_rules_gpg", "(", "self", ",", "collection_rules", ")", ":", "sig_text", "=", "self", ".", "fetch_gpg", "(", ")", "sig_response", "=", "NamedTemporaryFile", "(", "suffix", "=", "\".asc\"", ")", "sig_response", ".", "write", "(", "sig_text",...
Download the collection rules gpg signature
[ "Download", "the", "collection", "rules", "gpg", "signature" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L149-L158
225,981
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.write_collection_data
def write_collection_data(self, path, data): """ Write collections rules to disk """ flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC fd = os.open(path, flags, 0o600) with os.fdopen(fd, 'w') as dyn_conf_file: dyn_conf_file.write(data)
python
def write_collection_data(self, path, data): """ Write collections rules to disk """ flags = os.O_WRONLY | os.O_CREAT | os.O_TRUNC fd = os.open(path, flags, 0o600) with os.fdopen(fd, 'w') as dyn_conf_file: dyn_conf_file.write(data)
[ "def", "write_collection_data", "(", "self", ",", "path", ",", "data", ")", ":", "flags", "=", "os", ".", "O_WRONLY", "|", "os", ".", "O_CREAT", "|", "os", ".", "O_TRUNC", "fd", "=", "os", ".", "open", "(", "path", ",", "flags", ",", "0o600", ")", ...
Write collections rules to disk
[ "Write", "collections", "rules", "to", "disk" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L160-L167
225,982
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.get_conf_file
def get_conf_file(self): """ Get config from local config file, first try cache, then fallback. """ for conf_file in [self.collection_rules_file, self.fallback_file]: logger.debug("trying to read conf from: " + conf_file) conf = self.try_disk(conf_file, self.gpg) if not conf: continue version = conf.get('version', None) if version is None: raise ValueError("ERROR: Could not find version in json") conf['file'] = conf_file logger.debug("Success reading config") logger.debug(json.dumps(conf)) return conf raise ValueError("ERROR: Unable to download conf or read it from disk!")
python
def get_conf_file(self): """ Get config from local config file, first try cache, then fallback. """ for conf_file in [self.collection_rules_file, self.fallback_file]: logger.debug("trying to read conf from: " + conf_file) conf = self.try_disk(conf_file, self.gpg) if not conf: continue version = conf.get('version', None) if version is None: raise ValueError("ERROR: Could not find version in json") conf['file'] = conf_file logger.debug("Success reading config") logger.debug(json.dumps(conf)) return conf raise ValueError("ERROR: Unable to download conf or read it from disk!")
[ "def", "get_conf_file", "(", "self", ")", ":", "for", "conf_file", "in", "[", "self", ".", "collection_rules_file", ",", "self", ".", "fallback_file", "]", ":", "logger", ".", "debug", "(", "\"trying to read conf from: \"", "+", "conf_file", ")", "conf", "=", ...
Get config from local config file, first try cache, then fallback.
[ "Get", "config", "from", "local", "config", "file", "first", "try", "cache", "then", "fallback", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L169-L189
225,983
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.get_conf_update
def get_conf_update(self): """ Get updated config from URL, fallback to local file if download fails. """ dyn_conf = self.get_collection_rules() if not dyn_conf: return self.get_conf_file() version = dyn_conf.get('version', None) if version is None: raise ValueError("ERROR: Could not find version in json") dyn_conf['file'] = self.collection_rules_file logger.debug("Success reading config") config_hash = hashlib.sha1(json.dumps(dyn_conf).encode('utf-8')).hexdigest() logger.debug('sha1 of config: %s', config_hash) return dyn_conf
python
def get_conf_update(self): """ Get updated config from URL, fallback to local file if download fails. """ dyn_conf = self.get_collection_rules() if not dyn_conf: return self.get_conf_file() version = dyn_conf.get('version', None) if version is None: raise ValueError("ERROR: Could not find version in json") dyn_conf['file'] = self.collection_rules_file logger.debug("Success reading config") config_hash = hashlib.sha1(json.dumps(dyn_conf).encode('utf-8')).hexdigest() logger.debug('sha1 of config: %s', config_hash) return dyn_conf
[ "def", "get_conf_update", "(", "self", ")", ":", "dyn_conf", "=", "self", ".", "get_collection_rules", "(", ")", "if", "not", "dyn_conf", ":", "return", "self", ".", "get_conf_file", "(", ")", "version", "=", "dyn_conf", ".", "get", "(", "'version'", ",", ...
Get updated config from URL, fallback to local file if download fails.
[ "Get", "updated", "config", "from", "URL", "fallback", "to", "local", "file", "if", "download", "fails", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L191-L208
225,984
RedHatInsights/insights-core
insights/client/collection_rules.py
InsightsUploadConf.get_rm_conf
def get_rm_conf(self): """ Get excluded files config from remove_file. """ if not os.path.isfile(self.remove_file): return None # Convert config object into dict parsedconfig = ConfigParser.RawConfigParser() parsedconfig.read(self.remove_file) rm_conf = {} for item, value in parsedconfig.items('remove'): if six.PY3: rm_conf[item] = value.strip().encode('utf-8').decode('unicode-escape').split(',') else: rm_conf[item] = value.strip().decode('string-escape').split(',') return rm_conf
python
def get_rm_conf(self): """ Get excluded files config from remove_file. """ if not os.path.isfile(self.remove_file): return None # Convert config object into dict parsedconfig = ConfigParser.RawConfigParser() parsedconfig.read(self.remove_file) rm_conf = {} for item, value in parsedconfig.items('remove'): if six.PY3: rm_conf[item] = value.strip().encode('utf-8').decode('unicode-escape').split(',') else: rm_conf[item] = value.strip().decode('string-escape').split(',') return rm_conf
[ "def", "get_rm_conf", "(", "self", ")", ":", "if", "not", "os", ".", "path", ".", "isfile", "(", "self", ".", "remove_file", ")", ":", "return", "None", "# Convert config object into dict", "parsedconfig", "=", "ConfigParser", ".", "RawConfigParser", "(", ")",...
Get excluded files config from remove_file.
[ "Get", "excluded", "files", "config", "from", "remove_file", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/client/collection_rules.py#L210-L228
225,985
RedHatInsights/insights-core
insights/util/streams.py
stream
def stream(command, stdin=None, env=os.environ, timeout=None): """ Yields a generator of a command's output. For line oriented commands only. Args: command (str or list): a command without pipes. If it's not a list, ``shlex.split`` is applied. stdin (file like object): stream to use as the command's standard input. env (dict): The environment in which to execute the command. PATH should be defined. timeout (int): Amount of time in seconds to give the command to complete. The ``timeout`` utility must be installed to use this feature. Yields: The output stream for the command. It should typically be wrapped in a ``reader``. """ if not isinstance(command, list): command = shlex.split(command) cmd = which(command[0]) if cmd is None: path = env.get("PATH", "") raise Exception("Command [%s] not in PATH [%s]" % (command[0], path)) command[0] = cmd if timeout: if not timeout_command[0]: raise Exception("Timeout specified but timeout command not available.") command = timeout_command + [str(timeout)] + command output = None try: output = Popen(command, env=env, stdin=stdin, **stream_options) yield output.stdout finally: if output: output.wait()
python
def stream(command, stdin=None, env=os.environ, timeout=None): """ Yields a generator of a command's output. For line oriented commands only. Args: command (str or list): a command without pipes. If it's not a list, ``shlex.split`` is applied. stdin (file like object): stream to use as the command's standard input. env (dict): The environment in which to execute the command. PATH should be defined. timeout (int): Amount of time in seconds to give the command to complete. The ``timeout`` utility must be installed to use this feature. Yields: The output stream for the command. It should typically be wrapped in a ``reader``. """ if not isinstance(command, list): command = shlex.split(command) cmd = which(command[0]) if cmd is None: path = env.get("PATH", "") raise Exception("Command [%s] not in PATH [%s]" % (command[0], path)) command[0] = cmd if timeout: if not timeout_command[0]: raise Exception("Timeout specified but timeout command not available.") command = timeout_command + [str(timeout)] + command output = None try: output = Popen(command, env=env, stdin=stdin, **stream_options) yield output.stdout finally: if output: output.wait()
[ "def", "stream", "(", "command", ",", "stdin", "=", "None", ",", "env", "=", "os", ".", "environ", ",", "timeout", "=", "None", ")", ":", "if", "not", "isinstance", "(", "command", ",", "list", ")", ":", "command", "=", "shlex", ".", "split", "(", ...
Yields a generator of a command's output. For line oriented commands only. Args: command (str or list): a command without pipes. If it's not a list, ``shlex.split`` is applied. stdin (file like object): stream to use as the command's standard input. env (dict): The environment in which to execute the command. PATH should be defined. timeout (int): Amount of time in seconds to give the command to complete. The ``timeout`` utility must be installed to use this feature. Yields: The output stream for the command. It should typically be wrapped in a ``reader``.
[ "Yields", "a", "generator", "of", "a", "command", "s", "output", ".", "For", "line", "oriented", "commands", "only", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/streams.py#L30-L68
225,986
RedHatInsights/insights-core
insights/util/streams.py
connect
def connect(*cmds, **kwargs): """ Connects multiple command streams together and yields the final stream. Args: cmds (list): list of commands to pipe together. Each command will be an input to ``stream``. stdin (file like object): stream to use as the first command's standard input. env (dict): The environment in which to execute the commands. PATH should be defined. timeout (int): Amount of time in seconds to give the pipeline to complete. The ``timeout`` utility must be installed to use this feature. Yields: The output stream for the final command in the pipeline. It should typically be wrapped in a ``reader``. """ stdin = kwargs.get("stdin") env = kwargs.get("env", os.environ) timeout = kwargs.get("timeout") end = len(cmds) - 1 @contextmanager def inner(idx, inp): with stream(cmds[idx], stdin=inp, env=env, timeout=timeout) as s: if idx == end: yield s else: with inner(idx + 1, s) as c: yield c with inner(0, stdin) as s: yield s
python
def connect(*cmds, **kwargs): """ Connects multiple command streams together and yields the final stream. Args: cmds (list): list of commands to pipe together. Each command will be an input to ``stream``. stdin (file like object): stream to use as the first command's standard input. env (dict): The environment in which to execute the commands. PATH should be defined. timeout (int): Amount of time in seconds to give the pipeline to complete. The ``timeout`` utility must be installed to use this feature. Yields: The output stream for the final command in the pipeline. It should typically be wrapped in a ``reader``. """ stdin = kwargs.get("stdin") env = kwargs.get("env", os.environ) timeout = kwargs.get("timeout") end = len(cmds) - 1 @contextmanager def inner(idx, inp): with stream(cmds[idx], stdin=inp, env=env, timeout=timeout) as s: if idx == end: yield s else: with inner(idx + 1, s) as c: yield c with inner(0, stdin) as s: yield s
[ "def", "connect", "(", "*", "cmds", ",", "*", "*", "kwargs", ")", ":", "stdin", "=", "kwargs", ".", "get", "(", "\"stdin\"", ")", "env", "=", "kwargs", ".", "get", "(", "\"env\"", ",", "os", ".", "environ", ")", "timeout", "=", "kwargs", ".", "ge...
Connects multiple command streams together and yields the final stream. Args: cmds (list): list of commands to pipe together. Each command will be an input to ``stream``. stdin (file like object): stream to use as the first command's standard input. env (dict): The environment in which to execute the commands. PATH should be defined. timeout (int): Amount of time in seconds to give the pipeline to complete. The ``timeout`` utility must be installed to use this feature. Yields: The output stream for the final command in the pipeline. It should typically be wrapped in a ``reader``.
[ "Connects", "multiple", "command", "streams", "together", "and", "yields", "the", "final", "stream", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/util/streams.py#L72-L105
225,987
RedHatInsights/insights-core
insights/core/ls_parser.py
parse_non_selinux
def parse_non_selinux(parts): """ Parse part of an ls output line that isn't selinux. Args: parts (list): A four element list of strings representing the initial parts of an ls line after the permission bits. The parts are link count, owner, group, and everything else. Returns: A dict containing links, owner, group, date, and name. If the line represented a device, major and minor numbers are included. Otherwise, size is included. If the raw name was a symbolic link, link is included. """ links, owner, group, last = parts result = { "links": int(links), "owner": owner, "group": group, } # device numbers only go to 256. # If a comma is in the first four characters, the next two elements are # major and minor device numbers. Otherwise, the next element is the size. if "," in last[:4]: major, minor, rest = last.split(None, 2) result["major"] = int(major.rstrip(",")) result["minor"] = int(minor) else: size, rest = last.split(None, 1) result["size"] = int(size) # The date part is always 12 characters regardless of content. result["date"] = rest[:12] # Jump over the date and the following space to get the path part. path, link = parse_path(rest[13:]) result["name"] = path if link: result["link"] = link return result
python
def parse_non_selinux(parts): """ Parse part of an ls output line that isn't selinux. Args: parts (list): A four element list of strings representing the initial parts of an ls line after the permission bits. The parts are link count, owner, group, and everything else. Returns: A dict containing links, owner, group, date, and name. If the line represented a device, major and minor numbers are included. Otherwise, size is included. If the raw name was a symbolic link, link is included. """ links, owner, group, last = parts result = { "links": int(links), "owner": owner, "group": group, } # device numbers only go to 256. # If a comma is in the first four characters, the next two elements are # major and minor device numbers. Otherwise, the next element is the size. if "," in last[:4]: major, minor, rest = last.split(None, 2) result["major"] = int(major.rstrip(",")) result["minor"] = int(minor) else: size, rest = last.split(None, 1) result["size"] = int(size) # The date part is always 12 characters regardless of content. result["date"] = rest[:12] # Jump over the date and the following space to get the path part. path, link = parse_path(rest[13:]) result["name"] = path if link: result["link"] = link return result
[ "def", "parse_non_selinux", "(", "parts", ")", ":", "links", ",", "owner", ",", "group", ",", "last", "=", "parts", "result", "=", "{", "\"links\"", ":", "int", "(", "links", ")", ",", "\"owner\"", ":", "owner", ",", "\"group\"", ":", "group", ",", "...
Parse part of an ls output line that isn't selinux. Args: parts (list): A four element list of strings representing the initial parts of an ls line after the permission bits. The parts are link count, owner, group, and everything else. Returns: A dict containing links, owner, group, date, and name. If the line represented a device, major and minor numbers are included. Otherwise, size is included. If the raw name was a symbolic link, link is included.
[ "Parse", "part", "of", "an", "ls", "output", "line", "that", "isn", "t", "selinux", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/ls_parser.py#L23-L65
225,988
RedHatInsights/insights-core
insights/core/ls_parser.py
parse_selinux
def parse_selinux(parts): """ Parse part of an ls output line that is selinux. Args: parts (list): A four element list of strings representing the initial parts of an ls line after the permission bits. The parts are owner group, selinux info, and the path. Returns: A dict containing owner, group, se_user, se_role, se_type, se_mls, and name. If the raw name was a symbolic link, link is always included. """ owner, group = parts[:2] selinux = parts[2].split(":") lsel = len(selinux) path, link = parse_path(parts[-1]) result = { "owner": owner, "group": group, "se_user": selinux[0], "se_role": selinux[1] if lsel > 1 else None, "se_type": selinux[2] if lsel > 2 else None, "se_mls": selinux[3] if lsel > 3 else None, "name": path } if link: result["link"] = link return result
python
def parse_selinux(parts): """ Parse part of an ls output line that is selinux. Args: parts (list): A four element list of strings representing the initial parts of an ls line after the permission bits. The parts are owner group, selinux info, and the path. Returns: A dict containing owner, group, se_user, se_role, se_type, se_mls, and name. If the raw name was a symbolic link, link is always included. """ owner, group = parts[:2] selinux = parts[2].split(":") lsel = len(selinux) path, link = parse_path(parts[-1]) result = { "owner": owner, "group": group, "se_user": selinux[0], "se_role": selinux[1] if lsel > 1 else None, "se_type": selinux[2] if lsel > 2 else None, "se_mls": selinux[3] if lsel > 3 else None, "name": path } if link: result["link"] = link return result
[ "def", "parse_selinux", "(", "parts", ")", ":", "owner", ",", "group", "=", "parts", "[", ":", "2", "]", "selinux", "=", "parts", "[", "2", "]", ".", "split", "(", "\":\"", ")", "lsel", "=", "len", "(", "selinux", ")", "path", ",", "link", "=", ...
Parse part of an ls output line that is selinux. Args: parts (list): A four element list of strings representing the initial parts of an ls line after the permission bits. The parts are owner group, selinux info, and the path. Returns: A dict containing owner, group, se_user, se_role, se_type, se_mls, and name. If the raw name was a symbolic link, link is always included.
[ "Parse", "part", "of", "an", "ls", "output", "line", "that", "is", "selinux", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/ls_parser.py#L68-L98
225,989
RedHatInsights/insights-core
insights/core/ls_parser.py
parse
def parse(lines, root=None): """ Parses a list of lines from ls into dictionaries representing their components. Args: lines (list): A list of lines generated by ls. root (str): The directory name to be used for ls output stanzas that don't have a name. Returns: A dictionary representing the ls output. It's keyed by the path containing each ls stanza. """ doc = {} entries = [] name = None total = None for line in lines: line = line.strip() if not line: continue if line and line[0] == "/" and line[-1] == ":": if name is None: name = line[:-1] if entries: d = Directory(name, total or len(entries), entries) doc[root] = d total = None entries = [] else: d = Directory(name, total or len(entries), entries) doc[name or root] = d total = None entries = [] name = line[:-1] continue if line.startswith("total"): total = int(line.split(None, 1)[1]) continue entries.append(line) name = name or root doc[name] = Directory(name, total or len(entries), entries) return doc
python
def parse(lines, root=None): """ Parses a list of lines from ls into dictionaries representing their components. Args: lines (list): A list of lines generated by ls. root (str): The directory name to be used for ls output stanzas that don't have a name. Returns: A dictionary representing the ls output. It's keyed by the path containing each ls stanza. """ doc = {} entries = [] name = None total = None for line in lines: line = line.strip() if not line: continue if line and line[0] == "/" and line[-1] == ":": if name is None: name = line[:-1] if entries: d = Directory(name, total or len(entries), entries) doc[root] = d total = None entries = [] else: d = Directory(name, total or len(entries), entries) doc[name or root] = d total = None entries = [] name = line[:-1] continue if line.startswith("total"): total = int(line.split(None, 1)[1]) continue entries.append(line) name = name or root doc[name] = Directory(name, total or len(entries), entries) return doc
[ "def", "parse", "(", "lines", ",", "root", "=", "None", ")", ":", "doc", "=", "{", "}", "entries", "=", "[", "]", "name", "=", "None", "total", "=", "None", "for", "line", "in", "lines", ":", "line", "=", "line", ".", "strip", "(", ")", "if", ...
Parses a list of lines from ls into dictionaries representing their components. Args: lines (list): A list of lines generated by ls. root (str): The directory name to be used for ls output stanzas that don't have a name. Returns: A dictionary representing the ls output. It's keyed by the path containing each ls stanza.
[ "Parses", "a", "list", "of", "lines", "from", "ls", "into", "dictionaries", "representing", "their", "components", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/ls_parser.py#L181-L224
225,990
RedHatInsights/insights-core
insights/parsers/__init__.py
get_active_lines
def get_active_lines(lines, comment_char="#"): """ Returns lines, or parts of lines, from content that are not commented out or completely empty. The resulting lines are all individually stripped. This is useful for parsing many config files such as ifcfg. Parameters: lines (list): List of strings to parse. comment_char (str): String indicating that all chars following are part of a comment and will be removed from the output. Returns: list: List of valid lines remaining in the input. Examples: >>> lines = [ ... 'First line', ... ' ', ... '# Comment line', ... 'Inline comment # comment', ... ' Whitespace ', ... 'Last line'] >>> get_active_lines(lines) ['First line', 'Inline comment', 'Whitespace', 'Last line'] """ return list(filter(None, (line.split(comment_char, 1)[0].strip() for line in lines)))
python
def get_active_lines(lines, comment_char="#"): """ Returns lines, or parts of lines, from content that are not commented out or completely empty. The resulting lines are all individually stripped. This is useful for parsing many config files such as ifcfg. Parameters: lines (list): List of strings to parse. comment_char (str): String indicating that all chars following are part of a comment and will be removed from the output. Returns: list: List of valid lines remaining in the input. Examples: >>> lines = [ ... 'First line', ... ' ', ... '# Comment line', ... 'Inline comment # comment', ... ' Whitespace ', ... 'Last line'] >>> get_active_lines(lines) ['First line', 'Inline comment', 'Whitespace', 'Last line'] """ return list(filter(None, (line.split(comment_char, 1)[0].strip() for line in lines)))
[ "def", "get_active_lines", "(", "lines", ",", "comment_char", "=", "\"#\"", ")", ":", "return", "list", "(", "filter", "(", "None", ",", "(", "line", ".", "split", "(", "comment_char", ",", "1", ")", "[", "0", "]", ".", "strip", "(", ")", "for", "l...
Returns lines, or parts of lines, from content that are not commented out or completely empty. The resulting lines are all individually stripped. This is useful for parsing many config files such as ifcfg. Parameters: lines (list): List of strings to parse. comment_char (str): String indicating that all chars following are part of a comment and will be removed from the output. Returns: list: List of valid lines remaining in the input. Examples: >>> lines = [ ... 'First line', ... ' ', ... '# Comment line', ... 'Inline comment # comment', ... ' Whitespace ', ... 'Last line'] >>> get_active_lines(lines) ['First line', 'Inline comment', 'Whitespace', 'Last line']
[ "Returns", "lines", "or", "parts", "of", "lines", "from", "content", "that", "are", "not", "commented", "out", "or", "completely", "empty", ".", "The", "resulting", "lines", "are", "all", "individually", "stripped", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/__init__.py#L30-L56
225,991
RedHatInsights/insights-core
insights/parsers/__init__.py
optlist_to_dict
def optlist_to_dict(optlist, opt_sep=',', kv_sep='=', strip_quotes=False): """Parse an option list into a dictionary. Takes a list of options separated by ``opt_sep`` and places them into a dictionary with the default value of ``True``. If ``kv_sep`` option is specified then key/value options ``key=value`` are parsed. Useful for parsing options such as mount options in the format ``rw,ro,rsize=32168,xyz``. Parameters: optlist (str): String of options to parse. opt_sep (str): Separater used to split options. kv_sep (str): If not `None` then `optlist` includes key=value pairs to be split, and this str is used to split them. strip_quotes (bool): If set, will remove matching '"' and '"' characters from start and end of line. No quotes are removed from inside the string and mismatched quotes are not removed. Returns: dict: Returns a dictionary of names present in the list. If `kv_sep` is not `None` then the values will be the str on the right-hand side of `kv_sep`. If `kv_sep` is `None` then each key will have a default value of `True`. Examples: >>> optlist = 'rw,ro,rsize=32168,xyz' >>> optlist_to_dict(optlist) {'rw': True, 'ro': True, 'rsize': '32168', 'xyz': True} """ def make_kv(opt): if kv_sep is not None and kv_sep in opt: k, v = opt.split(kv_sep, 1) k = k.strip() if strip_quotes and v[0] in ('"', "'") and v[-1] == v[0]: return k, v[1:-1] else: return k, v else: return opt, True return dict(make_kv(opt) for opt in optlist.split(opt_sep))
python
def optlist_to_dict(optlist, opt_sep=',', kv_sep='=', strip_quotes=False): """Parse an option list into a dictionary. Takes a list of options separated by ``opt_sep`` and places them into a dictionary with the default value of ``True``. If ``kv_sep`` option is specified then key/value options ``key=value`` are parsed. Useful for parsing options such as mount options in the format ``rw,ro,rsize=32168,xyz``. Parameters: optlist (str): String of options to parse. opt_sep (str): Separater used to split options. kv_sep (str): If not `None` then `optlist` includes key=value pairs to be split, and this str is used to split them. strip_quotes (bool): If set, will remove matching '"' and '"' characters from start and end of line. No quotes are removed from inside the string and mismatched quotes are not removed. Returns: dict: Returns a dictionary of names present in the list. If `kv_sep` is not `None` then the values will be the str on the right-hand side of `kv_sep`. If `kv_sep` is `None` then each key will have a default value of `True`. Examples: >>> optlist = 'rw,ro,rsize=32168,xyz' >>> optlist_to_dict(optlist) {'rw': True, 'ro': True, 'rsize': '32168', 'xyz': True} """ def make_kv(opt): if kv_sep is not None and kv_sep in opt: k, v = opt.split(kv_sep, 1) k = k.strip() if strip_quotes and v[0] in ('"', "'") and v[-1] == v[0]: return k, v[1:-1] else: return k, v else: return opt, True return dict(make_kv(opt) for opt in optlist.split(opt_sep))
[ "def", "optlist_to_dict", "(", "optlist", ",", "opt_sep", "=", "','", ",", "kv_sep", "=", "'='", ",", "strip_quotes", "=", "False", ")", ":", "def", "make_kv", "(", "opt", ")", ":", "if", "kv_sep", "is", "not", "None", "and", "kv_sep", "in", "opt", "...
Parse an option list into a dictionary. Takes a list of options separated by ``opt_sep`` and places them into a dictionary with the default value of ``True``. If ``kv_sep`` option is specified then key/value options ``key=value`` are parsed. Useful for parsing options such as mount options in the format ``rw,ro,rsize=32168,xyz``. Parameters: optlist (str): String of options to parse. opt_sep (str): Separater used to split options. kv_sep (str): If not `None` then `optlist` includes key=value pairs to be split, and this str is used to split them. strip_quotes (bool): If set, will remove matching '"' and '"' characters from start and end of line. No quotes are removed from inside the string and mismatched quotes are not removed. Returns: dict: Returns a dictionary of names present in the list. If `kv_sep` is not `None` then the values will be the str on the right-hand side of `kv_sep`. If `kv_sep` is `None` then each key will have a default value of `True`. Examples: >>> optlist = 'rw,ro,rsize=32168,xyz' >>> optlist_to_dict(optlist) {'rw': True, 'ro': True, 'rsize': '32168', 'xyz': True}
[ "Parse", "an", "option", "list", "into", "a", "dictionary", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/__init__.py#L59-L99
225,992
RedHatInsights/insights-core
insights/parsers/__init__.py
unsplit_lines
def unsplit_lines(lines, cont_char='\\', keep_cont_char=False): """Recombine lines having a continuation character at end. Generator that recombines lines in the list that have the char `cont_char` at the end of a line. If `cont_char` is found in a line then then next line will be appended to the current line, this will continue for multiple continuation lines until the next line is found with no continuation character at the end. All lines found will be combined and returned. If the `keep_cont_char` option is set to True, the continuation character will be left on the end of the line. Otherwise, by default, it is removed. Parameters: lines (list): List of strings to be evaluated. cont_char (char): Char to search for at end of line. Default is ``\\``. keep_cont_char (bool): Whether to keep the continuation on the end of the line. Defaults to False, which causes the continuation character to be removed. Yields: line (str): Yields unsplit lines Examples: >>> lines = ['Line one \\', ' line one part 2', 'Line two'] >>> list(unsplit_lines(lines)) ['Line one line one part 2', 'Line two'] >>> list(unsplit_lines(lines, cont_char='2')) ['Line one \\', ' line one part Line two'] >>> list(unsplit_lines(lines, keep_cont_char=True) ['Line one \ line one part 2', 'Line two'] """ unsplit_lines = [] for line in lines: line = line.rstrip() if line.endswith(cont_char): unsplit_lines.append(line if keep_cont_char else line[:-1]) else: yield ''.join(unsplit_lines) + line unsplit_lines = [] if unsplit_lines: yield ''.join(unsplit_lines)
python
def unsplit_lines(lines, cont_char='\\', keep_cont_char=False): """Recombine lines having a continuation character at end. Generator that recombines lines in the list that have the char `cont_char` at the end of a line. If `cont_char` is found in a line then then next line will be appended to the current line, this will continue for multiple continuation lines until the next line is found with no continuation character at the end. All lines found will be combined and returned. If the `keep_cont_char` option is set to True, the continuation character will be left on the end of the line. Otherwise, by default, it is removed. Parameters: lines (list): List of strings to be evaluated. cont_char (char): Char to search for at end of line. Default is ``\\``. keep_cont_char (bool): Whether to keep the continuation on the end of the line. Defaults to False, which causes the continuation character to be removed. Yields: line (str): Yields unsplit lines Examples: >>> lines = ['Line one \\', ' line one part 2', 'Line two'] >>> list(unsplit_lines(lines)) ['Line one line one part 2', 'Line two'] >>> list(unsplit_lines(lines, cont_char='2')) ['Line one \\', ' line one part Line two'] >>> list(unsplit_lines(lines, keep_cont_char=True) ['Line one \ line one part 2', 'Line two'] """ unsplit_lines = [] for line in lines: line = line.rstrip() if line.endswith(cont_char): unsplit_lines.append(line if keep_cont_char else line[:-1]) else: yield ''.join(unsplit_lines) + line unsplit_lines = [] if unsplit_lines: yield ''.join(unsplit_lines)
[ "def", "unsplit_lines", "(", "lines", ",", "cont_char", "=", "'\\\\'", ",", "keep_cont_char", "=", "False", ")", ":", "unsplit_lines", "=", "[", "]", "for", "line", "in", "lines", ":", "line", "=", "line", ".", "rstrip", "(", ")", "if", "line", ".", ...
Recombine lines having a continuation character at end. Generator that recombines lines in the list that have the char `cont_char` at the end of a line. If `cont_char` is found in a line then then next line will be appended to the current line, this will continue for multiple continuation lines until the next line is found with no continuation character at the end. All lines found will be combined and returned. If the `keep_cont_char` option is set to True, the continuation character will be left on the end of the line. Otherwise, by default, it is removed. Parameters: lines (list): List of strings to be evaluated. cont_char (char): Char to search for at end of line. Default is ``\\``. keep_cont_char (bool): Whether to keep the continuation on the end of the line. Defaults to False, which causes the continuation character to be removed. Yields: line (str): Yields unsplit lines Examples: >>> lines = ['Line one \\', ' line one part 2', 'Line two'] >>> list(unsplit_lines(lines)) ['Line one line one part 2', 'Line two'] >>> list(unsplit_lines(lines, cont_char='2')) ['Line one \\', ' line one part Line two'] >>> list(unsplit_lines(lines, keep_cont_char=True) ['Line one \ line one part 2', 'Line two']
[ "Recombine", "lines", "having", "a", "continuation", "character", "at", "end", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/__init__.py#L179-L220
225,993
RedHatInsights/insights-core
insights/parsers/__init__.py
calc_offset
def calc_offset(lines, target, invert_search=False): """ Function to search for a line in a list starting with a target string. If `target` is `None` or an empty string then `0` is returned. This allows checking `target` here instead of having to check for an empty target in the calling function. Each line is stripped of leading spaces prior to comparison with each target however target is not stripped. See `parse_fixed_table` in this module for sample usage. Arguments: lines (list): List of strings. target (list): List of strings to search for at the beginning of any line in lines. invert_search (boolean): If `True` this flag causes the search to continue until the first line is found not matching anything in target. An empty line is implicitly included in target. Default is `False`. This would typically be used if trimming trailing lines off of a file by passing `reversed(lines)` as the `lines` argument. Returns: int: index into the `lines` indicating the location of `target`. If `target` is `None` or an empty string `0` is returned as the offset. If `invert_search` is `True` the index returned will point to the line after the last target was found. Raises: ValueError: Exception is raised if `target` string is specified and it was not found in the input lines. Examples: >>> lines = [ ... '# ', ... 'Warning line', ... 'Error line', ... ' data 1 line', ... ' data 2 line'] >>> target = ['data'] >>> calc_offset(lines, target) 3 >>> target = ['#', 'Warning', 'Error'] >>> calc_offset(lines, target, invert_search=True) 3 """ if target and target[0] is not None: for offset, line in enumerate(l.strip() for l in lines): found_any = any([line.startswith(t) for t in target]) if not invert_search and found_any: return offset elif invert_search and not(line == '' or found_any): return offset # If we get here then we didn't find any of the targets raise ValueError("Line containing '{}' was not found in table".format(','.join(target))) else: # If no target then return index 0 return 0
python
def calc_offset(lines, target, invert_search=False): """ Function to search for a line in a list starting with a target string. If `target` is `None` or an empty string then `0` is returned. This allows checking `target` here instead of having to check for an empty target in the calling function. Each line is stripped of leading spaces prior to comparison with each target however target is not stripped. See `parse_fixed_table` in this module for sample usage. Arguments: lines (list): List of strings. target (list): List of strings to search for at the beginning of any line in lines. invert_search (boolean): If `True` this flag causes the search to continue until the first line is found not matching anything in target. An empty line is implicitly included in target. Default is `False`. This would typically be used if trimming trailing lines off of a file by passing `reversed(lines)` as the `lines` argument. Returns: int: index into the `lines` indicating the location of `target`. If `target` is `None` or an empty string `0` is returned as the offset. If `invert_search` is `True` the index returned will point to the line after the last target was found. Raises: ValueError: Exception is raised if `target` string is specified and it was not found in the input lines. Examples: >>> lines = [ ... '# ', ... 'Warning line', ... 'Error line', ... ' data 1 line', ... ' data 2 line'] >>> target = ['data'] >>> calc_offset(lines, target) 3 >>> target = ['#', 'Warning', 'Error'] >>> calc_offset(lines, target, invert_search=True) 3 """ if target and target[0] is not None: for offset, line in enumerate(l.strip() for l in lines): found_any = any([line.startswith(t) for t in target]) if not invert_search and found_any: return offset elif invert_search and not(line == '' or found_any): return offset # If we get here then we didn't find any of the targets raise ValueError("Line containing '{}' was not found in table".format(','.join(target))) else: # If no target then return index 0 return 0
[ "def", "calc_offset", "(", "lines", ",", "target", ",", "invert_search", "=", "False", ")", ":", "if", "target", "and", "target", "[", "0", "]", "is", "not", "None", ":", "for", "offset", ",", "line", "in", "enumerate", "(", "l", ".", "strip", "(", ...
Function to search for a line in a list starting with a target string. If `target` is `None` or an empty string then `0` is returned. This allows checking `target` here instead of having to check for an empty target in the calling function. Each line is stripped of leading spaces prior to comparison with each target however target is not stripped. See `parse_fixed_table` in this module for sample usage. Arguments: lines (list): List of strings. target (list): List of strings to search for at the beginning of any line in lines. invert_search (boolean): If `True` this flag causes the search to continue until the first line is found not matching anything in target. An empty line is implicitly included in target. Default is `False`. This would typically be used if trimming trailing lines off of a file by passing `reversed(lines)` as the `lines` argument. Returns: int: index into the `lines` indicating the location of `target`. If `target` is `None` or an empty string `0` is returned as the offset. If `invert_search` is `True` the index returned will point to the line after the last target was found. Raises: ValueError: Exception is raised if `target` string is specified and it was not found in the input lines. Examples: >>> lines = [ ... '# ', ... 'Warning line', ... 'Error line', ... ' data 1 line', ... ' data 2 line'] >>> target = ['data'] >>> calc_offset(lines, target) 3 >>> target = ['#', 'Warning', 'Error'] >>> calc_offset(lines, target, invert_search=True) 3
[ "Function", "to", "search", "for", "a", "line", "in", "a", "list", "starting", "with", "a", "target", "string", ".", "If", "target", "is", "None", "or", "an", "empty", "string", "then", "0", "is", "returned", ".", "This", "allows", "checking", "target", ...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/__init__.py#L223-L278
225,994
RedHatInsights/insights-core
insights/parsers/__init__.py
parse_fixed_table
def parse_fixed_table(table_lines, heading_ignore=[], header_substitute=[], trailing_ignore=[]): """ Function to parse table data containing column headings in the first row and data in fixed positions in each remaining row of table data. Table columns must not contain spaces within the column name. Column headings are assumed to be left justified and the column data width is the width of the heading label plus all whitespace to the right of the label. This function will handle blank columns. Arguments: table_lines (list): List of strings with the first line containing column headings separated by spaces, and the remaining lines containing table data in left justified format. heading_ignore (list): Optional list of strings to search for at beginning of line. All lines before this line will be ignored. If specified then it must be present in the file or `ValueError` will be raised. header_substitute (list): Optional list of tuples containing `(old_string_value, new_string_value)` to be used to modify header values. If whitespace is present in a column it must be replaced with non-whitespace characters in order for the table to be parsed correctly. trailing_ignore (list): Optional list of strings to look for at the end rows of the content. Lines starting with these strings will be ignored, thereby truncating the rows of data. Returns: list: Returns a list of dict for each row of column data. Dict keys are the column headings in the same case as input. Raises: ValueError: Raised if `heading_ignore` is specified and not found in `table_lines`. Sample input:: Column1 Column2 Column3 data1 data 2 data 3 data4 data5 data6 Examples: >>> table_data = parse_fixed_table(table_lines) >>> table_data [{'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data 3'}, {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}] """ def calc_column_indices(line, headers): idx = [] for h in headers: i = idx[-1] + 1 if idx else 0 idx.append(line.index(h, i)) return idx first_line = calc_offset(table_lines, heading_ignore) try: last_line = len(table_lines) - calc_offset(reversed(table_lines), trailing_ignore, invert_search=True) except ValueError: last_line = len(table_lines) header = table_lines[first_line] if header_substitute: for old_val, new_val in header_substitute: header = header.replace(old_val, new_val) col_headers = header.strip().split() col_index = calc_column_indices(header, col_headers) table_data = [] for line in table_lines[first_line + 1:last_line]: col_data = dict( (col_headers[c], line[col_index[c]:col_index[c + 1]].strip()) for c in range(len(col_index) - 1) ) col_data[col_headers[-1]] = line[col_index[-1]:].strip() table_data.append(col_data) return table_data
python
def parse_fixed_table(table_lines, heading_ignore=[], header_substitute=[], trailing_ignore=[]): """ Function to parse table data containing column headings in the first row and data in fixed positions in each remaining row of table data. Table columns must not contain spaces within the column name. Column headings are assumed to be left justified and the column data width is the width of the heading label plus all whitespace to the right of the label. This function will handle blank columns. Arguments: table_lines (list): List of strings with the first line containing column headings separated by spaces, and the remaining lines containing table data in left justified format. heading_ignore (list): Optional list of strings to search for at beginning of line. All lines before this line will be ignored. If specified then it must be present in the file or `ValueError` will be raised. header_substitute (list): Optional list of tuples containing `(old_string_value, new_string_value)` to be used to modify header values. If whitespace is present in a column it must be replaced with non-whitespace characters in order for the table to be parsed correctly. trailing_ignore (list): Optional list of strings to look for at the end rows of the content. Lines starting with these strings will be ignored, thereby truncating the rows of data. Returns: list: Returns a list of dict for each row of column data. Dict keys are the column headings in the same case as input. Raises: ValueError: Raised if `heading_ignore` is specified and not found in `table_lines`. Sample input:: Column1 Column2 Column3 data1 data 2 data 3 data4 data5 data6 Examples: >>> table_data = parse_fixed_table(table_lines) >>> table_data [{'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data 3'}, {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}] """ def calc_column_indices(line, headers): idx = [] for h in headers: i = idx[-1] + 1 if idx else 0 idx.append(line.index(h, i)) return idx first_line = calc_offset(table_lines, heading_ignore) try: last_line = len(table_lines) - calc_offset(reversed(table_lines), trailing_ignore, invert_search=True) except ValueError: last_line = len(table_lines) header = table_lines[first_line] if header_substitute: for old_val, new_val in header_substitute: header = header.replace(old_val, new_val) col_headers = header.strip().split() col_index = calc_column_indices(header, col_headers) table_data = [] for line in table_lines[first_line + 1:last_line]: col_data = dict( (col_headers[c], line[col_index[c]:col_index[c + 1]].strip()) for c in range(len(col_index) - 1) ) col_data[col_headers[-1]] = line[col_index[-1]:].strip() table_data.append(col_data) return table_data
[ "def", "parse_fixed_table", "(", "table_lines", ",", "heading_ignore", "=", "[", "]", ",", "header_substitute", "=", "[", "]", ",", "trailing_ignore", "=", "[", "]", ")", ":", "def", "calc_column_indices", "(", "line", ",", "headers", ")", ":", "idx", "=",...
Function to parse table data containing column headings in the first row and data in fixed positions in each remaining row of table data. Table columns must not contain spaces within the column name. Column headings are assumed to be left justified and the column data width is the width of the heading label plus all whitespace to the right of the label. This function will handle blank columns. Arguments: table_lines (list): List of strings with the first line containing column headings separated by spaces, and the remaining lines containing table data in left justified format. heading_ignore (list): Optional list of strings to search for at beginning of line. All lines before this line will be ignored. If specified then it must be present in the file or `ValueError` will be raised. header_substitute (list): Optional list of tuples containing `(old_string_value, new_string_value)` to be used to modify header values. If whitespace is present in a column it must be replaced with non-whitespace characters in order for the table to be parsed correctly. trailing_ignore (list): Optional list of strings to look for at the end rows of the content. Lines starting with these strings will be ignored, thereby truncating the rows of data. Returns: list: Returns a list of dict for each row of column data. Dict keys are the column headings in the same case as input. Raises: ValueError: Raised if `heading_ignore` is specified and not found in `table_lines`. Sample input:: Column1 Column2 Column3 data1 data 2 data 3 data4 data5 data6 Examples: >>> table_data = parse_fixed_table(table_lines) >>> table_data [{'Column1': 'data1', 'Column2': 'data 2', 'Column3': 'data 3'}, {'Column1': 'data4', 'Column2': 'data5', 'Column3': 'data6'}]
[ "Function", "to", "parse", "table", "data", "containing", "column", "headings", "in", "the", "first", "row", "and", "data", "in", "fixed", "positions", "in", "each", "remaining", "row", "of", "table", "data", ".", "Table", "columns", "must", "not", "contain"...
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/__init__.py#L281-L359
225,995
RedHatInsights/insights-core
insights/parsers/__init__.py
keyword_search
def keyword_search(rows, **kwargs): """ Takes a list of dictionaries and finds all the dictionaries where the keys and values match those found in the keyword arguments. Keys in the row data have ' ' and '-' replaced with '_', so they can match the keyword argument parsing. For example, the keyword argument 'fix_up_path' will match a key named 'fix-up path'. In addition, several suffixes can be added to the key name to do partial matching of values: * '__contains' will test whether the data value contains the given value. * '__startswith' tests if the data value starts with the given value * '__lower_value' compares the lower-case version of the data and given values. Arguments: rows (list): A list of dictionaries representing the data to be searched. **kwargs (dict): keyword-value pairs corresponding to the fields that need to be found and their required values in the data rows. Returns: (list): The list of rows that match the search keywords. If no keyword arguments are given, no rows are returned. Examples: >>> rows = [ ... {'domain': 'oracle', 'type': 'soft', 'item': 'nofile', 'value': 1024}, ... {'domain': 'oracle', 'type': 'hard', 'item': 'nofile', 'value': 65536}, ... {'domain': 'oracle', 'type': 'soft', 'item': 'stack', 'value': 10240}, ... {'domain': 'oracle', 'type': 'hard', 'item': 'stack', 'value': 3276}, ... {'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] ... >>> keyword_search(rows, domain='root') [{'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] >>> keyword_search(rows, item__contains='c') [{'domain': 'oracle', 'type': 'soft', 'item': 'stack', 'value': 10240}, {'domain': 'oracle', 'type': 'hard', 'item': 'stack', 'value': 3276}, {'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] >>> keyword_search(rows, domain__startswith='r') [{'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] """ results = [] if not kwargs: return results # Allows us to transform the key and do lookups like __contains and # __startswith matchers = { 'default': lambda s, v: s == v, 'contains': lambda s, v: v in s, 'startswith': lambda s, v: s.startswith(v), 'lower_value': lambda s, v: s.lower() == v.lower(), } def key_match(row, key, value): # Translate ' ' and '-' of keys in dict to '_' to match keyword arguments. my_row = {} for my_key, val in row.items(): my_row[my_key.replace(' ', '_').replace('-', '_')] = val matcher_fn = matchers['default'] if '__' in key: key, matcher = key.split('__', 1) if matcher not in matchers: # put key back the way we found it, matcher fn unchanged key = key + '__' + matcher else: matcher_fn = matchers[matcher] return key in my_row and matcher_fn(my_row[key], value) data = [] for row in rows: if all(map(lambda kv: key_match(row, kv[0], kv[1]), kwargs.items())): data.append(row) return data
python
def keyword_search(rows, **kwargs): """ Takes a list of dictionaries and finds all the dictionaries where the keys and values match those found in the keyword arguments. Keys in the row data have ' ' and '-' replaced with '_', so they can match the keyword argument parsing. For example, the keyword argument 'fix_up_path' will match a key named 'fix-up path'. In addition, several suffixes can be added to the key name to do partial matching of values: * '__contains' will test whether the data value contains the given value. * '__startswith' tests if the data value starts with the given value * '__lower_value' compares the lower-case version of the data and given values. Arguments: rows (list): A list of dictionaries representing the data to be searched. **kwargs (dict): keyword-value pairs corresponding to the fields that need to be found and their required values in the data rows. Returns: (list): The list of rows that match the search keywords. If no keyword arguments are given, no rows are returned. Examples: >>> rows = [ ... {'domain': 'oracle', 'type': 'soft', 'item': 'nofile', 'value': 1024}, ... {'domain': 'oracle', 'type': 'hard', 'item': 'nofile', 'value': 65536}, ... {'domain': 'oracle', 'type': 'soft', 'item': 'stack', 'value': 10240}, ... {'domain': 'oracle', 'type': 'hard', 'item': 'stack', 'value': 3276}, ... {'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] ... >>> keyword_search(rows, domain='root') [{'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] >>> keyword_search(rows, item__contains='c') [{'domain': 'oracle', 'type': 'soft', 'item': 'stack', 'value': 10240}, {'domain': 'oracle', 'type': 'hard', 'item': 'stack', 'value': 3276}, {'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] >>> keyword_search(rows, domain__startswith='r') [{'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] """ results = [] if not kwargs: return results # Allows us to transform the key and do lookups like __contains and # __startswith matchers = { 'default': lambda s, v: s == v, 'contains': lambda s, v: v in s, 'startswith': lambda s, v: s.startswith(v), 'lower_value': lambda s, v: s.lower() == v.lower(), } def key_match(row, key, value): # Translate ' ' and '-' of keys in dict to '_' to match keyword arguments. my_row = {} for my_key, val in row.items(): my_row[my_key.replace(' ', '_').replace('-', '_')] = val matcher_fn = matchers['default'] if '__' in key: key, matcher = key.split('__', 1) if matcher not in matchers: # put key back the way we found it, matcher fn unchanged key = key + '__' + matcher else: matcher_fn = matchers[matcher] return key in my_row and matcher_fn(my_row[key], value) data = [] for row in rows: if all(map(lambda kv: key_match(row, kv[0], kv[1]), kwargs.items())): data.append(row) return data
[ "def", "keyword_search", "(", "rows", ",", "*", "*", "kwargs", ")", ":", "results", "=", "[", "]", "if", "not", "kwargs", ":", "return", "results", "# Allows us to transform the key and do lookups like __contains and", "# __startswith", "matchers", "=", "{", "'defau...
Takes a list of dictionaries and finds all the dictionaries where the keys and values match those found in the keyword arguments. Keys in the row data have ' ' and '-' replaced with '_', so they can match the keyword argument parsing. For example, the keyword argument 'fix_up_path' will match a key named 'fix-up path'. In addition, several suffixes can be added to the key name to do partial matching of values: * '__contains' will test whether the data value contains the given value. * '__startswith' tests if the data value starts with the given value * '__lower_value' compares the lower-case version of the data and given values. Arguments: rows (list): A list of dictionaries representing the data to be searched. **kwargs (dict): keyword-value pairs corresponding to the fields that need to be found and their required values in the data rows. Returns: (list): The list of rows that match the search keywords. If no keyword arguments are given, no rows are returned. Examples: >>> rows = [ ... {'domain': 'oracle', 'type': 'soft', 'item': 'nofile', 'value': 1024}, ... {'domain': 'oracle', 'type': 'hard', 'item': 'nofile', 'value': 65536}, ... {'domain': 'oracle', 'type': 'soft', 'item': 'stack', 'value': 10240}, ... {'domain': 'oracle', 'type': 'hard', 'item': 'stack', 'value': 3276}, ... {'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] ... >>> keyword_search(rows, domain='root') [{'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] >>> keyword_search(rows, item__contains='c') [{'domain': 'oracle', 'type': 'soft', 'item': 'stack', 'value': 10240}, {'domain': 'oracle', 'type': 'hard', 'item': 'stack', 'value': 3276}, {'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}] >>> keyword_search(rows, domain__startswith='r') [{'domain': 'root', 'type': 'soft', 'item': 'nproc', 'value': -1}]
[ "Takes", "a", "list", "of", "dictionaries", "and", "finds", "all", "the", "dictionaries", "where", "the", "keys", "and", "values", "match", "those", "found", "in", "the", "keyword", "arguments", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/parsers/__init__.py#L451-L528
225,996
RedHatInsights/insights-core
insights/formats/_markdown.py
MarkdownFormat.count_exceptions
def count_exceptions(self, c, broker): """ Count exceptions as processing proceeds """ if c in broker.exceptions: self.counts['exception'] += len(broker.exceptions[c]) return self
python
def count_exceptions(self, c, broker): """ Count exceptions as processing proceeds """ if c in broker.exceptions: self.counts['exception'] += len(broker.exceptions[c]) return self
[ "def", "count_exceptions", "(", "self", ",", "c", ",", "broker", ")", ":", "if", "c", "in", "broker", ".", "exceptions", ":", "self", ".", "counts", "[", "'exception'", "]", "+=", "len", "(", "broker", ".", "exceptions", "[", "c", "]", ")", "return",...
Count exceptions as processing proceeds
[ "Count", "exceptions", "as", "processing", "proceeds" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/formats/_markdown.py#L74-L80
225,997
RedHatInsights/insights-core
examples/cluster_rules/bash_version.py
bash_rule
def bash_rule(bash, hostnames): """ Cluster rule to process bash and hostname info ``bash`` and ``hostnames`` are Pandas DataFrames for the facts collected for each host in the cluster. See https://pandas.pydata.org/pandas-docs/stable/api.html#dataframe for information on available attributes and methods. Arguments: bash (pandas.DataFrame): Includes facts from ``bash_version`` fact with columns "name" and "version" and one row per host in the cluster. hostnames (pandas.DataFrame): Includes facts from ``get_hostname`` fact with column "hostname" and one row per host in the cluster. """ if isinstance(bash, dict): return make_fail('bash_rule', error_message="Run this rule with a cluster archive") return make_pass('bash_rule', bash=bash, hostname=hostnames)
python
def bash_rule(bash, hostnames): """ Cluster rule to process bash and hostname info ``bash`` and ``hostnames`` are Pandas DataFrames for the facts collected for each host in the cluster. See https://pandas.pydata.org/pandas-docs/stable/api.html#dataframe for information on available attributes and methods. Arguments: bash (pandas.DataFrame): Includes facts from ``bash_version`` fact with columns "name" and "version" and one row per host in the cluster. hostnames (pandas.DataFrame): Includes facts from ``get_hostname`` fact with column "hostname" and one row per host in the cluster. """ if isinstance(bash, dict): return make_fail('bash_rule', error_message="Run this rule with a cluster archive") return make_pass('bash_rule', bash=bash, hostname=hostnames)
[ "def", "bash_rule", "(", "bash", ",", "hostnames", ")", ":", "if", "isinstance", "(", "bash", ",", "dict", ")", ":", "return", "make_fail", "(", "'bash_rule'", ",", "error_message", "=", "\"Run this rule with a cluster archive\"", ")", "return", "make_pass", "("...
Cluster rule to process bash and hostname info ``bash`` and ``hostnames`` are Pandas DataFrames for the facts collected for each host in the cluster. See https://pandas.pydata.org/pandas-docs/stable/api.html#dataframe for information on available attributes and methods. Arguments: bash (pandas.DataFrame): Includes facts from ``bash_version`` fact with columns "name" and "version" and one row per host in the cluster. hostnames (pandas.DataFrame): Includes facts from ``get_hostname`` fact with column "hostname" and one row per host in the cluster.
[ "Cluster", "rule", "to", "process", "bash", "and", "hostname", "info" ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/examples/cluster_rules/bash_version.py#L37-L58
225,998
RedHatInsights/insights-core
insights/core/marshalling.py
Marshaller.marshal
def marshal(self, o, use_value_list=False): """ Packages the return from a parser for easy use in a rule. """ if o is None: return elif isinstance(o, dict): if use_value_list: for k, v in o.items(): o[k] = [v] return o elif isinstance(o, six.string_types): if use_value_list: return {o: [True]} else: return {o: True} else: raise TypeError("Marshaller doesn't support given type %s" % type(o))
python
def marshal(self, o, use_value_list=False): """ Packages the return from a parser for easy use in a rule. """ if o is None: return elif isinstance(o, dict): if use_value_list: for k, v in o.items(): o[k] = [v] return o elif isinstance(o, six.string_types): if use_value_list: return {o: [True]} else: return {o: True} else: raise TypeError("Marshaller doesn't support given type %s" % type(o))
[ "def", "marshal", "(", "self", ",", "o", ",", "use_value_list", "=", "False", ")", ":", "if", "o", "is", "None", ":", "return", "elif", "isinstance", "(", "o", ",", "dict", ")", ":", "if", "use_value_list", ":", "for", "k", ",", "v", "in", "o", "...
Packages the return from a parser for easy use in a rule.
[ "Packages", "the", "return", "from", "a", "parser", "for", "easy", "use", "in", "a", "rule", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/core/marshalling.py#L19-L37
225,999
RedHatInsights/insights-core
insights/collect.py
load_manifest
def load_manifest(data): """ Helper for loading a manifest yaml doc. """ if isinstance(data, dict): return data doc = yaml.safe_load(data) if not isinstance(doc, dict): raise Exception("Manifest didn't result in dict.") return doc
python
def load_manifest(data): """ Helper for loading a manifest yaml doc. """ if isinstance(data, dict): return data doc = yaml.safe_load(data) if not isinstance(doc, dict): raise Exception("Manifest didn't result in dict.") return doc
[ "def", "load_manifest", "(", "data", ")", ":", "if", "isinstance", "(", "data", ",", "dict", ")", ":", "return", "data", "doc", "=", "yaml", ".", "safe_load", "(", "data", ")", "if", "not", "isinstance", "(", "doc", ",", "dict", ")", ":", "raise", ...
Helper for loading a manifest yaml doc.
[ "Helper", "for", "loading", "a", "manifest", "yaml", "doc", "." ]
b57cbf8ed7c089672426ede0441e0a4f789ef4a1
https://github.com/RedHatInsights/insights-core/blob/b57cbf8ed7c089672426ede0441e0a4f789ef4a1/insights/collect.py#L112-L119