syncIgnore 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #! /usr/bin/env python3
  2. import sys
  3. import os
  4. currentDir = os.getcwd()
  5. BEGIN = '#$ IGNORE'
  6. END = '#$ END IGNORE'
  7. while not os.path.isfile(os.path.join(currentDir, '.sync/IgnoreList')):
  8. currentDir = os.path.abspath(os.path.join(currentDir, '..'))
  9. if currentDir == os.path.realpath('/'):
  10. print('Error: Did not find `.sync`-directory above current directory.')
  11. sys.exit(1)
  12. with open(os.path.join(currentDir, '.sync/IgnoreList'), 'r') as f:
  13. lineArray = f.read().split('\n')
  14. try:
  15. before = lineArray[:lineArray.index(BEGIN)]
  16. after = lineArray[lineArray.index(END) + 1:]
  17. except:
  18. before = lineArray
  19. after = ['']
  20. def findRepos(directory, repoArray=[]):
  21. for file in os.listdir(directory):
  22. if file.endswith('.syncignore'):
  23. repoArray.append(directory)
  24. elif os.path.isdir(os.path.join(directory, file)):
  25. findRepos(os.path.join(directory, file), repoArray)
  26. elif file.startswith('.'):
  27. pass
  28. return repoArray
  29. newIgnored = findRepos(currentDir)
  30. if len(newIgnored) == 0:
  31. print('Found no `.syncignore`-files.')
  32. sys.exit(0)
  33. for index, string in enumerate(newIgnored):
  34. newIgnored[index] = os.path.join('.', string.replace(currentDir, ''))
  35. print("Ignoring\n.", '\n.'.join(newIgnored))
  36. newIgnored.insert(0, BEGIN)
  37. newIgnored.append(END)
  38. with open(os.path.join(currentDir, '.sync/IgnoreList'), 'w') as f:
  39. f.write('\n'.join(before + newIgnored + after))