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

Improved version tracking and deprecated SVN #403

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
57 changes: 18 additions & 39 deletions EXOSIMS/Prototypes/SurveySimulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
from EXOSIMS.util.get_dirs import get_cache_dir
from EXOSIMS.util.get_module import get_module
from EXOSIMS.util.vprint import vprint
from EXOSIMS.util._numpy_compat import copy_if_needed
from EXOSIMS.util.version_util import get_version



Logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -1141,7 +1145,7 @@ def choose_next_target(self, old_sInd, sInds, slewTimes, intTimes):
allModes = OS.observingModes

# cast sInds to array
sInds = np.array(sInds, ndmin=1, copy=False)
sInds = np.array(sInds, ndmin=1, copy=copy_if_needed)
# calculate dt since previous observation
dt = TK.currentTimeNorm.copy() + slewTimes[sInds] - self.lastObsTimes[sInds]
# get dynamic completeness values
Expand Down Expand Up @@ -1405,7 +1409,7 @@ def filterOcculterSlews(self, sInds, slewTimes, obsTimeArray, intTimeArray, mode

# maximum allowed slew time based on integration times
maxAllowedSlewTime = maxIntTimes[good_inds].value - intTimes.value
maxAllowedSlewTime[maxAllowedSlewTime < 0] = -np.Inf
maxAllowedSlewTime[maxAllowedSlewTime < 0] = -np.inf
maxAllowedSlewTime += OBstartTimeNorm # calculated rel to currentTime norm

# checking to see if slewTimes are allowed
Expand Down Expand Up @@ -1556,9 +1560,9 @@ def findAllowableOcculterSlews(

conds = cond1 & cond2 & cond3 & cond4
minAllowedSlewTimes[np.invert(conds)] = (
np.Inf
np.inf
) # these are filtered during the next filter
maxAllowedSlewTimes[np.invert(conds)] = -np.Inf
maxAllowedSlewTimes[np.invert(conds)] = -np.inf

# one last condition to meet
map_i, map_j = np.where(
Expand Down Expand Up @@ -1636,8 +1640,8 @@ def findAllowableOcculterSlews(
cond5 = intTimes_int.value < maxIntTime_nOB.value
conds = cond1 & cond2 & cond3 & cond4 & cond5

minAllowedSlewTimes_nOB[np.invert(conds)] = np.Inf
maxAllowedSlewTimes_nOB[np.invert(conds)] = -np.Inf
minAllowedSlewTimes_nOB[np.invert(conds)] = np.inf
maxAllowedSlewTimes_nOB[np.invert(conds)] = -np.inf

# one last condition
map_i, map_j = np.where(
Expand Down Expand Up @@ -2519,40 +2523,15 @@ def genOutSpec(
if "SurveyEnsemble" not in out["modules"]:
out["modules"]["SurveyEnsemble"] = " "

# add in the SVN/Git revision
path = os.path.split(inspect.getfile(self.__class__))[0]
path = os.path.split(os.path.split(path)[0])[0]
# handle case where EXOSIMS was imported from the working directory
if path == "":
path = os.getcwd()
# comm = "git -C " + path + " log -1"
comm = "git --git-dir=%s --work-tree=%s log -1" % (
os.path.join(path, ".git"),
path,
)
rev = subprocess.Popen(
comm, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True
)
(gitRev, err) = rev.communicate()
gitRev = gitRev.decode("utf-8")
if isinstance(gitRev, str) & (len(gitRev) > 0):
tmp = re.compile(
r"\S*(commit [0-9a-fA-F]+)\n[\s\S]*Date: ([\S ]*)\n"
).match(gitRev)
if tmp:
out["Revision"] = "Github " + tmp.groups()[0] + " " + tmp.groups()[1]
else:
rev = subprocess.Popen(
"svn info " + path + "| grep \"Revision\" | awk '{print $2}'",
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True,
)
(svnRev, err) = rev.communicate()
if isinstance(svnRev, str) & (len(svnRev) > 0):
out["Revision"] = "SVN revision is " + svnRev[:-1]
# get version and Git information
version_info = get_version()
for key, value in version_info.items():
if isinstance(value, dict):
print(f'{key}:')
for sub_key, sub_value in value.items():
print(f' {sub_key}:'.ljust(25)+f'{sub_value}')
else:
out["Revision"] = "Not a valid Github or SVN revision."
print(f'{key}:'.ljust(25)+f'{value}')

# dump to file
if tofile is not None:
Expand Down
79 changes: 79 additions & 0 deletions EXOSIMS/util/version_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from importlib import metadata
import platform
import subprocess
import os

def get_git_info():
"""
Get Git information including commit hash and status of changes
"""
try:
# Check if we are in a Git repository
subprocess.run(['git', 'rev-parse'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)

# Get the current commit hash
commit_hash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).strip().decode('utf-8')

# Check for uncommitted changes
status = subprocess.check_output(['git', 'status', '--porcelain']).strip().decode('utf-8')

uncommitted_changes = status != ''

return commit_hash, uncommitted_changes
except subprocess.CalledProcessError:
return None, False # Not a Git repository

def is_editable_installation():
"""
Check if EXOSIMS is installed in editable mode
"""
try:
# Check if EXOSIMS is installed via pip in editable mode
site_packages = metadata.distribution('EXOSIMS').locate_file('').parent
editable_marker_file = os.path.join(site_packages, 'EXOSIMS.egg-link')
return os.path.exists(editable_marker_file)
except Exception:
return False

def get_version():
python_version = platform.python_version()

exosims_version = metadata.version('EXOSIMS')
editable = is_editable_installation()

if editable:
print('EXOSIMS is installed in editable mode')
exosims_version = f'{exosims_version} (editable'
commit_hash, uncommitted_changes = get_git_info()
if commit_hash is not None:
exosims_version = f'{exosims_version} / commit {commit_hash}'
if uncommitted_changes:
exosims_version = f'{exosims_version} / warning! uncommited changes'
exosims_version = f'{exosims_version})'
else:
exosims_version = f'{exosims_version} (not editable)'

reqs = metadata.distribution('EXOSIMS').requires
required_packages = [str(req) for req in reqs]

# Get installed versions of required packages
installed_packages = {dist.metadata['Name']: dist.version for dist in metadata.distributions()}

# Filter installed packages to those listed in requirements
relevant_packages = {pkg: installed_packages.get(pkg.split('>=')[0], "Not installed") for pkg in required_packages}

return {
'Python': python_version,
'EXOSIMS': exosims_version,
'Packages': relevant_packages
}


version_info = get_version()
for key, value in version_info.items():
if isinstance(value, dict):
print(f'{key}:')
for sub_key, sub_value in value.items():
print(f' {sub_key}:'.ljust(25)+f'{sub_value}')
else:
print(f'{key}:'.ljust(25)+f'{value}')
15 changes: 15 additions & 0 deletions tools/rec_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

from importlib import metadata
reqs = metadata.distribution('EXOSIMS').requires
required_packages = [str(req).split('>=')[0] for req in reqs]

with open('requirements.txt', 'r') as f:
lines = f.readlines()

for package in required_packages:
flag = False
for line in lines:
if package in line:
flag = True
assert flag, f'{package} not found in requirements.txt'
print(f'{package} found in requirements.txt')