Skip to content

Instantly share code, notes, and snippets.

@arpruss
Created July 31, 2023 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arpruss/a2d7345c5c9c23840ca17a2d466df8f2 to your computer and use it in GitHub Desktop.
Save arpruss/a2d7345c5c9c23840ca17a2d466df8f2 to your computer and use it in GitHub Desktop.
protectedmark.py
import os
import sys
import stat
import subprocess
rating = None
label = None
delete = False
base = "."
extensions = set(('.jpeg','.jpg','.dcf', '.arw', '.sr2', '.srf', '.dng', '.cr2', '.raf', '.nef', '.nrw', '.orf', '.rw2', '.raw'))
ratings = set(('0','1','2','3','4','5'))
args = sys.argv[1:]
reverse = False
while len(args) > 0:
if args[0].startswith("-"):
if args[0] == '-reverse':
reverse = not reverse
elif args[0] == '-delete':
delete = True
elif args[0].startswith('-label'):
args = args[1:]
if len(args):
label = args[0]
else:
break
elif args[0][1:] in ratings:
rating = args[0][1:]
else:
args = []
break
args = args[1:]
else:
break
if len(args) == 0 or (rating is None and label is None and not delete):
print("python protectedmark.py [-reverse|-delete] [-0|-1|-2|-3|-4|-5] [-label text] directory")
print(" Note: -delete deletes unprotected files (unless -reverse is set)")
sys.exit(1)
base = args[0]
if rating is not None or label is not None:
comm = subprocess.Popen(
["exiftool", "-@", "-"],
shell=True, stdin=subprocess.PIPE, universal_newlines=True)
comm.stdin.write("-F\n-overwrite_original\n-v\n")
if rating is not None:
comm.stdin.write("-rating="+rating+"\n")
if label is not None:
comm.stdin.write("-label="+label+"\n")
for (dir,_,names) in os.walk(base):
for name in names:
if os.path.splitext(name)[1].lower() in extensions:
filename = os.path.join(dir,name)
mode = os.stat(filename).st_mode
protected = not stat.S_ISDIR(mode) and not bool(mode & stat.S_IWUSR)
if reverse:
protected = not protected
if protected:
comm.stdin.write(filename+"\n")
elif delete:
os.unlink(filename)
comm.stdin.close()
elif delete:
for (dir,_,names) in os.walk(base):
for name in names:
if os.path.splitext(name)[1].lower() in extensions:
filename = os.path.join(dir,name)
mode = os.stat(filename).st_mode
protected = not stat.S_ISDIR(mode) and not bool(mode & stat.S_IWUSR)
if reverse:
protected = not protected
if not protected:
os.unlink(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment