def update_alarms_in_udts(root_path):
 """This script will update all the alarms under a base path so that the
 alarm is enabled based on the depth from the UDT and the other Meta tag bindings are updated accordingly."""

 def get_relative_meta_path(depth):
  if depth <= 1:
   return "[.]Meta/"
  return "[.]" + "../" * (depth - 1) + "Meta/"

 def update_alarm_config(base_path, path, meta_path):
  """This function will take a tag path as an input and update the alarms with depth-based meta references."""
  configs = system.tag.getConfiguration(path, False)

  for config in configs:
   if "alarms" in config:
    for alarm in config["alarms"]:
     print alarm
     alarm["Building"] = {"bindType": "Tag", "value": meta_path + "Building"}
     alarm["EqName"] = {"bindType": "Tag", "value": meta_path + "EqName"}
     alarm["AreaServed"] = {"bindType": "Tag", "value": meta_path + "Area Served"}
     alarm["Room"] = {"bindType": "Tag", "value": meta_path + "Room"}
     if "enabled" not in alarm or alarm['enabled']==True:
      alarm["enabled"] = {"bindType": "Tag", "value": meta_path + "Alarms Enabled"}
     else:
      # Check to see if the alarm if false. If false, skip edit.
      if alarm["enabled"]:
       enable_condition = str(alarm["enabled"]["value"])
       alarm["enabled"] = {"bindType": "Expression", "value": "{%sAlarms Enabled} && {%s}" % (meta_path, enable_condition)}
       # take the tag path and use in the expression
      # u'enabled': {u'bindType': u'Expression', u'value': u'{[.]../Meta/Alarms Enabled} && {[.]Communcation Loss}'}
    system.tag.configure(base_path, config, "o")
    print "Updated tag: %s" % config["path"]
   else:
    print "\nERROR: Tag has no alarms: %s\n" % config["path"]

 def recurse_tags(base_path, udt_depth):
  """Recursively browse tags and update alarms with respect to UDT depth."""
  tags = system.tag.browse(base_path)

  for tag in tags:
   tag_type = str(tag["tagType"])
   has_children = tag["hasChildren"]
   full_path = tag["fullPath"]

   if tag_type == "UdtType":
    # Entering a new UDT, so depth resets to 1
    recurse_tags(full_path, 1)
   elif tag_type == "AtomicTag" and "alarm" in tag["attributes"]:
    meta_path = get_relative_meta_path(udt_depth)
    update_alarm_config(base_path, full_path, meta_path)
   elif has_children:
    # Descending deeper from current UDT level
    recurse_tags(full_path, udt_depth + 1)

 recurse_tags(root_path, 0)
 