Skip to content

Commit 4722b74

Browse files
Merge remote-tracking branch 'main' into psa-storage-test-cases-never-supported-negative-framework
2 parents 8141968 + 57ee691 commit 4722b74

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
"""Install all the required Python packages, with the minimum Python version.
2+
"""
3+
4+
# Copyright The Mbed TLS Contributors
5+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6+
7+
import argparse
8+
import os
9+
import re
10+
import subprocess
11+
import sys
12+
import tempfile
13+
import typing
14+
15+
from typing import List, Optional
16+
17+
import framework_scripts_path # pylint: disable=unused-import
18+
from mbedtls_framework import typing_util
19+
20+
def pylint_doesn_t_notice_that_certain_types_are_used_in_annotations(
21+
_list: List[typing.Any],
22+
) -> None:
23+
pass
24+
25+
26+
class Requirements:
27+
"""Collect and massage Python requirements."""
28+
29+
def __init__(self) -> None:
30+
self.requirements = [] #type: List[str]
31+
32+
def adjust_requirement(self, req: str) -> str:
33+
"""Adjust a requirement to the minimum specified version."""
34+
# allow inheritance #pylint: disable=no-self-use
35+
# If a requirement specifies a minimum version, impose that version.
36+
split_req = req.split(';', 1)
37+
split_req[0] = re.sub(r'>=|~=', r'==', split_req[0])
38+
return ';'.join(split_req)
39+
40+
def add_file(self, filename: str) -> None:
41+
"""Add requirements from the specified file.
42+
43+
This method supports a subset of pip's requirement file syntax:
44+
* One requirement specifier per line, which is passed to
45+
`adjust_requirement`.
46+
* Comments (``#`` at the beginning of the line or after whitespace).
47+
* ``-r FILENAME`` to include another file.
48+
"""
49+
for line in open(filename):
50+
line = line.strip()
51+
line = re.sub(r'(\A|\s+)#.*', r'', line)
52+
if not line:
53+
continue
54+
m = re.match(r'-r\s+', line)
55+
if m:
56+
nested_file = os.path.join(os.path.dirname(filename),
57+
line[m.end(0):])
58+
self.add_file(nested_file)
59+
continue
60+
self.requirements.append(self.adjust_requirement(line))
61+
62+
def write(self, out: typing_util.Writable) -> None:
63+
"""List the gathered requirements."""
64+
for req in self.requirements:
65+
out.write(req + '\n')
66+
67+
def install(
68+
self,
69+
pip_general_options: Optional[List[str]] = None,
70+
pip_install_options: Optional[List[str]] = None,
71+
) -> None:
72+
"""Call pip to install the requirements."""
73+
if pip_general_options is None:
74+
pip_general_options = []
75+
if pip_install_options is None:
76+
pip_install_options = []
77+
with tempfile.TemporaryDirectory() as temp_dir:
78+
# This is more complicated than it needs to be for the sake
79+
# of Windows. Use a temporary file rather than the command line
80+
# to avoid quoting issues. Use a temporary directory rather
81+
# than NamedTemporaryFile because with a NamedTemporaryFile on
82+
# Windows, the subprocess can't open the file because this process
83+
# has an exclusive lock on it.
84+
req_file_name = os.path.join(temp_dir, 'requirements.txt')
85+
with open(req_file_name, 'w') as req_file:
86+
self.write(req_file)
87+
subprocess.check_call([sys.executable, '-m', 'pip'] +
88+
pip_general_options +
89+
['install'] + pip_install_options +
90+
['-r', req_file_name])
91+
92+
def main(default_requirement_file: str) -> None:
93+
"""Command line entry point."""
94+
parser = argparse.ArgumentParser(description=__doc__)
95+
parser.add_argument('--no-act', '-n',
96+
action='store_true',
97+
help="Don't act, just print what will be done")
98+
parser.add_argument('--pip-install-option',
99+
action='append', dest='pip_install_options',
100+
help="Pass this option to pip install")
101+
parser.add_argument('--pip-option',
102+
action='append', dest='pip_general_options',
103+
help="Pass this general option to pip")
104+
parser.add_argument('--user',
105+
action='append_const', dest='pip_install_options',
106+
const='--user',
107+
help="Install to the Python user install directory"
108+
" (short for --pip-install-option --user)")
109+
parser.add_argument('files', nargs='*', metavar='FILE',
110+
help="Requirement files"
111+
" (default: {})" \
112+
.format(default_requirement_file))
113+
options = parser.parse_args()
114+
if not options.files:
115+
options.files = [default_requirement_file]
116+
reqs = Requirements()
117+
for filename in options.files:
118+
reqs.add_file(filename)
119+
reqs.write(sys.stdout)
120+
if not options.no_act:
121+
reqs.install(pip_general_options=options.pip_general_options,
122+
pip_install_options=options.pip_install_options)

scripts/pkgconfig.sh

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
#
3+
# Copyright The Mbed TLS Contributors
4+
# SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
5+
#
6+
# Purpose
7+
#
8+
# Test pkgconfig files.
9+
#
10+
# For each of the build pkg-config files, .pc files, check that
11+
# they validate and do some basic sanity testing on the output,
12+
# i.e. that the strings are non-empty.
13+
#
14+
# NOTE: This requires the built pc files to be on the pkg-config
15+
# search path, this can be controlled with env variable
16+
# PKG_CONFIG_PATH. See man(1) pkg-config for details.
17+
#
18+
19+
set -e -u
20+
21+
if [ $# -le 0 ]
22+
then
23+
echo " [!] No package names specified" >&2
24+
echo "Usage: $0 <package name 1> <package name 2> ..." >&2
25+
exit 1
26+
fi
27+
28+
for pc in "$@"; do
29+
printf "testing package config file: ${pc} ... "
30+
pkg-config --validate "${pc}"
31+
version="$(pkg-config --modversion "${pc}")"
32+
test -n "$version"
33+
cflags="$(pkg-config --cflags "${pc}")"
34+
test -n "$cflags"
35+
libs="$(pkg-config --libs "${pc}")"
36+
test -n "$libs"
37+
printf "passed\n"
38+
done
39+
40+
exit 0

0 commit comments

Comments
 (0)