code
stringlengths 1
18.2k
|
|---|
def _get_fillcolor(resource_type, properties, known_sg=[], open_sg=[]): """Determine fillcolor for resources (public ones in this case) """ fillcolor = None # check security groups if 'SecurityGroups' in properties: # check for external security groups for sg in properties['SecurityGroups']: if 'Ref' in sg and (sg['Ref'] not in known_sg): fillcolor = 'yellow' break # check for open security groups for osg in open_sg: if {'Ref': osg} in properties['SecurityGroups']:
|
fillcolor = 'red' break # LoadBalancer if resource_type == 'LoadBalancer': if ('Scheme' not in properties) or \ properties['Scheme'] == 'internet-facing': fillcolor = 'red' return fillcolor
|
def svg_output(dotfile, outfile='cloudformation.svg'): """Render template into svg file using the dot command (must be installed). :param dotfile: path to the dotfile :param outfile: filename for the output file :return: """ try: cmd = ['dot', '-Tsvg', '-o' + outfile, dotfile] subprocess.check_output(cmd, stderr=subprocess.STDOUT) except subprocess.CalledProcessError as e: sys.stderr.write( '\033[01;31mError running command: %s resulted in the ' % e.cmd + 'following error: \033[01;32m %s' % e.output) return
|
1 return 0
|
def start(controller_class): """Start the Helper controller either in the foreground or as a daemon process. :param controller_class: The controller class handle to create and run :type controller_class: callable """ args = parser.parse() obj = controller_class(args, platform.operating_system()) if args.foreground: try: obj.start() except KeyboardInterrupt: obj.stop() else: try: with platform.Daemon(obj) as daemon: daemon.start() except (OSError, ValueError) as error: sys.stderr.write('\nError starting %s: %s\n\n' % (sys.argv[0], error)) sys.exit(1)
|
def run(self, steps=float('inf')): """ Run to the current end of the program or a number of steps :return: """ while len(self.program) > (self.register['PC'] - 1): steps -= 1 if steps < 0: break self.program[self.register['PC'] - 1]() self.register['PC'] += 1
|
def _validate_type(self, name, obj, *args): """ Helper function that checks the input object type against each in a list of classes. This function also allows the input value to be equal to None. :param name: Name of the object. :param obj: Object to check the type of. :param args: List of classes. :raises TypeError: if the input object is not of any of the
|
allowed types. """ if obj is None: return for arg in args: if isinstance(obj, arg): return raise TypeError(self.__class__.__name__ + '.' + name + ' is of type ' + type(obj).__name__ + '. Must be equal to None or one of the following types: ' + str(args))
|
def _validate_list_type(self, name, obj, *args): """ Helper function that checks the input object type against each in a list of classes, or if the input object is a list, each value that it contains against that list. :param name: Name of the object. :param obj: Object to check the type of. :param args: List of classes. :raises TypeError: if the input object is not
|
of any of the allowed types. """ if obj is None: return if isinstance(obj, list): for i in obj: self._validate_type_not_null(name, i, *args) else: self._validate_type(name, obj, *args)
|
def _validate_nested_list_type(self, name, obj, nested_level, *args): """ Helper function that checks the input object as a list then recursively until nested_level is 1. :param name: Name of the object. :param obj: Object to check the type of. :param nested_level: Integer with the current nested level. :param args: List of classes. :raises TypeError: if the input object is not of any of the allowed types.
|
""" if nested_level <= 1: self._validate_list_type(name, obj, *args) else: if obj is None: return if not isinstance(obj, list): raise TypeError(self.__class__.__name__ + '.' + name + ' contains value of type ' + type(obj).__name__ + ' where a list is expected') for sub_obj in obj: self._validate_nested_list_type(name, sub_obj, nested_level - 1, *args)
|
def B(self, params): """ B label Unconditional branch to the address at label """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # TODO check if label is within +- 2 KB # B label def B_func(): if label == '.': raise iarm.exceptions.EndOfProgram("You have reached an infinite loop") self.register['PC'] = self.labels[label] return B_func
|
def BL(self, params): """ BL label Branch to the label, storing the next instruction in the Link Register """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # TODO check if label is within +- 16 MB # BL label def BL_func(): self.register['LR'] = self.register['PC'] # No need for the + 1, PC already points to the next instruction self.register['PC'] = self.labels[label] return BL_func
|
def BLX(self, params): """ BLX Rj Branch to the address in Rj, storing the next instruction in the Link Register """ Rj = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(LR_or_general_purpose_registers=(Rj,)) def BLX_func(): self.register['LR'] = self.register['PC'] # No need for the + 1, PC already points to the next instruction self.register['PC'] = self.register[Rj] return BLX_func
|
def BX(self, params): """ BX Rj Jump to the address in the Link Register """ Rj = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(LR_or_general_purpose_registers=(Rj,)) def BX_func(): self.register['PC'] = self.register[Rj] return BX_func
|
def get_version(version): """ Returns a PEP 440-compliant version number from VERSION. Created by modifying django.utils.version.get_version """ # Now build the two parts of the version number: # major = X.Y[.Z] # sub = .devN - for development releases # | {a|b|rc}N - for alpha, beta and rc releases # | .postN - for post-release releases assert len(version) == 5 version_parts = version[:2] if version[2]
|
== 0 else version[:3] # Build the first part of the version major = '.'.join(str(x) for x in version_parts) # Just return it if this is a final release version if version[3] == 'final': return major # Add the rest sub = ''.join(str(x) for x in version[3:5]) if version[3] == 'dev': # Override the sub part. Add in a timestamp timestamp = get_git_changeset() sub
|
= 'dev%s' % (timestamp if timestamp else version[4]) return '%s.%s' % (major, sub) if version[3] == 'post': # We need a dot for post return '%s.%s' % (major, sub) elif version[3] in ('a', 'b', 'rc'): # No dot for these return '%s%s' % (major, sub) else: raise ValueError('Invalid version: %s' % str(version))
|
def get_git_changeset(): """Returns a numeric identifier of the latest git changeset. The result is the UTC timestamp of the changeset in YYYYMMDDHHMMSS format. This value isn't guaranteed to be unique, but collisions are very unlikely, so it's sufficient for generating the development version numbers. """ repo_dir = os.path.dirname(os.path.abspath(__file__)) git_log = subprocess.Popen('git log --pretty=format:%ct --quiet -1 HEAD', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, cwd=repo_dir, universal_newlines=True) timestamp = git_log.communicate()[0]
|
try: timestamp = datetime.datetime.utcfromtimestamp(int(timestamp)) return timestamp.strftime('%Y%m%d%H%M%S') except ValueError: return None
|
def load_cloudformation_template(path=None): """Load cloudformation template from path. :param path: Absolute or relative path of cloudformation template. Defaults to cwd. :return: module, success """ if not path: path = os.path.abspath('cloudformation.py') else: path = os.path.abspath(path) if isinstance(path, six.string_types): try: sp = sys.path # temporarily add folder to allow relative path sys.path.append(os.path.abspath(os.path.dirname(path))) cloudformation = imp.load_source('cloudformation', path) sys.path = sp # restore # use cfn template hooks if
|
not check_hook_mechanism_is_intact(cloudformation): # no hooks - do nothing log.debug( 'No valid hook configuration: \'%s\'. Not using hooks!', path) else: if check_register_present(cloudformation): # register the template hooks so they listen to gcdt_signals cloudformation.register() return cloudformation, True except GracefulExit: raise except ImportError as e: print('could not find package for import: %s' % e) except Exception as e: print('could not import cloudformation.py, maybe something wrong ', 'with
|
your code?') print(e) return None, False
|
def get_parameter_diff(awsclient, config): """get differences between local config and currently active config """ client_cf = awsclient.get_client('cloudformation') try: stack_name = config['stack']['StackName'] if stack_name: response = client_cf.describe_stacks(StackName=stack_name) if response['Stacks']: stack_id = response['Stacks'][0]['StackId'] stack = response['Stacks'][0] else: return None else: print( 'StackName is not configured, could not create parameter diff') return None except GracefulExit: raise except Exception: # probably the stack is not existent return None changed
|
= 0 table = [] table.append(['Parameter', 'Current Value', 'New Value']) # Check if there are parameters for the stack if 'Parameters' in stack: for param in stack['Parameters']: try: old = str(param['ParameterValue']) # can not compare list with str!! # if ',' in old: # old = old.split(',') new = config['parameters'][param['ParameterKey']] if old != new: if old.startswith('***'): # parameter is configured with `NoEcho=True` # this
|
means we can not really say if the value changed!! # for security reasons we block viewing the new value new = old table.append([param['ParameterKey'], old, new]) changed += 1 except GracefulExit: raise except Exception: print('Did not find %s in local config file' % param[ 'ParameterKey']) if changed > 0: print(tabulate(table, tablefmt='fancy_grid')) return changed > 0
|
def call_pre_hook(awsclient, cloudformation): """Invoke the pre_hook BEFORE the config is read. :param awsclient: :param cloudformation: """ # TODO: this is deprecated!! move this to glomex_config_reader # no config available if not hasattr(cloudformation, 'pre_hook'): # hook is not present return hook_func = getattr(cloudformation, 'pre_hook') if not hook_func.func_code.co_argcount: hook_func() # for compatibility with existing templates else: log.error('pre_hock can not have any arguments. The pre_hook it is
|
' + 'executed BEFORE config is read')
|
def deploy_stack(awsclient, context, conf, cloudformation, override_stack_policy=False): """Deploy the stack to AWS cloud. Does either create or update the stack. :param conf: :param override_stack_policy: :return: exit_code """ stack_name = _get_stack_name(conf) parameters = _generate_parameters(conf) if stack_exists(awsclient, stack_name): exit_code = _update_stack(awsclient, context, conf, cloudformation, parameters, override_stack_policy) else: exit_code = _create_stack(awsclient, context, conf, cloudformation, parameters) # add 'stack_output' to the context so it becomes available # in 'command_finalized'
|
hook context['stack_output'] = _get_stack_outputs( awsclient.get_client('cloudformation'), stack_name) _call_hook(awsclient, conf, stack_name, parameters, cloudformation, hook='post_hook', message='CloudFormation is done, now executing post hook...') return exit_code
|
def delete_stack(awsclient, conf, feedback=True): """Delete the stack from AWS cloud. :param awsclient: :param conf: :param feedback: print out stack events (defaults to True) """ client_cf = awsclient.get_client('cloudformation') stack_name = _get_stack_name(conf) last_event = _get_stack_events_last_timestamp(awsclient, stack_name) request = {} dict_selective_merge(request, conf['stack'], ['StackName', 'RoleARN']) response = client_cf.delete_stack(**request) if feedback: return _poll_stack_events(awsclient, stack_name, last_event)
|
def list_stacks(awsclient): """Print out the list of stacks deployed at AWS cloud. :param awsclient: :return: """ client_cf = awsclient.get_client('cloudformation') response = client_cf.list_stacks( StackStatusFilter=[ 'CREATE_IN_PROGRESS', 'CREATE_COMPLETE', 'ROLLBACK_IN_PROGRESS', 'ROLLBACK_COMPLETE', 'DELETE_IN_PROGRESS', 'DELETE_FAILED', 'UPDATE_IN_PROGRESS', 'UPDATE_COMPLETE_CLEANUP_IN_PROGRESS', 'UPDATE_COMPLETE', 'UPDATE_ROLLBACK_IN_PROGRESS', 'UPDATE_ROLLBACK_FAILED', 'UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS', 'UPDATE_ROLLBACK_COMPLETE', ] ) result = {} stack_sum = 0 for summary in response['StackSummaries']: result['StackName'] = summary["StackName"] result['CreationTime'] = summary['CreationTime'] result['StackStatus'] = summary['StackStatus'] print(json2table(result)) stack_sum += 1 print('listed %s stacks'
|
% str(stack_sum))
|
def describe_change_set(awsclient, change_set_name, stack_name): """Print out the change_set to console. This needs to run create_change_set first. :param awsclient: :param change_set_name: :param stack_name: """ client = awsclient.get_client('cloudformation') status = None while status not in ['CREATE_COMPLETE', 'FAILED']: response = client.describe_change_set( ChangeSetName=change_set_name, StackName=stack_name) status = response['Status'] # print('##### %s' % status) if status == 'FAILED': print(response['StatusReason']) elif status == 'CREATE_COMPLETE': for change in response['Changes']: print(json2table(change['ResourceChange']))
|
def delete_change_set(awsclient, change_set_name, stack_name): """Delete specified change set. Currently we only use this during automated regression testing. But we have plans so lets locate this functionality here :param awsclient: :param change_set_name: :param stack_name: """ client = awsclient.get_client('cloudformation') response = client.delete_change_set( ChangeSetName=change_set_name, StackName=stack_name)
|
def write_template_to_file(conf, template_body): """Writes the template to disk """ template_file_name = _get_stack_name(conf) + '-generated-cf-template.json' with open(template_file_name, 'w') as opened_file: opened_file.write(template_body) print('wrote cf-template for %s to disk: %s' % ( get_env(), template_file_name)) return template_file_name
|
def generate_template(context, config, cloudformation): """call cloudformation to generate the template (json format). :param context: :param config: :param cloudformation: :return: """ spec = inspect.getargspec(cloudformation.generate_template)[0] if len(spec) == 0: return cloudformation.generate_template() elif spec == ['context', 'config']: return cloudformation.generate_template(context, config) else: raise Exception('Arguments of \'generate_template\' not as expected: %s' % spec)
|
def info(awsclient, config, format=None): """ collect info and output to console :param awsclient: :param config: :param json: True / False to use json format as output :return: """ if format is None: format = 'tabular' stack_name = _get_stack_name(config) client_cfn = awsclient.get_client('cloudformation') resources = all_pages( client_cfn.list_stack_resources, {'StackName': stack_name}, lambda x: [(r['ResourceType'], r['LogicalResourceId'], r['ResourceStatus']) for r in x['StackResourceSummaries']] ) infos = { 'stack_output': _get_stack_outputs(client_cfn, stack_name), 'stack_state':
|
_get_stack_state(client_cfn, stack_name), 'resources': resources } if format == 'json': print(json.dumps(infos)) elif format == 'tabular': print('stack output:') print(tabulate(infos['stack_output'], tablefmt='fancy_grid')) print('\nstack_state: %s' % infos['stack_state']) print('\nresources:') print(tabulate(infos['resources'], tablefmt='fancy_grid'))
|
def BCC(self, params): """ BCC label Branch to the instruction at label if the C flag is not set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BCC label def BCC_func(): if not self.is_C_set(): self.register['PC'] = self.labels[label] return BCC_func
|
def BCS(self, params): """ BCS label Branch to the instruction at label if the C flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BCS label def BCS_func(): if self.is_C_set(): self.register['PC'] = self.labels[label] return BCS_func
|
def BEQ(self, params): """ BEQ label Branch to the instruction at label if the Z flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BEQ label def BEQ_func(): if self.is_Z_set(): self.register['PC'] = self.labels[label] return BEQ_func
|
def BGE(self, params): """ BGE label Branch to the instruction at label if the N flag is the same as the V flag """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BGE label def BGE_func(): if self.is_N_set() == self.is_V_set(): self.register['PC'] = self.labels[label] return BGE_func
|
def BGT(self, params): """ BGT label Branch to the instruction at label if the N flag is the same as the V flag and the Z flag is not set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BGT label def BGT_func(): if (self.is_N_set() == self.is_V_set()) and not self.is_Z_set(): self.register['PC'] = self.labels[label] return BGT_func
|
def BHI(self, params): """ BHI label Branch to the instruction at label if the C flag is set and the Z flag is not set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BHI label def BHI_func(): if self.is_C_set() and not self.is_Z_set(): self.register['PC'] = self.labels[label] return BHI_func
|
def BHS(self, params): """ BHS label Branch to the instruction at label if the C flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BHS label def BHS_func(): if self.is_C_set(): self.register['PC'] = self.labels[label] return BHS_func
|
def BLE(self, params): """ BLE label Branch to the instruction at label if the Z flag is set or if the N flag is not the same as the V flag """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BLE label def BLE_func(): if self.is_Z_set() or (self.is_N_set() != self.is_V_set()): self.register['PC'] = self.labels[label] return BLE_func
|
def BLO(self, params): """ BLO label Branch to the instruction at label if the C flag is not set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BLO label def BLO_func(): if not self.is_C_set(): self.register['PC'] = self.labels[label] return BLO_func
|
def BLS(self, params): """ BLS label Branch to the instruction at label if the C flag is not set or the Z flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BLS label def BLS_func(): if (not self.is_C_set()) or self.is_Z_set(): self.register['PC'] = self.labels[label] return BLS_func
|
def BLT(self, params): """ BLT label Branch to the instruction at label if the N flag is not the same as the V flag """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BLT label def BLT_func(): if self.is_N_set() != self.is_V_set(): self.register['PC'] = self.labels[label] return BLT_func
|
def BMI(self, params): """ BMI label Branch to the instruction at label if the N flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BMI label def BMI_func(): if self.is_N_set(): self.register['PC'] = self.labels[label] return BMI_func
|
def BNE(self, params): """ BNE label Branch to the instruction at label if the Z flag is not set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BNE label def BNE_func(): if not self.is_Z_set(): self.register['PC'] = self.labels[label] return BNE_func
|
def BPL(self, params): """ BPL label Branch to the instruction at label if the N flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BPL label def BPL_func(): if not self.is_N_set(): self.register['PC'] = self.labels[label] return BPL_func
|
def BVC(self, params): """ BVC label Branch to the instruction at label if the V flag is not set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BVC label def BVC_func(): if not self.is_V_set(): self.register['PC'] = self.labels[label] return BVC_func
|
def BVS(self, params): """ BVS label Branch to the instruction at label if the V flag is set """ label = self.get_one_parameter(self.ONE_PARAMETER, params) self.check_arguments(label_exists=(label,)) # BVS label def BVS_func(): if self.is_V_set(): self.register['PC'] = self.labels[label] return BVS_func
|
def get_queryset(self, request): """Shows one entry per distinct metric name""" queryset = super(MetricGroupAdmin, self).get_queryset(request) # poor-man's DISTINCT ON for Sqlite3 qs_values = queryset.values('id', 'name') # 2.7+ only :( # = {metric['name']: metric['id'] for metric in qs_values} distinct_names = {} for metric in qs_values: distinct_names[metric['name']] = metric['id'] queryset = self.model.objects.filter(id__in=distinct_names.values()) return queryset
|
def save_model(self, request, obj, form, change): """Updates all metrics with the same name""" like_metrics = self.model.objects.filter(name=obj.name) # 2.7+ only :( # = {key: form.cleaned_data[key] for key in form.changed_data} updates = {} for key in form.changed_data: updates[key] = form.cleaned_data[key] like_metrics.update(**updates)
|
def parse_lines(self, code): """ Return a list of the parsed code For each line, return a three-tuple containing: 1. The label 2. The instruction 3. Any arguments or parameters An element in the tuple may be None or '' if it did not find anything :param code: The code to parse :return: A list of tuples in the form of (label, instruction, parameters) """
|
remove_comments = re.compile(r'^([^;@\n]*);?.*$', re.MULTILINE) code = '\n'.join(remove_comments.findall(code)) # TODO can probably do this better # TODO labels with spaces between pipes is allowed `|label with space| INST OPER` parser = re.compile(r'^(\S*)?[\s]*(\S*)([^\n]*)$', re.MULTILINE) res = parser.findall(code) # Make all parsing of labels and instructions adhere to all uppercase res = [(label.upper(), instruction.upper(), parameters.strip()) for (label, instruction, parameters) in res] return res
|
def check_register(self, arg): """ Is the parameter a register in the form of 'R<d>', and if so is it within the bounds of registers defined Raises an exception if 1. The parameter is not in the form of 'R<d>' 2. <d> is outside the range of registers defined in the init value registers or _max_registers :param arg: The parameter to check :return: The number
|
of the register """ self.check_parameter(arg) match = re.search(self.REGISTER_REGEX, arg) if match is None: raise iarm.exceptions.RuleError("Parameter {} is not a register".format(arg)) try: r_num = int(match.groups()[0]) except ValueError: r_num = int(match.groups()[0], 16) except TypeError: if arg in 'lr|LR': return 14 elif arg in 'sp|SP': return 13 elif arg in 'fp|FP': return 7 ## TODO this could be 7 or 11 depending on THUMB and ARM mode
|
http://www.keil.com/support/man/docs/armcc/armcc_chr1359124947957.htm else: raise if r_num > self._max_registers: raise iarm.exceptions.RuleError( "Register {} is greater than defined registers of {}".format(arg, self._max_registers)) return r_num
|
def check_immediate(self, arg): """ Is the parameter an immediate in the form of '#<d>', Raises an exception if 1. The parameter is not in the form of '#<d>' :param arg: The parameter to check :return: The value of the immediate """ self.check_parameter(arg) match = re.search(self.IMMEDIATE_REGEX, arg) if match is None: raise iarm.exceptions.RuleError("Parameter {} is not an immediate".format(arg)) return self.convert_to_integer(match.groups()[0])
|
def check_immediate_unsigned_value(self, arg, bit): """ Is the immediate within the unsigned value of 2**bit - 1 Raises an exception if 1. The immediate value is > 2**bit - 1 :param arg: The parameter to check :param bit: The number of bits to use in 2**bit :return: The value of the immediate """ i_num = self.check_immediate(arg) if (i_num > (2 ** bit - 1)) or
|
(i_num < 0): raise iarm.exceptions.RuleError("Immediate {} is not an unsigned {} bit value".format(arg, bit)) return i_num
|
def check_immediate_value(self, arg, _max, _min=0): """ Is the immediate within the range of [_min, _max] Raises an exception if 1. The immediate value is < _min or > _max :param arg: The parameter to check :param _max: The maximum value :param _min: The minimum value, optional, default is zero :return: The immediate value """ i_num = self.check_immediate(arg) if (i_num > _max) or (i_num <
|
_min): raise iarm.exceptions.RuleError("Immediate {} is not within the range of [{}, {}]".format(arg, _min, _max)) return i_num
|
def rule_low_registers(self, arg): """Low registers are R0 - R7""" r_num = self.check_register(arg) if r_num > 7: raise iarm.exceptions.RuleError( "Register {} is not a low register".format(arg))
|
def get_parameters(self, regex_exp, parameters): """ Given a regex expression and the string with the paramers, either return a regex match object or raise an exception if the regex did not find a match :param regex_exp: :param parameters: :return: """ # TODO find a better way to do the equate replacement for rep in self.equates: parameters = parameters.replace(rep, str(self.equates[rep])) match = re.match(regex_exp, parameters) if not
|
match: raise iarm.exceptions.ParsingError("Parameters are None, did you miss a comma?") return match.groups()
|
def get_one_parameter(self, regex_exp, parameters): """ Get three parameters from a given regex expression Raise an exception if more than three were found :param regex_exp: :param parameters: :return: """ Rx, other = self.get_parameters(regex_exp, parameters) if other is not None and other.strip(): raise iarm.exceptions.ParsingError("Extra arguments found: {}".format(other)) return Rx.upper()
|
def get_two_parameters(self, regex_exp, parameters): """ Get two parameters from a given regex expression Raise an exception if more than two were found :param regex_exp: :param parameters: :return: """ Rx, Ry, other = self.get_parameters(regex_exp, parameters) if other is not None and other.strip(): raise iarm.exceptions.ParsingError("Extra arguments found: {}".format(other)) if Rx and Ry: return Rx.upper(), Ry.upper() elif not Rx: raise iarm.exceptions.ParsingError("Missing first positional argument") else: raise iarm.exceptions.ParsingError("Missing
|
second positional argument")
|
def get_three_parameters(self, regex_exp, parameters): """ Get three parameters from a given regex expression Raise an exception if more than three were found :param regex_exp: :param parameters: :return: """ Rx, Ry, Rz, other = self.get_parameters(regex_exp, parameters) if other is not None and other.strip(): raise iarm.exceptions.ParsingError("Extra arguments found: {}".format(other)) return Rx.upper(), Ry.upper(), Rz.upper()
|
def set_APSR_flag_to_value(self, flag, value): """ Set or clear flag in ASPR :param flag: The flag to set :param value: If value evaulates to true, it is set, cleared otherwise :return: """ if flag == 'N': bit = 31 elif flag == 'Z': bit = 30 elif flag == 'C': bit = 29 elif flag == 'V': bit = 28 else: raise AttributeError("Flag {} does
|
not exist in the APSR".format(flag)) if value: self.register['APSR'] |= (1 << bit) else: self.register['APSR'] -= (1 << bit) if (self.register['APSR'] & (1 << bit)) else 0
|
def rule_special_registers(self, arg): """Raises an exception if the register is not a special register""" # TODO is PSR supposed to be here? special_registers = "PSR APSR IPSR EPSR PRIMASK FAULTMASK BASEPRI CONTROL" if arg not in special_registers.split(): raise iarm.exceptions.RuleError("{} is not a special register; Must be [{}]".format(arg, special_registers))
|
def set_C_flag(self, oper_1, oper_2, result, type): """ Set C flag C flag is set if the unsigned number overflows This condition is obtained if: 1. In addition, the result is smaller than either of the operands 2. In subtraction, if the second operand is larger than the first This should not be used for shifting as each shift will need to set the C
|
flag differently """ # TODO is this correct? if type == 'add': if result < oper_1: self.set_APSR_flag_to_value('C', 1) else: self.set_APSR_flag_to_value('C', 0) elif type == 'sub': if oper_1 < oper_2: # If there was a borrow, then set to zero self.set_APSR_flag_to_value('C', 0) else: self.set_APSR_flag_to_value('C', 1) elif type == 'shift-left': if (oper_2 > 0) and (oper_2 < (self._bit_width - 1)): self.set_APSR_flag_to_value('C', oper_1 & (1 << (self._bit_width
|
- oper_2))) else: self.set_APSR_flag_to_value('C', 0) else: raise iarm.exceptions.BrainFart("_type is not 'add' or 'sub'")
|
def version(): """Output version of gcdt tools and plugins.""" log.info('gcdt version %s' % __version__) tools = get_plugin_versions('gcdttool10') if tools: log.info('gcdt tools:') for p, v in tools.items(): log.info(' * %s version %s' % (p, v)) log.info('gcdt plugins:') for p, v in get_plugin_versions().items(): log.info(' * %s version %s' % (p, v)) generators = get_plugin_versions('gcdtgen10') if generators: log.info('gcdt scaffolding generators:') for p, v in generators.items(): log.info(' *
|
%s version %s' % (p, v))
|
def retries(max_tries, delay=1, backoff=2, exceptions=(Exception,), hook=None): """Function decorator implementing retrying logic. delay: Sleep this many seconds * backoff * try number after failure backoff: Multiply delay by this factor after each failure exceptions: A tuple of exception classes; default (Exception,) hook: A function with the signature: (tries_remaining, exception, mydelay) """ """ def example_hook(tries_remaining, exception, delay): '''Example exception handler; prints a warning to stderr. tries_remaining:
|
The number of tries remaining. exception: The exception instance which was raised. ''' print >> sys.stderr, "Caught '%s', %d tries remaining, sleeping for %s seconds" % (exception, tries_remaining, delay) The decorator will call the function up to max_tries times if it raises an exception. By default it catches instances of the Exception class and subclasses. This will recover after all but the most fatal
|
errors. You may specify a custom tuple of exception classes with the 'exceptions' argument; the function will only be retried if it raises one of the specified exceptions. Additionally you may specify a hook function which will be called prior to retrying with the number of remaining tries and the exception instance; see given example. This is primarily intended to give the opportunity to
|
log the failure. Hook is not called after failure if no retries remain. """ def dec(func): def f2(*args, **kwargs): mydelay = delay #tries = range(max_tries) #tries.reverse() tries = range(max_tries-1, -1, -1) for tries_remaining in tries: try: return func(*args, **kwargs) except GracefulExit: raise except exceptions as e: if tries_remaining > 0: if hook is not None: hook(tries_remaining, e, mydelay) sleep(mydelay) mydelay *= backoff else: raise
|
return f2 return dec
|
def get_env(): """ Read environment from ENV and mangle it to a (lower case) representation Note: gcdt.utils get_env() is used in many cloudformation.py templates :return: Environment as lower case string (or None if not matched) """ env = os.getenv('ENV', os.getenv('env', None)) if env: env = env.lower() return env
|
def get_context(awsclient, env, tool, command, arguments=None): """This assembles the tool context. Private members are preceded by a '_'. :param tool: :param command: :return: dictionary containing the gcdt tool context """ # TODO: elapsed, artifact(stack, depl-grp, lambda, api) if arguments is None: arguments = {} context = { '_awsclient': awsclient, 'env': env, 'tool': tool, 'command': command, '_arguments': arguments, # TODO clean up arguments -> args
|
'version': __version__, 'user': _get_user(), 'plugins': get_plugin_versions().keys() } return context
|
def get_command(arguments): """Extract the first argument from arguments parsed by docopt. :param arguments parsed by docopt: :return: command """ return [k for k, v in arguments.items() if not k.startswith('-') and v is True][0]
|
def check_gcdt_update(): """Check whether a newer gcdt is available and output a warning. """ try: inst_version, latest_version = get_package_versions('gcdt') if inst_version < latest_version: log.warn('Please consider an update to gcdt version: %s' % latest_version) except GracefulExit: raise except Exception: log.warn('PyPi appears to be down - we currently can\'t check for newer gcdt versions')
|
def dict_selective_merge(a, b, selection, path=None): """Conditionally merges b into a if b's keys are contained in selection :param a: :param b: :param selection: limit merge to these top-level keys :param path: :return: """ if path is None: path = [] for key in b: if key in selection: if key in a: if isinstance(a[key], dict) and isinstance(b[key], dict): dict_selective_merge(a[key], b[key], b[key].keys(), path + [str(key)])
|
elif a[key] != b[key]: # update the value a[key] = b[key] else: a[key] = b[key] return a
|
def dict_merge(a, b, path=None): """merges b into a""" return dict_selective_merge(a, b, b.keys(), path)
|
def are_credentials_still_valid(awsclient): """Check whether the credentials have expired. :param awsclient: :return: exit_code """ client = awsclient.get_client('lambda') try: client.list_functions() except GracefulExit: raise except Exception as e: log.debug(e) log.error(e) return 1 return 0
|
def flatten(lis): """Given a list, possibly nested to any level, return it flattened.""" new_lis = [] for item in lis: if isinstance(item, collections.Sequence) and not isinstance(item, basestring): new_lis.extend(flatten(item)) else: new_lis.append(item) return new_lis
|
def signal_handler(signum, frame): """ handle signals. example: 'signal.signal(signal.SIGTERM, signal_handler)' """ # signals are CONSTANTS so there is no mapping from signum to description # so please add to the mapping in case you use more signals! description = '%d' % signum if signum == 2: description = 'SIGINT' elif signum == 15: description = 'SIGTERM' raise GracefulExit(description)
|
def json2table(json): """This does format a dictionary into a table. Note this expects a dictionary (not a json string!) :param json: :return: """ filter_terms = ['ResponseMetadata'] table = [] try: for k in filter(lambda k: k not in filter_terms, json.keys()): table.append([k.encode('ascii', 'ignore'), str(json[k]).encode('ascii', 'ignore')]) return tabulate(table, tablefmt='fancy_grid') except GracefulExit: raise except Exception as e: log.error(e) return json
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.