-
Notifications
You must be signed in to change notification settings - Fork 5
/
flatpak-curl
executable file
·94 lines (76 loc) · 2.31 KB
/
flatpak-curl
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
#!/usr/bin/env python3
__license__ = 'MIT'
import argparse
import json
import hashlib
import os
import ruamel.yaml
import shutil
import sys
import tempfile
import urllib.request
from collections import OrderedDict
from ruamel.yaml.representer import RoundTripRepresenter
def download(url: str, tempdir: str) -> str:
with urllib.request.urlopen(url) as response:
file_path = os.path.join(tempdir, url.split('/')[-1])
with open(file_path, 'x+b') as tar_file:
shutil.copyfileobj(response, tar_file)
return file_path
def get_file_hash(filename: str) -> str:
sha = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
data = f.read(1024 * 1024 * 32)
if not data:
break
sha.update(data)
return sha.hexdigest()
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument('url')
parser.add_argument(
'-y',
'--yaml',
action='store_true',
help='generate in yaml format',
)
opts = parser.parse_args()
url = opts.url
filename = url.split('/')[-1]
name, extension = os.path.splitext(filename)
name = name.replace('.tar', '')
prefix = 'flatpak-curl-generator'
with tempfile.TemporaryDirectory(prefix=prefix) as tempdir:
filename = download(url, tempdir)
sha256 = get_file_hash(filename)
if extension in ['.gz', '.zip', '.xz', '.bz2']:
file_type = 'archive'
else:
file_type = 'file'
source = OrderedDict(
[('type', file_type), ('url', url), ('sha256', sha256)]
)
module = OrderedDict(
[
('name', name),
('buildsystem', 'simple'),
('build-commands', []),
('sources', [source]),
]
)
if opts.yaml:
print_yaml(module)
else:
print(json.dumps(module, indent=4))
def print_yaml(module: OrderedDict) -> None:
class MyRepresenter(RoundTripRepresenter):
pass
ruamel.yaml.add_representer(OrderedDict, MyRepresenter.represent_dict,
representer=MyRepresenter)
yaml = ruamel.yaml.YAML()
yaml.Representer = MyRepresenter
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.dump(module, sys.stdout)
if __name__ == '__main__':
main()