Coverage for appr/pack.py: 33%

# Coverage for appr/pack.py : 33%

## 115 statements 38 run 77 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | from __future__ import absolute_import, division, print_function import base64 import fnmatch import glob import gzip import hashlib import io import os import tarfile AUTHORIZED_FILES = [ "*.libsonnet", "*.libjsonnet", "*.jsonnet", "*.yaml", "README.md", "LICENSE", "AUTHORS", "NOTICE", "manifests", "deps/*.kub" ] AUTHORIZED_TEMPLATES = ["*.yaml", "*.jsonnet", "*.libjsonnet", "*.yml", "*.j2", "*.libsonnet"] def authorized_files(): files = [] for name in AUTHORIZED_FILES: for filename in glob.glob(name): files.append(filename) for root, _, filenames in os.walk('templates'): for name in AUTHORIZED_TEMPLATES: for filename in fnmatch.filter(filenames, name): files.append(os.path.join(root, filename)) return files def all_files(): files = [] ignore_files = [] for filename in ['.helmignore', '.apprignore', '.kpmignore']: if os.path.exists(filename): with open(filename, 'r') as f: ignore_files = ignore_files + f.read().split("\n") for root, _, filenames in os.walk('.'): for filename in filenames: path = os.path.join(root, filename) skip = False for ignore_pattern in ignore_files: if fnmatch.fnmatch(path, ignore_pattern): skip = True break if not skip: files.append(path.replace("./", "")) return files def pack_kub(kub, filter_files=True, prefix=None): tar = tarfile.open(kub, "w:gz") if filter_files: files = authorized_files() else: files = all_files() for filepath in files: arcname = None if prefix: arcname = os.path.join(prefix, filepath) tar.add(filepath, arcname=arcname) tar.close() def unpack_kub(kub, dest="."): tar = tarfile.open(kub, "r:gz") tar.extractall(dest) tar.close() class ApprPackage(object): def __init__(self, blob=None, b64_encoded=True, skip_prefix=None): self.files = {} self.tar = None self.blob = None self.io_file = None self._digest = None self._size = None self.b64blob = None if blob is not None: self.load(blob, b64_encoded, skip_prefix) def _load_blob(self, blob, b64_encoded): if b64_encoded: self.b64blob = blob self.blob = base64.b64decode(blob) else: self.b64blob = base64.b64encode(blob) self.blob = blob def load(self, blob, b64_encoded=True, skip_prefix=None): self._digest = None self._load_blob(blob, b64_encoded) self.io_file = io.BytesIO(self.blob) self.tar = tarfile.open(fileobj=self.io_file, mode='r:gz') for member in self.tar.getmembers(): tfile = self.tar.extractfile(member) if tfile is not None: name = tfile.name if skip_prefix: name = name.replace(skip_prefix, '') self.files[name] = tfile.read() def extract(self, dest): self.tar.extractall(dest) def pack(self, dest): with open(dest, "wb") as destfile: destfile.write(self.blob) def tree(self, directory=None): files = list(self.files.keys()) files.sort() if directory is not None: filtered = [x for x in files if x.startswith(directory)] else: filtered = files return filtered def file(self, filename): return self.files[filename] @property def manifest(self): manifests_files = ["manifest.yaml", "manifest.jsonnet", "Chart.yaml", "Chart.yml"] for filename in manifests_files: if filename in self.files: return self.files[filename] raise RuntimeError("Unknown manifest format") @property def size(self): if self._size is None: self.io_file.seek(0, os.SEEK_END) self._size = self.io_file.tell() return self._size @property def digest(self): if self._digest is None: self.io_file.seek(0) gunzip = gzip.GzipFile(fileobj=self.io_file, mode='r').read() self._digest = hashlib.sha256(gunzip).hexdigest() self.io_file.seek(0) return self._digest