Search is not available for this dataset
text
stringlengths
75
104k
def _distribute_jobs(self): """ Distributes jobs from the active job set to any waiting get_job callbacks. """ while (self._active_js.job_available() and len(self._ready_callbacks) > 0): job = self._active_js.get_job() self._job_sources[job] = self._active_js callback = self._ready_callbacks.popleft() callback(job)
def add_job_set(self, job_list): """ Adds a job set to the manager's queue. If there is no job set running, it is activated immediately. A new job set handle is returned. """ assert not self._closed results = Results(loop=self._loop) js = JobSet(job_list, results, self, loop=self._loop) if not js.is_done(): if self._active_js is None: self._active_js = js logger.debug("activated job set") self._distribute_jobs() else: self._js_queue.append(js) else: logger.debug("new job set has no jobs") return JobSetHandle(js, results)
def get_job(self, callback): """ Calls the given callback function when a job becomes available. """ assert not self._closed if self._active_js is None or not self._active_js.job_available(): self._ready_callbacks.append(callback) else: job = self._active_js.get_job() self._job_sources[job] = self._active_js callback(job)
def return_job(self, job): """ Returns a job to its source job set to be run again later. """ if self._closed: return js = self._job_sources[job] if len(self._ready_callbacks) > 0: callback = self._ready_callbacks.popleft() callback(job) else: del self._job_sources[job] js.return_job(job)
def add_result(self, job, result): """ Adds the result of a job to the results list of the job's source job set. """ if self._closed: return js = self._job_sources[job] del self._job_sources[job] js.add_result(result)
def job_set_done(self, js): """ Called when a job set has been completed or cancelled. If the job set was active, the next incomplete job set is loaded from the job set queue and is activated. """ if self._closed: return if self._active_js != js: return try: while self._active_js.is_done(): logger.debug("job set done") self._active_js = self._js_queue.popleft() logger.debug("activated job set") except IndexError: self._active_js = None else: self._distribute_jobs()
def close(self): """ Closes the job manager. No more jobs will be assigned, no more job sets will be added, and any queued or active job sets will be cancelled. """ if self._closed: return self._closed = True if self._active_js is not None: self._active_js.cancel() for js in self._js_queue: js.cancel()
def entry_point(items=tuple()): """ External entry point which calls main() and if Stop is raised, calls sys.exit() """ try: if not items: from .example import ExampleCommand from .version import Version items = [(ExampleCommand.NAME, ExampleCommand), (Version.NAME, Version)] main("yaclifw", items=items) except Stop as stop: print(stop) sys.exit(stop.rc) except SystemExit: raise except KeyboardInterrupt: print("Cancelled") sys.exit(1) except Exception: traceback.print_exc() sys.exit(1)
def _uniquify(_list): """Remove duplicates in a list.""" seen = set() result = [] for x in _list: if x not in seen: result.append(x) seen.add(x) return result
def _match_regex(regex, obj): """ Returns true if the regex matches the object, or a string in the object if it is some sort of container. :param regex: A regex. :type regex: ``regex`` :param obj: An arbitrary object. :type object: ``object`` :rtype: ``bool`` """ if isinstance(obj, six.string_types): return len(regex.findall(obj)) > 0 elif isinstance(obj, dict): return _match_regex(regex, obj.values()) elif hasattr(obj, '__iter__'): # Object is a list or some other iterable. return any(_match_regex(regex, s) for s in obj if isinstance(s, six.string_types)) else: return False
def get_entries(latest, filters, exclude, limit=None): """ Lists all available instances. :param latest: If true, ignores the cache and grabs the latest list. :type latest: ``bool`` :param filters: Filters to apply to results. A result will only be shown if it includes all text in all filters. :type filters: [``str``] :param exclude: The opposite of filters. Results will be rejected if they include any of these strings. :type exclude: [``str``] :param limit: Maximum number of entries to show (default no maximum). :type limit: ``int`` or ``NoneType`` :return: A list of host entries. :rtype: ``list`` of :py:class:`HostEntry` """ entry_list = _list_all_latest() if latest is True or not _is_valid_cache()\ else _list_all_cached() filtered = filter_entries(entry_list, filters, exclude) if limit is not None: return filtered[:limit] else: return filtered
def get_region(): """Use the environment to get the current region""" global _REGION if _REGION is None: region_name = os.getenv("AWS_DEFAULT_REGION") or "us-east-1" region_dict = {r.name: r for r in boto.regioninfo.get_regions("ec2")} if region_name not in region_dict: raise ValueError("No such EC2 region: {}. Check AWS_DEFAULT_REGION " "environment variable".format(region_name)) _REGION = region_dict[region_name] return _REGION
def _is_valid_cache(): """ Returns if the cache is valid (exists and modified within the interval). :return: Whether the cache is valid. :rtype: ``bool`` """ if not os.path.exists(get_cache_location()): return False modified = os.path.getmtime(get_cache_location()) modified = time.ctime(modified) modified = datetime.strptime(modified, '%a %b %d %H:%M:%S %Y') return datetime.now() - modified <= CACHE_EXPIRATION_INTERVAL
def _list_all_cached(): """ Reads the description cache, returning each instance's information. :return: A list of host entries. :rtype: [:py:class:`HostEntry`] """ with open(get_cache_location()) as f: contents = f.read() objects = json.loads(contents) return [HostEntry.from_dict(obj) for obj in objects]
def filter_entries(entries, filters, exclude): """ Filters a list of host entries according to the given filters. :param entries: A list of host entries. :type entries: [:py:class:`HostEntry`] :param filters: Regexes that must match a `HostEntry`. :type filters: [``str``] :param exclude: Regexes that must NOT match a `HostEntry`. :type exclude: [``str``] :return: The filtered list of host entries. :rtype: [:py:class:`HostEntry`] """ filtered = [entry for entry in entries if all(entry.matches(f) for f in filters) and not any(entry.matches(e) for e in exclude)] return filtered
def get_host(name): """ Prints the public dns name of `name`, if it exists. :param name: The instance name. :type name: ``str`` """ f = {'instance-state-name': 'running', 'tag:Name': name} ec2 = boto.connect_ec2(region=get_region()) rs = ec2.get_all_instances(filters=f) if len(rs) == 0: raise Exception('Host "%s" not found' % name) print(rs[0].instances[0].public_dns_name)
def from_dict(cls, entry_dict): """Deserialize a HostEntry from a dictionary. This is more or less the same as calling HostEntry(**entry_dict), but is clearer if something is missing. :param entry_dict: A dictionary in the format outputted by to_dict(). :type entry_dict: ``dict`` :return: A HostEntry object. :rtype: ``cls`` """ return cls( name=entry_dict["name"], instance_type=entry_dict["instance_type"], hostname=entry_dict["hostname"], private_ip=entry_dict["private_ip"], public_ip=entry_dict["public_ip"], stack_name=entry_dict["stack_name"], stack_id=entry_dict["stack_id"], logical_id=entry_dict["logical_id"], security_groups=entry_dict["security_groups"], tags=entry_dict["tags"], ami_id=entry_dict["ami_id"], launch_time=entry_dict["launch_time"], instance_id=entry_dict["instance_id"] )
def _get_attrib(self, attr, convert_to_str=False): """ Given an attribute name, looks it up on the entry. Names that start with ``tags.`` are looked up in the ``tags`` dictionary. :param attr: Name of attribute to look up. :type attr: ``str`` :param convert_to_str: Convert result to a string. :type convert_to_str: ``bool`` :rtype: ``object`` """ if attr.startswith('tags.'): tag = attr[len('tags.'):] if tag in self.tags and self.tags[tag] != '': return self.tags[tag] elif convert_to_str is True: return '<not set>' else: return self.tags.get(tag) elif not hasattr(self, attr): raise AttributeError('Invalid attribute: {0}. Perhaps you meant ' '{1}?'.format(red(attr), green('tags.' + attr))) else: result = getattr(self, attr) if convert_to_str is True and not result: return '<none>' elif convert_to_str is True and isinstance(result, list): return ', '.join(result) elif convert_to_str is True: return str(result) else: return result
def list_attributes(cls): """ Lists all of the attributes to be found on an instance of this class. It creates a "fake instance" by passing in `None` to all of the ``__init__`` arguments, then returns all of the attributes of that instance. :return: A list of instance attributes of this class. :rtype: ``list`` of ``str`` """ fake_args = [None for _ in inspect.getargspec(cls.__init__).args[1:]] fake_instance = cls(*fake_args) return vars(fake_instance).keys()
def sort_by(cls, entries, attribute): """ Sorts a list of entries by the given attribute. """ def key(entry): return entry._get_attrib(attribute, convert_to_str=True) return sorted(entries, key=key)
def repr_as_line(self, additional_columns=None, only_show=None, sep=','): """ Returns a representation of the host as a single line, with columns joined by ``sep``. :param additional_columns: Columns to show in addition to defaults. :type additional_columns: ``list`` of ``str`` :param only_show: A specific list of columns to show. :type only_show: ``NoneType`` or ``list`` of ``str`` :param sep: The column separator to use. :type sep: ``str`` :rtype: ``str`` """ additional_columns = additional_columns or [] if only_show is not None: columns = _uniquify(only_show) else: columns = _uniquify(self.DEFAULT_COLUMNS + additional_columns) to_display = [self._get_attrib(c, convert_to_str=True) for c in columns] return sep.join(to_display)
def from_boto_instance(cls, instance): """ Loads a ``HostEntry`` from a boto instance. :param instance: A boto instance object. :type instance: :py:class:`boto.ec2.instanceInstance` :rtype: :py:class:`HostEntry` """ return cls( name=instance.tags.get('Name'), private_ip=instance.private_ip_address, public_ip=instance.ip_address, instance_type=instance.instance_type, instance_id=instance.id, hostname=instance.dns_name, stack_id=instance.tags.get('aws:cloudformation:stack-id'), stack_name=instance.tags.get('aws:cloudformation:stack-name'), logical_id=instance.tags.get('aws:cloudformation:logical-id'), security_groups=[g.name for g in instance.groups], launch_time=instance.launch_time, ami_id=instance.image_id, tags={k.lower(): v for k, v in six.iteritems(instance.tags)} )
def matches(self, _filter): """ Returns whether the instance matches the given filter text. :param _filter: A regex filter. If it starts with `<identifier>:`, then the part before the colon will be used as an attribute and the part after will be applied to that attribute. :type _filter: ``basestring`` :return: True if the entry matches the filter. :rtype: ``bool`` """ within_attrib = re.match(r'^([a-z_.]+):(.*)', _filter) having_attrib = re.match(r'^([a-z_.]+)\?$', _filter) if within_attrib is not None: # Then we're matching against a specific attribute. val = self._get_attrib(within_attrib.group(1)) sub_regex = within_attrib.group(2) if len(sub_regex) > 0: sub_regex = re.compile(sub_regex, re.IGNORECASE) return _match_regex(sub_regex, val) else: # Then we are matching on the value being empty. return val == '' or val is None or val == [] elif having_attrib is not None: # Then we're searching for anything that has a specific attribute. val = self._get_attrib(having_attrib.group(1)) return val != '' and val is not None and val != [] else: regex = re.compile(_filter, re.IGNORECASE) return _match_regex(regex, vars(self))
def display(self): """ Returns the best name to display for this host. Uses the instance name if available; else just the public IP. :rtype: ``str`` """ if isinstance(self.name, six.string_types) and len(self.name) > 0: return '{0} ({1})'.format(self.name, self.public_ip) else: return self.public_ip
def prettyname(cls, attrib_name): """ Returns the "pretty name" (capitalized, etc) of an attribute, by looking it up in ``cls.COLUMN_NAMES`` if it exists there. :param attrib_name: An attribute name. :type attrib_name: ``str`` :rtype: ``str`` """ if attrib_name.startswith('tags.'): tagname = attrib_name[len('tags.'):] return '{} (tag)'.format(tagname) elif attrib_name in cls.COLUMN_NAMES: return cls.COLUMN_NAMES[attrib_name] else: return attrib_name
def format_string(self, fmat_string): """ Takes a string containing 0 or more {variables} and formats it according to this instance's attributes. :param fmat_string: A string, e.g. '{name}-foo.txt' :type fmat_string: ``str`` :return: The string formatted according to this instance. E.g. 'production-runtime-foo.txt' :rtype: ``str`` """ try: return fmat_string.format(**vars(self)) except KeyError as e: raise ValueError('Invalid format string: {0}. Instance has no ' 'attribute {1}.'.format(repr(fmat_string), repr(e)))
def render_entries(cls, entries, additional_columns=None, only_show=None, numbers=False): """ Pretty-prints a list of entries. If the window is wide enough to support printing as a table, runs the `print_table.render_table` function on the table. Otherwise, constructs a line-by-line representation.. :param entries: A list of entries. :type entries: [:py:class:`HostEntry`] :param additional_columns: Columns to show in addition to defaults. :type additional_columns: ``list`` of ``str`` :param only_show: A specific list of columns to show. :type only_show: ``NoneType`` or ``list`` of ``str`` :param numbers: Whether to include a number column. :type numbers: ``bool`` :return: A pretty-printed string. :rtype: ``str`` """ additional_columns = additional_columns or [] if only_show is not None: columns = _uniquify(only_show) else: columns = _uniquify(cls.DEFAULT_COLUMNS + additional_columns) top_row = [cls.prettyname(col) for col in columns] table = [top_row] if numbers is False else [[''] + top_row] for i, entry in enumerate(entries): row = [entry._get_attrib(c, convert_to_str=True) for c in columns] table.append(row if numbers is False else [i] + row) cur_width = get_current_terminal_width() colors = [get_color_hash(c, MIN_COLOR_BRIGHT, MAX_COLOR_BRIGHT) for c in columns] if cur_width >= get_table_width(table): return render_table(table, column_colors=colors if numbers is False else [green] + colors) else: result = [] first_index = 1 if numbers is True else 0 for row in table[1:]: rep = [green('%s:' % row[0] if numbers is True else '-----')] for i, val in enumerate(row[first_index:]): color = colors[i-1 if numbers is True else i] name = columns[i] rep.append(' %s: %s' % (name, color(val))) result.append('\n'.join(rep)) return '\n'.join(result)
def add_timestamp(logger_class, log_method, event_dict): ''' Attach the event time, as unix epoch ''' event_dict['timestamp'] = calendar.timegm(time.gmtime()) return event_dict
def setup(level='debug', output=None): ''' Hivy formated logger ''' output = output or settings.LOG['file'] level = level.upper() handlers = [ logbook.NullHandler() ] if output == 'stdout': handlers.append( logbook.StreamHandler(sys.stdout, format_string=settings.LOG['format'], level=level)) else: handlers.append( logbook.FileHandler(output, format_string=settings.LOG['format'], level=level)) sentry_dns = settings.LOG['sentry_dns'] if sentry_dns: handlers.append(SentryHandler(sentry_dns, level='ERROR')) return logbook.NestedSetup(handlers)
def logger(name=__name__, output=None, uuid=False, timestamp=False): ''' Configure and return a new logger for hivy modules ''' processors = [] if output == 'json': processors.append(structlog.processors.JSONRenderer()) if uuid: processors.append(add_unique_id) if uuid: processors.append(add_timestamp) return structlog.wrap_logger( logbook.Logger(name), processors=processors )
def setup(title, output='json', timezone=None): ''' Implement celery workers using json and redis ''' timezone = timezone or dna.time_utils._detect_timezone() broker_url = 'redis://{}:{}/{}'.format( os.environ.get('BROKER_HOST', 'localhost'), os.environ.get('BROKER_PORT', 6379), 0 ) app = Celery(title, broker=broker_url) app.conf.update( CELERY_TASK_SERIALIZER=output, CELERY_ACCEPT_CONTENT=[output], # Ignore other content CELERY_RESULT_SERIALIZER=output, CELERY_RESULT_BACKEND=broker_url, CELERY_TIMEZONE=timezone, CELERYD_FORCE_EXECV=True, CELERY_ENABLE_UTC=True, CELERY_IGNORE_RESULT=False ) return app
def get(self, worker_id): ''' Return status report ''' code = 200 if worker_id == 'all': report = {'workers': [{ 'id': job, 'report': self._inspect_worker(job)} for job in self.jobs] } elif worker_id in self.jobs: report = { 'id': worker_id, 'report': self._inspect_worker(worker_id) } else: report = {'error': 'job {} unknown'.format(worker_id)} code = 404 return flask.jsonify(report), code
def delete(self, worker_id): ''' Stop and remove a worker ''' code = 200 if worker_id in self.jobs: # NOTE pop it if done ? self.jobs[worker_id]['worker'].revoke(terminate=True) report = { 'id': worker_id, 'revoked': True # FIXME Unable to serialize self.jobs[worker_id] # 'session': self.jobs.pop(worker_id) } self.jobs.pop(worker_id) else: report = {'error': 'job {} unknown'.format(worker_id)} code = 404 return flask.jsonify(report), code
def main(fw_name, args=None, items=None): """ Reusable entry point. Arguments are parsed via the argparse-subcommands configured via each Command class found in globals(). Stop exceptions are propagated to callers. The name of the framework will be used in logging and similar. """ global DEBUG_LEVEL global FRAMEWORK_NAME debug_name = "%s_DEBUG_LEVEL" % fw_name.upper() if debug_name in os.environ: try: DEBUG_LEVEL = int(os.environ.get(debug_name)) except ValueError: DEBUG_LEVEL = 10 # Assume poorly formatted means "debug" FRAMEWORK_NAME = fw_name if args is None: args = sys.argv[1:] if items is None: items = list(globals().items()) yaclifw_parser, sub_parsers = parsers() for name, MyCommand in sorted(items): if not isinstance(MyCommand, type): continue if not issubclass(MyCommand, Command): continue if MyCommand.NAME == "abstract": continue MyCommand(sub_parsers) ns = yaclifw_parser.parse_args(args) ns.func(ns) if hasattr(ns, 'callback'): if callable(ns.callback): ns.callback() else: raise Stop(3, "Callback not callable")
def switch_opt(default, shortname, help_msg): """Define a switchable ConfOpt. This creates a boolean option. If you use it in your CLI, it can be switched on and off by prepending + or - to its name: +opt / -opt. Args: default (bool): the default value of the swith option. shortname (str): short name of the option, no shortname will be used if it is set to None. help_msg (str): short description of the option. Returns: :class:`~loam.manager.ConfOpt`: a configuration option with the given properties. """ return ConfOpt(bool(default), True, shortname, dict(action=internal.Switch), True, help_msg, None)
def config_conf_section(): """Define a configuration section handling config file. Returns: dict of ConfOpt: it defines the 'create', 'update', 'edit' and 'editor' configuration options. """ config_dict = OrderedDict(( ('create', ConfOpt(None, True, None, {'action': 'store_true'}, False, 'create most global config file')), ('create_local', ConfOpt(None, True, None, {'action': 'store_true'}, False, 'create most local config file')), ('update', ConfOpt(None, True, None, {'action': 'store_true'}, False, 'add missing entries to config file')), ('edit', ConfOpt(None, True, None, {'action': 'store_true'}, False, 'open config file in a text editor')), ('editor', ConfOpt('vim', False, None, {}, True, 'text editor')), )) return config_dict
def set_conf_str(conf, optstrs): """Set options from a list of section.option=value string. Args: conf (:class:`~loam.manager.ConfigurationManager`): the conf to update. optstrs (list of str): the list of 'section.option=value' formatted string. """ falsy = ['0', 'no', 'n', 'off', 'false', 'f'] bool_actions = ['store_true', 'store_false', internal.Switch] for optstr in optstrs: opt, val = optstr.split('=', 1) sec, opt = opt.split('.', 1) if sec not in conf: raise error.SectionError(sec) if opt not in conf[sec]: raise error.OptionError(opt) meta = conf[sec].def_[opt] if meta.default is None: if 'type' in meta.cmd_kwargs: cast = meta.cmd_kwargs['type'] else: act = meta.cmd_kwargs.get('action') cast = bool if act in bool_actions else str else: cast = type(meta.default) if cast is bool and val.lower() in falsy: val = '' conf[sec][opt] = cast(val)
def config_cmd_handler(conf, config='config'): """Implement the behavior of a subcmd using config_conf_section Args: conf (:class:`~loam.manager.ConfigurationManager`): it should contain a section created with :func:`config_conf_section` function. config (str): name of the configuration section created with :func:`config_conf_section` function. """ if conf[config].create or conf[config].update: conf.create_config_(update=conf[config].update) if conf[config].create_local: conf.create_config_(index=-1, update=conf[config].update) if conf[config].edit: if not conf.config_files_[0].is_file(): conf.create_config_(update=conf[config].update) subprocess.call(shlex.split('{} {}'.format(conf[config].editor, conf.config_files_[0])))
def create_complete_files(climan, path, cmd, *cmds, zsh_sourceable=False): """Create completion files for bash and zsh. Args: climan (:class:`~loam.cli.CLIManager`): CLI manager. path (path-like): directory in which the config files should be created. It is created if it doesn't exist. cmd (str): command name that should be completed. cmds (str): extra command names that should be completed. zsh_sourceable (bool): if True, the generated file will contain an explicit call to ``compdef``, which means it can be sourced to activate CLI completion. """ path = pathlib.Path(path) zsh_dir = path / 'zsh' if not zsh_dir.exists(): zsh_dir.mkdir(parents=True) zsh_file = zsh_dir / '_{}.sh'.format(cmd) bash_dir = path / 'bash' if not bash_dir.exists(): bash_dir.mkdir(parents=True) bash_file = bash_dir / '{}.sh'.format(cmd) climan.zsh_complete(zsh_file, cmd, *cmds, sourceable=zsh_sourceable) climan.bash_complete(bash_file, cmd, *cmds)
def render_columns(columns, write_borders=True, column_colors=None): """ Renders a list of columns. :param columns: A list of columns, where each column is a list of strings. :type columns: [[``str``]] :param write_borders: Whether to write the top and bottom borders. :type write_borders: ``bool`` :param column_colors: A list of coloring functions, one for each column. Optional. :type column_colors: [``str`` -> ``str``] or ``NoneType`` :return: The rendered columns. :rtype: ``str`` """ if column_colors is not None and len(column_colors) != len(columns): raise ValueError('Wrong number of column colors') widths = [max(len(cell) for cell in column) for column in columns] max_column_length = max(len(column) for column in columns) result = '\n'.join(render_row(i, columns, widths, column_colors) for i in range(max_column_length)) if write_borders: border = '+%s+' % '|'.join('-' * (w + 2) for w in widths) return '%s\n%s\n%s' % (border, result, border) else: return result
def render_row(num, columns, widths, column_colors=None): """ Render the `num`th row of each column in `columns`. :param num: Which row to render. :type num: ``int`` :param columns: The list of columns. :type columns: [[``str``]] :param widths: The widths of each column. :type widths: [``int``] :param column_colors: An optional list of coloring functions. :type column_colors: [``str`` -> ``str``] or ``NoneType`` :return: The rendered row. :rtype: ``str`` """ row_str = '|' cell_strs = [] for i, column in enumerate(columns): try: cell = column[num] # We choose the number of spaces before we color the string, so # that the coloring codes don't affect the length. spaces = ' ' * (widths[i] - len(cell)) if column_colors is not None and column_colors[i] is not None: cell = column_colors[i](cell) cell_strs.append(' %s%s ' % (cell, spaces)) except IndexError: # If the index is out of range, just print an empty cell. cell_strs.append(' ' * (widths[i] + 2)) return '|%s|' % '|'.join(cell_strs)
def render_table(table, write_borders=True, column_colors=None): """ Renders a table. A table is a list of rows, each of which is a list of arbitrary objects. The `.str` method will be called on each element of the row. Jagged tables are ok; in this case, each row will be expanded to the maximum row length. :param table: A list of rows, as described above. :type table: [[``object``]] :param write_borders: Whether there should be a border on the top and bottom. Defaults to ``True``. :type write_borders: ``bool`` :param column_colors: An optional list of coloring *functions* to be applied to each cell in each column. If provided, the list's length must be equal to the maximum number of columns. ``None`` can be mixed in to this list so that a selection of columns can be colored. :type column_colors: [``str`` -> ``str``] or ``NoneType`` :return: The rendered table. :rtype: ``str`` """ prepare_rows(table) columns = transpose_table(table) return render_columns(columns, write_borders, column_colors)
def transpose_table(table): """ Transposes a table, turning rows into columns. :param table: A 2D string grid. :type table: [[``str``]] :return: The same table, with rows and columns flipped. :rtype: [[``str``]] """ if len(table) == 0: return table else: num_columns = len(table[0]) return [[row[i] for row in table] for i in range(num_columns)]
def prepare_rows(table): """ Prepare the rows so they're all strings, and all the same length. :param table: A 2D grid of anything. :type table: [[``object``]] :return: A table of strings, where every row is the same length. :rtype: [[``str``]] """ num_columns = max(len(row) for row in table) for row in table: while len(row) < num_columns: row.append('') for i in range(num_columns): row[i] = str(row[i]) if row[i] is not None else '' return table
def get_table_width(table): """ Gets the width of the table that would be printed. :rtype: ``int`` """ columns = transpose_table(prepare_rows(table)) widths = [max(len(cell) for cell in column) for column in columns] return len('+' + '|'.join('-' * (w + 2) for w in widths) + '+')
def color(number): """ Returns a function that colors a string with a number from 0 to 255. """ if supports_256(): template = "\033[38;5;{number}m{text}\033[0m" else: template = "\033[{number}m{text}\033[0m" def _color(text): if not all([sys.stdout.isatty(), sys.stderr.isatty()]): return text else: return template.format(number=number, text=text) return _color
def get_color_hash(string, _min=MIN_COLOR_BRIGHT, _max=MAX_COLOR_BRIGHT): """ Hashes a string and returns a number between ``min`` and ``max``. """ hash_num = int(hashlib.sha1(string.encode('utf-8')).hexdigest()[:6], 16) _range = _max - _min num_in_range = hash_num % _range return color(_min + num_in_range)
def random_color(_min=MIN_COLOR, _max=MAX_COLOR): """Returns a random color between min and max.""" return color(random.randint(_min, _max))
def get_input(prompt, default=None, exit_msg='bye!'): """ Reads stdin, exits with a message if interrupted, EOF, or a quit message. :return: The entered input. Converts to an integer if possible. :rtype: ``str`` or ``int`` """ try: response = six.moves.input(prompt) except (KeyboardInterrupt, EOFError): print() print(exit_msg) exit() try: return int(response) except ValueError: if response.strip() == "" and default is not None: return default else: return response
def check_credentials(username, password): ''' Verify basic http authentification ''' user = models.User.objects( username=username, password=password ).first() return user or None
def check_token(token): ''' Verify http header token authentification ''' user = models.User.objects(api_key=token).first() return user or None
def requires_basic_auth(resource): ''' Flask decorator protecting ressources using username/password scheme ''' @functools.wraps(resource) def decorated(*args, **kwargs): ''' Check provided username/password ''' auth = flask.request.authorization user = check_credentials(auth.username, auth.password) if not auth or user is None: log.warn('authentification failed', credentials=auth) return auth_failed() log.info('authentification succeeded', credentials=auth) flask.g.user = user return resource(*args, **kwargs) return decorated
def requires_token_auth(resource): ''' Flask decorator protecting ressources using token scheme ''' @functools.wraps(resource) def decorated(*args, **kwargs): ''' Check provided token ''' token = flask.request.headers.get('Authorization') user = check_token(token) if not token or user is None: log.warn('authentification failed', token=token) return auth_failed() flask.g.user = user log.info('authentification succeeded', token=token, user=flask.g.user) return resource(*args, **kwargs) return decorated
def is_running(process): ''' `pgrep` returns an error code if no process was found ''' try: pgrep = sh.Command('/usr/bin/pgrep') pgrep(process) flag = True except sh.ErrorReturnCode_1: flag = False return flag
def dynamic_import(mod_path, obj_name=None): ''' Take a string and return the corresponding module ''' try: module = __import__(mod_path, fromlist=['whatever']) except ImportError, error: raise errors.DynamicImportFailed( module='.'.join([mod_path, obj_name]), reason=error) # Make sure we're up-to-date reload(module) if obj_name is None: obj = module elif hasattr(module, obj_name): obj = getattr(module, obj_name) else: raise errors.DynamicImportFailed( module='.'.join([mod_path, obj_name]), reason='module {} has no attribute {}'. format(module.__name__, obj_name)) return None return obj
def self_ip(public=False): ''' Utility for logbook information injection ''' try: if public: data = str(urlopen('http://checkip.dyndns.com/').read()) ip_addr = re.compile( r'Address: (\d+\.\d+\.\d+\.\d+)').search(data).group(1) else: sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.connect(('google.com', 0)) ip_addr = sock.getsockname()[0] except Exception, error: print('Online test failed : {}'.format(error)) raise return ip_addr
def request(self, method, url, query_params=None, headers=None, post_params=None, body=None): """ Makes the HTTP request using RESTClient. """ if method == "GET": return self.rest_client.GET(url, query_params=query_params, headers=headers) elif method == "HEAD": return self.rest_client.HEAD(url, query_params=query_params, headers=headers) elif method == "OPTIONS": return self.rest_client.OPTIONS(url, query_params=query_params, headers=headers, post_params=post_params, body=body) elif method == "POST": return self.rest_client.POST(url, query_params=query_params, headers=headers, post_params=post_params, body=body) elif method == "PUT": return self.rest_client.PUT(url, query_params=query_params, headers=headers, post_params=post_params, body=body) elif method == "PATCH": return self.rest_client.PATCH(url, query_params=query_params, headers=headers, post_params=post_params, body=body) elif method == "DELETE": return self.rest_client.DELETE(url, query_params=query_params, headers=headers) else: raise ValueError( "http method must be `GET`, `HEAD`," " `POST`, `PATCH`, `PUT` or `DELETE`." )
def prepare_post_parameters(self, post_params=None, files=None): """ Builds form parameters. :param post_params: Normal form parameters. :param files: File parameters. :return: Form parameters with files. """ params = {} if post_params: params.update(post_params) if files: for k, v in iteritems(files): if not v: continue with open(v, 'rb') as f: filename = os.path.basename(f.name) filedata = f.read() mimetype = mimetypes.\ guess_type(filename)[0] or 'application/octet-stream' params[k] = tuple([filename, filedata, mimetype]) return params
def serve(self, app_docopt=DEFAULT_DOC, description=''): ''' Configure from cli and run the server ''' exit_status = 0 if isinstance(app_docopt, str): args = docopt(app_docopt, version=description) elif isinstance(app_docopt, dict): args = app_docopt else: raise ValueError('unknown configuration object ({})' .format(type(app_docopt))) log_level = args.get('--log', 'debug') is_debug = args.get('--debug', False) # TODO More serious default log_output = 'stdout' if is_debug else 'apy.log' safe_bind = args.get('--bind', '127.0.0.1') safe_port = int(args.get('--port', 5000)) log_setup = dna.logging.setup(level=log_level, output=log_output) with log_setup.applicationbound(): try: log.info('server ready', version=description, log=log_level, debug=is_debug, bind='{}:{}'.format(safe_bind, safe_port)) self.app.run(host=safe_bind, port=safe_port, debug=is_debug) except Exception as error: if is_debug: raise log.error('{}: {}'.format(type(error).__name__, str(error))) exit_status = 1 finally: log.info('session ended with status {}'.format(exit_status)) return exit_status
def render(self, name, value, attrs=None): """Include a hidden input to stored the serialized upload value.""" context = attrs or {} context.update({'name': name, 'value': value, }) return render_to_string(self.template_name, context)
def stream_command(command, formatter=None, write_stdin=None, ignore_empty=False): """ Starts `command` in a subprocess. Prints every line the command prints, prefaced with `description`. :param command: The bash command to run. Must use fully-qualified paths. :type command: ``str`` :param formatter: An optional formatting function to apply to each line. :type formatter: ``function`` or ``NoneType`` :param write_stdin: An optional string to write to the process' stdin. :type write_stdin: ``str`` or ``NoneType`` :param ignore_empty: If true, empty or whitespace-only lines will be skipped. :type ignore_empty: ``bool`` """ command_list = shlex.split(command) try: proc = subprocess.Popen(command_list, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin=subprocess.PIPE) except Exception as e: raise IOError('Encountered error: {0} when running command {1}' .format(e.message, ' '.join(command_list))) if write_stdin is not None: proc.stdin.write(write_stdin) proc.stdin.flush() while proc.poll() is None: try: line = proc.stdout.readline() except KeyboardInterrupt: sys.exit('Keyboard interrupt while running {}'.format(command)) if len(line.strip()) == 0 and ignore_empty is True: continue elif 'killed by signal 1' in decode(line).lower(): continue elif 'to the list of known hosts' in decode(line).lower(): continue if formatter is not None: line = formatter(line) sys.stdout.write(line) result = proc.poll() return result
def stream_command_dicts(commands, parallel=False): """ Takes a list of dictionaries with keys corresponding to ``stream_command`` arguments, and runs all concurrently. :param commands: A list of dictionaries, the keys of which should line up with the arguments to ``stream_command`` function. :type commands: ``list`` of ``dict`` :param parallel: If true, commands will be run in parallel. :type parallel: ``bool`` """ if parallel is True: threads = [] for command in commands: target = lambda: stream_command(**command) thread = Thread(target=target) thread.start() threads.append(thread) for t in threads: t.join() else: for command in commands: stream_command(**command)
def stream_commands(commands, hash_colors=True, parallel=False): """ Runs multiple commands, optionally in parallel. Each command should be a dictionary with a 'command' key and optionally 'description' and 'write_stdin' keys. """ def _get_color(string): if hash_colors is True: return get_color_hash(string) else: return DEFAULT_COLOR fixed_commands = [] for command in commands: cmd_text = command['command'] description = command.get('description') color = _get_color(description or '') write_stdin = command.get('write_stdin') description = color(description) if color is not None else description formatter = _format_with_description(description) fixed_commands.append({ 'command': cmd_text, 'formatter': formatter, 'write_stdin': write_stdin, 'ignore_empty': True }) stream_command_dicts(fixed_commands, parallel=parallel)
def networkdays(from_date, to_date, locale='en-US'): """ Return the net work days according to RH's calendar. """ holidays = locales[locale] return workdays.networkdays(from_date, to_date, holidays)
def merge(dicto, other): """ Recursive dict merge. Inspired by :meth:``dict.update()``, instead of updating only top-level keys, dict_merge recurses down into dicts nested to an arbitrary depth, updating keys. The ``other`` is merged into ``dicto``. :param dicto: dict onto which the merge is executed :param other: dict that is going to merged into dicto :return: None """ if not isinstance(dicto, Dicto): dicto = Dicto(dicto) if not isinstance(other, Dicto): other = Dicto(other) for k, v in other.__dict__.items(): if k in dicto and isinstance(dicto[k], Dicto) and isinstance(other[k], Dicto): dicto[k] = merge(dicto[k], other[k]) else: dicto[k] = other[k] return dicto
def _run_ssh(entries, username, idfile, no_prompt=False, command=None, show=None, only=None, sort_by=None, limit=None, tunnel=None, num=None, random=False): """ Lets the user choose which instance to SSH into. :param entries: The list of host entries. :type entries: [:py:class:`HostEntry`] :param username: The SSH username to use. Defaults to current user. :type username: ``str`` or ``NoneType`` :param idfile: The identity file to use. Optional. :type idfile: ``str`` or ``NoneType`` :param no_prompt: Whether to disable confirmation for SSH command. :type no_prompt: ``bool`` :param command: SSH command to run on matching instances. :type command: ``str`` or ``NoneType`` :param show: Instance attributes to show in addition to defaults. :type show: ``NoneType`` or ``list`` of ``str`` :param only: If not ``None``, will *only* show these attributes. :type only: ``NoneType`` or ``list`` of ``str`` :param sort_by: What to sort columns by. By default, sort by 'name'. :type sort_by: ``str`` :param limit: At most how many results to show. :type limit: ``int`` or ``NoneType`` :param num: If not None, choose the given entry from within filters. :type num: ``int`` or ``NoneType`` :param random: If true, choose a random entry from within filters. :type random: ``bool`` """ _print_entries = num is None _print_help = False if len(entries) == 0: exit('No entries matched the filters.') if no_prompt is True and command is not None: return _run_ssh_command(entries, username, idfile, command, tunnel) elif len(entries) == 1 or random is True: entry = py_random.choice(entries) if command is None: return _connect_ssh(entry, username, idfile, tunnel) else: return _run_ssh_command([entry], username, idfile, command, tunnel) elif command is not None: print(HostEntry.render_entries(entries, additional_columns=show, only_show=only, numbers=True)) if no_prompt is False: get_input("Press enter to run command {} on the {} " "above machines (Ctrl-C to cancel)" .format(cyan(command), len(entries))) return _run_ssh_command(entries, username, idfile, command, tunnel) else: while True: if sort_by is not None: entries = HostEntry.sort_by(entries, sort_by) if limit is not None: entries = entries[:limit] if _print_entries is True: print(HostEntry.render_entries(entries, additional_columns=show, only_show=only, numbers=True)) print('%s matching entries.' % len(entries)) _print_entries = False if _print_help is True: cmd_str = green(command) if command is not None else 'none set' msg = COMMANDS_STRING.format(username=username or 'none set', idfile=idfile or 'none set', cur_cmd=cmd_str) print(msg) _print_help = False elif command is not None: print('Set to run ssh command: %s' % cyan(command)) msg = 'Enter command (%s for help, %s to quit): ' % (cyan('h'), cyan('q')) if num is not None: choice = num else: choice = get_input(msg) if isinstance(choice, int): if 0 <= choice <= len(entries): break else: if num is not None: exit("Invalid number {}: must be between 0 and {}" .format(num, len(entries) - 1)) else: msg = "Invalid number: must be between 0 and %s" print(msg % (len(entries) - 1)) elif choice == 'x': if command is None: print('No command has been set. Set command with `c`') else: return _run_ssh_command(entries, username, idfile, command, tunnel) elif choice == 'h': _print_help = True elif choice in ['q', 'quit', 'exit']: print('bye!') return else: # All of these commands take one or more arguments, so the # split length must be at least 2. commands = choice.split() if len(commands) < 2: print(yellow('Unknown command "%s".' % choice)) else: cmd = commands[0] if cmd in ['u', 'i', 'p']: if cmd == 'u': username = commands[1] elif cmd == 'i': _idfile = commands[1] if not os.path.exists(_idfile): print(yellow('No such file: %s' % _idfile)) continue idfile = _idfile elif cmd == 'p': p = commands[1] try: profile = LsiProfile.load(p) _username = profile.username _idfile = expanduser(profile.identity_file) except LsiProfile.LoadError: print(yellow('No such profile: %s' % repr(p))) continue username = _username idfile = _idfile print('username: %s' % green(repr(username))) print('identity file: %s' % green(repr(idfile))) elif cmd == 'f': entries = filter_entries(entries, commands[1:], []) _print_entries = True elif cmd == 'e': entries = filter_entries(entries, [], commands[1:]) _print_entries = True elif cmd == 'c': command = ' '.join(commands[1:]) elif cmd == 'limit': try: limit = int(commands[1]) _print_entries = True except ValueError: print(yellow('Invalid limit (must be an integer)')) elif cmd == 'sort': sort_by = commands[1] if sort_by not in show: show.append(sort_by) _print_entries = True elif cmd == 'show': if show is None: show = commands[1:] else: show.extend(commands[1:]) _print_entries = True else: print(yellow('Unknown command "%s".' % cmd)) return _connect_ssh(entries[choice], username, idfile, tunnel)
def _get_path(cmd): """Queries bash to find the path to a commmand on the system.""" if cmd in _PATHS: return _PATHS[cmd] out = subprocess.check_output('which {}'.format(cmd), shell=True) _PATHS[cmd] = out.decode("utf-8").strip() return _PATHS[cmd]
def _build_ssh_command(hostname, username, idfile, ssh_command, tunnel): """Uses hostname and other info to construct an SSH command.""" command = [_get_path('ssh'), '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=5'] if idfile is not None: command.extend(['-i', idfile]) if tunnel is not None: # If there's a tunnel, run the ssh command on the tunneled host. command.extend(['-A', '-t', tunnel, 'ssh', '-A', '-t']) if username is not None: command.append('{}@{}'.format(username, hostname)) else: command.append(hostname) if ssh_command is not None: command.append(repr(ssh_command)) return(' '.join(command))
def _build_scp_command(hostname, username, idfile, is_get, local_path, remote_path): """ Uses hostname and other info to construct an SCP command. :param hostname: The hostname of the remote machine. :type hostname: ``str`` :param username: The username to use on the remote machine. :type username: ``str`` :param idfile: A path to the identity file to use. :type idfile: ``str`` :param is_get: If true, we are getting a file rather than putting a file. :type is_get: ``bool`` :param local_path: The path on the local file system. :type local_path: ``str`` :param remote_path: The path on the remote file system. :type remote_path: ``str`` """ if hostname.strip() == '' or hostname is None: raise ValueError('Empty hostname') command = [_get_path('scp'), '-o', 'StrictHostKeyChecking=no', '-o', 'ConnectTimeout=5', '-o', 'UserKnownHostsFile={}'.format(_KNOWN_HOSTS_FILE)] if idfile is not None: command.extend(['-i', idfile]) if username is not None: hostname = '%s@%s' % (username, hostname) remote_path = '{}:{}'.format(hostname, remote_path) if is_get: command.extend([remote_path, local_path]) else: command.extend([local_path, remote_path]) return ' '.join(command)
def _copy_to(entries, remote_path, local_path, profile): """ Performs an SCP command where the remote_path is the target and the local_path is the source. :param entries: A list of entries. :type entries: ``list`` of :py:class:`HostEntry` :param remote_path: The target path on the remote machine(s). :type remote_path: ``str`` :param local_path: The source path on the local machine. :type local_path: ``str`` :param profile: The profile, holding username/idfile info, etc. :type profile: :py:class:`Profile` """ commands = [] for entry in entries: hname = entry.hostname or entry.public_ip cmd = _build_scp_command(hname, profile.username, profile.identity_file, is_get=False, local_path=local_path, remote_path=remote_path) print('Command:', cmd) commands.append({ 'command': cmd, 'description': entry.display() }) stream_commands(commands) print(green('Finished copying'))
def _copy_from(entries, remote_path, local_path, profile): """ Performs an SCP command where the remote_path is the source and the local_path is a format string, formatted individually for each host being copied from so as to create one or more distinct paths on the local system. :param entries: A list of entries. :type entries: ``list`` of :py:class:`HostEntry` :param remote_path: The source path on the remote machine(s). :type remote_path: ``str`` :param local_path: A format string for the path on the local machine. :type local_path: ``str`` :param profile: The profile, holding username/idfile info, etc. :type profile: :py:class:`Profile` """ commands = [] paths = set() for entry in entries: hname = entry.hostname or entry.public_ip _local_path = entry.format_string(local_path) if _local_path in paths: raise ValueError('Duplicate local paths: one or more paths ' 'had value {} after formatting.' .format(local_path)) paths.add(_local_path) # If the path references a folder, create the folder if it doesn't # exist. _folder = os.path.split(_local_path)[0] if len(_folder) > 0: if not os.path.exists(_folder): print('Creating directory ' + _folder) os.makedirs(_folder) cmd = _build_scp_command(hname, profile.username, profile.identity_file, is_get=True, local_path=_local_path, remote_path=remote_path) print('Command:', cmd) commands.append({ 'command': cmd, 'description': entry.display() }) stream_commands(commands) print(green('Finished copying'))
def _run_ssh_command(entries, username, idfile, command, tunnel, parallel=False): """ Runs the given command over SSH in parallel on all hosts in `entries`. :param entries: The host entries the hostnames from. :type entries: ``list`` of :py:class:`HostEntry` :param username: To use a specific username. :type username: ``str`` or ``NoneType`` :param idfile: The SSH identity file to use, or none. :type idfile: ``str`` or ``NoneType`` :param command: The command to run. :type command: ``str`` :param parallel: If true, commands will be run in parallel. :type parallel: ``bool`` """ if len(entries) == 0: print('(No hosts to run command on)') return 1 if command.strip() == '' or command is None: raise ValueError('No command given') print('Running command {0} on {1} matching hosts' .format(green(repr(command)), len(entries))) shell_cmds = [] for entry in entries: hname = entry.hostname or entry.public_ip cmd = _build_ssh_command(hname, username, idfile, command, tunnel) shell_cmds.append({ 'command': cmd, 'description': entry.display() }) stream_commands(shell_cmds, parallel=parallel) print(green('All commands finished'))
def _connect_ssh(entry, username, idfile, tunnel=None): """ SSH into to a host. :param entry: The host entry to pull the hostname from. :type entry: :py:class:`HostEntry` :param username: To use a specific username. :type username: ``str`` or ``NoneType`` :param idfile: The SSH identity file to use, if supplying a username. :type idfile: ``str`` or ``NoneType`` :param tunnel: Host to tunnel SSH command through. :type tunnel: ``str`` or ``NoneType`` :return: An exit status code. :rtype: ``int`` """ if entry.hostname != "" and entry.hostname is not None: _host = entry.hostname elif entry.public_ip != "" and entry.public_ip is not None: _host = entry.public_ip elif entry.private_ip != "" and entry.private_ip is not None: if tunnel is None: raise ValueError("Entry does not have a hostname or public IP. " "You can connect via private IP if you use a " "tunnel.") _host = entry.private_ip else: raise ValueError("No hostname, public IP or private IP information " "found on host entry. I don't know how to connect.") command = _build_ssh_command(_host, username, idfile, None, tunnel) print('Connecting to %s...' % cyan(entry.display())) print('SSH command: %s' % green(command)) proc = subprocess.Popen(command, shell=True) return proc.wait()
def _get_args(): """Parse command-line arguments.""" parser = argparse.ArgumentParser(description='List EC2 instances') parser.add_argument('-l', '--latest', action='store_true', default=False, help='Query AWS for latest instances') parser.add_argument('--version', action='store_true', default=False, help='Print version and exit') parser.add_argument('--refresh-only', action='store_true', default=False, help='Refresh cache and exit') parser.add_argument('--host', help='Specific host to list', default=None) parser.add_argument('-s', '--ssh', action='store_true', help='SSH to instance', default=False) parser.add_argument('-i', '--identity-file', help='SSH identify file', default=None) parser.add_argument('-u', '--username', default=None, help='Log in as this user') parser.add_argument('filters', nargs='*', help='Text filters for output lines') parser.add_argument('-v', '--exclude', nargs='+', help='Exclude results that match these') parser.add_argument('-c', '--command', type=str, help='Command to run on matching instance(s)') parser.add_argument('-y', '--no-prompt', action='store_true', default=False, help="Don't ask for confirmation before running a " "command") parser.add_argument('-p', '--profile', type=str, help='Profile to use (defined in ~/.lsi)') parser.add_argument('--show', nargs='+', default=None, help='Instance attributes to show') parser.add_argument('--only', nargs='+', default=None, help='Show ONLY these instance attributes') parser.add_argument('--sep', type=str, default=None, help='Simple output with given separator') parser.add_argument('--sort-by', type=str, default="name", help='What to sort list by') parser.add_argument('-L', '--limit', type=int, default=None, help='Show at most this many entries') parser.add_argument('--attributes', action='store_true', help='Show all available attributes') parser.add_argument('--get', nargs=2, default=None, help='Get files from matching instances, must be ' 'followed by the source and destination filenames') parser.add_argument('--put', nargs=2, default=None, help='Put a local file on matching instances, must be ' 'followed by the source and destination filenames') parser.add_argument('-t', '--tunnel', default=None, help='Connect via the tunneled host.') parser.add_argument("-r", "--random", action="store_true", default=False, help="Choose a random instance from within results.") parser.add_argument("-n", "--num", type=int, default=None, help="Choose the given number from within results.") args = parser.parse_args() if args.exclude is None: args.exclude = [] # Presumably, if someone is sorting by something, they want to show that # thing... if args.sort_by is not None: args.show = (args.show or []) + [args.sort_by] return args
def load(cls, profile_name=None): """Loads the user's LSI profile, or provides a default.""" lsi_location = os.path.expanduser('~/.lsi') if not os.path.exists(lsi_location): return LsiProfile() cfg_parser = ConfigParser() cfg_parser.read(lsi_location) if profile_name is None: # Load the default profile if one exists; otherwise return empty. if cfg_parser.has_section('default'): profile_name = 'default' else: return cls() elif not cfg_parser.has_section(profile_name): raise cls.LoadError('No such profile {}'.format(profile_name)) def _get(option, alt=None): """Gets an option if it exists; else returns `alt`.""" if cfg_parser.has_option(profile_name, option): return cfg_parser.get(profile_name, option) else: return alt if cfg_parser.has_option(profile_name, 'inherit'): profile = cls.load(cfg_parser.get(profile_name, 'inherit')) else: profile = cls() profile.override('username', _get('username')) profile.override('identity_file', _get('identity file')) profile.override('command', _get('command')) filters = [s for s in _get('filters', '').split(',') if len(s) > 0] exclude = [s for s in _get('exclude', '').split(',') if len(s) > 0] profile.filters.extend(filters) profile.exclude.extend(exclude) return profile
def from_args(args): """Takes arguments parsed from argparse and returns a profile.""" # If the args specify a username explicitly, don't load from file. if args.username is not None or args.identity_file is not None: profile = LsiProfile() else: profile = LsiProfile.load(args.profile) profile.override('username', args.username) profile.override('identity_file', args.identity_file) profile.override('command', args.command) profile.no_prompt = args.no_prompt profile.filters.extend(args.filters) profile.exclude.extend(args.exclude) if profile.identity_file is not None: profile.identity_file = os.path.expanduser(profile.identity_file) return profile
def merge_(self, merge_dct): """ Recursive dict merge. Inspired by :meth:``dict.update()``, instead of updating only top-level keys, dict_merge recurses down into dicts nested to an arbitrary depth, updating keys. The ``merge_dct`` is merged into ``self``. :param self: dict onto which the merge is executed :param merge_dct: self merged into self :return: None """ for k, v in merge_dct.items(): if (k in self and isinstance(self[k], dict) and isinstance(merge_dct[k], collections.Mapping)): self[k].merge_(dicto(merge_dct[k])) else: self[k] = merge_dct[k] return self
def relate(self, part, id=None): """Relate this package component to the supplied part.""" assert part.name.startswith(self.base) name = part.name[len(self.base):].lstrip('/') rel = Relationship(self, name, part.rel_type, id=id) self.relationships.add(rel) return rel
def related(self, reltype): """Return a list of parts related to this one via reltype.""" parts = [] package = getattr(self, 'package', None) or self for rel in self.relationships.types.get(reltype, []): parts.append(package[posixpath.join(self.base, rel.target)]) return parts
def _load_rels(self, source): """Load relationships from source XML.""" # don't get confused here - the original source is string data; # the parameter source below is a Part object self.relationships.load(source=self, data=source)
def add(self, part, override=True): """Add a part to the package. It will also add a content-type - by default an override. If override is False then it will add a content-type for the extension if one isn't already present. """ ct_add_method = [ self.content_types.add_default, self.content_types.add_override, ][override] self[part.name] = part ct_add_method(part)
def _load_part(self, rel_type, name, data): """ Load a part into this package based on its relationship type """ if self.content_types.find_for(name) is None: log.warning('no content type found for part %(name)s' % vars()) return cls = Part.classes_by_rel_type[rel_type] part = cls(self, name) part.load(data) self[name] = part return part
def get_parts_by_class(self, cls): """ Return all parts of this package that are instances of cls (where cls is passed directly to isinstance, so can be a class or sequence of classes). """ return (part for part in self.parts.values() if isinstance(part, cls))
def find_for(self, name): """ Get the correct content type for a given name """ map = self.items # first search the overrides (by name) # then fall back to the defaults (by extension) # finally, return None if unmatched return map.get(name, None) or map.get(get_ext(name) or None, None)
def from_element(cls, element): "given an element, parse out the proper ContentType" # disambiguate the subclass ns, class_name = parse_tag(element.tag) class_ = getattr(ContentType, class_name) if not class_: msg = 'Invalid Types child element: %(class_name)s' % vars() raise ValueError(msg) # construct the subclass key = element.get(class_.key_name) name = element.get('ContentType') return class_(name, key)
def parse(input_string, prefix=''): """Parses the given DSL string and returns parsed results. Args: input_string (str): DSL string prefix (str): Optional prefix to add to every element name, useful to namespace things Returns: dict: Parsed content """ tree = parser.parse(input_string) visitor = ChatlVisitor(prefix) visit_parse_tree(tree, visitor) return visitor.parsed
def build(self, secret_key): """Builds a final copy of the token using the given secret key. :param secret_key(string): The secret key that corresponds to this builder's access key. """ key = jwk.JWK( kty='oct', k=base64url_encode(uuid.UUID(secret_key).bytes), ) header = { 'alg': 'dir', 'enc': 'A128GCM', 'zip': 'DEF', 'cty': 'JWT', 'kid': self._access_key, } now = int(time.time()) payload = { 'iat': now, 'nbf': now, } if self._expiration is not None: payload['exp'] = int(calendar.timegm(self._expiration.utctimetuple())) if len(self._view_identifiers) > 0: payload[VIEW_IDENTIFIERS_CLAIM_NAME] = self._view_identifiers if len(self._parameters) > 0: parameters = [] for parameter in self._parameters: serialized = { 'field': parameter.field, 'op': parameter.op, } if hasattr(parameter, '__iter__'): serialized['any'] = list(parameter.value) else: serialized['value'] = parameter.value parameters.append(serialized) payload[PARAMETERS_CLAIM_NAME] = parameters if len(self._attributes) > 0: payload[ATTRIBUTES_CLAIM_NAME] = self._attributes tok = jwe.JWE(json_encode(payload), protected=header) tok.add_recipient(key) return tok.serialize(compact=True)
def assign_force_field(ampal_obj, ff): """Assigns force field parameters to Atoms in the AMPAL object. Parameters ---------- ampal_obj : AMPAL Object Any AMPAL object with a `get_atoms` method. ff: BuffForceField The force field to be used for scoring. """ if hasattr(ampal_obj, 'ligands'): atoms = ampal_obj.get_atoms(ligands=True, inc_alt_states=True) else: atoms = ampal_obj.get_atoms(inc_alt_states=True) for atom in atoms: w_str = None a_ff_id = None if atom.element == 'H': continue elif atom.parent.mol_code.upper() in ff: if atom.res_label.upper() in ff[atom.parent.mol_code]: a_ff_id = (atom.parent.mol_code.upper(), atom.res_label.upper()) elif atom.res_label.upper() in ff['WLD']: a_ff_id = ('WLD', atom.res_label.upper()) else: w_str = ('{} atom is not parameterised in the selected ' 'force field for {} residues, this will be ' 'ignored.').format( atom.res_label, atom.parent.mol_code) elif atom.res_label.upper() in ff['WLD']: a_ff_id = ('WLD', atom.res_label.upper()) else: w_str = ('{} ({}) atom is not parameterised in the selected' ' residue force field.').format( atom.res_label, atom.parent.mol_code) if w_str: warnings.warn(w_str, NotParameterisedWarning) atom.tags['_buff_ff_id'] = a_ff_id return
def find_max_rad_npnp(self): """Finds the maximum radius and npnp in the force field. Returns ------- (max_rad, max_npnp): (float, float) Maximum radius and npnp distance in the loaded force field. """ max_rad = 0 max_npnp = 0 for res, _ in self.items(): if res != 'KEY': for _, ff_params in self[res].items(): if max_rad < ff_params[1]: max_rad = ff_params[1] if max_npnp < ff_params[4]: max_npnp = ff_params[4] return max_rad, max_npnp
def _make_ff_params_dict(self): """Makes a dictionary containing PyAtomData for the force field. Returns ------- ff_params_struct_dict: dict Dictionary containing PyAtomData structs for the force field parameters for each atom in the force field. """ try: ff_params_struct_dict = {} for res in self.keys(): if res == 'KEY': continue if res not in ff_params_struct_dict: ff_params_struct_dict[res] = {} for atom, params in self[res].items(): ff_params_struct_dict[res][atom] = PyAtomData( atom.encode(), params[0].encode(), *params[1:]) except TypeError: raise ForceFieldParameterError( 'Badly formatted force field parameters: {}'.format(params)) return ff_params_struct_dict
def save(self, target=None): """ Save this package to target, which should be a filename or open file stream. If target is not supplied, and this package has a filename attribute (such as when this package was created from an existing file), it will be used. """ target = target or getattr(self, 'filename', None) if isinstance(target, six.string_types): self.filename = target target = open(target, 'wb') if target is None: msg = ( "Target filename required if %s was not opened from a file" % self.__class__.__name__ ) raise ValueError(msg) self._store(target)
def as_stream(self): """ Return a zipped package as a readable stream """ stream = io.BytesIO() self._store(stream) stream.seek(0) return stream
def _get_matching_segments(self, zf, name): """ Return a generator yielding each of the segments who's names match name. """ for n in zf.namelist(): if n.startswith(name): yield zf.read(n)
def copy_dir(bucket_name, src_path, dest_path, aws_access_key_id=None, aws_secret_access_key=None, aws_profile=None, surrogate_key=None, cache_control=None, surrogate_control=None, create_directory_redirect_object=True): """Copy objects from one directory in a bucket to another directory in the same bucket. Object metadata is preserved while copying, with the following exceptions: - If a new surrogate key is provided it will replace the original one. - If ``cache_control`` and ``surrogate_control`` values are provided they will replace the old one. Parameters ---------- bucket_name : `str` Name of an S3 bucket. src_path : `str` Source directory in the S3 bucket. The ``src_path`` should ideally end in a trailing `'/'`. E.g. `'dir/dir2/'`. dest_path : `str` Destination directory in the S3 bucket. The ``dest_path`` should ideally end in a trailing `'/'`. E.g. `'dir/dir2/'`. The destination path cannot contain the source path. aws_access_key_id : `str` The access key for your AWS account. Also set ``aws_secret_access_key``. aws_secret_access_key : `str` The secret key for your AWS account. aws_profile : `str`, optional Name of AWS profile in :file:`~/.aws/credentials`. Use this instead of ``aws_access_key_id`` and ``aws_secret_access_key`` for file-based credentials. surrogate_key : `str`, optional The surrogate key to insert in the header of all objects in the ``x-amz-meta-surrogate-key`` field. This key is used to purge builds from the Fastly CDN when Editions change. If `None` then no header will be set. If the object already has a ``x-amz-meta-surrogate-key`` header then it will be replaced. cache_control : `str`, optional This sets (and overrides) the ``Cache-Control`` header on the copied files. The ``Cache-Control`` header specifically dictates how content is cached by the browser (if ``surrogate_control`` is also set). surrogate_control : `str`, optional This sets (and overrides) the ``x-amz-meta-surrogate-control`` header on the copied files. The ``Surrogate-Control`` or ``x-amz-meta-surrogate-control`` header is used in priority by Fastly to givern it's caching. This caching policy is *not* passed to the browser. create_directory_redirect_object : `bool`, optional Create a directory redirect object for the root directory. The directory redirect object is an empty S3 object named after the directory (without a trailing slash) that contains a ``x-amz-meta-dir-redirect=true`` HTTP header. LSST the Docs' Fastly VCL is configured to redirect requests for a directory path to the directory's ``index.html`` (known as *courtesy redirects*). Raises ------ ltdconveyor.s3.S3Error Thrown by any unexpected faults from the S3 API. RuntimeError Thrown when the source and destination directories are the same. """ if not src_path.endswith('/'): src_path += '/' if not dest_path.endswith('/'): dest_path += '/' # Ensure the src_path and dest_path don't contain each other common_prefix = os.path.commonprefix([src_path, dest_path]) if common_prefix == src_path: msg = 'Common prefix {0} is same as source dir {1}'.format( common_prefix, src_path) raise RuntimeError(msg) if common_prefix == dest_path: msg = 'Common prefix {0} is same as dest dir {1}'.format( common_prefix, dest_path) raise RuntimeError(msg) # Delete any existing objects in the destination delete_dir(bucket_name, dest_path, aws_access_key_id, aws_secret_access_key) session = boto3.session.Session( aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key, profile_name=aws_profile) s3 = session.resource('s3') bucket = s3.Bucket(bucket_name) # Copy each object from source to destination for src_obj in bucket.objects.filter(Prefix=src_path): src_rel_path = os.path.relpath(src_obj.key, start=src_path) dest_key_path = os.path.join(dest_path, src_rel_path) # the src_obj (ObjectSummary) doesn't include headers afaik head = s3.meta.client.head_object(Bucket=bucket_name, Key=src_obj.key) metadata = head['Metadata'] content_type = head['ContentType'] # try to use original Cache-Control header if new one is not set if cache_control is None and 'CacheControl' in head: cache_control = head['CacheControl'] if surrogate_control is not None: metadata['surrogate-control'] = surrogate_control if surrogate_key is not None: metadata['surrogate-key'] = surrogate_key s3.meta.client.copy_object( Bucket=bucket_name, Key=dest_key_path, CopySource={'Bucket': bucket_name, 'Key': src_obj.key}, MetadataDirective='REPLACE', Metadata=metadata, ACL='public-read', CacheControl=cache_control, ContentType=content_type) if create_directory_redirect_object: dest_dirname = dest_path.rstrip('/') obj = bucket.Object(dest_dirname) metadata = {'dir-redirect': 'true'} obj.put(Body='', ACL='public-read', Metadata=metadata, CacheControl=cache_control)
def open_bucket(bucket_name, aws_access_key_id=None, aws_secret_access_key=None, aws_profile=None): """Open an S3 Bucket resource. Parameters ---------- bucket_name : `str` Name of the S3 bucket. aws_access_key_id : `str`, optional The access key for your AWS account. Also set ``aws_secret_access_key``. aws_secret_access_key : `str`, optional The secret key for your AWS account. aws_profile : `str`, optional Name of AWS profile in :file:`~/.aws/credentials`. Use this instead of ``aws_access_key_id`` and ``aws_secret_access_key`` for file-based credentials. Returns ------- bucket : Boto3 S3 Bucket instance The S3 bucket as a Boto3 instance. """ session = boto3.session.Session( profile_name=aws_profile, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) s3 = session.resource('s3') bucket = s3.Bucket(bucket_name) return bucket
def upload_dir(bucket_name, path_prefix, source_dir, upload_dir_redirect_objects=True, surrogate_key=None, surrogate_control=None, cache_control=None, acl=None, aws_access_key_id=None, aws_secret_access_key=None, aws_profile=None): """Upload a directory of files to S3. This function places the contents of the Sphinx HTML build directory into the ``/path_prefix/`` directory of an *existing* S3 bucket. Existing files on S3 are overwritten; files that no longer exist in the ``source_dir`` are deleted from S3. Parameters ---------- bucket_name : `str` Name of the S3 bucket where documentation is uploaded. path_prefix : `str` The root directory in the bucket where documentation is stored. source_dir : `str` Path of the Sphinx HTML build directory on the local file system. The contents of this directory are uploaded into the ``/path_prefix/`` directory of the S3 bucket. upload_dir_redirect_objects : `bool`, optional A feature flag to enable uploading objects to S3 for every directory. These objects contain ``x-amz-meta-dir-redirect=true`` HTTP headers that tell Fastly to issue a 301 redirect from the directory object to the `index.html`` in that directory. surrogate_key : `str`, optional The surrogate key to insert in the header of all objects in the ``x-amz-meta-surrogate-key`` field. This key is used to purge builds from the Fastly CDN when Editions change. If `None` then no header will be set. cache_control : `str`, optional This sets the ``Cache-Control`` header on the uploaded files. The ``Cache-Control`` header specifically dictates how content is cached by the browser (if ``surrogate_control`` is also set). surrogate_control : `str`, optional This sets the ``x-amz-meta-surrogate-control`` header on the uploaded files. The ``Surrogate-Control`` or ``x-amz-meta-surrogate-control`` header is used in priority by Fastly to givern it's caching. This caching policy is *not* passed to the browser. acl : `str`, optional The pre-canned AWS access control list to apply to this upload. Can be ``'public-read'``, which allow files to be downloaded over HTTP by the public. See https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl for an overview of S3's pre-canned ACL lists. Note that ACL settings are not validated locally. Default is `None`, meaning that no ACL is applied to an individual object. In this case, use ACLs applied to the bucket itself. aws_access_key_id : `str`, optional The access key for your AWS account. Also set ``aws_secret_access_key``. aws_secret_access_key : `str`, optional The secret key for your AWS account. aws_profile : `str`, optional Name of AWS profile in :file:`~/.aws/credentials`. Use this instead of ``aws_access_key_id`` and ``aws_secret_access_key`` for file-based credentials. Notes ----- ``cache_control`` and ``surrogate_control`` can be used together. ``surrogate_control`` takes priority in setting Fastly's POP caching, while ``cache_control`` then sets the browser's caching. For example: - ``cache_control='no-cache'`` - ``surrogate_control='max-age=31536000'`` together will ensure that the browser always does an ETAG server query, but that Fastly will cache the content for one year (or until purged). This configuration is good for files that are frequently changed in place. For immutable uploads simply using ``cache_control`` is more efficient since it allows the browser to also locally cache content. .. seelso: - `Fastly: Cache control tutorial <https://docs.fastly.com/guides/tutorials/cache-control-tutorial>`_. - `Google: HTTP caching <http://ls.st/39v>`_. """ logger = logging.getLogger(__name__) logger.debug('s3upload.upload({0}, {1}, {2})'.format( bucket_name, path_prefix, source_dir)) session = boto3.session.Session( profile_name=aws_profile, aws_access_key_id=aws_access_key_id, aws_secret_access_key=aws_secret_access_key) s3 = session.resource('s3') bucket = s3.Bucket(bucket_name) metadata = {} if surrogate_key is not None: metadata['surrogate-key'] = surrogate_key if surrogate_control is not None: metadata['surrogate-control'] = surrogate_control manager = ObjectManager(session, bucket_name, path_prefix) for (rootdir, dirnames, filenames) in os.walk(source_dir): # name of root directory on S3 bucket bucket_root = os.path.relpath(rootdir, start=source_dir) if bucket_root in ('.', '/'): bucket_root = '' # Delete bucket directories that no longer exist in source bucket_dirnames = manager.list_dirnames_in_directory(bucket_root) for bucket_dirname in bucket_dirnames: if bucket_dirname not in dirnames: logger.debug(('Deleting bucket directory {0}'.format( bucket_dirname))) manager.delete_directory(bucket_dirname) # Delete files that no longer exist in source bucket_filenames = manager.list_filenames_in_directory(bucket_root) for bucket_filename in bucket_filenames: if bucket_filename not in filenames: bucket_filename = os.path.join(bucket_root, bucket_filename) logger.debug( 'Deleting bucket file {0}'.format(bucket_filename)) manager.delete_file(bucket_filename) # Upload files in directory for filename in filenames: local_path = os.path.join(rootdir, filename) bucket_path = os.path.join(path_prefix, bucket_root, filename) logger.debug('Uploading to {0}'.format(bucket_path)) upload_file(local_path, bucket_path, bucket, metadata=metadata, acl=acl, cache_control=cache_control) # Upload a directory redirect object if upload_dir_redirect_objects is True: bucket_dir_path = os.path.join(path_prefix, bucket_root) create_dir_redirect_object( bucket_dir_path, bucket, metadata=metadata, acl=acl, cache_control=cache_control)
def upload_file(local_path, bucket_path, bucket, metadata=None, acl=None, cache_control=None): """Upload a file to the S3 bucket. This function uses the mimetypes module to guess and then set the Content-Type and Encoding-Type headers. Parameters ---------- local_path : `str` Full path to a file on the local file system. bucket_path : `str` Destination path (also known as the key name) of the file in the S3 bucket. bucket : boto3 Bucket instance S3 bucket. metadata : `dict`, optional Header metadata values. These keys will appear in headers as ``x-amz-meta-*``. acl : `str`, optional A pre-canned access control list. See https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl Default is `None`, mean that no ACL is applied to the object. cache_control : `str`, optional The cache-control header value. For example, ``'max-age=31536000'``. """ logger = logging.getLogger(__name__) extra_args = {} if acl is not None: extra_args['ACL'] = acl if metadata is not None and len(metadata) > 0: # avoid empty Metadata extra_args['Metadata'] = metadata if cache_control is not None: extra_args['CacheControl'] = cache_control # guess_type returns None if it cannot detect a type content_type, content_encoding = mimetypes.guess_type(local_path, strict=False) if content_type is not None: extra_args['ContentType'] = content_type logger.debug(str(extra_args)) obj = bucket.Object(bucket_path) # no return status from the upload_file api obj.upload_file(local_path, ExtraArgs=extra_args)
def upload_object(bucket_path, bucket, content='', metadata=None, acl=None, cache_control=None, content_type=None): """Upload an arbitrary object to an S3 bucket. Parameters ---------- bucket_path : `str` Destination path (also known as the key name) of the file in the S3 bucket. content : `str` or `bytes`, optional Object content. bucket : boto3 Bucket instance S3 bucket. metadata : `dict`, optional Header metadata values. These keys will appear in headers as ``x-amz-meta-*``. acl : `str`, optional A pre-canned access control list. See https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl Default is `None`, meaning that no ACL is applied to the object. cache_control : `str`, optional The cache-control header value. For example, ``'max-age=31536000'``. content_type : `str`, optional The object's content type (such as ``text/html``). If left unset, no MIME type is passed to boto3 (which defaults to ``binary/octet-stream``). """ obj = bucket.Object(bucket_path) # Object.put seems to be sensitive to None-type kwargs, so we filter first args = {} if metadata is not None and len(metadata) > 0: # avoid empty Metadata args['Metadata'] = metadata if acl is not None: args['ACL'] = acl if cache_control is not None: args['CacheControl'] = cache_control if content_type is not None: args['ContentType'] = content_type obj.put(Body=content, **args)
def create_dir_redirect_object(bucket_dir_path, bucket, metadata=None, acl=None, cache_control=None): """Create an S3 object representing a directory that's designed to redirect a browser (via Fastly) to the ``index.html`` contained inside that directory. Parameters ---------- bucket_dir_path : `str` Full name of the object in the S3, which is equivalent to a POSIX directory path, like ``dir1/dir2``. bucket : boto3 Bucket instance S3 bucket. metadata : `dict`, optional Header metadata values. These keys will appear in headers as ``x-amz-meta-*``. acl : `str`, optional A pre-canned access control list. See https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl Default is None, meaning that no ACL is applied to the object. cache_control : `str`, optional The cache-control header value. For example, ``'max-age=31536000'``. """ # Just the name of the 'directory' itself bucket_dir_path = bucket_dir_path.rstrip('/') # create a copy of metadata if metadata is not None: metadata = dict(metadata) else: metadata = {} # header used by LTD's Fastly Varnish config to create a 301 redirect metadata['dir-redirect'] = 'true' upload_object(bucket_dir_path, content='', bucket=bucket, metadata=metadata, acl=acl, cache_control=cache_control)
def list_filenames_in_directory(self, dirname): """List all file-type object names that exist at the root of this bucket directory. Parameters ---------- dirname : `str` Directory name in the bucket relative to ``bucket_root/``. Returns ------- filenames : `list` List of file names (`str`), relative to ``bucket_root/``, that exist at the root of ``dirname``. """ prefix = self._create_prefix(dirname) filenames = [] for obj in self._bucket.objects.filter(Prefix=prefix): if obj.key.endswith('/'): # a directory redirect object, not a file continue obj_dirname = os.path.dirname(obj.key) if obj_dirname == prefix: # object is at root of directory filenames.append(os.path.relpath(obj.key, start=prefix)) return filenames