Created
April 5, 2021 23:23
-
-
Save homebysix/33fd0fac32d5decaa9baee8164e59971 to your computer and use it in GitHub Desktop.
anonymizer.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Name: anonymizer.py | |
Description: Simple Python script that anonymizes personal and company | |
information in an input file. Useful for running on logs | |
and Terminal outputs before sharing on GitHub or Slack. | |
Author: Elliot Jordan <[email protected]> | |
Created: 2021-02-03 | |
Last Modified: 2021-04-05 | |
Version: 1.1.0 | |
""" | |
import sys | |
import os | |
def anonymize(text, verbose=True): | |
"""Perform anonymization on specified text.""" | |
replacements = ( | |
("/Users/johndoe/Developer/munki_repo", "/Users/Shared/munki_repo"), | |
("/Users/johndoe/Developer/autopkg_recipes", "~/Library/AutoPkg"), | |
("/Users/johndoe", "~"), | |
("johndoe", "testuser"), | |
("john", "testuser"), | |
("bigcorp", "pretendco"), | |
("Bigcorp", "PretendCo"), | |
("bgcp", "ptnd"), | |
("BGCP", "PTND"), | |
("bigcpe", "cpe"), | |
("BigCPE", "CPE"), | |
) | |
for repl in replacements: | |
if repl[0] in text: | |
if verbose: | |
print("%s --> %s" % (repl[0], repl[1])) | |
text = text.replace(repl[0], repl[1]) | |
return text | |
def main(): | |
"""Main process.""" | |
if len(sys.argv) > 1: | |
# Input file | |
filepath = sys.argv[1] | |
if not os.path.isfile(filepath): | |
sys.exit("File %s does not exist." % filepath) | |
with open(filepath, "r") as openfile: | |
text = openfile.read() | |
with open(filepath, "w") as openfile: | |
openfile.write(anonymize(text, verbose=True)) | |
else: | |
# Standard in | |
print(anonymize(sys.stdin.read(), verbose=False)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment