pyrcc_main.py
5.2 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# Copyright (c) 2021 Riverbank Computing Limited <info@riverbankcomputing.com>
#
# This file is part of PyQt5.
#
# This file may be used under the terms of the GNU General Public License
# version 3.0 as published by the Free Software Foundation and appearing in
# the file LICENSE included in the packaging of this file. Please review the
# following information to ensure the GNU General Public License version 3.0
# requirements will be met: http://www.gnu.org/copyleft/gpl.html.
#
# If you do not wish to use this file under the terms of the GPL version 3.0
# then you may purchase a commercial license. For more information contact
# info@riverbankcomputing.com.
#
# This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
# WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
import sys
from PyQt5.QtCore import PYQT_VERSION_STR, QDir, QFile
from .pyrcc import *
# Initialise the globals.
verbose = False
compressLevel = CONSTANT_COMPRESSLEVEL_DEFAULT
compressThreshold = CONSTANT_COMPRESSTHRESHOLD_DEFAULT
resourceRoot = ''
def processResourceFile(filenamesIn, filenameOut, listFiles):
if verbose:
sys.stderr.write("PyQt5 resource compiler\n")
# Setup.
library = RCCResourceLibrary()
library.setInputFiles(filenamesIn)
library.setVerbose(verbose)
library.setCompressLevel(compressLevel)
library.setCompressThreshold(compressThreshold)
library.setResourceRoot(resourceRoot)
if not library.readFiles():
return False
if filenameOut == '-':
filenameOut = ''
if listFiles:
# Open the output file or use stdout if not specified.
if filenameOut:
try:
out_fd = open(filenameOut, 'w')
except Exception:
sys.stderr.write(
"Unable to open %s for writing\n" % filenameOut)
return False
else:
out_fd = sys.stdout
for df in library.dataFiles():
out_fd.write("%s\n" % QDir.cleanPath(df))
if out_fd is not sys.stdout:
out_fd.close()
return True
return library.output(filenameOut)
def showHelp(error):
sys.stderr.write("PyQt5 resource compiler\n")
if error:
sys.stderr.write("pyrcc5: %s\n" % error)
sys.stderr.write(
"Usage: pyrcc5 [options] <inputs>\n"
"\n"
"Options:\n"
" -o file Write output to file rather than stdout\n"
" -threshold level Threshold to consider compressing files\n"
" -compress level Compress input files by level\n"
" -root path Prefix resource access path with root path\n"
" -no-compress Disable all compression\n"
" -version Display version\n"
" -help Display this information\n")
def main():
# Parse the command line. Note that this mimics the original C++ (warts
# and all) in order to preserve backwards compatibility.
global verbose
global compressLevel
global compressThreshold
global resourceRoot
outFilename = ''
helpRequested = False
listFiles = False
files = []
errorMsg = None
argc = len(sys.argv)
i = 1
while i < argc:
arg = sys.argv[i]
i += 1
if arg[0] == '-':
opt = arg[1:]
if opt == "o":
if i >= argc:
errorMsg = "Missing output name"
break
outFilename = sys.argv[i]
i += 1
elif opt == "root":
if i >= argc:
errorMsg = "Missing root path"
break
resourceRoot = QDir.cleanPath(sys.argv[i])
i += 1
if resourceRoot == '' or resourceRoot[0] != '/':
errorMsg = "Root must start with a /"
break
elif opt == "compress":
if i >= argc:
errorMsg = "Missing compression level"
break
compressLevel = int(sys.argv[i])
i += 1
elif opt == "threshold":
if i >= argc:
errorMsg = "Missing compression threshold"
break
compressThreshold = int(sys.argv[i])
i += 1
elif opt == "verbose":
verbose = True
elif opt == "list":
listFiles = True
elif opt == "version":
sys.stderr.write("pyrcc5 v%s\n" % PYQT_VERSION_STR)
sys.exit(1)
elif opt == "help" or opt == "h":
helpRequested = True
elif opt == "no-compress":
compressLevel = -2
else:
errorMsg = "Unknown option: '%s'" % arg
break
else:
if not QFile.exists(arg):
sys.stderr.write(
"%s: File does not exist '%s'\n" % (sys.argv[0], arg))
sys.exit(1)
files.append(arg)
# Handle any errors or a request for help.
if len(files) == 0 or errorMsg is not None or helpRequested:
showHelp(errorMsg)
sys.exit(1)
if not processResourceFile(files, outFilename, listFiles):
sys.exit(1)
if __name__ == '__main__':
main()