1 #! /usr/bin/env nix-shell
2 #! nix-shell -i python3 -p python3 python3.pkgs.packaging python3.pkgs.requests
11 from packaging
import version
14 logging
.basicConfig(stream
=sys
.stdout
, level
=logging
.DEBUG
)
16 current_path
= pathlib
.Path(__file__
).parent
17 DATA_JSON
= current_path
.joinpath("data.json").resolve()
18 logging
.debug(f
"Path to version file: {DATA_JSON}")
19 last_new_version
= None
21 with
open(DATA_JSON
, "r") as versions_file
:
22 versions
= json
.load(versions_file
)
24 def find_latest_version(arch
):
25 CHECK_URL
= f
'https://apt.enpass.io/dists/stable/main/binary-{arch}/Packages.gz'
26 packages
= gzip
.decompress(requests
.get(CHECK_URL
).content
).decode()
28 # Loop every package to find the newest one!
29 version_selector
= re
.compile("Version: (?P<version>.+)")
30 path_selector
= re
.compile("Filename: (?P<path>.+)")
31 hash_selector
= re
.compile("SHA256: (?P<sha256>.+)")
32 last_version
= version
.parse("0")
33 for package
in packages
.split("\n\n"):
34 matches
= version_selector
.search(package
)
35 matched_version
= matches
.group('version') if matches
and matches
.group('version') else "0"
36 parsed_version
= version
.parse(matched_version
)
37 if parsed_version
> last_version
:
38 path
= path_selector
.search(package
).group('path')
39 sha256
= hash_selector
.search(package
).group('sha256')
40 last_version
= parsed_version
41 return {"path": path
, "sha256": sha256
, "version": matched_version
}
43 for arch
in versions
.keys():
44 current_version
= versions
[arch
]['version']
45 logging
.info(f
"Current Version for {arch} is {current_version}")
46 new_version
= find_latest_version(arch
)
48 if not new_version
or new_version
['version'] == current_version
:
51 last_current_version
= current_version
52 last_new_version
= new_version
53 logging
.info(f
"Update found ({arch}): enpass: {current_version} -> {new_version['version']}")
54 versions
[arch
]['path'] = new_version
['path']
55 versions
[arch
]['sha256'] = new_version
['sha256']
56 versions
[arch
]['version'] = new_version
['version']
59 if not last_new_version
:
60 logging
.info('#### No update found ####')
63 # write new versions back
64 with
open(DATA_JSON
, "w") as versions_file
:
65 json
.dump(versions
, versions_file
, indent
=2)
66 versions_file
.write("\n")
69 logging
.info("Committing changes...")
70 commit_message
= f
"enpass: {last_current_version} -> {last_new_version['version']}"
71 subprocess
.run(['git', 'add', DATA_JSON
], check
=True)
72 subprocess
.run(['git', 'commit', '--file=-'], input=commit_message
.encode(), check
=True)