<commit_before>#!/usr/bin/env python
# Copyright 2011 <NAME>. All Rights Reserved. Use of this
# source code is governed by a BSD-style license that can be found in the
# LICENSE file.

"""Generates a diff to apply to /usr/share/polkit-1/actions to fix using gnome
administrative applications from a NX (nomachine) session.

NX sessions are considered 'inactive'.

Example usage:
cd /usr/share/polkit-1/actions
~/fix_inactive_policy.py | patch --dry-run

If it succeeds, then you can run:
~/fix_inactive_policy.py | sudo patch --backup --no-backup-if-mismatch
sudo pkill polkitd
"""

from __future__ import with_statement
import difflib
import os
import re
import sys


def main():
  out = fix_inactive_deny('/usr/share/polkit-1/actions/')
  for filename, original, new in out:
    print ''.join(difflib.unified_diff(original, new, filename, filename))
  return 0


def fix_inactive_deny(path):
  """Crude way to modified policies.

  In theory it'd be better to use xml.etree but there is no way to keep the
  original formatting.
  """
  inactive = re.compile(r'<allow_inactive>no</allow_inactive>')
  active = re.compile(r'<allow_active>(.+?)</allow_active>')

  out = []
  for filename in os.listdir(path):
    if filename.endswith('.orig'):
      continue
    filepath = os.path.join(path, filename)
    with open(filepath) as f:
      lines = f.read().splitlines(True)

    original = lines[:]
    for index, line in enumerate(lines):
      if inactive.search(line):
        m = active.search(lines[index + 1])
        lines[index] = line.replace(
            '<allow_inactive>no</allow_inactive>',
            '<allow_inactive>%s</allow_inactive>' %
               m.group(1).encode('ascii', 'xmlcharrefreplace'))
    if original != lines:
      out.append((filename, original, lines))
  return out


if __name__ == '__main__':
  sys.exit(main())<commit_msg>Fix also allow_any<commit_after>#!/usr/bin/env python
# Copyright 2011 <NAME>. All Rights Reserved. Use of this
# source code is governed by a BSD-style license that can be found in the
# LICENSE file.

"""Generates a diff to apply to /usr/share/polkit-1/actions to fix using gnome
administrative applications from a NX (nomachine) session.

NX sessions are considered 'inactive'.

Example usage:
cd /usr/share/polkit-1/actions
fix_inactive_policy.py | patch --dry-run

If it succeeds, then you can run:
fix_inactive_policy.py | sudo patch --backup --no-backup-if-mismatch
sudo pkill polkitd

You can restore with:
for i in *.orig; do sudo mv "$i" "${i/.orig}"; done
"""

from __future__ import with_statement
import difflib
import os
import re
import sys


def main():
  out = fix_inactive_deny('/usr/share/polkit-1/actions/')
  for filename, original, new in out:
    print ''.join(difflib.unified_diff(original, new, filename, filename))
  return 0


def fix_inactive_deny(path):
  """Crude way to modified policies.

  In theory it'd be better to use xml.etree but there is no way to keep the
  original formatting.
  """
  out = []
  filenames = os.listdir(path)
  if any(f.endswith('.orig') for f in filenames):
    print >> sys.stderr, "Found backup files, aborting!"
    return out

  re_active = re.compile(r'<allow_active>(.+?)</allow_active>')
  for filename in filenames:
    filepath = os.path.join(path, filename)
    with open(filepath) as f:
      lines = f.read().splitlines(True)

    original = lines[:]
    for index, line in enumerate(lines):
      if re.search(r'<allow_any>no</allow_any>', line):
        m = re_active.search(lines[index + 1])
        if not m:
          m = re_active.search(lines[index + 2])
        lines[index] = line.replace(
            '<allow_any>no</allow_any>',
            '<allow_any>%s</allow_any>' %
               m.group(1).encode('ascii', 'xmlcharrefreplace'))
      elif re.search(r'<allow_inactive>no</allow_inactive>', line):
        m = re_active.search(lines[index + 1])
        lines[index] = line.replace(
            '<allow_inactive>no</allow_inactive>',
            '<allow_inactive>%s</allow_inactive>' %
               m.group(1).encode('ascii', 'xmlcharrefreplace'))
    if original != lines:
      out.append((filename, original, lines))
  return out


if __name__ == '__main__':
  sys.exit(main())
{'fraction_non_alphanumeric': 0.12801133679735474, 'fraction_numerical': 0.00543221539914974, 'mean_word_length': 3.4022869022869022, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 5}, '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': '476051', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1368, 'n_words': 399}