|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Licensed to Cloudera, Inc. under one |
| 4 | +# or more contributor license agreements. See the NOTICE file |
| 5 | +# distributed with this work for additional information |
| 6 | +# regarding copyright ownership. Cloudera, Inc. licenses this file |
| 7 | +# to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance |
| 9 | +# with the License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +# See the License for the specific language governing permissions and |
| 17 | +# limitations under the License. |
| 18 | + |
| 19 | +# This program creates a manifest.json file from a directory of parcels and |
| 20 | +# places the file in the same directory as the parcels. |
| 21 | +# Once created, the directory can be served over http as a parcel repository. |
| 22 | + |
| 23 | +import hashlib |
| 24 | +import json |
| 25 | +import os |
| 26 | +import re |
| 27 | +import sys |
| 28 | +import tarfile |
| 29 | +import time |
| 30 | + |
| 31 | +def _get_parcel_dirname(parcel_name): |
| 32 | + """ |
| 33 | + Extract the required parcel directory name for a given parcel. |
| 34 | +
|
| 35 | + eg: CDH-5.0.0-el6.parcel -> CDH-5.0.0 |
| 36 | + """ |
| 37 | + parts = re.match(r"^(.*?)-(.*)-(.*?)$", parcel_name).groups() |
| 38 | + return parts[0] + '-' + parts[1] |
| 39 | + |
| 40 | +def _safe_copy(key, src, dest): |
| 41 | + """ |
| 42 | + Conditionally copy a key/value pair from one dictionary to another. |
| 43 | +
|
| 44 | + Nothing is done if the key is not present in the source dictionary |
| 45 | + """ |
| 46 | + if key in src: |
| 47 | + dest[key] = src[key] |
| 48 | + |
| 49 | +def make_manifest(path, timestamp=time.time()): |
| 50 | + """ |
| 51 | + Make a manifest.json document from the contents of a directory. |
| 52 | +
|
| 53 | + This function will scan the specified directory, identify any parcel files |
| 54 | + in it, and then build a manifest from those files. Certain metadata will be |
| 55 | + extracted from the parcel and copied into the manifest. |
| 56 | +
|
| 57 | + @param path: The path of the directory to scan for parcels |
| 58 | + @param timestamp: Unix timestamp to place in manifest.json |
| 59 | + @return: the manifest.json as a string |
| 60 | + """ |
| 61 | + manifest = {} |
| 62 | + manifest['lastUpdated'] = int(timestamp * 1000) |
| 63 | + manifest['parcels'] = [] |
| 64 | + |
| 65 | + files = os.listdir(path) |
| 66 | + for f in files: |
| 67 | + if not f.endswith('.parcel'): |
| 68 | + continue |
| 69 | + |
| 70 | + print("Found parcel %s" % (f,)) |
| 71 | + entry = {} |
| 72 | + entry['parcelName'] = f |
| 73 | + |
| 74 | + fullpath = os.path.join(path, f) |
| 75 | + |
| 76 | + with open(fullpath, 'rb') as fp: |
| 77 | + entry['hash'] = hashlib.sha1(fp.read()).hexdigest() |
| 78 | + |
| 79 | + with tarfile.open(fullpath, 'r') as tar: |
| 80 | + try: |
| 81 | + json_member = tar.getmember(os.path.join(_get_parcel_dirname(f), |
| 82 | + 'meta', 'parcel.json')) |
| 83 | + except KeyError: |
| 84 | + print("Parcel does not contain parcel.json") |
| 85 | + continue |
| 86 | + try: |
| 87 | + parcel = json.loads(tar.extractfile(json_member).read().decode(encoding='UTF-8')) |
| 88 | + except: |
| 89 | + print("Failed to parse parcel.json") |
| 90 | + continue |
| 91 | + _safe_copy('depends', parcel, entry) |
| 92 | + _safe_copy('replaces', parcel, entry) |
| 93 | + _safe_copy('conflicts', parcel, entry) |
| 94 | + _safe_copy('components', parcel, entry) |
| 95 | + _safe_copy('servicesRestartInfo', parcel, entry) |
| 96 | + |
| 97 | + try: |
| 98 | + notes_member = tar.getmember(os.path.join(_get_parcel_dirname(f), |
| 99 | + 'meta', 'release-notes.txt')) |
| 100 | + entry['releaseNotes'] = tar.extractfile(notes_member).read().decode(encoding='UTF-8') |
| 101 | + except KeyError: |
| 102 | + # No problem if there's no release notes |
| 103 | + pass |
| 104 | + |
| 105 | + manifest['parcels'].append(entry) |
| 106 | + |
| 107 | + return json.dumps(manifest, indent=4, separators=(',', ': ')) |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + path = os.path.curdir |
| 111 | + if len(sys.argv) > 1: |
| 112 | + path = sys.argv[1] |
| 113 | + print("Scanning directory: %s" % (path)) |
| 114 | + |
| 115 | + manifest = make_manifest(path) |
| 116 | + with open(os.path.join(path, 'manifest.json'), 'w') as fp: |
| 117 | + fp.write(manifest) |
| 118 | + |
0 commit comments