forked from GoogleContainerTools/distroless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
61 lines (54 loc) · 2.06 KB
/
util.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
import hashlib
import base64
import collections
import tarfile
import os
def sha256_checksum(filename, block_size=65536):
sha256 = hashlib.sha256()
with open(filename, 'rb') as f:
for block in iter(lambda: f.read(block_size), b''):
sha256.update(block)
return sha256.hexdigest()
def package_to_rule(workspace_name, s):
return "@" + workspace_name + "//file:" + encode_package_name(s)
def encode_package_name(s):
return base64.urlsafe_b64encode(s.encode('utf-8')).decode('utf-8') + ".deb"
DEBIAN_RELEASES = {
"buster": "10",
"stretch": "9",
"jessie": "8",
"wheezy": "7",
"squeeze": "6.0",
"lenny": "5.0",
"etch": "4.0",
"sarge": "3.1",
"woody": "3.0",
"potato": "2.2",
}
def generate_os_release(distro, os_release_file):
""" Generates an /etc/os-release like file with information about the
package distribution. VERSION and VERSION_ID are left unset if the package
source is from an unknown debian release.
"""
os_release = collections.OrderedDict([
("PRETTY_NAME", "Distroless"),
("NAME", "Debian GNU/Linux"),
("ID", "debian"),
("VERSION_ID", ""),
("VERSION", ""),
("HOME_URL", "https://github.com/GoogleContainerTools/distroless"),
("SUPPORT_URL", "https://github.com/GoogleContainerTools/distroless/blob/master/README.md"),
("BUG_REPORT_URL", "https://github.com/GoogleContainerTools/distroless/issues/new"),
])
if distro in DEBIAN_RELEASES:
os_release["VERSION_ID"] = DEBIAN_RELEASES[distro]
os_release["VERSION"] = '{0} {1} ({2})'.format(os_release["NAME"], os_release["VERSION_ID"], distro)
for k, val in os_release.items():
if val:
os_release_file.write('{0}=\"{1}\"\n'.format(k, val))
def build_os_release_tar(distro, os_release_file, os_release_path, tar_file_name):
os.makedirs(os_release_path)
with open(os_release_file, 'w') as os_release:
generate_os_release(distro, os_release)
with tarfile.open(tar_file_name, "w") as tar:
tar.add(os_release_file)