forked from maqmigh/package-jetbrains-ide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
package.py
executable file
·367 lines (318 loc) · 16.4 KB
/
package.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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import argparse
from urllib.error import URLError
import util
import sys
import os
import urllib.request
import re
import json
__author__ = 'Andreas Bader'
__version__ = '0.02'
newVersionURL = "https://data.services.jetbrains.com/products/releases?code=%s&latest=true&type=release"
# format: key = name
# list of [VersionVarNamesDict, VersionRegex]
# VersionRegex greps the regex out of version.js
supportedIDEs = {"pycharm": [{"community": "PCC", "professional": "PCP"},
"[0-9]+\.[0-9]+(\.[0-9]+){0,1}"],
"idea": [{"community": "IIC", "professional": "IIU"},
"[0-9]+\.[0-9]+(\.[0-9]+){0,1}"]
}
supportedEditions = ['community', 'professional']
def cleanup(code, log):
if util.check_folder(os.path.join(script_path, "tmp"), logger, False, False):
if not util.delete_folder(os.path.join(script_path, "tmp"), logger, True):
log.error("%s does exist and can not be deleted." % os.path.join(script_path, "tmp"))
sys.exit(-1)
sys.exit(code)
def get_download_link(varnames, edition, log, embeddedjava):
varname = varnames[edition]
try:
response = urllib.request.urlopen(newVersionURL % varname, timeout=10)
except URLError:
log.error("Error while opening %s. Error was '%s'." % (newVersionURL % varname, sys.exc_info()[0]))
return None
try:
content = response.read().decode('utf-8')
except UnicodeDecodeError:
log.error("Error while retrieving %s. Error was '%s'." % (newVersionURL % varname, sys.exc_info()[0]))
return None
if response is not None and response.status == 200:
try:
parsedjson = json.loads(content)
except ValueError:
log.error("Error while parsing json from %s. Error was '%s'." %
(newVersionURL % varname, sys.exc_info()[0]))
return None
linuxKey = 'linux'
if not embeddedjava:
linuxKey = 'linuxWithoutJDK'
if varname in parsedjson.keys():
if len(parsedjson[varname]) > 0:
if "downloads" in parsedjson[varname][0].keys():
if linuxKey in parsedjson[varname][0]["downloads"].keys():
if "link" in parsedjson[varname][0]["downloads"][linuxKey].keys():
return parsedjson[varname][0]["downloads"][linuxKey]["link"]
else:
log.error("Error while parsing '%s': No 'link' in dictionary." % newVersionURL % varname)
else:
log.error("Error while parsing '%s': No '%' in dictionary." % newVersionURL % varname, linuxKey)
else:
log.error("Error while parsing '%s': No 'downloads' in dictionary." % newVersionURL % varname)
else:
log.error("Error while parsing '%s': No entries in list." % newVersionURL % varname)
else:
log.error("Error while parsing '%s': No '%s' in dictionary." % (newVersionURL % varname, varname))
return None
def fix_vm_options(build_root, ide, appname, bits=""):
# Fixing vmoptions file(s)
file1 = open(os.path.join(build_root, "etc", args.ide, "%s.vmoptions.README" % appname), "a")
file2 = open(os.path.join(build_root, "usr", "share", "jetbrains", appname, "bin", "%s64.vmoptions" % args.ide),
"r")
file3 = open(os.path.join(build_root, "usr", "share", "jetbrains", appname, "bin", "%s.vmoptions2" % args.ide), "w")
file1.write("\nOriginal pycharm.vmoptions:\n")
for line in file2:
file1.write(line)
if "yjpagent" not in line:
file3.write(line)
file1.close()
file2.close()
file3.close()
fullpath = os.path.join(build_root, "usr", "share", "jetbrains", appname, "bin",
"%s64.vmoptions" % args.ide)
if not util.delete_file(fullpath, logger):
logger.error("Error while deleting '%s'." % fullpath)
cleanup(-1, logger)
p1 = os.path.join(build_root, "usr", "share", "jetbrains", appname, "bin",
"%s.vmoptions2" % args.ide)
p2 = os.path.join(build_root, "usr", "share",
"jetbrains", appname, "bin",
"%s64.vmoptions" % args.ide)
if not util.copy_file(p1, p2, logger):
logger.error("Error while copying '{} to '{}'.".format(p1, p2))
cleanup(-1, logger)
fullpath = os.path.join(build_root, "usr", "share", "jetbrains",
appname, "bin",
"%s.vmoptions2" % args.ide)
if not util.delete_file(fullpath, logger):
logger.error(fullpath)
cleanup(-1, logger)
# Configure ArgumentParser
parser = argparse.ArgumentParser(prog="package.py", epilog="Supported IDEs: %s\nSupported Editions: %s"
% (list(supportedIDEs.keys()), supportedEditions),
description="Packages Jetbrains IDEs for Debian/Ubuntu.",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-e", "--edition", metavar="EDITION", default=supportedEditions[0],
choices=supportedEditions, help="Which Edition should be packaged?")
parser.add_argument("-i", "--ide", metavar="IDE", choices=supportedIDEs.keys(), default=list(supportedIDEs.keys())[0],
help="Which IDE should be packaged?")
parser.add_argument("-j", "--java", metavar="JAVA", choices={'y', 'n'}, default='y',
help="Which IDE should be packaged?")
parser.add_argument("-l", "--list", action='store_true', help="list all supported IDEs")
parser.add_argument("-c", "--check", action='store_true',
help="check if installed version is older than the newest version available (needs dpkg)")
parser.add_argument('-v', '--version', action='version', version='%(prog)s ' + __version__)
parser.add_argument('-n', '--no-download', action='store_true', help="skip downloading - assumes already downloaded")
args = parser.parse_args()
# Configure Logging
logLevel = logging.WARN
logging.basicConfig(level=logLevel)
logger = logging.getLogger(__name__)
if args.list:
print("Supported JetBrains IDEs:")
for key in supportedIDEs.keys():
print(key)
sys.exit(0)
# Checking tools
for tool in ["tar", "dpkg", "fakeroot", "dpkg-deb"]:
if not util.cmd_exists(tool):
logger.error("%s not found or not usable." % tool)
sys.exit(-1)
# Get URL
link = get_download_link(supportedIDEs[args.ide][0], args.edition, logger, args.java != 'n')
if link is None:
logger.error("Could not get url for %s." % args.ide)
sys.exit(-1)
version = re.search(supportedIDEs[args.ide][1], link.split("/")[-1])
if version is None:
logger.error("Could not parse version out of '%s'." % link.split("/")[-1])
sys.exit(-1)
if args.check:
result = util.run_cmd("dpkg -l | grep '%s' | grep -E -o '%s' | cat" %
(args.ide, supportedIDEs[args.ide][1]), logger, True, True).decode('utf-8').replace("\n", "")
if result is None:
logger.error("Error while running '%s'." % "dpkg -l | grep '%s' | grep -E -o '%s'" %
(args.ide, supportedIDEs[args.ide][1]))
sys.exit(-1)
if result != version.group() and result != "":
print("There is a newer version (%s) than installed (%s) available!" % (version.group(), result))
sys.exit(1)
if result == "":
print("%s %s is not installed." % (args.ide, args.edition))
sys.exit(0)
appname = "{}-{}".format(args.ide, args.edition)
script_path = util.get_script_path()
build_root = os.path.join(script_path, "tmp", "root")
# Checking folders
if not util.check_folder(os.path.join(script_path, "output"), logger, False, True):
if not util.create_folder(os.path.join(script_path, "output")):
logger.error("%s does not exist and can not be created." % os.path.join(script_path, "output"))
sys.exit(-1)
if not args.no_download:
if util.check_folder(os.path.join(script_path, "tmp"), logger, False, True):
if not util.delete_folder(os.path.join(script_path, "tmp"), logger, True):
logger.error("%s does exist and can not be deleted." % os.path.join(script_path, "tmp"))
sys.exit(-1)
for folder in [os.path.join(script_path, "tmp"),
os.path.join(build_root, "usr", "share", "jetbrains", appname),
os.path.join(build_root, "usr", "share", "applications"),
os.path.join(build_root, "usr", "bin"),
os.path.join(build_root, "etc", args.ide),
os.path.join(build_root, "etc", "sysctl.d"),
os.path.join(build_root, "DEBIAN")]:
# if not util.create_folder(folder):
if not os.path.exists(folder): # and not os.makedirs(folder):
os.makedirs(folder)
# logger.error("%s cannot be created." % folder)
# sys.exit(-1)
if not util.check_folder(os.path.join(script_path, "data"), logger, False, False):
cleanup(-1, logger)
if not util.check_folder(os.path.join(script_path, "data", args.ide), logger, False, False):
cleanup(-1, logger)
if not util.check_folder(os.path.join(script_path, "data", args.ide, "debian"), logger, False, False):
cleanup(-1, logger)
# Checking files
for file in ["control.in", "postinst", "sysctl-99.conf"]:
if not util.check_file_exists(os.path.join(script_path, "data", args.ide, "debian", file)) and not \
util.check_file_readable(os.path.join(script_path, "data", args.ide, "debian", file)):
logger.error("%s does not exist or is not readable." % file)
cleanup(-1, logger)
for file in ["LICENSE", "Makefile", "pkginfo.in", "prototype.in", "icon.desktop", "start.sh", "vmoptions.README"]:
if not util.check_file_exists(os.path.join(script_path, "data", args.ide, file)) and not \
util.check_file_readable(os.path.join(script_path, "data", args.ide, file)):
logger.error("%s does not exist or is not readable." % file)
cleanup(-1, logger)
# Download URL
if not args.no_download:
if util.check_file_exists(os.path.join(script_path, "tmp", link.split("/")[-1])):
if not util.delete_file(os.path.join(script_path, "tmp", link.split("/")[-1]), logger, False):
cleanup(-1, logger)
if not args.no_download:
resp = urllib.request.urlretrieve(link, os.path.join(script_path, "tmp", link.split("/")[-1]), util.progress_hook)
if resp is None or resp[1]["Connection"] != "close" or int(resp[1]["Content-Length"]) < 100000:
logger.error("Error while downloading '%s'." % os.path.join(script_path, "tmp", link.split("/")[-1]))
cleanup(-1, logger)
if not util.run_cmd("tar --strip-components 1 -C %s -zxf %s" %
(os.path.join(build_root, "usr", "share", "jetbrains", appname),
os.path.join(script_path, "tmp", link.split("/")[-1])), logger, False):
logger.error("Error while unpacking '%s' to '%s'." %
(os.path.join(script_path, "tmp", link.split("/")[-1])),
os.path.join(build_root, "usr", "share", "jetbrains", appname))
cleanup(-1, logger)
# Copy Files
copyList = [
[os.path.join(script_path, "data", args.ide, "vmoptions.README"),
os.path.join(build_root, "etc", args.ide, "%s.vmoptions.README" % appname)],
[os.path.join(script_path, "data", args.ide, "debian", "sysctl-99.conf"),
os.path.join(build_root, "etc", "sysctl.d", "99-%s.conf" % appname)],
]
for copyTuple in copyList:
if not util.copy_file(copyTuple[0], copyTuple[1], logger):
cleanup(-1, logger)
fix_vm_options(build_root, args.ide, appname, bits="")
# Copy files that needed fixes (inserts ide name etc.)
copyList = [
[os.path.join(script_path, "data", args.ide, "icon.desktop"),
os.path.join(build_root, "usr", "share",
"applications", "%s.desktop" % appname)],
[os.path.join(script_path, "data", args.ide, "start.sh"),
os.path.join(build_root, "usr", "bin", appname)],
[os.path.join(script_path, "data", args.ide, "debian", "postinst"),
os.path.join(build_root, "DEBIAN", "postinst")],
[os.path.join(script_path, "data", args.ide, "debian", "templates"),
os.path.join(build_root, "DEBIAN", "templates")],
[os.path.join(script_path, "data", args.ide, "debian", "control.in"),
os.path.join(build_root, "DEBIAN", "control")]
]
for copyTuple in copyList:
# Check is destination exists
if util.check_file_exists(copyTuple[1]):
if not util.delete_file(copyTuple[1], logger, False):
cleanup(-1, logger)
file1 = open(copyTuple[0], "r")
file2 = open(copyTuple[1], "w")
otherEdition = 'community'
oldEdition = 'iu'
otherOldEdition = 'ic'
if args.edition == otherEdition:
otherEdition = 'professional'
oldEdition = "ic"
otherOldEdition = 'iu'
for line in file1:
file2.write(
line.replace("OTHER_EDITION2", otherEdition.upper())
.replace("OTHER_EDITION", otherEdition)
.replace("VERSION", version.group())
.replace("EDITION2", args.edition.upper())
.replace("EDITION", args.edition)
.replace("OLD1", oldEdition)
.replace("OLD2", oldEdition.upper())
.replace("OLD3", otherOldEdition)
.replace("OLD4", otherOldEdition.upper())
.replace("APPNAME", appname)
)
file1.close()
file2.close()
# Chmod Start Script and sysctl
for file in [os.path.join(build_root, "usr", "bin", appname),
os.path.join(build_root, "etc", "sysctl.d", "99-%s.conf" % appname),
os.path.join(build_root, "DEBIAN", "postinst")]:
if not util.run_cmd("chmod +rx %s" % file, logger, False):
logger.error("Error while running chmod +rx on '%s'." % file)
cleanup(-1, logger)
if util.check_file_exists(os.path.join(script_path, "tmp", "fakeroot.save")):
if not util.delete_file(os.path.join(script_path, "tmp", "fakeroot.save"), logger, False):
cleanup(-1, logger)
file1 = open(os.path.join(script_path, "tmp", "fakeroot.save"), "w")
file1.write("")
file1.close()
# package it!
cmd = "fakeroot -i %s -s %s -- chown -R root:root %s" % (os.path.join(script_path, "tmp", "fakeroot.save"),
os.path.join(script_path, "tmp", "fakeroot.save"),
os.path.join(build_root))
if not util.run_cmd(cmd, logger, False):
logger.error("Error while exexuting '%s'." % cmd)
cleanup(-1, logger)
cmd = "fakeroot -i %s -s %s -- dpkg-deb -b %s %s" % (os.path.join(script_path, "tmp", "fakeroot.save"),
os.path.join(script_path, "tmp", "fakeroot.save"),
os.path.join(build_root),
os.path.join(script_path,
"tmp", "%s-%s-%s.deb"
% (args.ide, args.edition, version.group())))
if not util.run_cmd(cmd, logger, False):
logger.error("Error while exexuting '%s'." % cmd)
cleanup(-1, logger)
# copy package
if not util.check_file_exists(
os.path.join(script_path, "tmp", "%s-%s-%s.deb" % (args.ide, args.edition, version.group()))):
logger.error("Error '%s' was not created." %
os.path.join(script_path,
"tmp", "%s-%s-%s.deb" % (args.ide, args.edition, version.group())))
cleanup(-1, logger)
if not util.copy_file(
os.path.join(script_path, "tmp", "%s-%s-%s.deb" % (args.ide, args.edition, version.group())),
os.path.join(script_path, "output", "%s-%s-%s.deb" % (args.ide, args.edition, version.group())),
logger):
cleanup(-1, logger)
# cleanup
# if util.check_file_exists(os.path.join(script_path, "tmp", "fakeroot.save")):
# if not util.delete_file(os.path.join(script_path, "tmp", "fakeroot.save"), logger, False):
# cleanup(-1, logger)
print("Finished packaging %s to %s.\n Install now with:\ndpkg -i %s"
% (args.ide,
os.path.join(script_path, "output", "%s-%s-%s.deb" % (args.ide, args.edition, version.group())),
os.path.join(script_path, "output", "%s-%s-%s.deb" % (args.ide, args.edition, version.group()))))
cleanup(0, logger)
sys.exit(0)