-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_linux_snap.py
executable file
·116 lines (94 loc) · 4.25 KB
/
build_linux_snap.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
#!/usr/bin/env python3
import requests
import argparse
import json
import os
import tempfile
import subprocess
download_fwupd_filename = 'fwupd.snap'
repack_fwupd_filename = 'fwupd-repack.snap'
def sendGetRequest(url, headers):
try:
r = requests.get(url, verify=False, headers=headers)
return r
except requests.exceptions.ConnectionError:
print("[-] Failed to establish connection\n")
exit(-1)
def http_get_snap(snap_file: str):
token_header={
'Snap-Device-Series': '16',
'User-Agent': "Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0)"
}
url = 'http://api.snapcraft.io/v2/snaps/info/fwupd'
resp = sendGetRequest(url, headers=token_header)
json_data = json.loads(resp.text.rstrip('\n'))
for item in json_data['channel-map']:
if item['channel']['architecture'] == 'amd64' and \
item['channel']['name'] == 'stable' and \
item['channel']['track'] == 'latest':
url_snap = item['download']['url']
revision = item['revision']
version = item['version']
name = json_data['name']
r = requests.get(url_snap, allow_redirects=True)
open(snap_file, 'wb').write(r.content)
print("[snap] name: %s" % name)
print("[snap] version: %s" % version)
print("[snap] revision: %s" % revision)
print("[snap] url_snap: %s" % url_snap)
def repack_snap_no_warn(snap_file: str):
tmpdir = tempfile.TemporaryDirectory(prefix='fwupd.')
commandFile = os.path.join(tmpdir.name, "fwupd-command")
try:
# extract the snap
subprocess.run(["unsquashfs", "-f", "-d", tmpdir.name, snap_file], check=True)
# file present?
if not os.path.isfile(commandFile):
raise ValueError("file not found: fwupd-command")
# insert environment variant
with open(commandFile,'r+') as fd:
contents = fd.readlines()
line_str = "exec \"$@\""
line_insert = "export FWUPD_SUPPORTED=1\n"
for index, line in enumerate(contents):
if line_str in line:
contents.insert(index, line_insert)
break
fd.seek(0)
fd.writelines(contents)
# wrap up the new one
new_file = os.path.join(os.getcwd(), repack_fwupd_filename)
print("[Info] wrap up source directory %s" % tmpdir.name)
subprocess.run(["snap",
"pack",
"--filename",
repack_fwupd_filename,
tmpdir.name,
os.getcwd()],
check=True)
if not os.path.isfile(new_file):
raise ValueError("file not found: %s" % new_file)
except subprocess.CalledProcessError as e:
print("[Error] %d, %s" % (e.returncode, e.stderr))
except ValueError as e:
print("[Error] ", e)
def main(argv):
parser = argparse.ArgumentParser()
parser.add_argument('--snap', help="Use existing snap rather latest/stable")
args = parser.parse_args()
snap_download_file = os.path.join(os.getcwd(), download_fwupd_filename)
snap_new_file = os.path.join(os.getcwd(), repack_fwupd_filename)
if os.path.isfile(snap_new_file):
os.remove(snap_new_file)
if args.snap and os.path.isfile(args.snap):
snap_download_file = args.snap
else:
if os.path.isfile(snap_download_file):
os.remove(snap_download_file)
http_get_snap(snap_download_file)
repack_snap_no_warn(snap_download_file)
return True
## Entry Point ##
if __name__ == '__main__':
import sys
main(sys.argv)