Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pip build #1347

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ install_requires =
tabulate
websockets
aiortc; python_version>='3.8' and python_version<'3.13'
keeper-dag @ https://github.com/Keeper-Security/Commander/raw/refs/heads/master/libs/keeper_dag-1.0.20-py3-none-any.whl
discovery_common @ https://github.com/Keeper-Security/Commander/raw/refs/heads/master/libs/discovery_common-1.0.26-py3-none-any.whl

[options.extras_require]
test =
Expand Down
100 changes: 1 addition & 99 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,102 +1,4 @@
from setuptools import setup

from setuptools.command.install import install as install_command
import os
import subprocess
import shutil
import re


class Wheel(install_command):

user_options = install_command.user_options + [
('whlsrc=', None, "Build a wheel for the python code that is in this directory. Copy into 'libs' directory."),
('libdir=', None, "The directory to put the whl files."),
('reqfiles=', None, "List of requirement.txt to update."),
]

def initialize_options(self):
install_command.initialize_options(self)
self.whlsrc = None
self.libdir = None
self.reqfiles = None

def finalize_options(self):
install_command.finalize_options(self)

def run(self):
global whlsrc
global libdir
global reqfiles
whlsrc = self.whlsrc
libdir = self.libdir
reqfiles = self.reqfiles

if isinstance(reqfiles, list) is False:
reqfiles = [reqfiles]

current_dir = os.getcwd()
try:
# Get existing fiels in the lib directory.
os.chdir(self.libdir)
sp = subprocess.run(["ls"], capture_output=True, text=True)
existing_whls = []
for file in sp.stdout.split("\n"):
if file.endswith("whl") is True:
existing_whls.append(file)

# Installed required modules and build a wheel
os.chdir(whlsrc)
subprocess.run(["pip3", "install", "-r", "requirements.txt"])
subprocess.run(["python3", "setup.py", "bdist_wheel"])

# Find the whl file in the dist folder.
os.chdir(os.path.join(whlsrc, "dist"))
sp = subprocess.run(["ls"], capture_output=True, text=True)
wheel_file = None
for file in sp.stdout.split("\n"):
if file.endswith("whl") is True:
wheel_file = file
break
if wheel_file is None:
raise ValueError(f"Cannot find a whl file in the dist directory of the {whlsrc} project.")

# Copy the whl to the lib directory
subprocess.run(["cp", wheel_file, self.libdir])

project_name = wheel_file[:wheel_file.index("-")]

# Remove old versions of the wheel.
os.chdir(self.libdir)
for existing_whl in existing_whls:
if existing_whl.startswith(project_name) is False:
continue
if existing_whl == wheel_file:
continue
os.unlink(existing_whl)

for req in reqfiles:
shutil.copy(req, f"{req}.bak")
requirement_data = []
with open(req, "r") as fh:
requirement_data = fh.readlines()
fh.close()

pattern = re.compile(re.escape(project_name) + "-.*?.whl" )
with open(req, "w") as fh:
for line in requirement_data:
line = re.sub(pattern, wheel_file, line)
fh.write(line)
fh.close()
os.unlink(f"{req}.bak")

finally:
os.chdir(current_dir)


if __name__ == '__main__':
setup(
cmdclass={
'wheel': Wheel
}
)
setup()
Loading