Skip to content

fix OpenAPI Context #524

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

Closed
wants to merge 12 commits into from
Closed
31 changes: 31 additions & 0 deletions dpdispatcher/contexts/openapi_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,37 @@
DP_CLOUD_SERVER_HOME_DIR = os.path.join(
os.path.expanduser("~"), ".dpdispatcher/", "dp_cloud_server/"
)
os.makedirs(DP_CLOUD_SERVER_HOME_DIR, exist_ok=True)
Comment on lines 23 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid creating directories at module import time.

Creating directories during module import can cause issues in restricted environments. Consider creating the directory lazily when first needed.

Move the directory creation to methods that actually use it, or create a helper function:

def ensure_home_dir_exists():
    os.makedirs(DP_CLOUD_SERVER_HOME_DIR, exist_ok=True)

Then call this function in methods like write_home_file and read_home_file.

🤖 Prompt for AI Agents
In dpdispatcher/contexts/openapi_context.py around lines 23 to 26, the directory
is created at module import time, which can cause issues in restricted
environments. Remove the os.makedirs call from the module level and instead
create a helper function that ensures the directory exists. Call this helper
function inside methods that use the directory, such as write_home_file and
read_home_file, to create the directory lazily when first needed.



def unzip_file(zip_file, out_dir="./"):
obj = ZipFile(zip_file, "r")
for item in obj.namelist():
obj.extract(item, out_dir)


def zip_file_list(root_path, zip_filename, file_list=[]):
out_zip_file = os.path.join(root_path, zip_filename)
# print('debug: file_list', file_list)
zip_obj = ZipFile(out_zip_file, "w")
for f in file_list:
matched_files = os.path.join(root_path, f)
for ii in glob.glob(matched_files):
# print('debug: matched_files:ii', ii)
if os.path.isdir(ii):
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
for root, dirs, files in os.walk(ii):
for file in files:
filename = os.path.join(root, file)
arcname = os.path.relpath(filename, start=root_path)
# print('debug: filename:arcname:root_path', filename, arcname, root_path)
zip_obj.write(filename, arcname)
else:
arcname = os.path.relpath(ii, start=root_path)
zip_obj.write(ii, arcname)
zip_obj.close()
return out_zip_file


def unzip_file(zip_file, out_dir="./"):
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ dependencies = [
'typing_extensions; python_version < "3.7"',
'pyyaml',
'tomli >= 1.1.0; python_version < "3.11"',
'httpx',
'distro',
'pyhumps'
Comment on lines +32 to +34
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be claimed in bohrium-sdk. I don't think they should be claimed in dpdispatcher

]
requires-python = ">=3.7"
readme = "README.md"
Expand Down