Created
October 24, 2024 01:29
-
-
Save justingreerbbi/75de679f07ee04f5272e578e8fca8fbc to your computer and use it in GitHub Desktop.
Python script to sync two folders for Git and remove sensitive data from source.
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
# To use this script create a parent directory and two directorys inside it (src & github). Place this file in the parent directory. | |
# Place your source code in the 'src' directory. When you want to publish, run this script to copy all source files to the github directory and replace sensitive data. | |
# This way you can run sensitive data in your working copy and the Github version is stripped of sensitive data. | |
# Be sure to update this snippet to include your find and replace variables. | |
import shutil | |
import os | |
def copy_src_to_github(src, github): | |
if not os.path.exists(src): | |
print(f"Source directory {src} does not exist.") | |
return | |
if not os.path.exists(github): | |
os.makedirs(github) | |
try: | |
# Delete all files and directories in github that match the names in the src directory | |
for item in os.listdir(github): | |
item_path = os.path.join(github, item) | |
if item not in [".gitattributes", ".gitignore", ".git"]: | |
if os.path.isdir(item_path): | |
#print("Remove Directory: ", item_path) | |
shutil.rmtree(item_path) | |
else: | |
#print("Remove File: ", item_path) | |
os.remove(item_path) | |
shutil.copytree(src, github, dirs_exist_ok=True) | |
# search and replace text in all files | |
for root, dirs, files in os.walk(github): | |
for file in files: | |
file_path = os.path.join(root, file) | |
with open(file_path, "r") as f: | |
content = f.read() | |
content = content.replace("find-me", "{replace-with}") | |
with open(file_path, "w") as f: | |
f.write(content) | |
print(f"All files and directories from {src} have been copied to {github}.") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
if __name__ == "__main__": | |
src = "./src" | |
dest = "./github" | |
copy_src_to_github(src, dest) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment