<commit_before># A simple script to snag deprecated proto fields and add them to runtime_features.h

import re
import subprocess
import fileinput

grep_output = subprocess.check_output('grep -r "deprecated = true" api/*', shell=True)

filenames_and_fields = set()

# Compile the set of deprecated fields and the files they're in, deduping via set.
deprecated_regex = re.compile(r'.*\/([^\/]*.proto):[^=]* ([^= ]+) =.*')
for line in grep_output.splitlines():
  match = deprecated_regex.match(line)
  if match:
    filenames_and_fields.add(tuple([match.group(1), match.group(2)]))
  else:
    print 'no match in ' + line + ' please address manually!'

# Now discard any deprecated features already listed in runtime_features
exiting_deprecated_regex = re.compile(r'.*"envoy.deprecated_features.(.*):(.*)",.*')
with open('source/common/runtime/runtime_features.h', 'r') as features:
  for line in features.readlines():
    match = exiting_deprecated_regex.match(line)
    if match:
      filenames_and_fields.discard(tuple([match.group(1), match.group(2)]))

# Finally sort out the code to add to runtime_features.h and a canned email for envoy-announce.
code = ''
email = 'The latest Envoy release will deprecate the following configuration fields:\n'
for (filename, field) in filenames_and_fields:
  code += ('    "envoy.deprecated_features.' + filename + ':' + field + '",\n')
  email += (field + ' from ' + filename + '\n')

print '\n\nSuggested runtime changes: '
print code

if not raw_input('Apply runtime changes? [yN] ').strip().lower() in ('y', 'yes'):
  exit(1)

for line in fileinput.FileInput('source/common/runtime/runtime_features.h', inplace=1):
  if 'envoy.deprecated_features.deprecated.proto:is_deprecated_fatal' in line:
    line = line.replace(line, line + code)
  print line,

print '\nChanges applied.  Please create an upstream PR and send the following to envoy-announce:\n'

print email<commit_msg>tools: updating deprecation scripts (#6289)<commit_after># A simple script to snag deprecated proto fields and add them to runtime_features.h

import re
import subprocess
import fileinput


# Sorts out the list of deprecated proto fields which should be disallowed and returns a tuple of
# email and code changes.
def deprecate_proto():
  grep_output = subprocess.check_output('grep -r "deprecated = true" api/*', shell=True)

  filenames_and_fields = set()

  # Compile the set of deprecated fields and the files they're in, deduping via set.
  deprecated_regex = re.compile(r'.*\/([^\/]*.proto):[^=]* ([^= ]+) =.*')
  for line in grep_output.splitlines():
    match = deprecated_regex.match(line)
    if match:
      filenames_and_fields.add(tuple([match.group(1), match.group(2)]))
    else:
      print 'no match in ' + line + ' please address manually!'

  # Now discard any deprecated features already listed in runtime_features
  exiting_deprecated_regex = re.compile(r'.*"envoy.deprecated_features.(.*):(.*)",.*')
  with open('source/common/runtime/runtime_features.cc', 'r') as features:
    for line in features.readlines():
      match = exiting_deprecated_regex.match(line)
      if match:
        filenames_and_fields.discard(tuple([match.group(1), match.group(2)]))

  # Finally sort out the code to add to runtime_features.cc and a canned email for envoy-announce.
  code_snippets = []
  email_snippets = []
  for (filename, field) in filenames_and_fields:
    code_snippets.append('    "envoy.deprecated_features.' + filename + ':' + field + '",\n')
    email_snippets.append(field + ' from ' + filename + '\n')
  code = ''.join(code_snippets)
  email = ''
  if email_snippets:
    email = ('\nThe following deprecated configuration fields will be disallowed by default:\n' +
             ''.join(email_snippets))

  return email, code


# Sorts out the list of features which should be default enabled and returns a tuple of
# email and code changes.
def flip_runtime_features():
  grep_output = subprocess.check_output(
      'grep -r "envoy.reloadable_features\." source/*', shell=True)

  features_to_flip = set()

  # Compile the set of features to flip, deduping via set.
  deprecated_regex = re.compile(r'.*"(envoy.reloadable_features\.[^"]+)".*')
  for line in grep_output.splitlines():
    match = deprecated_regex.match(line)
    if match:
      features_to_flip.add(match.group(1))
    else:
      print 'no match in ' + line + ' please address manually!'

  # Exempt the two test flags.
  features_to_flip.remove('envoy.reloadable_features.my_feature_name')
  features_to_flip.remove('envoy.reloadable_features.test_feature_true')

  code_snippets = []
  email_snippets = []
  for (feature) in features_to_flip:
    code_snippets.append('    "' + feature + '",\n')
    email_snippets.append(feature + '\n')
  code = ''.join(code_snippets)
  email = ''
  if email_snippets:
    email = 'the following features will be defaulted to true:\n' + ''.join(email_snippets)

  return email, code


# Gather code and suggested email changes.
runtime_email, runtime_features_code = flip_runtime_features()
deprecate_email, deprecate_code = deprecate_proto()

email = ('The Envoy maintainer team is cutting the next Envoy release.  In the new release ' +
         runtime_email + deprecate_email)

print '\n\nSuggested envoy-announce email: \n'
print email

if not raw_input('Apply relevant runtime changes? [yN] ').strip().lower() in ('y', 'yes'):
  exit(1)

for line in fileinput.FileInput('source/common/runtime/runtime_features.cc', inplace=1):
  if 'envoy.reloadable_features.test_feature_true' in line:
    line = line.replace(line, line + runtime_features_code)
  if 'envoy.deprecated_features.deprecated.proto:is_deprecated_fatal' in line:
    line = line.replace(line, line + deprecate_code)
  print line,

print '\nChanges applied.  Please send the email above to envoy-announce.\n'
{'fraction_non_alphanumeric': 0.12454966546577458, 'fraction_numerical': 0.002916452221650369, 'mean_word_length': 4.348623853211009, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7383391', 'n_tokens_mistral': 1885, 'n_tokens_neox': 1822, 'n_words': 525}