|
| 1 | +# Copyright (c) 2025, Oracle and/or its affiliates. |
| 2 | +# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ |
| 3 | +""" |
| 4 | +Create a zipapp distribution of drgn-tools which can provided to customers. |
| 5 | +""" |
| 6 | +import argparse |
| 7 | +import shutil |
| 8 | +import subprocess |
| 9 | +import sys |
| 10 | +import tempfile |
| 11 | +import zipapp |
| 12 | +from pathlib import Path |
| 13 | + |
| 14 | + |
| 15 | +def main(): |
| 16 | + entry_points = { |
| 17 | + "corelens": "drgn_tools.corelens:main", |
| 18 | + "cli": "drgn_tools.cli:main", |
| 19 | + } |
| 20 | + parser = argparse.ArgumentParser( |
| 21 | + description="create drgn-tools distributions" |
| 22 | + ) |
| 23 | + parser.add_argument( |
| 24 | + "--interpreter", |
| 25 | + default="/usr/bin/python3", |
| 26 | + help="Set the interpreter (if different from target system python)", |
| 27 | + ) |
| 28 | + parser.add_argument( |
| 29 | + "--output", |
| 30 | + "-o", |
| 31 | + default=None, |
| 32 | + help="Set the output file", |
| 33 | + ) |
| 34 | + parser.add_argument( |
| 35 | + "--entry-point", |
| 36 | + default="corelens", |
| 37 | + help=f"Select an entry point ({','.join(entry_points.keys())} " |
| 38 | + "or a function name)", |
| 39 | + ) |
| 40 | + parser.add_argument( |
| 41 | + "--quiet", |
| 42 | + "-q", |
| 43 | + action="store_true", |
| 44 | + help="just do it without any prompts or info", |
| 45 | + ) |
| 46 | + args = parser.parse_args() |
| 47 | + |
| 48 | + print( |
| 49 | + """\ |
| 50 | +Please note: the contents of the drgn_tools/ directory will be used to create |
| 51 | +this distribution AS-IS! If you have any contents in that directory which should |
| 52 | +not be distributed to a customer, please Ctrl-C now and clean them up. You may |
| 53 | +want to use: |
| 54 | +
|
| 55 | + git clean -ndx drgn_tools/ |
| 56 | +
|
| 57 | +To see if you have any untracked files. You can use: |
| 58 | +
|
| 59 | + git clean -fdx drgn_tools/ |
| 60 | +
|
| 61 | +To delete everything listed by the prior command. Finally, you can use: |
| 62 | +
|
| 63 | + git status drgn_tools/ |
| 64 | +
|
| 65 | +To verify which files have uncommitted changes. It's totally fine to include |
| 66 | +extra files & uncommitted changes, but it's important to be sure you only |
| 67 | +include what you intended. |
| 68 | +
|
| 69 | +Please hit enter to acknowledge and continue, or Ctrl-C to abort.\ |
| 70 | +""" |
| 71 | + ) |
| 72 | + input() |
| 73 | + base_dir = Path(__file__).parent |
| 74 | + if args.entry_point in entry_points: |
| 75 | + output_file = args.output or f"{args.entry_point}.pyz" |
| 76 | + entry_point = entry_points[args.entry_point] |
| 77 | + else: |
| 78 | + output_file = args.output or "drgn_tools.pyz" |
| 79 | + entry_point = args.entry_point |
| 80 | + |
| 81 | + # Be sure that we re-generate the "_version.py" file for accuracy |
| 82 | + subprocess.run( |
| 83 | + [sys.executable, base_dir / "setup.py", "--version"], |
| 84 | + check=True, |
| 85 | + stdout=subprocess.DEVNULL, |
| 86 | + cwd=base_dir, |
| 87 | + ) |
| 88 | + |
| 89 | + # Only the contents of "drgn_tools" should be included. All other files that |
| 90 | + # are part of the project should be excluded. |
| 91 | + with tempfile.TemporaryDirectory() as td: |
| 92 | + tmp = Path(td) |
| 93 | + shutil.copytree(base_dir / "drgn_tools", tmp / "drgn_tools") |
| 94 | + zipapp.create_archive( |
| 95 | + td, output_file, interpreter=args.interpreter, main=entry_point |
| 96 | + ) |
| 97 | + |
| 98 | + print( |
| 99 | + f"""\ |
| 100 | +Created a distribution: {output_file} |
| 101 | +
|
| 102 | +It can be directly copied to a target system, and it can be directly executed. |
| 103 | +The target system MUST have a /usr/bin/python3 and have drgn installed. The |
| 104 | +target system DOES NOT need to have drgn-tools installed -- but if it does, that |
| 105 | +is fine. Nothing on the target system will be modified. |
| 106 | +
|
| 107 | +You can use "unzip -l {output_file}" to check the contents of the zip file to |
| 108 | +ensure only what you intended to include is present.\ |
| 109 | +""" |
| 110 | + ) |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments