-
Notifications
You must be signed in to change notification settings - Fork 3
/
cargo-play
executable file
·85 lines (61 loc) · 2.09 KB
/
cargo-play
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env python3
# A local rust playground~like thing
import tempfile
import os
import subprocess
from pathlib import Path
from datetime import datetime, timedelta
import dbus
DELETE_TIME = timedelta(days=7)
def get_delete_time() -> str:
"""gets the time a day from now"""
t = datetime.now() + DELETE_TIME
return t.strftime("%Y-%m-%d %H:%M:%S")
bus = dbus.SessionBus()
sd = bus.get_object("org.freedesktop.systemd1", "/org/freedesktop/systemd1")
man = dbus.Interface(sd, "org.freedesktop.systemd1.Manager")
def make_delete_timer(unit_name: str, tempdir: Path) -> str:
DESC = "Delete playground directory"
deltime = get_delete_time()
man.StartTransientUnit(
f"{unit_name}.timer",
"fail",
[
("Description", DESC),
("TimersCalendar", [("OnCalendar", deltime)]),
("RemainAfterElapse", False),
],
[
(
f"{unit_name}.service",
[
("Description", DESC),
('Type', 'simple'),
("ExecStart", [("rm", ["rm", "-rf", str(tempdir)], False)]),
],
)
],
)
return deltime
PWAYGWOUMD = Path("~/.dotfiles/pwaygwoumd").expanduser()
def get_editor() -> str:
ed = os.environ.get("VISUAL") or os.environ.get("EDITOR")
if not ed:
raise ValueError("no EDITOR or VISUAL")
return ed
def main():
editor = get_editor()
tmpdir = Path(tempfile.mkdtemp(prefix="play-"))
unit_name = f"delete-{tmpdir.name}"
os.chdir(tmpdir)
subprocess.run(["cargo", "init", "--vcs=none", str(tmpdir)]).check_returncode()
os.environ["PATH"] += f":{PWAYGWOUMD}/bin"
os.environ.update({"PWAYGWOUMD_DIR": str(tmpdir), "PWAYGWOUMD_UNITSTEM": unit_name})
subprocess.run([editor, "src/main.rs"])
if tmpdir.exists():
delete_time = make_delete_timer(unit_name, tmpdir)
print(
f"{tmpdir} will be wiped at {delete_time}, run \n systemctl --user stop {unit_name}.{{timer,service}}\nto cancel"
)
if __name__ == "__main__":
main()