Skip to content

Instantly share code, notes, and snippets.

@bpanthi977
Last active May 14, 2025 20:41
Show Gist options
  • Save bpanthi977/0b8e50588d710a1513d8d2a55d70f758 to your computer and use it in GitHub Desktop.
Save bpanthi977/0b8e50588d710a1513d8d2a55d70f758 to your computer and use it in GitHub Desktop.
Create rclone --exclude-from list by recursively visting .gitignore files
#!/usr/bin/env python
import os
import subprocess
def append_parentpath(path, ignore_string):
return [path + '/' + line for line in ignore_string.splitlines()]
def traverse_and_generate(path, printpath=""):
dirs = os.listdir(path)
if '.git' in dirs:
# Generate IgnoreList
os.chdir(path)
print(path)
ignore_list = subprocess.run(['git', 'ls-files', '--others', '--ignored', '--exclude-standard'], capture_output = True).stdout
return append_parentpath(printpath, ignore_list.decode())
ignore_list = []
for basename in dirs:
fullname = os.path.join(path, basename)
if os.path.isdir(fullname):
for item in traverse_and_generate(fullname, printpath+"/"+basename):
ignore_list.append(item)
return ignore_list
def write_list_to_file(lines , f):
with open(f, 'w') as file:
for l in lines:
file.write(l+'\n')
def main():
root_path = os.sys.argv[1]
write_file_path = os.sys.argv[2]
l = traverse_and_generate(root_path)
relative_path = os.path.relpath(write_file_path, root_path)
if relative_path[:3] != "../":
l.append(relative_path)
write_list_to_file(l, write_file_path)
if len(os.sys.argv) < 3:
print("""rclone-exclude.py : Generate exclude fileslist from .gitignores
USAGE rclone-exclude.py RootDir ExcludeFile
RootDir : Root of directory tree for which to generate exclude list
ExcludeFile : File to write the exclude files list""")
else:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment