def insertBetweenMarkers(old, new): """Inserts text between markers (= special meaning lines). 'old' must contain firsts and last line of 'new' -- the text between them is replaced with 'new'.""" begin = new[0] end = new[-1] if begin not in old: raise errors.Error('marker %s not present in the file' % repr(begin)) if end not in old: raise errors.Error('marker %s not present in the file' % repr(end)) out = [] i = 0 while i < len(old) and old[i] != begin: out.append(old[i]) i += 1 while i < len(old) and old[i] != end: i += 1 i += 1 out += new while i < len(old): out.append(old[i]) i += 1 return out
def finish(self): try: try: # NB: "finally" section below is still called after these # return statements if self.pretend: return 0 if self.process == None: return 0 if self.process.returncode == 0: dependencies.load(self.tempDeps) dependencies.addCmdLine( os.path.abspath(self.filename), self.format, files[self.filename].flags[self.format]) return _countLines(self.tempChanges) else: # failed, returncode != 0 if keepGoing: sys.stderr.write( '[bakefile_gen] bakefile exited with error (%i), ignoring\n' % self.process.returncode) return 0 # no modified files else: raise errors.Error( 'bakefile exited with error (%i)' % self.process.returncode) except IOError, e: raise errors.Error('failed to run bakefile: %s' % e) finally: os.remove(self.tempDeps) os.remove(self.tempChanges)
def ref(var, target=None, context=None): if __refEval: if context != None: context = __refContexts[context] if target != None and target not in mk.targets: raise errors.Error("target '%s' cannot be used in ref() since it doesn't exist" % target, context=context) try: if target == None or var not in mk.targets[target].vars: return mk.vars[var] else: return mk.targets[target].vars[var] except KeyError: if target == None: raise errors.Error("undefined variable '%s'" % var, context=context) else: raise errors.Error("undefined variable '%s' on target '%s'" % (var, target), context=context) else: if mk.__trackUsage: mk.__usageTracker.refs += 1 if context == None: context = len(__refContexts) __refContexts[context] = errors.getCtx() if target == None: return "$(ref('%s',None,%i))" % (var, context) else: return "$(ref('%s','%s',%i))" % (var, target, context)
def makeCondition(cond_str): # FIXME: this is broken if cond_str.find(' or ') != -1: raise errors.Error( "'%s': only 'and' operator allowed when creating a conditional variable" % cond_str) cond_list = __splitConjunction(cond_str) condexpr_list = [] for cond in cond_list: if evalCondition(cond) == '1': continue # FIXME: this is broken pos = cond.find('==') if pos == -1: return None name = cond[:pos] value = cond[pos+2:] if value[0] == value[-1] and value[0] in ['"',"'"]: # strip quotes from value value = value[1:-1] else: try: value = str(int(value)) except ValueError: return None if name in options: condexpr_list.append(Condition.Expr(options[name], value)) elif name in cond_vars: cvar = cond_vars[name] convlist = removeCondVarDependencyFromCondition(cvar, value) condexpr_list = condexpr_list + convlist elif name not in vars: raise errors.Error("undefined variable '%s' in condition" % name) else: raise errors.Error("conditional variables can only depend on options or other conditional variables and '%s' is not one" % name) # optimization: simplify expressions removing redundant terms (A and A => A) optimized_list = [] for a in condexpr_list: # add to the optimized list only non-redundant tokens if a not in optimized_list: optimized_list.append(a) condexpr_list = optimized_list def safeValue(s): return str(s).replace('.','_').replace('/','').replace('\\','') condexpr_list.sort() cname = '_'.join(['%s_%s' % (e.option.name, safeValue(e.value)) \ for e in condexpr_list]) if cname in conditions: assert conditions[cname].exprs == condexpr_list return conditions[cname] else: c = Condition(cname, condexpr_list) addCondition(c) return c
def scripts(request): upload_new_script_action = 'upload_new_script' new_script_field_name = 'new_script' delete_script_action_prefix = 'delete_script_' if request.method == 'POST': try: # add script stanza if upload_new_script_action in request.POST: if new_script_field_name in request.FILES: source_file = request.FILES[new_script_field_name] if source_file.name.lower().endswith('.r'): source_file_noext, _ = os.path.splitext( source_file.name) scriptlib.add(request.service, source_file_noext, source_file.read()) else: raise errors.Error( 'Wrong file extension. It has to be \'r\'.') else: raise errors.Error('File missing') # delete script stanza else: for key in request.POST: if key.startswith(delete_script_action_prefix): file_name = key[len(delete_script_action_prefix):] source_file_noext, _ = os.path.splitext(file_name) scriptlib.remove(request.service, source_file_noext) except errors.Error as e: return HttpResponseRedirect('./?add_error=%s' % str(e)) except Exception as e: return HttpResponseRedirect('./?add_fatal_error=%s' % str(e)) return HttpResponseRedirect('./') # scan for R script stanzas r_scripts = [] for stanza, name in scriptlib.iter_stanzas(request.service): r_scripts.append({ 'file_name': name + '.r', 'name': name, 'is_removable': stanza.access['removable'] == '1', 'owner': stanza.access['owner'], }) return { 'scripts': r_scripts, 'app_label': request.service.apps[app_id].label, 'request': request, 'can_upload': scriptlib.can_upload(request.service), 'new_script_field_name': new_script_field_name, 'upload_new_script_action': upload_new_script_action, 'delete_script_action_prefix': delete_script_action_prefix, 'add_error': request.GET.get('add_error', ''), 'add_fatal_error': request.GET.get('add_fatal_error', ''), }
def _first_rpc_result(self, result): """result is returned from make_rpc. Get the first data record or throw an exception if it was an error.""" if type(result) == list: result = result[0] error = result.get('error') if error: raise errors.Error('RPC Error' + str(error['code']) + ': ' + error['message']) data = result.get('data') if data: return data raise errors.Error('RPC Error: No data record.')
def invoke(writer, file, method): if writer.endswith('.empy'): return invoke_em(writer, file, method) elif writer.endswith('.py'): return invoke_py(writer, file, method) else: raise errors.Error("unknown type of writer: '%s'" % writer)
def init_handles(self, config): try: self.init_cameraXY(config) self.init_cameraTilt(config) self.init_canUSB() except (errors.CANError, errors.CameraError): raise errors.Error("Handles initialization failed")
def get_waveservice(self): """Return the currently installed waveservice if available. Throws an exception if no service is installed.""" if self._waveservice is None: raise errors.Error('Oauth has not been setup') return self._waveservice
def make_or_verify_dir(directory, mode=0o755, uid=0, strict=False): """Make sure directory exists with proper permissions. :param str directory: Path to a directory. :param int mode: Directory mode. :param int uid: Directory owner. :param bool strict: require directory to be owned by current user :raises .errors.Error: if a directory already exists, but has wrong permissions or owner :raises OSError: if invalid or inaccessible file names and paths, or other arguments that have the correct type, but are not accepted by the operating system. """ try: os.makedirs(directory, mode) except OSError as exception: if exception.errno == errno.EEXIST: if strict and not check_permissions(directory, mode, uid): raise errors.Error( "%s exists, but it should be owned by user %d with" "permissions %s" % (directory, uid, oct(mode))) else: raise
def _getValue(x): try: return dict[x] except KeyError: raise errors.Error( "value '%s' not allowed in this context: not one of %s" % (x, dict.keys()))
def to_python(cls, value): value = value.lower() if value == 't': return True elif value == 'f': return False raise errors.Error('Cannot convert "%s" to bool' % value)
def create_test(self, test_name=None, test_server=None): """Create a single test with defaults taken from settings.ini and from passed values. Args: test_name: The name of the test as displayed in the ThousandEyes web interface. test_server: The IP address that will be tested in ThousandEyes. Returns: A boolean of True if the creation was successful or False if an error occurred. """ if test_name is None or test_server is None: raise errors.Error("[%s.%s] - You must provide a test name and server." % (__name__, self.__class__.__name__)) else: test = thousandeyes.NetworkTest() test.name = self.settings.te_test_prefix + " " + test_name test.server = test_server test.alerts_enabled = bool(self.settings.te_test_alerts) test.bandwidth_measurements = False test.mtu_measurements = True test.network_measurements = True test.bgp_measurements = True test.protocol = self.settings.te_test_protocol test.port = int(self.settings.te_test_port) test.interval = int(self.settings.te_test_interval) for agent_id in self.get_agent_ids(): test.agents.append({"agentId": agent_id}) if self.te_api.create_network_test(test): return True else: return False
def _extractDictForTag(e, target, dict): # $(value) expands to the thing passed as tag's text value: try: dict2 = {'value': mk.evalExpr(e.value, target=target, add_dict=dict)} except Exception, err: raise errors.Error("incorrect argument value '%s': %s" % (e.value, err))
def __init__(self, api_request=None): if api_request is None: raise errors.Error( "[%s.%s] - You must provide a ThousandEyes APIRequest instance." % (__name__, self.__class__.__name__)) else: self.api_request = api_request
def from_json(self, json_agent): try: if "agentId" in json_agent: self.id = int(json_agent["agentId"]) if "agentName" in json_agent: self.name = json_agent["agentName"] if "agentType" in json_agent: self.type = json_agent["agentType"] if "countryId" in json_agent: self.county_id = json_agent["countryId"] if "location" in json_agent: self.location = json_agent["location"] if "prefix" in json_agent: self.prefix = bool(json_agent["prefix"]) if "enabled" in json_agent: self.enabled = json_agent["enabled"] if "network" in json_agent: self.network = json_agent["network"] if "lastSeen" in json_agent: self.last_seen = datetime.strptime(json_agent["lastSeen"], "%Y-%m-%d %H:%M:%S") if "agentState" in json_agent: self.state = json_agent["agentState"] if "utilization" in json_agent: self.utilisation = int(json_agent["utilization"]) if "verifySslCertificates" in json_agent: self.verify_ssl_certs = bool( json_agent["verifySslCertificates"]) if "keepBrowserCache" in json_agent: self.keep_browser_cache = bool(json_agent["keepBrowserCache"]) if "ipAddresses" in json_agent: for ip in json_agent["ipAddresses"]: self.ip_addresses.append(ip) if "publicIpAddresses" in json_agent: for ip in json_agent["publicIpAddresses"]: self.public_ip_addresses.append(ip) except KeyError as e: raise errors.Error( "[%s.%s] - Key not found while parsing json. Detail: %s" % (__name__, self.__class__.__name__, e))
def from_json(self, json_test): try: if "testId" in json_test: self.id = int(json_test["testId"]) if "testName" in json_test: self.name = json_test["testName"] if "enabled" in json_test: self.enabled = bool(json_test["enabled"]) if "alertsEnabled" in json_test: self.alerts_enabled = bool(json_test["alertsEnabled"]) if "protocol" in json_test: self.protocol = json_test["protocol"] if "port" in json_test: self.port = int(json_test["port"]) if "savedEvent" in json_test: self.saved_event = int(json_test["savedEvent"]) if "server" in json_test: self.server = json_test["server"].split(":")[0] if "url" in json_test: self.url = json_test["url"] if "bandwidthMeasurements" in json_test: self.bandwidth_measurements = bool( json_test["bandwidthMeasurements"]) if "mtuMeasurements" in json_test: self.mtu_measurements = bool(json_test["mtuMeasurements"]) if "networkMeasurements" in json_test: self.network_measurements = bool( json_test["networkMeasurements"]) if "bgpMeasurements" in json_test: self.bgp_measurements = bool(json_test["bgpMeasurements"]) if "interval" in json_test: self.interval = int(json_test["interval"]) if "liveShare" in json_test: self.live_share = int(json_test["liveShare"]) if "modifiedDate" in json_test: self.modified_date = datetime.strptime( json_test["modifiedDate"], "%Y-%m-%d %H:%M:%S") if "modifiedBy" in json_test: self.modified_by = json_test["modifiedBy"] if "createdDate" in json_test: self.created_date = datetime.strptime(json_test["createdDate"], "%Y-%m-%d %H:%M:%S") if "createdBy" in json_test: self.created_by = json_test["createdBy"] except KeyError as e: raise errors.Error( "[%s.%s] - Key not found while parsing json. Detail: %s" % (__name__, self.__class__.__name__, e)) except ValueError as e: raise errors.Error( "[%s.%s] - An error occurred while converting between json and NetworkTest types. " "Detail: %s" % (__name__, self.__class__.__name__, e))
def formatIfNotEmpty(fmt, value): """Return fmt % value (prefix: e.g. "%s"), unless value is empty string (in which case it returns empty string). Can handle following forms of 'value': - empty string - anything beginning with literal - anything with a literal in it - $(cv) where cv is conditional variable """ if fmt == '': return '' value = value.strip() if value == '' or value.isspace(): return '' if __certainlyNotEmpty(value): return fmt % value if value.startswith('$(') and value[-1] == ')': # FIXME: this is too limited, it should be done inside __doEvalExpr # callbacks instead. condname = value[2:-1] if condname in mk.options: if mk.options[condname].isNeverEmpty(): return fmt % value else: raise errors.Error("formatIfNotEmpty failed: option '%s' may be empty" % condname) if condname in mk.cond_vars: cond = mk.cond_vars[condname] var = mk.CondVar(makeUniqueCondVarName('%s_p' % cond.name)) mk.addCondVar(var) for v in cond.values: var.add(v.cond, formatIfNotEmpty(fmt, v.value)) return '$(%s)' % var.name if condname in mk.make_vars: form = formatIfNotEmpty(fmt, mk.make_vars[condname]) if form == '': return '' return fmt % value if condname in mk.vars: form = formatIfNotEmpty(fmt, mk.vars[condname]) if form == '': return '' return fmt % value raise errors.Error("formatIfNotEmpty failed: '%s' too complicated" % value)
def __getitem__(self, item): """returns a BlipRefs for the given slice.""" if isinstance(item, slice): if item.step: raise errors.Error('Step not supported for blip slices') return self.range(item.start, item.stop) else: return self.at(item)
def __init__(self, address=None, status=None, map_api=None): if address is None or status is None or map_api is None: raise errors.Error( "[%s.%s] - You must provide an address, a status and a map api instance." % (__name__, self.__class__.__name__)) else: self.address = address self.status = status self.map_api = map_api
def evalDefault(self): if self.default == None: return try: self.default = evalExpr(self.default, use_options=0) except NameError, err: raise errors.Error("can't use options or conditional variables in default value of option '%s' (%s)" % (self.name, err), context=self.context)
def __init__(self, te_api=None, sw_api=None, settings=None, logger=None): if te_api is None or sw_api is None or settings is None or logger is None: raise errors.Error("[%s.%s] - You must provide instances of a Logger, SolarEyesSettings," "ThousandEyes & SolarWinds APIs." % (__name__, self.__class__.__name__)) else: self.te_api = te_api self.sw_api = sw_api self.settings = settings self.logger = logger
def __init__(self, latlng=None, title=None, image_path=None): if latlng is None or title is None or image_path is None: raise errors.Error( "[%s.%s] - You must provide a marker latlng, a title and an image path." % (__name__, self.__class__.__name__)) else: self.latlng = latlng self.title = title self.image_path = image_path
def _split_format(format): """Splits format specification into (base) format and variant; e.g. "watcom(os2)" is separated into "watcom" (base format) and "os2" (variant).""" pos = format.find("(") if pos == -1: return (format, None) if format[-1] != ")": raise errors.Error("invalid format specification: '%s'" % format) return (format[:pos], format[pos + 1:-1])
def __init__(self, api_url, auth_email, auth_token): if not api_url or not auth_email or not auth_token: raise errors.Error( "[%s.%s] - You must provide a ThousandEyes API url, email and auth token." % (__name__, self.__class__.__name__)) else: self.api_url = api_url self.auth_email = auth_email self.auth_token = auth_token
def txn_async(): if (yield b.key.get_async()): # pragma: no cover raise errors.Error('build number collision') futs = [b.put_async()] if sync_task: futs.append( tq.enqueue_async(swarming.SYNC_QUEUE_NAME, [sync_task])) yield futs
def test_error(self): """Function: test_error Description: Test with no arguments. Arguments: """ self.assertTrue(errors.Error())
def loadFormats(): """Find all format specification in search paths.""" for path in config.searchPath: manifest = os.path.join(path, 'FORMATS.bkmanifest') if os.path.isfile(manifest): try: loadManifestFile(manifest) except xmlparser.ParsingError: raise errors.Error("malformed format manifest file %s" % manifest)
def __init__(self, centre_address=None, zoom=None): if centre_address is None or zoom is None: raise errors.Error( "[%s.%s] - You must provide an address string to centre the map around, " "and a zoom level for this location." % (__name__, self.__class__.__name__)) else: self.centre_latlng = self.geocode(centre_address) self.zoom = zoom self._markers = []
def getlocationurl(): try: configLoader = config_loader.ConfigurationLoader() configLoader.set_configsection('Location_API') location_api = configLoader.getconfig() location_url = location_api['location.url'] print(location_url) return location_url except TypeError as er: errors.Error("Exception while getting location url: ", er)