use tempfile.mkstemp instead of homebrew solution

This commit is contained in:
phixxy 2026-03-05 02:37:53 -08:00
parent a90c9244c1
commit f210bd0535

View file

@ -2,10 +2,9 @@
import sys import sys
import os import os
import random
import string
import argparse import argparse
import time import time
import tempfile
def make_dir(d): def make_dir(d):
if not os.path.exists(d): if not os.path.exists(d):
@ -45,25 +44,12 @@ def get_editor(use_v):
return v return v
return e return e
def mkstemp(fp):
if fp[-6:] != "XXXXXX":
print(f'Error: mkstemp got filepath: {fp}')
return None
for x in range(0,1000):
r = ''.join(random.choices(string.ascii_letters, k=6))
fp = fp[:-6] + r
if not os.path.exists(fp):
return fp
print("Error: Could not create new file, try running skratch -c and try again")
return None
def run(editor, sk_path, filename=None): def run(editor, sk_path, filename=None):
if not filename: if not filename:
t = time.localtime() t = time.localtime()
filename = f"skratch-{t.tm_year}-{t.tm_mon}-{t.tm_mday}-XXXXXX" filename = f"skratch-{t.tm_year}-{t.tm_mon}-{t.tm_mday}-"
fp = os.path.join(sk_path, filename) fd, fp = tempfile.mkstemp(prefix=filename, dir=sk_path)
fp = mkstemp(fp) os.close(fd)
else: else:
fp = os.path.join(sk_path, filename) fp = os.path.join(sk_path, filename)
if fp != None: if fp != None: