This repository has been archived by the owner on Mar 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
validate_package.py
executable file
·127 lines (99 loc) · 4.19 KB
/
validate_package.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
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
#!/usr/bin/env python3
#############################################################################
##
## This file is part of GAP, a system for computational discrete algebra.
##
## Copyright of GAP belongs to its developers, whose names are too numerous
## to list here. Please refer to the COPYRIGHT file for details.
##
## SPDX-License-Identifier: GPL-2.0-or-later
##
# pylint: disable=C0116, C0103
"""
Runs some basic validation of the pkgname/meta.json against the package
archive, and the old meta data.
Should be run after scan_for_updates.py. Arguments can be either package
names, or the path to a meta.json file. For example:
_tools/validate_package.py aclib digraphs walrus/meta.json
aclib: _archives/aclib-1.3.2.tar.gz already exists, not downloading again
aclib: unpacking _archives/aclib-1.3.2.tar.gz into _unpacked_archives ...
aclib: current release version is 1.3.2, but previous release version was 1.3.2, FAILED!
aclib: validation FAILED!
digraphs: _archives/digraphs-1.5.0.tar.gz already exists, not downloading again
digraphs: unpacking _archives/digraphs-1.5.0.tar.gz into _unpacked_archives ...
digraphs: validated ok!
walrus: _archives/walrus-0.9991.tar.gz already exists, not downloading again
walrus: unpacking _archives/walrus-0.9991.tar.gz into _unpacked_archives ...
walrus: the file walrus/meta.yml.old is missing, FAILED!
"""
import os
import sys
import shutil
from os.path import join
from accepts import accepts
from download_packages import download_archive
from scan_for_updates import download_pkg_info, gap_exec
from utils import notice, warning, normalize_pkg_name, archive_name, metadata, sha256
def unpack_archive(archive_dir, unpack_dir, pkg_name):
archive_fname = join(archive_dir, archive_name(pkg_name))
if not os.path.exists(unpack_dir):
os.mkdir(unpack_dir)
notice("unpacking {} into {} ...".format(archive_fname, unpack_dir))
shutil.unpack_archive(archive_fname, unpack_dir)
def unpacked_archive_name(unpack_dir, pkg_name):
for x in os.listdir(unpack_dir):
if len(x) >= len(pkg_name) and x[: len(pkg_name)].lower() == pkg_name:
return join(unpack_dir, x)
warning("{}: couldn't determine the unpacked archive directory!")
def validate_package(archive_dir, unpack_dir, pkg_name):
result = True
pkg_json = metadata(pkg_name)
pkg_info_name = join(
unpacked_archive_name(unpack_dir, pkg_name), "PackageInfo.g"
)
if pkg_json["PackageInfoSHA256"] != sha256(pkg_info_name):
warning(
"{0}: {0}/meta.yml:PackageInfoSHA256 is not the SHA256 of {1}, FAILED!".format(
pkg_name, pkg_info_name
)
)
result = False
packed_name = join(archive_dir, archive_name(pkg_name))
if pkg_json["ArchiveSHA256"] != sha256(packed_name):
warning(
"{0}: {0}/meta.yml:ArchiveSHA256 is not the SHA256 of {1}, FAILED!".format(
pkg_name, packed_name
)
)
result = False
if not os.path.exists(join(pkg_name, "meta.json.old")):
notice(
"{0}: the file {0}/meta.yml.old is not present, new package!".format(
pkg_name
)
)
return result
# TODO: check SHA256 hashes for PackageinfoURL and archive are the same.
def main(pkg_name):
unpack_dir = "_unpacked_archives"
archive_dir = "_archives"
download_archive(archive_dir, pkg_name)
unpack_archive(archive_dir, unpack_dir, pkg_name)
if validate_package(archive_dir, unpack_dir, pkg_name):
dir_of_this_file = os.path.dirname(os.path.realpath(__file__))
unpacked_name = unpacked_archive_name(unpack_dir, pkg_name)
if (
gap_exec(
r"ValidatePackagesArchive(\"{}\", \"{}\");".format(
unpacked_name, pkg_name
),
gap="gap {}/validate_package.g".format(dir_of_this_file),
)
!= 0
):
warning("{}: validation FAILED!".format(pkg_name))
else:
notice("{}: validated ok!".format(pkg_name))
if __name__ == "__main__":
for i in range(1, len(sys.argv)):
main(normalize_pkg_name(sys.argv[i]))