Skip to content

Commit ee5ce83

Browse files
authored
Create makelock.py
1 parent 57df92f commit ee5ce83

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

makelock.py

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#!/usr/bin/env python3
2+
3+
"""
4+
makelock.py
5+
Create an Avid bin lock (.lck) file with a custom display name
6+
By Michael Jordan <michael@glowingpixel.com>
7+
8+
Usage: makelock.py LockText path/to/output.lck
9+
"""
10+
11+
import sys, pathlib
12+
13+
# Config
14+
MAX_LENGTH = 255
15+
FILE_EXTENSION = ".lck"
16+
ALLOW_OVERWRITE = False
17+
18+
def format_lock_contents(name:str) -> bytes:
19+
"""Encode a given string into Avid lockfile contents"""
20+
21+
if len(name) > MAX_LENGTH:
22+
raise ValueError(f"Lock name cannot exceed {MAX_LENGTH} characters")
23+
24+
return name.ljust(MAX_LENGTH, '\0').encode("utf-16le")
25+
26+
def main(name:str, output:str) -> pathlib.Path:
27+
"""Create an Avid lockfile with a custom name"""
28+
29+
# Create and validate the output path, ensuring we're using the correct file extension
30+
path_output = pathlib.Path(output).with_suffix(FILE_EXTENSION)
31+
32+
if not path_output.parent.is_dir():
33+
raise FileNotFoundError("The given output path does not exist")
34+
elif path_output.exists() and not ALLOW_OVERWRITE:
35+
raise FileExistsError(f"A lock already exists at {path_output}")
36+
37+
# Create the lockfile contents
38+
lock_bytes = format_lock_contents(name)
39+
40+
# Write the lockfile contents to the output path
41+
with path_output.open('wb') as file_output:
42+
file_output.write(lock_bytes)
43+
44+
return path_output
45+
46+
if __name__ == "__main__":
47+
48+
if len(sys.argv) < 3:
49+
sys.exit(f"Usage: {__file__} LockText outputpath.lck")
50+
51+
try:
52+
path_output = main(name=sys.argv[1], output=sys.argv[2])
53+
except Exception as e:
54+
sys.exit(f"Error creating lock file: {e}")
55+
else:
56+
print(f"Lock file written to {path_output}")

0 commit comments

Comments
 (0)