3454a55636
cffconvert / validate (push) Waiting to run
ci-workflow / pre-commit (push) Waiting to run
ci-workflow / Minimal NLTK Download Test (macos-latest) (push) Waiting to run
ci-workflow / Minimal NLTK Download Test (ubuntu-latest) (push) Waiting to run
ci-workflow / Minimal NLTK Download Test (windows-latest) (push) Waiting to run
ci-workflow / Python 3.10 on macos-latest (push) Blocked by required conditions
ci-workflow / Python 3.11 on macos-latest (push) Blocked by required conditions
ci-workflow / Python 3.12 on macos-latest (push) Blocked by required conditions
ci-workflow / Python 3.13 on macos-latest (push) Blocked by required conditions
ci-workflow / Python 3.14 on macos-latest (push) Blocked by required conditions
ci-workflow / Python 3.14t on macos-latest (push) Blocked by required conditions
ci-workflow / Python 3.10 on ubuntu-latest (push) Blocked by required conditions
ci-workflow / Python 3.11 on ubuntu-latest (push) Blocked by required conditions
ci-workflow / Python 3.12 on ubuntu-latest (push) Blocked by required conditions
ci-workflow / Python 3.13 on ubuntu-latest (push) Blocked by required conditions
ci-workflow / Python 3.14 on ubuntu-latest (push) Blocked by required conditions
ci-workflow / Python 3.14t on ubuntu-latest (push) Blocked by required conditions
ci-workflow / Python 3.10 on windows-latest (push) Blocked by required conditions
ci-workflow / Python 3.11 on windows-latest (push) Blocked by required conditions
ci-workflow / Python 3.12 on windows-latest (push) Blocked by required conditions
ci-workflow / Python 3.13 on windows-latest (push) Blocked by required conditions
ci-workflow / Python 3.14 on windows-latest (push) Blocked by required conditions
61 lines
1.6 KiB
Python
Executable File
61 lines
1.6 KiB
Python
Executable File
#!/usr/bin/env python
|
|
#
|
|
# Natural Language Toolkit: substitute a pattern with
|
|
# a replacement in every file
|
|
# Copyright (C) 2001-2026 NLTK Project
|
|
# Author: Edward Loper <edloper@gmail.com>
|
|
# Steven Bird <stevenbird1@gmail.com>
|
|
# URL: <https://www.nltk.org/>
|
|
# For license information, see LICENSE.TXT
|
|
|
|
# NB Should work on all platforms,
|
|
# http://www.python.org/doc/2.5.2/lib/os-file-dir.html
|
|
|
|
import os
|
|
import stat
|
|
import sys
|
|
|
|
|
|
def update(file, pattern, replacement):
|
|
try:
|
|
# make sure we can write the file
|
|
old_perm = os.stat(file)[0]
|
|
if not os.access(file, os.W_OK):
|
|
os.chmod(file, old_perm | stat.S_IWRITE)
|
|
|
|
# write the file
|
|
s = open(file, "rb").read().decode("utf-8")
|
|
t = s.replace(pattern, replacement)
|
|
out = open(file, "wb")
|
|
out.write(t.encode("utf-8"))
|
|
out.close()
|
|
|
|
# restore permissions
|
|
os.chmod(file, old_perm)
|
|
|
|
return s != t
|
|
|
|
except Exception:
|
|
exc_type, exc_obj, exc_tb = sys.exc_info()
|
|
print(f"Unable to check {file:s} {str(exc_type):s}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) != 3:
|
|
exit("Usage: %s <pattern> <replacement>" % sys.argv[0])
|
|
|
|
pattern = sys.argv[1]
|
|
replacement = sys.argv[2]
|
|
count = 0
|
|
|
|
for root, dirs, files in os.walk("."):
|
|
if not ("/.git" in root or "/.tox" in root):
|
|
for file in files:
|
|
path = os.path.join(root, file)
|
|
if update(path, pattern, replacement):
|
|
print("Updated:", path)
|
|
count += 1
|
|
|
|
print(f"Updated {count} files")
|