forked from Xpra-org/xpra-html5
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setup.py
executable file
·582 lines (551 loc) · 22.7 KB
/
setup.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env python3
# This file is part of Xpra.
# Copyright (C) 2017-2022 Antoine Martin <[email protected]>
# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
# later version. See the file LICENSE for details.
import io
import re
import sys
import time
import shutil
import os.path
from subprocess import Popen, PIPE
VERSION = "5.0"
AUTHOR = "Antoine Martin"
AUTHOR_EMAIL = "[email protected]"
# Configuration files that must not be minified or compressed.
# They must be moved to /etc/xpra and symlinked to /usr/share/xpra/www if no
# custom installation directory is set
CONFIGURATION_FILES = ["default-settings.txt", ]
def glob_recurse(srcdir):
m = {}
for root, _, files in os.walk(srcdir):
for f in files:
dirname = root[len(srcdir)+1:]
filename = os.path.join(root, f)
m.setdefault(dirname, []).append(filename)
return m
def get_status_output(*args, **kwargs):
kwargs["stdout"] = PIPE
kwargs["stderr"] = PIPE
try:
p = Popen(*args, **kwargs)
except Exception as e:
print("error running %s,%s: %s" % (args, kwargs, e))
return -1, "", ""
stdout, stderr = p.communicate()
return p.returncode, stdout.decode("utf-8"), stderr.decode("utf-8")
def install_symlink(symlink_options, dst):
for symlink_option in symlink_options:
if symlink_option.find("*"):
import glob
#this is a glob, find at least one match:
matches = glob.glob(symlink_option)
if matches:
symlink_option = matches[0]
else:
continue
if os.path.exists(symlink_option):
print("symlinked %s from %s" % (dst, symlink_option))
if os.path.exists(dst):
os.unlink(dst)
else:
ddir = os.path.dirname(dst)
if not os.path.exists(ddir):
os.makedirs(ddir, mode=0o755)
os.symlink(symlink_option, dst)
return True
#print("no symlinks found for %s from %s" % (dst, symlink_options))
return False
def get_vcs_info():
info = {}
branch = None
for cmd in (
r"git branch --show-current",
#when in detached state, the one above does not work, but this one does:
r"git branch --remote --verbose --no-abbrev --contains | sed -rne 's/^[^\/]*\/([^\ ]+).*$/\1/p' | tail -n 1",
#if all else fails:
r"git branch | grep '* ' | awk '{print $2}'",
):
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
out, _ = proc.communicate()
if proc.returncode==0:
branch_out = out.decode("utf-8").splitlines()
if branch_out:
branch = branch_out[0]
if branch:
break
if not branch:
print("Warning: could not get branch information")
else:
info["BRANCH"] = branch
def get_output_line(cmd):
proc = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
out, _ = proc.communicate()
if proc.returncode!=0:
print("Error: %s returned %s" % (cmd, proc.returncode))
return None
v = out.decode("utf-8").splitlines()[0]
return v
parts = get_output_line("git describe --always --tags").split("-")
#ie: parts = ["v4.0.6", "85", "gf253d3f9d"]
rev = 0
if len(parts)==3:
rev = parts[1]
if branch=="master":
rev = get_output_line("git rev-list --count HEAD --first-parent")
if rev:
try:
rev = int(rev)
except ValueError:
print("invalid revision number %r" % (rev,))
else:
info["REVISION"] = rev
proc = Popen("git status", stdout=PIPE, stderr=PIPE, shell=True)
out, _ = proc.communicate()
changes = 0
if proc.returncode==0:
changes = 0
lines = out.decode('utf-8').splitlines()
for line in lines:
sline = line.strip()
if sline.startswith("modified: ") or sline.startswith("new file:") or sline.startswith("deleted:"):
changes += 1
if changes:
info["LOCAL_MODIFICATIONS"] = changes
return info
def record_vcs_info():
info = get_vcs_info()
if info:
with open("./vcs-info", 'w') as f:
for k,v in info.items():
f.write("%s=%s\n" % (k,v))
#record revision in packaging:
rev = info.get("REVISION")
if rev is not None:
#add full version string to control:
fdata = open("./packaging/debian/control", "r").read()
lines = fdata.splitlines()
for i, line in enumerate(lines):
if line.startswith("Version: "):
lines[i] = line.split("-", 1)[0]+"-r%i-1" % rev
break
lines.append("")
open("./packaging/debian/control", "w").write("\n".join(lines))
#preserve the changelog version, but update the revision:
fdata = open("./packaging/debian/changelog", "r").read()
lines = fdata.splitlines()
changelog_version = re.match(r".*\(([0-9\.]+)\-[r0-9\-]*\).*", lines[0]).group(1)
assert changelog_version, "version not found in changelog first line '%s'" % (lines[0],)
lines[0] = "xpra-html5 (%s-r%s-1) UNRELEASED; urgency=low" % (changelog_version, rev)
lines.append("")
open("./packaging/debian/changelog", "w").write("\n".join(lines))
#ie: %define release 1.r1000.fc34
fdata = open("./packaging/rpm/xpra-html5.spec", "r").read()
lines = fdata.splitlines()
for i, line in enumerate(lines):
if line.startswith("%define release "):
lines[i] = "%%define release 1.r%s%%{?dist}" % rev
break
lines.append("")
open("./packaging/rpm/xpra-html5.spec", "w").write("\n".join(lines))
def load_vcs_info():
info = {}
if os.path.exists("./vcs-info"):
with open("./vcs-info", 'r') as f:
for line in f:
if line.startswith("#"):
continue
parts = line.strip("\n\r").split("=")
if len(parts)==2:
info[parts[0]] = parts[1]
return info
def install_html5(root="/", install_dir="/usr/share/xpra/www/", config_dir="/etc/xpra/html5-client",
configuration_files=CONFIGURATION_FILES,
minifier="uglifyjs",
gzip=True, brotli=True):
print("install_html5%s" % ((root, install_dir, config_dir, configuration_files, minifier, gzip, brotli),))
if minifier not in ("", None, "copy"):
print("minifying html5 client to '%s' using %s" % (install_dir, minifier))
else:
print("copying html5 client to '%s'" % (install_dir, ))
brotli_cmd = None
brotli_version = None
if brotli:
#find brotli on $PATH
paths = os.environ.get("PATH", "").split(os.pathsep)
if os.name=="posix":
#not always present,
#but brotli is often installed there (install from source):
paths.append("/usr/local/bin")
for x in paths:
br = os.path.join(x, "brotli")
if sys.platform.startswith("win"):
br += ".exe"
if os.path.exists(br):
proc = Popen([br, "--version"], stdout=PIPE, stderr=PIPE)
stdout = proc.communicate()[0]
if proc.wait()==0:
brotli_version = stdout.strip(b"\n\r").decode()
brotli_cmd = br
break
print("brotli_cmd=%s" % (brotli_cmd))
if brotli_version:
print(" %s" % (brotli_version))
#those are used to replace the file we ship in source form
#with one that is maintained by the distribution:
symlinks = {
"jquery.js" : [
"/usr/share/javascript/jquery/jquery.js",
"/usr/share/javascript/jquery/latest/jquery.js",
"/usr/share/javascript/jquery/3/jquery.js",
],
"jquery-ui.js" : [
"/usr/share/javascript/jquery-ui/jquery-ui.js",
"/usr/share/javascript/jquery-ui/latest/jquery-ui.js",
"/usr/share/javascript/jquery-ui/3/jquery-ui.js",
],
"materialicons-regular.ttf" : [
"/usr/share/fonts/truetype/material-design-icons-iconfont/MaterialIcons-Regular.ttf",
],
"materialicons-regular.woff" : [
"/usr/share/fonts/woff/material-design-icons-iconfont/MaterialIcons-Regular.woff",
],
"materialicons-regular.woff2" : [
"/usr/share/fonts/woff/material-design-icons-iconfont/MaterialIcons-Regular.woff2",
],
}
for k,files in glob_recurse("html5").items():
if k!="":
k = os.sep+k
for fname in files:
if fname.endswith(".tmp"):
continue
src = os.path.join(os.getcwd(), fname)
parts = fname.split(os.path.sep)
if parts[0]=="html5":
fname = os.path.join(*parts[1:])
if install_dir==".":
install_dir = os.getcwd()
dst = os.path.join(root+install_dir, fname)
if os.path.exists(dst):
os.unlink(dst)
if fname in configuration_files and config_dir and config_dir!=install_dir:
#install configuration files in `config_dir` in root:
cdir = root+config_dir
if not os.path.exists(cdir):
os.makedirs(cdir, 0o755)
config_file = os.path.join(cdir, fname)
shutil.copyfile(src, config_file)
os.chmod(config_file, 0o644)
#and create a symlink from `install_dir`:
#pointing to the config_file without the root:
config_file = os.path.join(config_dir, fname)
os.symlink(config_file, dst)
print("config file symlinked: %s -> %s" % (dst, config_file))
continue
#try to find an existing installed library and symlink it:
symlink_options = symlinks.get(os.path.basename(fname), [])
if install_symlink(symlink_options, dst):
#we've created a symlink, skip minification and compression
continue
ddir = os.path.split(dst)[0]
if ddir and not os.path.exists(ddir):
os.makedirs(ddir, 0o755)
ftype = os.path.splitext(fname)[1].lstrip(".")
bname = os.path.basename(src)
fsrc = src
if ftype=="js" or fname.endswith("index.html"):
#save to a temporary file after replacing strings:
with io.open(src, mode='r', encoding='utf8') as f:
odata = f.read()
data = odata
for regexp, replacewith in {
r"^\s*for\s*\(\s*let\s+" : "for(var ",
r"^\s*let\s+" : "var ",
r"^\s*for\s*\(\s*const\s+" : "for(var ",
r"^\s*const\s+" : "var ",
}.items():
p = re.compile(regexp)
newdata = []
for line in data.splitlines():
newdata.append(p.sub(replacewith, line))
data = "\n".join(newdata)
if data!=odata:
fsrc = src+".tmp"
with io.open(fsrc, "w", encoding='utf8') as f:
f.write(data)
os.chmod(fsrc, 0o644)
if minifier not in ("", None, "copy") and ftype=="js":
if minifier=="uglifyjs":
minify_cmd = ["uglifyjs",
fsrc,
"-o", dst,
"--compress",
]
else:
assert minifier=="yuicompressor"
try:
import yuicompressor # @UnresolvedImport
jar = yuicompressor.get_jar_filename()
java_cmd = os.environ.get("JAVA", "java")
minify_cmd = [java_cmd, "-jar", jar]
except Exception:
minify_cmd = ["yuicompressor"]
minify_cmd += [
fsrc,
"--nomunge",
"--line-break", "400",
"--type", ftype,
"-o", dst,
]
r = get_status_output(minify_cmd)[0]
if r!=0:
print("Error: failed to minify '%s', command %s returned error %i" % (
bname, minify_cmd, r))
shutil.copyfile(fsrc, dst)
os.chmod(dst, 0o644)
print("minified %s" % (fname, ))
else:
print("copied %s" % (fname, ))
shutil.copyfile(fsrc, dst)
os.chmod(dst, 0o644)
if fsrc!=src:
os.unlink(fsrc)
if ftype not in ("png", ) and fname not in configuration_files:
if gzip:
gzip_dst = "%s.gz" % dst
if os.path.exists(gzip_dst):
os.unlink(gzip_dst)
cmd = ["gzip", "-f", "-n", "-9", "-k", dst]
get_status_output(cmd)
if os.path.exists(gzip_dst):
os.chmod(gzip_dst, 0o644)
if brotli and brotli_cmd:
br_dst = "%s.br" % dst
if os.path.exists(br_dst):
os.unlink(br_dst)
if brotli_version and brotli_version>="1":
cmd = [brotli_cmd, "-k", dst]
else:
cmd = [brotli_cmd, "--input", dst, "--output", br_dst, "-q", "11"]
code, out, err = get_status_output(cmd)
if code!=0:
print("brotli error code=%i on %s" % (code, cmd))
if out:
print("stdout=%s" % out)
if err:
print("stderr=%s" % err)
elif os.path.exists(br_dst):
os.chmod(br_dst, 0o644)
else:
print("Warning: brotli did not create '%s'" % br_dst)
if os.name=="posix":
paths = [
"/usr/share/backgrounds/images/default.png",
"/usr/share/backgrounds/images/*default*.png",
"/usr/share/backgrounds/*default*png",
"/usr/share/backgrounds/gnome/adwaita*.jpg", #Debian Stretch
"/usr/share/backgrounds/images/*jpg", #CentOS 7
]
if paths:
extra_symlinks = {"background.png" : paths}
for f, symlink_options in extra_symlinks.items():
dst = os.path.join(root, install_dir, f)
if install_symlink(symlink_options, dst):
break
def set_version(NEW_VERSION):
vcs_info = load_vcs_info() or get_vcs_info()
REVISION = vcs_info.get("REVISION", 0)
LOCAL_MODIFICATIONS = vcs_info.get("LOCAL_MODIFICATIONS", 0)
BRANCH = vcs_info.get("BRANCH", "master")
for filename, replace in {
"./packaging/debian/control" : {
r"Version: %s.*" % VERSION : r"Version: %s-r%s-1" % (NEW_VERSION, REVISION),
},
"./packaging/rpm/xpra-html5.spec" : {
r"%%define version %s" % VERSION : r"%%define version %s" % NEW_VERSION,
r"%%define release .*" : r"%%define release 1.r%s%%{?dist}" % REVISION,
},
"./html5/js/Utilities.js" : {
r'VERSION : "%s"' % VERSION : r'VERSION : "%s"' % NEW_VERSION,
r"REVISION : [0-9]*" : r"REVISION : %s" % REVISION,
r'LOCAL_MODIFICATIONS : [0-9]*' : r'LOCAL_MODIFICATIONS : %s' % LOCAL_MODIFICATIONS,
r'BRANCH : "[a-zA-Z]*"' : r'BRANCH : "%s"' % BRANCH,
},
"./html5/index.html" : {
r"<h3>Version .*</h3>" : "<h3>Version %s</h3>" % NEW_VERSION,
},
"./setup.py" : {
r'VERSION = "%s"' % VERSION : r'VERSION = "%s"' % NEW_VERSION,
},
}.items():
file_sub(filename, replace)
#add changelogs:
from datetime import datetime
now = datetime.now()
deb_date = now.strftime("%a, %d %b %Y %H:%M:%S +0700")
utc_delta = -time.timezone*100//3600
deb_date += " %+04d" % utc_delta
fdata = open("./packaging/debian/changelog", "r").read()
lines = fdata.splitlines()
lines.insert(0, "xpra-html5 (%s-r%s-1) UNRELEASED; urgency=low" % (NEW_VERSION, REVISION))
lines.insert(1, " * TODO")
lines.insert(2, "")
# -- Antoine Martin <[email protected]> Fri, 30 Apr 2021 12:07:59 +0700
lines.insert(3, " -- %s %s %s" % (AUTHOR, AUTHOR_EMAIL, deb_date))
lines.insert(4, "")
lines.append("")
open("./packaging/debian/changelog", "w").write("\n".join(lines))
fdata = open("./packaging/rpm/xpra-html5.spec", "r").read()
lines = fdata.splitlines()
changelog_lineno = lines.index("%changelog")
assert changelog_lineno, "'%changelog' not found!"
rpm_date = now.strftime("%a %b %d %Y")
#* Tue May 04 2021 Antoine Martin <[email protected]> 4.2-1
lines.insert(changelog_lineno+1, "* %s %s <%s> %s-%s-1" % (rpm_date, AUTHOR, AUTHOR_EMAIL, NEW_VERSION, REVISION))
lines.insert(changelog_lineno+2, "- TODO")
lines.insert(changelog_lineno+3, "")
lines.append("")
open("./packaging/rpm/xpra-html5.spec", "w").write("\n".join(lines))
def file_sub(filename, replace):
fdata = open(filename, "r").read()
for old, new in replace.items():
fdata = re.sub(old, new, fdata)
open(filename, "w").write(fdata)
def make_deb():
if os.path.exists("xpra-html5.deb"):
os.unlink("xpra-html5.deb")
root = "./xpra-html5"
if os.path.exists(root):
shutil.rmtree(root)
os.mkdir(root)
shutil.copytree("./packaging/debian", root+"/DEBIAN")
install_html5(root)
# Create debian package
assert Popen(["dpkg-deb", "-Zxz", "--build", "xpra-html5"]).wait()==0
assert os.path.exists("./xpra-html5.deb")
shutil.rmtree(root)
version = ""
with open("./packaging/debian/changelog", "r") as f:
line = f.readline()
#ie: 'xpra-html5 (4.2-r872-1) UNRELEASED; urgency=low'
try:
version = re.match(r".*\(([0-9\.]+\-[r0-9\-]*)\).*", line).group(1)
#ie: VERSION=4.1-1
except Exception:
pass
if not os.path.exists("./dist"):
os.mkdir("./dist")
os.rename("xpra-html5.deb", "./dist/xpra-html5-%s.deb" % version)
def make_rpm():
tarxz = "xpra-html5-%s.tar.xz" % VERSION
disttarxz = os.path.join("dist", tarxz)
if os.path.exists(disttarxz):
os.unlink(disttarxz)
try:
saved = sys.argv
sys.argv = ["./setup.py", "sdist", "--formats=xztar"]
sdist()
finally:
sys.argv = saved
RPMBUILD = os.path.expanduser("~/rpmbuild/")
SOURCES = os.path.join(RPMBUILD, "SOURCES")
if not os.path.exists(SOURCES):
os.makedirs(SOURCES, 0o755)
from shutil import copy2
copy2(disttarxz, SOURCES)
SPEC = "./packaging/rpm/xpra-html5.spec"
proc = Popen(["rpmspec", "-q", "--rpms", SPEC], stdout=PIPE, stderr=PIPE)
out, err = proc.communicate()
print(err.decode())
rpms = []
for line in out.decode().splitlines():
rpms.append(line)
print("building: %s" % (rpms,))
assert Popen(["rpmbuild", "-ba", SPEC]).wait()==0
NOARCH = RPMBUILD+"/RPMS/noarch/"
for rpm in rpms:
try:
os.unlink("./dist/%s.rpm")
except OSError:
pass
rpmfile = "%s.rpm" % rpm
copy2(os.path.join(NOARCH, rpmfile), "./dist")
def sdist():
record_vcs_info()
from distutils.core import setup
setup(name = "xpra-html5",
version = VERSION,
license = "MPL-2",
author = AUTHOR,
author_email = AUTHOR_EMAIL,
url = "https://xpra.org/",
download_url = "https://xpra.org/src/",
description = "HTML5 client for xpra",
)
def main(args):
if len(args)<2 or len(args)>=7:
print("invalid number of arguments, usage:")
print("%s sdist" % (args[0],))
print("%s install [ROOT] [INSTALL_DIR] [MINIFIER]" % (args[0],))
print("%s deb" % (args[0],))
print("%s rpm" % (args[0],))
print("%s set-version VERSION" % (args[0],))
return 1
cmd = args[1]
if cmd=="sdist":
sdist()
return 0
if cmd=="install":
if not load_vcs_info():
try:
record_vcs_info()
except Exception:
print("Warning: src_info is missing")
minifier = "yuicompressor" if sys.platform.startswith("win") else "uglifyjs"
root_dir = ""
install_dir = os.path.normpath(os.path.join(sys.prefix, "share/xpra/www"))
# Platform-dependent configuration file location:
if sys.platform.startswith('freebsd'):
# FreeBSD
config_dir = os.path.normpath(os.path.join(sys.prefix, "etc/xpra/html5-client"))
elif os.name=="posix":
# POSIX but not FreeBSD
config_dir = "/etc/xpra/html5-client"
else:
# windows
config_dir = install_dir
#these default paths can be overriden on the command line:
if len(args)>=3:
root_dir = os.path.normpath(args[2])
if sys.platform.startswith("win") or sys.platform.startswith("darwin") and len(args)==3:
#backwards compatibility with xpra builds that include the html5 client
#on MS Windows and MacOS, everything is installed in the root directory:
install_dir = ""
config_dir = ""
if len(args)>=4:
install_dir = os.path.normpath(args[3])
if len(args)>=5:
config_dir = os.path.normpath(args[4])
if len(args)>=6:
minifier = args[5]
# Perform installation
install_html5(root_dir, install_dir, config_dir, minifier=minifier)
return 0
if cmd=="deb":
make_deb()
return 0
if cmd=="rpm":
make_rpm()
return 0
if cmd=="set-version":
assert len(args)==3, "invalid number of arguments for 'set-version' subcommand"
NEW_VERSION = args[2]
set_version(NEW_VERSION)
#add changelog entries if not present yet?
return 0
print("invalid arguments, use 'sdist' or 'install'")
sys.exit(1)
if __name__ == "__main__":
sys.exit(main(sys.argv))