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 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
49 changes: 35 additions & 14 deletions sky/setup_files/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,18 @@
import platform
import re
import subprocess
import sys
from typing import Dict, List

import setuptools

ROOT_DIR = os.path.dirname(__file__)
INIT_FILE_PATH = os.path.join(ROOT_DIR, 'sky', '__init__.py')
_COMMIT_FAILURE_MESSAGE = (
'WARNING: SkyPilot fail to {verb} the commit hash in '
f'{INIT_FILE_PATH!r} (SkyPilot can still be normally used): '
'{error}')

original_init_content = None

system = platform.system()
Expand Down Expand Up @@ -70,28 +76,43 @@ 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(_COMMIT_FAILURE_MESSAGE.format(verb='get', error=str(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(_COMMIT_FAILURE_MESSAGE.format(verb='replace', error=str(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 as e: # pylint: disable=broad-except
# Avoid breaking the installation when there is no permission to write
# the file.
print(_COMMIT_FAILURE_MESSAGE.format(verb='replace', error=str(e)),
file=sys.stderr)


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