code: def get_target_expansion_query(
      targets: Iterable[str],
      tests_only: bool,
      exclude_manual: bool,
      include_tags: Optional[Iterable[str]] = None,
      exclude_tags: Optional[Iterable[str]] = None,
  ) -> str:
      """Generate the bazel query to obtain individual rules."""
      included_targets, excluded_targets = partition_targets(targets)
  
      included_targets = quote_targets(included_targets)
      excluded_targets = quote_targets(excluded_targets)
  
      query = f"set({included_targets})"
  
      if include_tags:
          tags_regex = generate_regex_from_tags(include_tags)
          # Each rule has to have at least one tag from
          # include_tags
          query = f'attr("tags", "{tags_regex}", {query})'
  
      if tests_only:
          # Discard any non-test rules
          query = f"tests({query})"
  
      if excluded_targets:
          # Exclude the targets we do not want
          excluded_set = f"set({excluded_targets})"
          query = f"{query} except {excluded_set}"
  
      if exclude_manual:
          # Exclude targets with 'manual' tag
          exclude_tags = exclude_tags or set()
          exclude_tags.add("manual")
  
      if exclude_tags:
          # Exclude targets which have at least one exclude_tag
          tags_regex = generate_regex_from_tags(exclude_tags)
          query = f'{query} except attr("tags", "{tags_regex}", set({included_targets}))'
  
      return query
  
imports: from collections import defaultdict
  from dataclasses import dataclass
  from typing import Iterable, List, Optional, Set, Tuple
  import argparse
  import os
  import re
  import shlex
  import subprocess
  import sys
  import xml.etree.ElementTree
label_desc: Use image node_id 5 for calling a global function with example usage: get_target_expansion_query(targets, tests_only, exclude_manual, include_tags, exclude_tags) and returns: query