1beae8164abac0f7c1962a5e40a6f4aae90cdaa7
Hanno Böck add update script

Hanno Böck authored 4 years ago

1) #!/usr/bin/python3
2) 
Hanno Böck sort imports

Hanno Böck authored 5 months ago

3) import io
Hanno Böck add update script

Hanno Böck authored 4 years ago

4) import os
5) import pathlib
6) import sys
7) import tarfile
Hanno Böck sort imports

Hanno Böck authored 5 months ago

8) import urllib.request
Hanno Böck add update script

Hanno Böck authored 4 years ago

9) 
10) DBURL = "https://freewvsdb.schokokeks.org/"
Hanno Böck use black codingstyle

Hanno Böck authored 1 year ago

11) dbpaths = ["/var/lib/freewvs/", str(pathlib.Path.home()) + "/.cache/freewvs/"]
Hanno Böck add update script

Hanno Böck authored 4 years ago

12) 
13) target = False
14) for dbpath in dbpaths:
15)     if not os.path.isdir(dbpath):
16)         try:
17)             os.makedirs(dbpath)
18)         except PermissionError:
19)             continue
20)     if os.access(dbpath, os.W_OK):
21)         target = dbpath
22)         break
23) 
24) if not target:
25)     sys.exit("Can't write to " + " or ".join(dbpaths))
26) 
27) old = 0
28) if os.path.isfile(target + "timestamp"):
Hanno Böck fix new pylint warnings

Hanno Böck authored 2 years ago

29)     with open(target + "timestamp", encoding="ascii") as f:
Hanno Böck add update script

Hanno Böck authored 4 years ago

30)         old = int(f.read())
31) 
32) new = int(urllib.request.urlopen(DBURL + "freewvsdb.timestamp").read())
33) 
34) if new == old:
35)     # nothing to do
36)     sys.exit()
37) 
38) tarball = urllib.request.urlopen(DBURL + str(new) + ".tar.xz").read()
39) 
40) tf = tarfile.open(fileobj=io.BytesIO(tarball))
Hanno Böck Use explicit filter for tar...

Hanno Böck authored 1 year ago

41) if sys.version_info < (3, 11, 4):
42)     tf.extractall(path=target)
43) else:
44)     tf.extractall(path=target, filter="data")
Hanno Böck fix new pylint warnings

Hanno Böck authored 2 years ago

45) with open(target + "timestamp", "w", encoding="ascii") as f: