projects/guix·blob python-team · refs

96 lines · 3.4 KiB

1# -*- coding: utf-8 -*-
2# GNU Guix --- Functional package management for GNU
3# Copyright © 2021, 2022 Lars-Dominik Braun <[email protected]>
4#
5# This file is part of GNU Guix.
6#
7# GNU Guix is free software; you can redistribute it and/or modify it
8# under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 3 of the License, or (at
10# your option) any later version.
11#
12# GNU Guix is distributed in the hope that it will be useful, but
13# WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20from __future__ import print_function # Python 2 support.
21import importlib
22import sys
23import traceback
24try:
25 import pkg_resources
26except ImportError:
27 print('Warning: Skipping, because python-setuptools are not available.')
28 sys.exit(0)
29
30try:
31 from importlib.machinery import PathFinder
32except ImportError:
33 PathFinder = None
34
35ret = 0
36
37# Only check site-packages installed by this package, but not dependencies
38# (which pkg_resources.working_set would include). Path supplied via argv.
39ws = pkg_resources.find_distributions(sys.argv[1])
40
41for dist in ws:
42 print('validating', repr(dist.project_name), dist.location)
43 try:
44 print('...checking requirements: ', end='')
45 req = str(dist.as_requirement())
46 # dist.activate() is not enough to actually check requirements, we
47 # have to .require() it.
48 pkg_resources.require(req)
49 print('OK')
50 except Exception as e:
51 print('ERROR:', req, repr(e))
52 ret = 1
53 continue
54
55 # Try to load top level modules. This should not have any side-effects.
56 try:
57 metalines = dist.get_metadata_lines('top_level.txt')
58 except (KeyError, EnvironmentError):
59 # distutils (i.e. #:use-setuptools? #f) will not install any metadata.
60 # This file is also missing for packages built using a PEP 517 builder
61 # such as poetry.
62 print('WARNING: cannot determine top-level modules')
63 continue
64 for name in metalines:
65 # Only available on Python 3.
66 if PathFinder and PathFinder.find_spec(name) is None:
67 # Ignore unavailable modules, often C modules, which were not
68 # installed at the top-level. Cannot use ModuleNotFoundError,
69 # because it is raised by failed imports too.
70 continue
71 try:
72 print('...trying to load module', name, end=': ')
73 importlib.import_module(name)
74 print('OK')
75 except Exception:
76 print('ERROR:')
77 traceback.print_exc(file=sys.stdout)
78 ret = 1
79
80 # Try to load entry points of console scripts too, making sure they
81 # work. They should be removed if they don't. Other groups may not be
82 # safe, as they can depend on optional packages.
83 for group, v in dist.get_entry_map().items():
84 if group not in {'console_scripts', 'gui_scripts'}:
85 continue
86 for name, ep in v.items():
87 try:
88 print('...trying to load endpoint', group, name, end=': ')
89 ep.load()
90 print('OK')
91 except Exception:
92 print('ERROR:')
93 traceback.print_exc(file=sys.stdout)
94 ret = 1
95
96sys.exit(ret)