def test_Uri(self): self.assertEqual( StringSerialization.SerializeItem( UriTypeInfo(), Uri.FromString("https://foo.bar.baz")), "https://foo.bar.baz") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( UriTypeInfo(), "this is not a valid uri"))
def test_Date(self): self.assertEqual( StringSerialization.SerializeItem( DateTypeInfo(), datetime.date(year=2018, month=4, day=28)), "2018-04-28") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( DateTypeInfo(), "this is not a valid date"))
def test_Time(self): self.assertEqual( StringSerialization.SerializeItem( TimeTypeInfo(), datetime.time(hour=15, minute=15, second=12)), "15:15:12") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( TimeTypeInfo(), "this is not a valid time"))
def test_Filename(self): self.assertEqual( StringSerialization.SerializeItem( FilenameTypeInfo(ensure_exists=False), os.path.join("foo", "bar")), "foo/bar") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( FilenameTypeInfo(), os.path.join("foo", "bar")))
def test_Guid(self): self.assertEqual( StringSerialization.SerializeItem( GuidTypeInfo(), uuid.UUID("363D3427-1871-40C5-83DF-3C9D5CE7B60D")), "363D3427-1871-40C5-83DF-3C9D5CE7B60D".lower()) self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( GuidTypeInfo(), "this is not a valid guid"))
def test_Bool(self): self.assertEqual( StringSerialization.SerializeItem(BoolTypeInfo(), True), "True") self.assertEqual( StringSerialization.SerializeItem(BoolTypeInfo(), False), "False") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( BoolTypeInfo(), "this is not a bool"))
def test_Date(self): self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "2018-04-28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04-28-2018"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "18-04-28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04-28-18"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "2018.04.28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04.28.2018"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "18.04.28"), datetime.date(year=2018, month=4, day=28)) self.assertEqual( StringSerialization.DeserializeItem(DateTypeInfo(), "04.28.18"), datetime.date(year=2018, month=4, day=28)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DateTimeTypeInfo(), "2018-04.28"))
def test_Filename(self): self.assertEqual( StringSerialization.DeserializeItem( FilenameTypeInfo(ensure_exists=False), "foo/bar"), os.path.realpath(os.path.normpath(os.path.join("foo", "bar")))) self.assertEqual( StringSerialization.DeserializeItem( FilenameTypeInfo(ensure_exists=False), "foo/bar", normalize=False), os.path.join("foo", "bar")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DirectoryTypeInfo(), ''))
def test_Int(self): self.assertEqual(StringSerialization.SerializeItem(IntTypeInfo(), 20), "20") self.assertEqual(StringSerialization.SerializeItem(IntTypeInfo(), -20), "-20") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( IntTypeInfo(), "this is not a valid int")) self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem(IntTypeInfo(min=0), -10)) self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem(IntTypeInfo(max=10), 20))
def test_Int(self): self.assertEqual( StringSerialization.DeserializeItem(IntTypeInfo(), "10"), 10) self.assertEqual( StringSerialization.DeserializeItem(IntTypeInfo(), "-10"), -10) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( IntTypeInfo(), "not a valid int")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( IntTypeInfo(min=0), "-10")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( IntTypeInfo(max=10), "100"))
def test_Float(self): self.assertEqual( StringSerialization.SerializeItem(FloatTypeInfo(), -10.5), "-10.5") self.assertEqual( StringSerialization.SerializeItem(FloatTypeInfo(), 10), "10") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( FloatTypeInfo(), "this is not a valid float")) self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( FloatTypeInfo(min=0), -10)) self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( FloatTypeInfo(max=10), 20))
def test_String(self): self.assertEqual( StringSerialization.DeserializeItem(StringTypeInfo(), "foo"), "foo") self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem(StringTypeInfo(), "")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( StringTypeInfo(min_length=2), "1")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( StringTypeInfo(max_length=2), "123")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( StringTypeInfo(validation_expression="test"), "not test"))
def test_DateTime(self): self.assertEqual( StringSerialization.SerializeItem( DateTimeTypeInfo(), datetime.datetime(year=2018, month=4, day=28, hour=14, minute=46, second=12)), "2018-04-28 14:46:12") self.assertEqual( StringSerialization.SerializeItem(DateTimeTypeInfo(), datetime.datetime(year=2018, month=4, day=28, hour=14, minute=46, second=12), sep='T'), "2018-04-28T14:46:12") self.assertEqual( StringSerialization.SerializeItem( DateTimeTypeInfo(), datetime.datetime(year=2018, month=4, day=28, hour=14, minute=46, second=12, microsecond=3939)), "2018-04-28 14:46:12.003939") self.assertEqual( StringSerialization.SerializeItem( DateTimeTypeInfo(), datetime.datetime(year=2018, month=4, day=28, hour=14, minute=46, second=12, microsecond=3939), microseconds=False), "2018-04-28 14:46:12") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( DateTimeTypeInfo(), "this is not a datetime"))
def _CreateContext(cls, metadata, status_stream): # Ensure that all plugin settings are present and that they are the # expected type. custom_settings = OrderedDict([ (k, v) for k, v in plugin.GenerateCustomMetadataSettingsAndDefaults() ]) assert "plugin_settings" in metadata plugin_settings = metadata["plugin_settings"] for k, v in six.iteritems(plugin_settings): if k not in custom_settings: raise Exception( "'{}' is not a valid plugin setting".format(k)) desired_type = type(custom_settings[k]) if type( v ) != desired_type: # <prefer isinstance> pylint: disable = C0123 assert isinstance(v, six.string_types), (v, type(v)) plugin_settings[k] = StringSerialization.DeserializeItem( CreateFromPythonType(desired_type), v) for k, v in six.iteritems(custom_settings): if k not in plugin_settings: plugin_settings[k] = v # Ensure that the required metadata is present for required in plugin.GetRequiredMetadataNames(): if required not in plugin_settings: raise Exception( "'{}' is required but was not found".format(required)) metadata["plugin_settings"] = plugin.PreprocessMetadata( plugin_settings) context = plugin.PreprocessContext(metadata) # Create data based on the input files context["plugin_context"] = ExtractContent( context["inputs"], status_stream, ) context["output_filenames"] = [ os.path.join(context["output_dir"], filename) for filename in plugin.GenerateOutputFilenames(context) ] context = plugin.PostprocessContext(context) del context["include_regexes"] del context["exclude_regexes"] return super(CodeGenerator, cls)._CreateContext(context, status_stream)
def test_Time(self): self.assertEqual( StringSerialization.DeserializeItem(TimeTypeInfo(), "10:15:01"), datetime.time(hour=10, minute=15, second=1)) self.assertEqual( StringSerialization.DeserializeItem(TimeTypeInfo(), "10:15:01.678"), datetime.time(hour=10, minute=15, second=1, microsecond=678000)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( TimeTypeInfo(), "not a valid time")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( TimeTypeInfo(), "10:15:01+10:30")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( TimeTypeInfo(), "10:15:01-03:13"))
def test_Enum(self): self.assertEqual( StringSerialization.DeserializeItem(EnumTypeInfo([ "one", "two", ]), "one"), "one") self.assertEqual( StringSerialization.DeserializeItem(EnumTypeInfo([ "one", "two", ]), "two"), "two") self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( EnumTypeInfo([ "one", "two", ]), "three"))
def _filter_Item(cls, item, always_include_optional, process_additional_data): attributes = OrderedDict() # continue_processing try: cls._ApplyOptionalAttribute( item, "continue_processing", attributes, cls.filter_continue_processing, always_include_optional, default_value_func=lambda: StringSerialization.DeserializeItem( _filter_continue_processing_TypeInfo, "true")) except: _DecorateActiveException("continue_processing") result = _CreatePythonObject(attributes=attributes, ) # includes try: cls._ApplyOptionalChildren(item, "includes", result, cls.filter_includes, always_include_optional) except: _DecorateActiveException("includes") # excludes try: cls._ApplyOptionalChildren(item, "excludes", result, cls.filter_excludes, always_include_optional) except: _DecorateActiveException("excludes") # Additional data if process_additional_data: cls._ApplyAdditionalData( item, result, exclude_names={"includes", "excludes", "continue_processing"}, ) else: cls._RejectAdditionalData( item, exclude_names=["includes", "excludes", "continue_processing"], ) _filter_TypeInfo.ValidateItem( result, recurse=False, require_exact_match=not process_additional_data, ) return result
def _ValidateMetadata(filter_unsupported_metadata, item): for item in item.Enumerate(): # Ensure that required values are present for md in item.metadata.RequiredItems: if md.Name not in item.metadata.Values: raise Exceptions.ValidateMissingAttributeException( item.Source, item.Line, item.Column, name=md.Name, ) # Verify / eliminate / Convert extra metadata md_lookup = { md.Name: md for md in itertools.chain(item.metadata.RequiredItems, item.metadata.OptionalItems) } md_keys = list(six.iterkeys(item.metadata.Values)) for k in md_keys: if k not in md_lookup: if filter_unsupported_metadata: del item.metadata.Values[k] continue raise Exceptions.ValidateExtraneousAttributeException( item.Source, item.Line, item.Column, name=k, ) md = md_lookup[k] value = item.metadata.Values[k].Value try: if isinstance(value, six.string_types): value = StringSerialization.DeserializeItem( md.TypeInfo, value) else: md.TypeInfo.Validate(value) except ValidationException as ex: raise Exceptions.ValidateInvalidAttributeException( item.Source, item.Line, item.Column, name=k, reason=str(ex), ) item.metadata.Values[k] = item.metadata.Values[k]._replace( Value=value, )
def test_Guid(self): self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "{9A3A5A9A-0755-4FAC-9019-176C609665C5}"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "9A3A5A9A-0755-4FAC-9019-176C609665C5"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "{9A3A5A9A07554FAC9019176C609665C5}"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertEqual( StringSerialization.DeserializeItem( GuidTypeInfo(), "9A3A5A9A07554FAC9019176C609665C5"), uuid.UUID("9A3A5A9A-0755-4FAC-9019-176C609665C5")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( GuidTypeInfo(), "not a valid guid"))
def DateAndBranch(date, branch, operator): assert date if branch: # ---------------------------------------------------------------------- def BranchGenerator(): yield branch # ---------------------------------------------------------------------- else: # ---------------------------------------------------------------------- def BranchGenerator(): for branch in [ cls.GetCurrentBranch(repo_root), cls.DefaultBranch, ]: yield branch # ---------------------------------------------------------------------- assert BranchGenerator if not operator: operator = '<' else: operator = '>' errors = OrderedDict() for branch in BranchGenerator(): command_line = '''hg log --branch "{branch}" --rev "sort(date('{operator}{date}'), -date)" --limit 1 --template "{{rev}}"''' \ .format( branch=branch, operator=operator, date=StringSerialization.SerializeItem(DateTimeTypeInfo(), date, microseconds=False), ) result, output = cls.Execute(repo_root, command_line, strip=True) if result == 0 and output: return output errors[command_line] = output raise Exception( "Change not found ({branch}, {date})\n{errors}".format( branch=branch, date=date, errors='\n\n'.join([ "{}\n{}".format(k, v) for k, v in six.iteritems(errors) ]), ))
def _CreateContext(cls, metadata, status_stream): if metadata["plugin_name"] not in plugin_map: raise CommandLine.UsageException( "'{}' is not a valid plugin".format( metadata["plugin_name"])) plugin = plugin_map[metadata["plugin_name"]].Plugin # Ensure that all plugin settings are present and that they # are the expected type. custom_settings = OrderedDict([ (k, v) for k, v in plugin.GenerateCustomSettingsAndDefaults() ]) plugin_settings = metadata["plugin_settings"] for k, v in six.iteritems(plugin_settings): if k not in custom_settings: raise CommandLine.UsageException( "'{}' is not a valid plugin setting".format(k)) desired_type = type(custom_settings[k]) if type(v) != desired_type: assert isinstance(v, (str, UnicodeDecodeError)), (v, type(v)) plugin_settings[k] = StringSerialization.DeserializeItem( CreateFromPythonType(desired_type), v) for k, v in six.iteritems(custom_settings): if k not in plugin_settings: plugin_settings[k] = v metadata["plugin_settings"] = plugin.PreprocessMetadata( plugin_settings) # Invoke custom functionality context = create_context_func(metadata, plugin) context = plugin.PreprocessContext(context) context["output_filenames"] = [ os.path.join(context["output_dir"], filename) for filename in plugin.GenerateOutputFilenames(context) ] context = plugin.PostprocessContext(context) if postprocess_context_func: context = postprocess_context_func(context, plugin) return super(CodeGenerator, cls)._CreateContext(context, status_stream)
def test_Duration(self): self.assertEqual( StringSerialization.SerializeItem(DurationTypeInfo(), datetime.timedelta(hours=2)), "2:00:00") self.assertEqual( StringSerialization.SerializeItem( DurationTypeInfo(), datetime.timedelta(days=1, hours=2)), "1.02:00:00") self.assertEqual( StringSerialization.SerializeItem(DurationTypeInfo(), datetime.timedelta(days=1, hours=2), sep=':'), "1:02:00:00") self.assertEqual( StringSerialization.SerializeItem( DurationTypeInfo(), datetime.timedelta(days=1, hours=2, microseconds=3456)), "1.02:00:00.003456") self.assertRaises( ValidationException, lambda: StringSerialization.SerializeItem( DurationTypeInfo(), "this is not a valid duration"))
def IsList(): # This item is a list if it meets the following conditions: # # 1) An arity was explicitly provided # 2) The arity is a collection # 3) The referenced item (or one of its descendants) has an arity # 4) The referenced item's arity (or one of its descendants) is a collection. # 5) 'refines_arity' is not provided or set to False # 1 and 2 if item.arity is None or not item.arity.IsCollection: return False # 3 and 4 ref = reference while ref.element_type == Elements.ReferenceElement and ref.arity is None: assert len(ref.references) == 1, ref.references ref = ref.references[0] if ref.arity is None or not ref.arity.IsCollection: return False # 5 if Attributes.COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME in item.metadata.Values: refines_arity = item.metadata.Values[ Attributes. COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME].Value del item.metadata.Values[ Attributes.COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME] try: refines_arity = StringSerialization.DeserializeItem( BoolTypeInfo(), refines_arity) except ValidationException as ex: raise Exceptions.InvalidAttributeException( item.Source, item.Line, item.Column, desc="'{}' is not valid: {}".fromat( Attributes. COLLECTION_REFINES_ARITY_ATTRIBUTE_NAME, str(ex)), ) if refines_arity: return False return True
def _Collectionize(element, schema): arity = element.TypeInfo.Arity if arity.Max == 1: if arity.Min == 0 and hasattr(element, "default"): schema["default"] = StringSerialization.DeserializeItem( element.TypeInfo, element.default) return schema schema = {"type": "array", "items": schema} if arity.Min != 0: schema["minItems"] = arity.Min if arity.Max is not None: schema["maxItems"] = arity.Max return schema
def test_DateTime(self): self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "2018-04-28 10:05:00"), datetime.datetime(year=2018, month=4, day=28, hour=10, minute=5, second=0)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "2018-04-28T10:05:00"), datetime.datetime(year=2018, month=4, day=28, hour=10, minute=5, second=0)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "1528171793"), datetime.datetime(2018, 6, 5, 4, 9, 53)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "@1528171793 -0700"), datetime.datetime(2018, 6, 5, 11, 9, 53)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "@1528171793 +0700"), datetime.datetime(2018, 6, 4, 21, 9, 53)) self.assertEqual( StringSerialization.DeserializeItem(DateTimeTypeInfo(), "@1528171793 0700"), datetime.datetime(2018, 6, 4, 21, 9, 53)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DateTimeTypeInfo(), "not a valid datetime"))
def test_Duration(self): self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1.02:03:04"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1:02:03:04"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "02:03:04"), datetime.timedelta(hours=2, minutes=3, seconds=4)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1.02:03:04.5"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "1:02:03:04.5"), datetime.timedelta(days=1, hours=2, minutes=3, seconds=4, microseconds=5)) self.assertEqual( StringSerialization.DeserializeItem(DurationTypeInfo(), "02:03:04.5"), datetime.timedelta(hours=2, minutes=3, seconds=4, microseconds=5)) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( DurationTypeInfo(), "not a valid duration"))
def GetChangesSinceLastMerge(cls, repo_root, dest_branch, source_merge_arg=None): source_branch = None additional_filters = [] # ---------------------------------------------------------------------- def GetDateOperator(arg): if arg is None or arg: return '>' return '<' # ---------------------------------------------------------------------- if isinstance(source_merge_arg, EmptyUpdateMergeArg): source_branch = cls.GetCurrentBranch(repo_root) elif isinstance(source_merge_arg, ChangeUpdateMergeArg): source_branch = cls._GetBranchAssociatedWithChange( repo_root, source_merge_arg.Change) additional_filters.append("{}::".format(source_merge_arg.Change)) elif isinstance(source_merge_arg, DateUpdateMergeArg): source_branch = cls.GetCurrentBranch(repo_root) additional_filters.append("date('{}{}')".format( GetDateOperator(source_merge_arg.GreaterThan), StringSerialization.SerializeItem(DateTimeTypeInfo(), source_merge_arg.Date, microseconds=False), )) elif isinstance(source_merge_arg, BranchAndDateUpdateMergeArg): source_branch = source_merge_arg.Branch additional_filters.append("date('{}{}')".format( GetDateOperator(source_merge_arg.GreaterThan), StringSerialization.SerializeItem(DateTimeTypeInfo(), source_merge_arg.Date, microseconds=False), )) elif isinstance(source_merge_arg, BranchUpdateMergeArg): source_branch = source_merge_arg.Branch else: assert False, type(source_merge_arg) filter = "::{} and not ::{}".format(source_branch, dest_branch) if additional_filters: filter += " and {}".format(" and ".join(additional_filters)) command_line = r'hg log --branch "{source_branch}" --rev "{filter}" --template "{{rev}}\n"' \ .format( source_branch=source_branch, filter=filter, ) result, output = cls.Execute(repo_root, command_line) assert result == 0, (result, output) return [line.strip() for line in output.split('\n') if line.strip()]
def ApplyImpl(parameter, arg): if isinstance(parameter.TypeInfo, DictTypeInfo): # Add the dictionary values; we will validate later. match = self._dict_regex.match(arg) if not match: return "'{}' is not a valid dictionary entry".format(arg) tag = match.group("tag") tag = tag.replace( "\\{}".format(self.CommandLineDictTagValueSeparator), self.CommandLineDictTagValueSeparator) value = match.group("value") argument_values.setdefault(parameter.Name, OrderedDict()) if tag not in argument_values[parameter.Name]: if parameter.AllowDuplicates: argument_values[parameter.Name][tag] = [ value, ] else: argument_values[parameter.Name][tag] = value else: if not parameter.AllowDuplicates: return "A value for '{}'s tag '{}' has already been provided ({})".format( parameter.Name, tag, argument_values[parameter.Name][tag], ) argument_values[parameter.Name][tag].append(value) return None if parameter.IsSwitch: # Preserve the value as a string so it can be deserialized below arg = StringSerialization.SerializeItem( parameter.TypeInfo, not parameter.DefaultValue) try: if isinstance(parameter.TypeInfo, tuple(ALL_FUNDAMENTAL_TYPES)): value = StringSerialization.DeserializeItem( parameter.TypeInfo, arg) elif isinstance(parameter.TypeInfo, AnyOfTypeInfo): found = False for eti in parameter.TypeInfo.ElementTypeInfos: try: value = StringSerialization.DeserializeItem( eti, arg) found = True break except ValidationException: pass if not found: value = arg else: value = arg if parameter.PostprocessFunc: value = parameter.PostprocessFunc(value) except ValidationException as ex: return str(ex) if parameter.DisplayArity in [ '?', '1', ]: if parameter.Name in argument_values: return "A value for '{}' has already been provided ({})".format( parameter.Name, argument_values[parameter.Name], ) argument_values[parameter.Name] = value elif parameter.DisplayArity in [ '*', '+', ]: argument_values.setdefault(parameter.Name, []).append(value) else: assert False return None
def test_Bool(self): self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "true")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "t")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "yes")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "y")) self.assertTrue( StringSerialization.DeserializeItem(BoolTypeInfo(), "1")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "false")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "f")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "no")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "n")) self.assertFalse( StringSerialization.DeserializeItem(BoolTypeInfo(), "0")) self.assertRaises( ValidationException, lambda: StringSerialization.DeserializeItem( BoolTypeInfo(), "not_valid"))
def GetChangesSinceLastMerge(cls, repo_root, dest_branch, source_merge_arg=None): # Git is really screwed up. After a 30 minute search, I couldn't find a way to # specify a branch and beginning revision in a single command. Therefore, I am # doing it manually. source_branch = None additional_filters = [] post_decorator_func = None # ---------------------------------------------------------------------- def GetDateOperator(arg): if arg is None or arg: return "since" return "until" # ---------------------------------------------------------------------- if source_merge_arg is None or isinstance(source_merge_arg, EmptyUpdateMergeArg): source_branch = cls.GetCurrentBranch(repo_root) elif isinstance(source_merge_arg, ChangeUpdateMergeArg): source_branch = cls._GetBranchAssociatedWithChange( source_merge_arg.Change) # ---------------------------------------------------------------------- def AfterChangeDecorator(changes): starting_index = None for index, change in enumerate(changes): if change == source_merge_arg.Change: starting_index = index break if starting_index is None: return [] return changes[starting_index:] # ---------------------------------------------------------------------- post_decorator_func = AfterChangeDecorator elif isinstance(source_merge_arg, DateUpdateMergeArg): source_branch = cls.GetCurrentBranch(repo_root) additional_filters.append('--{}="{}"'.format( GetDateOperator(source_merge_arg.GreaterThan), StringSerialization.SerializeItem(DateTimeTypeInfo(), source_merge_arg.Date), )) elif isinstance(source_merge_arg, BranchUpdateMergeArg): source_branch = source_merge_arg.Branch elif isinstance(source_merge_arg, BranchAndDateUpdateMergeArg): source_branch = source_merge_arg.Branch additional_filters.append('--{}="{}"'.format( GetDateOperator(source_merge_arg.GreaterThan), StringSerialization.SerializeItem(DateTimeTypeInfo(), source_merge_arg.Date), )) else: assert False, type(source_merge_arg) command_line = 'git --no-pager log "{source_branch}" --not "{dest_branch}" --format="%H" --no-merges{additional_filters}' \ .format( source_branch=source_branch, dest_branch=dest_branch, additional_filters='' if not additional_filters else " {}".format(' '.join(additional_filters)), ) result, output = cls.Execute(repo_root, command_line) assert result == 0, (result, output) changes = [line.strip() for line in output.split('\n') if line.strip()] if post_decorator_func: changes = post_decorator_func(changes) return changes