make_domsub_script.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. # Copyright (c) 2023 The ungoogled-chromium Authors. All rights reserved.
  4. # Use of this source code is governed by a BSD-style license that can be
  5. # found in the LICENSE file.
  6. """
  7. Generate standalone script that performs the domain substitution.
  8. """
  9. from pathlib import Path
  10. import argparse
  11. import re
  12. def make_domain_substitution_script(regex_path, files_path, output_path):
  13. """
  14. Generate a standalone shell script (which uses Perl) that performs
  15. domain substitution on the appropriate files.
  16. regex_path is a pathlib.Path to domain_regex.list
  17. files_path is a pathlib.Path to domain_substitution.list
  18. output_path is a pathlib.Path to the output file.
  19. Raises FileNotFoundError if the regex or file lists do not exist.
  20. Raises FileExistsError if the output file already exists.
  21. """
  22. if not regex_path.exists():
  23. raise FileNotFoundError(regex_path)
  24. if not files_path.exists():
  25. raise FileNotFoundError(files_path)
  26. if output_path.exists():
  27. raise FileExistsError(output_path)
  28. regex_list = tuple(filter(len, regex_path.read_text().splitlines()))
  29. files_list = tuple(filter(len, files_path.read_text().splitlines()))
  30. # Convert the Python-style regexes into a Perl s/// op
  31. perl_replace_list = ['s#' + re.sub(r'\\g<(\d+)>', r'${\1}', x) + '#g' for x in regex_list]
  32. files_list_str = '\n'.join(files_list)
  33. perl_replace_list_str = '\n'.join([f' {x};' for x in perl_replace_list])
  34. with open(output_path, 'w') as out:
  35. out.write("""#!/bin/sh -e
  36. #
  37. # This script performs domain substitution on the Chromium source files.
  38. #
  39. # Generated by make_domsub_script.py, part of the ungoogled-chromium project:
  40. # https://github.com/ungoogled-software/ungoogled-chromium.git
  41. #
  42. # Check that we are inside the Chromium source tree
  43. test -f build/config/compiler/BUILD.gn
  44. # These filenames may contain spaces and/or other unusual characters
  45. print_file_list() {
  46. cat <<'__END__'
  47. %s
  48. __END__
  49. }
  50. echo "Creating backup archive ..."
  51. backup=domain-substitution.orig.tar
  52. print_file_list | tar cf $backup --verbatim-files-from --files-from=-
  53. echo "Applying ungoogled-chromium domain substitution to %d files ..."
  54. print_file_list | xargs -d '\\n' perl -0777 -C0 -pwi -e '
  55. %s
  56. '
  57. # end
  58. """ % (files_list_str, len(files_list), perl_replace_list_str))
  59. def _callback(args):
  60. """CLI Callback"""
  61. make_domain_substitution_script(args.regex, args.files, args.output)
  62. def main():
  63. """CLI Entrypoint"""
  64. parser = argparse.ArgumentParser()
  65. parser.set_defaults(callback=_callback)
  66. parser.add_argument('-r', '--regex', type=Path, required=True, help='Path to domain_regex.list')
  67. parser.add_argument(
  68. '-f', '--files', type=Path, required=True, help='Path to domain_substitution.list')
  69. parser.add_argument(
  70. '-o', '--output', type=Path, required=True, help='Path to script file to create')
  71. args = parser.parse_args()
  72. args.callback(args)
  73. if __name__ == '__main__':
  74. main()