]>
git.proxmox.com Git - ceph.git/blob - ceph/src/cephadm/build.py
2 """Build cephadm from one or more files into a standalone executable.
4 # TODO: If cephadm is being built and packaged within a format such as RPM
5 # do we have to do anything special wrt passing in the version
6 # of python to build with? Even with the intermediate cmake layer?
27 log
= logging
.getLogger(__name__
)
31 """Switch to the selected version of python by exec'ing into the desired
33 Sets the _BUILD_PYTHON_SET env variable as a sentinel to indicate exec has
36 env
= os
.environ
.copy()
37 env
["_BUILD_PYTHON_SET"] = python
38 os
.execvpe(python
, [python
, __file__
] + sys
.argv
[1:], env
)
42 """Returns true if the process has already exec'ed into the desired python
45 return bool(os
.environ
.get("_BUILD_PYTHON_SET", ""))
48 def _build(dest
, src
):
49 """Build the binary."""
51 tempdir
= pathlib
.Path(tempfile
.mkdtemp(suffix
=".cephadm.build"))
52 log
.debug("working in %s", tempdir
)
54 if os
.path
.isfile("requirements.txt"):
55 _install_deps(tempdir
)
56 log
.info("Copying contents")
57 # TODO: currently the only file relevant to a compiled cephadm is the
58 # cephadm.py file. Once cephadm is broken up into multiple py files
59 # (and possibly other libs from python-common, etc) we'll want some
60 # sort organized structure to track what gets copied into the
61 # dir to be zipped. For now we just have a simple call to copy
62 # (and rename) the one file we care about.
63 shutil
.copy("cephadm.py", tempdir
/ "__main__.py")
64 _compile(dest
, tempdir
)
66 shutil
.rmtree(tempdir
)
69 def _compile(dest
, tempdir
):
70 """Compile the zipapp."""
71 log
.info("Byte-compiling py to pyc")
72 compileall
.compile_dir(
79 # TODO we could explicitly pass a python version here
80 log
.info("Constructing the zipapp file")
82 zipapp
.create_archive(
85 interpreter
=sys
.executable
,
88 log
.info("Zipapp created with compression")
90 # automatically fall back to uncompressed
91 zipapp
.create_archive(
94 interpreter
=sys
.executable
,
96 log
.info("Zipapp created without compression")
99 def _install_deps(tempdir
):
100 """Install dependencies with pip."""
101 # TODO we could explicitly pass a python version here
102 log
.info("Installing dependencies")
103 # apparently pip doesn't have an API, just a cli.
104 subprocess
.check_call(
119 handler
= logging
.StreamHandler(sys
.stdout
)
120 handler
.setFormatter(logging
.Formatter("cephadm/build.py: %(message)s"))
121 log
.addHandler(handler
)
122 log
.setLevel(logging
.INFO
)
124 log
.debug("argv: %r", sys
.argv
)
125 parser
= argparse
.ArgumentParser()
127 "dest", help="Destination path name for new cephadm binary"
130 "--source", help="Directory containing cephadm sources"
133 "--python", help="The path to the desired version of python"
135 args
= parser
.parse_args()
137 if not _did_rexec() and args
.python
:
141 "Python Version: {v.major}.{v.minor}.{v.micro}".format(
145 log
.info("Args: %s", vars(args
))
147 # Unconditionally display an error that the version of python
148 # lacks zipapp (probably too old).
149 print("error: zipapp module not found", file=sys
.stderr
)
151 "(zipapp is available in Python 3.5 or later."
152 " are you using a new enough version?)",
157 source
= pathlib
.Path(args
.source
).absolute()
159 source
= pathlib
.Path(__file__
).absolute().parent
160 dest
= pathlib
.Path(args
.dest
).absolute()
161 log
.info("Source Dir: %s", source
)
162 log
.info("Destination Path: %s", dest
)
166 if __name__
== "__main__":