-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_python_scripts_shortcuts.py
executable file
·119 lines (83 loc) · 2.8 KB
/
create_python_scripts_shortcuts.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python3.4
"""
Creates shell-accessible command line shortcuts for applications that have a meta-tag.
# TODO
- Merge with install.py?
"""
import os
import re
import sys
import dcore.search_files as fsearch
import dcore.data as data
import dcore.utils as dutils
shell_meta_search = '_meta_shell_command'
_meta_shell_command = 'generate_shortcuts'
def isOK(file):
if file.find('__') != -1: return False
dirname = os.path.dirname(file)
dirs = dirname.split('/')
for d in dirs:
if d == 'deprecated':
return False
return True
def getRoots():
return [data.dcoreRoot(), data.dcoreExtRoot()]
def findPyFiles():
scriptFolders = getRoots()
P = []
for scriptFolder in scriptFolders:
pyfiles = fsearch.getAllFilesRecursively('*.py', scriptFolder)
P += [pyfile for pyfile in pyfiles if isOK(pyfile)]
return P
def getAutogenFileTemplate():
file_template = r"""
# Automatically created by '%s', do not modify.
# Tag for easy deletion: %s
__custom__ """ % (os.path.normpath(__file__), data.tagShortcutsForDeletion())
return file_template
def getMetadataFromPyFiles(pyfiles):
"""
meta: (python file, shell command, special flags)
Improve: meta[0, 1 or 2] is confusing. Just create a class with names entities.
"""
meta = []
for file in pyfiles:
fh = open(file, 'r')
lines = fh.readlines()
fh.close()
for line in lines:
m = re.search(shell_meta_search + ".*=.*'(.*)'", line)
if m is not None:
command = m.groups(0)[0]
if len(command.split(' ')) > 1:
laucher = command.split(' ')[0]
args = " ".join(command.split(' ')[1:])
else:
laucher = command
args = ''
meta.append((file, laucher, args))
return meta
def createShortcuts(lMeta):
file_template = getAutogenFileTemplate()
scriptsOutputFolder = data.pathExt()
for meta in lMeta:
fileContent = file_template
fileContent = fileContent.replace('__py_file__', meta[0])
fileContent = fileContent.replace('__opt_cmd__', meta[2])
fileContent = fileContent.replace('__custom__',
'python3 %s $*' % meta[0])
fileOut = scriptsOutputFolder + "/" + meta[1]
fileOut = os.path.normpath(fileOut)
print((meta, fileOut))
fh = open(fileOut, 'w')
fh.write(fileContent)
fh.close()
cmd = 'chmod +x "%s"' % fileOut
assert os.system(cmd) == 0
def do():
pyFiles = findPyFiles()
dutils.delCurrentShortcuts(data.tagShortcutsForDeletion())
meta = getMetadataFromPyFiles(pyFiles)
createShortcuts(meta)
if __name__ == "__main__":
do()