-
Notifications
You must be signed in to change notification settings - Fork 3
/
buildplugin
executable file
·140 lines (120 loc) · 4.99 KB
/
buildplugin
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:ts=4:sw=4:softtabstop=4:smarttab:expandtab
from __future__ import unicode_literals, division, absolute_import, print_function
import os
import sys
import re
import subprocess
import shutil
import inspect
import zipfile
import optparse
SCRIPT_DIR = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))
PLUGIN_NAME = 'TagMechanic'
TEMP_DIR = os.path.join(SCRIPT_DIR, PLUGIN_NAME)
TRANS_NAME = 'translations'
TRANS_SRC = os.path.join(SCRIPT_DIR, TRANS_NAME)
TRANS_DEST = os.path.join(SCRIPT_DIR, PLUGIN_NAME, TRANS_NAME)
PLUGIN_FILES = ['dialogs.py',
'parsing_engine.py',
'plugin.py',
'plugin.xml',
'plugin_utils.py',
'utilities.py',
'config.svg',
'plugin.svg',
'plugin.png',]
def findVersion():
_version_pattern = re.compile(r'<version>([^<]*)</version>')
with open('plugin.xml', 'r') as fd:
data = fd.read()
match = re.search(_version_pattern, data)
if match is not None:
return '{}'.format(match.group(1))
return '0.X.X'
# Find version info from plugin.xml and build zip file name from it
VERS_INFO = findVersion()
ARCHIVE_NAME = os.path.join(SCRIPT_DIR, '{}_v{}.zip'.format(PLUGIN_NAME, VERS_INFO))
# recursive zip creation support routine
def zipUpDir(myzip, tdir, localname):
currentdir = tdir
if localname != "":
currentdir = os.path.join(currentdir,localname)
dir_contents = os.listdir(currentdir)
for entry in dir_contents:
afilename = entry
localfilePath = os.path.join(localname, afilename)
realfilePath = os.path.join(currentdir, entry)
if os.path.isfile(realfilePath):
myzip.write(realfilePath, localfilePath, zipfile.ZIP_DEFLATED)
elif os.path.isdir(realfilePath):
zipUpDir(myzip, tdir, localfilePath)
def removePreviousTmp(rmzip=False):
# Remove temp folder and contents if it exists
if os.path.exists(TEMP_DIR) and os.path.isdir(TEMP_DIR):
shutil.rmtree(TEMP_DIR)
if rmzip: # Remove zip file if indicated.
print('Removing any current zip file ...')
if os.path.exists(ARCHIVE_NAME):
os.remove(ARCHIVE_NAME)
def ignore_in_dirs(base, items, ignored_dirs=None):
ans = []
if ignored_dirs is None:
ignored_dirs = {'.git', '__pycache__'}
for name in items:
path = os.path.join(base, name)
if os.path.isdir(path):
if name in ignored_dirs:
ans.append(name)
else:
if name.rpartition('.')[-1] in ('pyc', 'pyo'):
ans.append(name)
return ans
if __name__ == "__main__":
parser = optparse.OptionParser()
parser.add_option('-l', '--language',
dest="compile_lang",
default=False,
action="store_true")
options, args = parser.parse_args()
print('Removing any previous build leftovers ...')
removePreviousTmp(rmzip=True)
print('Creating temp {} directory ...'.format(PLUGIN_NAME))
os.mkdir(TEMP_DIR)
print('Copying everything to temp {} directory ...'.format(PLUGIN_NAME))
for entry in PLUGIN_FILES:
entry_path = os.path.join(SCRIPT_DIR, entry)
if os.path.exists(entry_path) and os.path.isdir(entry_path):
shutil.copytree(entry_path, os.path.join(TEMP_DIR, entry), ignore=ignore_in_dirs)
elif os.path.exists(entry_path) and os.path.isfile(entry_path):
shutil.copy2(entry_path, os.path.join(TEMP_DIR, entry))
else:
sys.exit('Couldn\'t copy necessary plugin files!')
os.mkdir(TRANS_DEST)
if options.compile_lang:
if os.path.exists(TRANS_SRC) and os.path.isdir(TRANS_SRC):
for name in os.listdir(TRANS_SRC):
if name !='template.ts' and name.rpartition('.')[-1] == ('ts'):
ts_name = os.path.join(TRANS_SRC, name)
qm_name = '{}.{}'.format(os.path.join(TRANS_DEST, os.path.splitext(name)[0]), 'qm')
command = ['lrelease',
ts_name,
'-qm',
qm_name
]
print('Subprocess command', command)
print('Compiling {} with lrelease'.format(name))
# proc = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
try:
proc = subprocess.run(command, capture_output=True, text=True)
print("stdout:", proc.stdout)
except Exception:
print("stderr:", proc.stderr)
print('Creating {} ...'.format(os.path.basename(ARCHIVE_NAME)))
outzip = zipfile.ZipFile(ARCHIVE_NAME, 'w')
zipUpDir(outzip, SCRIPT_DIR, os.path.basename(TEMP_DIR))
outzip.close()
print('Plugin successfully created!')
print('Removing temp build directory ...')
removePreviousTmp()