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

[Setup] Make setup more robust when there is no permission to get/write commit hash #2731

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Changes from 3 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
51 changes: 37 additions & 14 deletions sky/setup_files/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import platform
import re
import subprocess
import sys
from typing import Dict, List

import setuptools
Expand Down Expand Up @@ -70,28 +71,50 @@ def get_commit_hash():
if changes:
commit_hash += '-dirty'
return commit_hash
except Exception: # pylint: disable=broad-except
except Exception as e: # pylint: disable=broad-except
print(
'WARNING: SkyPilot fail to get the commit hash in '
Copy link
Collaborator

Choose a reason for hiding this comment

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

Probably use a constant for the error message to avoid duplication?

f'{INIT_FILE_PATH!r} (SkyPilot can still be normally used): {e}',
file=sys.stderr)
return commit_hash


def replace_commit_hash():
"""Fill in the commit hash in the __init__.py file."""
with open(INIT_FILE_PATH, 'r') as fp:
content = fp.read()
global original_init_content
original_init_content = content
content = re.sub(r'^_SKYPILOT_COMMIT_SHA = [\'"]([^\'"]*)[\'"]',
f'_SKYPILOT_COMMIT_SHA = \'{get_commit_hash()}\'',
content,
flags=re.M)
with open(INIT_FILE_PATH, 'w') as fp:
fp.write(content)
try:
with open(INIT_FILE_PATH, 'r') as fp:
content = fp.read()
global original_init_content
original_init_content = content
content = re.sub(r'^_SKYPILOT_COMMIT_SHA = [\'"]([^\'"]*)[\'"]',
f'_SKYPILOT_COMMIT_SHA = \'{get_commit_hash()}\'',
content,
flags=re.M)
with open(INIT_FILE_PATH, 'w') as fp:
fp.write(content)
except Exception as e: # pylint: disable=broad-except
# Avoid breaking the installation when there is no permission to write
# the file.
print(
'WARNING: SkyPilot fail to replace the commit hash in '
f'{INIT_FILE_PATH!r} (SkyPilot can still be normally used): {e}',
file=sys.stderr)
pass


def revert_commit_hash():
if original_init_content is not None:
with open(INIT_FILE_PATH, 'w') as fp:
fp.write(original_init_content)
try:
if original_init_content is not None:
with open(INIT_FILE_PATH, 'w') as fp:
fp.write(original_init_content)
except Exception: # pylint: disable=broad-except
# Avoid breaking the installation when there is no permission to write
# the file.
print(
'WARNING: SkyPilot fail to replace the commit hash in '
f'{INIT_FILE_PATH!r} (SkyPilot can still be normally used): {e}',
file=sys.stderr)
pass


def parse_readme(readme: str) -> str:
Expand Down