-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script to generate bundled provides
Signed-off-by: mulhern <[email protected]>
- Loading branch information
Showing
1 changed file
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
#!/usr/bin/python3 | ||
# | ||
# Copyright 2023 Red Hat, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
""" | ||
Generate vendored provides list from vendor tarfile. | ||
""" | ||
|
||
# isort: STDLIB | ||
import argparse | ||
import os | ||
import sys | ||
import tarfile | ||
import tomllib | ||
|
||
|
||
def main(): | ||
""" | ||
Main function | ||
""" | ||
|
||
parser = argparse.ArgumentParser( | ||
description=( | ||
"Generate a list of bundled provides suitable for including in an " | ||
"rpm spec file. Include in the list only those dependencies which " | ||
"have source code in the vendor tarfile. Prints list to stdout." | ||
) | ||
) | ||
|
||
parser.add_argument( | ||
"vendor_tarfile", | ||
action="store", | ||
help="path to vendor tarfile", | ||
type=os.path.abspath, | ||
) | ||
|
||
namespace = parser.parse_args() | ||
|
||
with tarfile.open(namespace.vendor_tarfile, "r") as tar: | ||
for member in tar.getmembers(): | ||
components = member.name.split("/") | ||
|
||
if ( | ||
len(components) == 3 | ||
and components[0] == "vendor" | ||
and components[2] == "Cargo.toml" | ||
): | ||
manifest = tar.extractfile(member) | ||
metadata = tomllib.load(manifest) | ||
cargo_name = components[1] | ||
package = metadata["package"] | ||
package_version = package["version"] | ||
continue | ||
|
||
if ( | ||
len(components) == 4 | ||
and components[0] == "vendor" | ||
and components[2] == "src" | ||
and components[3] == "lib.rs" | ||
): | ||
size = member.size | ||
if size != 0: | ||
if cargo_name == components[1]: | ||
print( | ||
f"Provides: bundled(crate({components[1]})) = {package_version}" | ||
) | ||
else: | ||
raise RuntimeError( | ||
"Found an entry for bundled provides, but no version information" | ||
) | ||
|
||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
try: | ||
main() | ||
except Exception as err: # pylint: disable=broad-except | ||
print(err) | ||
sys.exit(1) |