• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17import argparse
18import os
19import hashlib
20sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
21import file_utils  # noqa: E402
22from scripts.util import build_utils  # noqa: E402
23
24__MAX_BUF = 1024 * 1024
25
26
27def _gen_signature(input_file):
28    if not os.path.isfile(input_file):
29        raise Exception()
30    hash_value = ''
31    sha256obj = hashlib.sha256()
32    try:
33        with open(input_file, 'rb') as file_obj:
34            while True:
35                buf = file_obj.read(__MAX_BUF)
36                if not buf:
37                    break
38                sha256obj.update(buf)
39            hash_value = sha256obj.hexdigest()
40    except OSError as err:
41        sys.stdout.write("read file failed. {}".format(err))
42    return hash_value
43
44
45def _write_signature_file(signature_file, hash_value):
46    if os.path.exists(signature_file):
47        os.remove(signature_file)
48    file_utils.write_file(signature_file, hash_value)
49
50
51def _update_signature(signature_file, new_hash_value):
52    if os.path.exists(signature_file):
53        data = file_utils.read_file(signature_file)
54        if data is None:
55            raise Exception(
56                "read signatrue file '{}' failed.".format(signature_file))
57        old_value = data[0]
58        if old_value is None or old_value == '':
59            raise Exception(
60                "signatrue file '{}' content error.".format(signature_file))
61        if old_value == new_hash_value:
62            return
63    _write_signature_file(signature_file, new_hash_value)
64
65
66def main():
67    parser = argparse.ArgumentParser()
68    parser.add_argument('--input-dir', required=True)
69    parser.add_argument('--output-zipfile', required=True)
70    parser.add_argument('--signature-file', required=True)
71    args = parser.parse_args()
72
73    if os.path.exists(args.output_zipfile):
74        os.remove(args.output_zipfile)
75    build_utils.zip_dir(args.output_zipfile, args.input_dir)
76    if not os.path.exists(args.output_zipfile):
77        raise Exception("generate zipfile '{}' failed.".format(
78            args.output_zipfile))
79
80    hash_value = _gen_signature(args.output_zipfile)
81    _update_signature(args.signature_file, hash_value)
82    return 0
83
84
85if __name__ == '__main__':
86    sys.exit(main())
87