forked from WEEE-Open/peracotta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolkit.py
executable file
·89 lines (73 loc) · 2.9 KB
/
polkit.py
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
86
87
88
89
import os
from time import sleep
# launch script with: ./generate_files.pkexec /path/to/tmp
# in annotate key path to generate_files.sh
# this should be saved as /usr/share/polkit-1/actions/it.weeeopen.peracotta.generate-files.policy
dotpolicy_content = """<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE policyconfig PUBLIC
"-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN"
"http://www.freedesktop.org/standards/PolicyKit/1/policyconfig.dtd">
<policyconfig>
<action id="it.weeeopen.peracotta.generate-files">
<description>Run P.E.R.A.C.O.T.T.A.</description>
<message>Authentication is required to run generate_files</message>
<defaults>
<allow_any>yes</allow_any>
<allow_inactive>yes</allow_inactive>
<allow_active>yes</allow_active>
</defaults>
<annotate key="org.freedesktop.policykit.exec.path"></annotate>
<annotate key="org.freedesktop.policykit.exec.allow_gui">TRUE</annotate>
</action>
</policyconfig>
"""
path_to_dotpolicy = (
"/usr/share/polkit-1/actions/it.weeeopen.peracotta.generate-files.policy"
)
local_path_to_dotpolicy = "./it.weeeopen.peracotta.generate-files.policy"
# path to generate_files.sh in between dquotes
# this should be saved as ./generate_files.pkexec
dotpkexec_content = """#!/bin/bash
[[ $# -eq 0 ]] && OUT="." || OUT="$@"
pkexec "" $(readlink -f "$OUT")
"""
path_to_dotpkexec = "./generate_files.pkexec"
def make_dotfiles(path_to_generate_files_sh: str):
dotpolicy_split = '<annotate key="org.freedesktop.policykit.exec.path">'
dotpkexec_split = 'pkexec "'
dotpolicy_with_path = (
dotpolicy_content.split(dotpolicy_split)[0]
+ dotpolicy_split
+ path_to_generate_files_sh
+ dotpolicy_content.split(dotpolicy_split)[1]
)
dotpkexec_with_path = (
dotpkexec_content.split(dotpkexec_split)[0]
+ dotpkexec_split
+ path_to_generate_files_sh
+ dotpkexec_content.split(dotpkexec_split)[1]
)
# print(dotpolicy_with_path)
# print()
# print(dotpkexec_with_path)
with open(local_path_to_dotpolicy, "w") as f:
f.write(dotpolicy_with_path)
os.system("./scripts/move_pkexec_policy_file.sh")
while not os.path.exists(path_to_dotpolicy):
sleep(0.1)
print(path_to_dotpolicy, "was created!")
with open(path_to_dotpkexec, "w") as f:
f.write(dotpkexec_with_path)
# make file executable -- octal is needed
os.chmod(path_to_dotpkexec, 0o776)
print(path_to_dotpkexec, "was created!")
if __name__ == "__main__":
working_directory = os.getcwd()
if not os.path.isdir(os.path.join(working_directory, "tmp")):
os.makedirs(os.path.join(working_directory, "tmp"))
folder_name = "tmp"
path_to_gen_files_sh = os.path.join(
working_directory, "scripts", "generate_files.sh"
)
print(path_to_gen_files_sh)
make_dotfiles(path_to_generate_files_sh=path_to_gen_files_sh)