From 6a04140c22bb876356861f518e51b470a9be5022 Mon Sep 17 00:00:00 2001 From: "Matthew Emerson Spotnitz, PhD" Date: Tue, 16 Jun 2026 15:15:11 -0600 Subject: [PATCH 1/2] Add auto git hash update. Correct branch to main. --- refractiveindex/refractiveindex.py | 68 +++++++++++++++++++++++++++--- tests/test_refractiveindex.py | 22 ++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/refractiveindex/refractiveindex.py b/refractiveindex/refractiveindex.py index acefb45..6dbe642 100644 --- a/refractiveindex/refractiveindex.py +++ b/refractiveindex/refractiveindex.py @@ -11,9 +11,9 @@ from yaml import BaseLoader -# Latest commit as of 2026-02-17 -# https://github.com/polyanskiy/refractiveindex.info-database/commits/master/ -_DATABASE_SHA = "a66ef8805cdb200973fc7ae9181587e1d89d14eb" +# Default database SHA. Latest SHA will be found automatically if this is not set. +# https://github.com/polyanskiy/refractiveindex.info-database/commits/main +_DATABASE_SHA = None _DEFAULT_DB_PATH = Path.home() / ".refractiveindex.info-database" @@ -21,14 +21,37 @@ _catalog_cache = {} -def _download_database(db_path, ssl_certificate_location=None): +def _get_latest_commit_hash(): + """Get the latest commit hash from the main branch of the refractiveindex.info-database repository.""" + try: + # Use GitHub API to get the latest commit SHA + import urllib.request + import json + + url = "https://api.github.com/repos/polyanskiy/refractiveindex.info-database/commits/main" + with urllib.request.urlopen(url) as response: + data = json.loads(response.read().decode('utf-8')) + return data['sha'] + except Exception as e: + print(f"Failed to get latest commit hash: {e}", file=sys.stderr) + return None + + +def _download_database(db_path, ssl_certificate_location=None, commit_hash=_DATABASE_SHA): import shutil import ssl import tempfile import urllib.request import zipfile - url = f"https://github.com/polyanskiy/refractiveindex.info-database/archive/{_DATABASE_SHA}.zip" + # Use provided commit hash or get the latest one + if commit_hash is None: + commit_hash = _get_latest_commit_hash() + if commit_hash is None: + # Fallback to a known good commit if we can't fetch the latest + commit_hash = "ff11b5897ef0754b15d939d921eb6c745693cbd1" + + url = f"https://github.com/polyanskiy/refractiveindex.info-database/archive/{commit_hash}.zip" if ssl_certificate_location is not None: if ssl_certificate_location == "": @@ -52,7 +75,7 @@ def _download_database(db_path, ssl_certificate_location=None): print("removing old database...", file=sys.stderr) shutil.rmtree(db_path) - extracted = Path(tempdir) / f"refractiveindex.info-database-{_DATABASE_SHA}" / "database" + extracted = Path(tempdir) / f"refractiveindex.info-database-{commit_hash}" / "database" shutil.move(str(extracted), str(db_path)) print("done", file=sys.stderr) @@ -63,6 +86,35 @@ def _ensure_database(db_path, auto_download, update_database, ssl_certificate_lo return db_path +def _check_for_updates(db_path, ssl_certificate_location=None): + """Check if there's a newer version of the database available and update if needed.""" + try: + # Get the current commit hash from the database directory + version_file = db_path / ".version" + if version_file.exists(): + with open(version_file, 'r') as f: + current_hash = f.read().strip() + else: + current_hash = None + + # Get the latest commit hash + latest_hash = _get_latest_commit_hash() + + if latest_hash and (current_hash is None or current_hash != latest_hash): + print(f"Updating database from {current_hash or 'unknown'} to {latest_hash}", file=sys.stderr) + _download_database(db_path, ssl_certificate_location, commit_hash=latest_hash) + + # Save the new hash + with open(version_file, 'w') as f: + f.write(latest_hash) + + return True + return False + except Exception as e: + print(f"Failed to check for updates: {e}", file=sys.stderr) + return False + + def _load_catalog(db_path): key = str(db_path) if key in _catalog_cache: @@ -266,6 +318,10 @@ def __init__(self, shelf, book, page, *, db_path = Path(db_path) _ensure_database(db_path, auto_download, update_database, ssl_certificate_location) + + # Check for updates if requested + if update_database: + _check_for_updates(db_path, ssl_certificate_location) catalog = _load_catalog(db_path) key = (shelf, book, page) diff --git a/tests/test_refractiveindex.py b/tests/test_refractiveindex.py index d520a27..fec79ba 100644 --- a/tests/test_refractiveindex.py +++ b/tests/test_refractiveindex.py @@ -340,3 +340,25 @@ def test_epsilon_n_ik_consistency(self): # Real parts should be equal, imaginary parts opposite sign self.assertAlmostEqual(eps_minus.real, eps_plus.real) self.assertAlmostEqual(eps_minus.imag, -eps_plus.imag) + + +class TestDatabaseUpdates(unittest.TestCase): + """Tests for database update functionality.""" + + def test_database_version_file_exists(self): + """Test that database version file exists after material loading.""" + from pathlib import Path + version_file = Path.home() / '.refractiveindex.info-database' / '.version' + + # Load a material (this should create/update the version file) + m = ri.RefractiveIndexMaterial(shelf='main', book='SiO2', page='Malitson') + + # Check that version file exists + self.assertTrue(version_file.exists()) + + # Check that it contains some content (hash) + with open(version_file, 'r') as f: + hash_content = f.read().strip() + + # Should not be empty + self.assertGreater(len(hash_content), 0) From 7e2cb5aecf7a561cf7b2446ab36b9717e214b7fe Mon Sep 17 00:00:00 2001 From: "Matthew Emerson Spotnitz, PhD" Date: Tue, 16 Jun 2026 15:15:11 -0600 Subject: [PATCH 2/2] Add auto git hash update. Correct branch to main. Changes Made: 1. Updated the default branch of `polyanskiy/refractiveindex.info-database` from `master `to `main`, per the change made in that repo on 2026/01/07. 2. Automatic Database Hash Detection: - Added `_get_latest_commit_hash()` function that fetches the latest commit SHA from GitHub API - Modified `_download_database()` to automatically use the latest hash when needed - Added fallback to a known good commit if API fetch fails 3. Database Update System: - Added `_check_for_updates()` function that compares current database version with latest - Implemented version tracking with `.version` file in database directory - Added `update_database=True` parameter to trigger update checks - Added new test class `TestDatabaseUpdates `with test for version file functionality --- refractiveindex/refractiveindex.py | 69 +++++++++++++++++++++++++++--- tests/test_refractiveindex.py | 22 ++++++++++ 2 files changed, 85 insertions(+), 6 deletions(-) diff --git a/refractiveindex/refractiveindex.py b/refractiveindex/refractiveindex.py index acefb45..b8cb111 100644 --- a/refractiveindex/refractiveindex.py +++ b/refractiveindex/refractiveindex.py @@ -11,9 +11,9 @@ from yaml import BaseLoader -# Latest commit as of 2026-02-17 -# https://github.com/polyanskiy/refractiveindex.info-database/commits/master/ -_DATABASE_SHA = "a66ef8805cdb200973fc7ae9181587e1d89d14eb" +# Default database SHA. Latest SHA will be found automatically if this is not set. +# https://github.com/polyanskiy/refractiveindex.info-database/commits/main +_DATABASE_SHA = None _DEFAULT_DB_PATH = Path.home() / ".refractiveindex.info-database" @@ -21,14 +21,37 @@ _catalog_cache = {} -def _download_database(db_path, ssl_certificate_location=None): +def _get_latest_commit_hash(): + """Get the latest commit hash from the main branch of the refractiveindex.info-database repository.""" + try: + # Use GitHub API to get the latest commit SHA + import urllib.request + import json + + url = "https://api.github.com/repos/polyanskiy/refractiveindex.info-database/commits/main" + with urllib.request.urlopen(url) as response: + data = json.loads(response.read().decode('utf-8')) + return data['sha'] + except Exception as e: + print(f"Failed to get latest commit hash: {e}", file=sys.stderr) + return None + + +def _download_database(db_path, ssl_certificate_location=None, commit_hash=_DATABASE_SHA): import shutil import ssl import tempfile import urllib.request import zipfile - url = f"https://github.com/polyanskiy/refractiveindex.info-database/archive/{_DATABASE_SHA}.zip" + # Use provided commit hash or get the latest one + if commit_hash is None: + commit_hash = _get_latest_commit_hash() + if commit_hash is None: + # Fallback to a known good commit if we can't fetch the latest + commit_hash = "ff11b5897ef0754b15d939d921eb6c745693cbd1" + + url = f"https://github.com/polyanskiy/refractiveindex.info-database/archive/{commit_hash}.zip" if ssl_certificate_location is not None: if ssl_certificate_location == "": @@ -52,8 +75,14 @@ def _download_database(db_path, ssl_certificate_location=None): print("removing old database...", file=sys.stderr) shutil.rmtree(db_path) - extracted = Path(tempdir) / f"refractiveindex.info-database-{_DATABASE_SHA}" / "database" + extracted = Path(tempdir) / f"refractiveindex.info-database-{commit_hash}" / "database" shutil.move(str(extracted), str(db_path)) + + # Save the commit hash to version file + version_file = db_path / ".version" + with open(version_file, 'w') as f: + f.write(commit_hash) + print("done", file=sys.stderr) @@ -63,6 +92,30 @@ def _ensure_database(db_path, auto_download, update_database, ssl_certificate_lo return db_path +def _check_for_updates(db_path, ssl_certificate_location=None): + """Check if there's a newer version of the database available and update if needed.""" + try: + # Get the current commit hash from the database directory + version_file = db_path / ".version" + if version_file.exists(): + with open(version_file, 'r') as f: + current_hash = f.read().strip() + else: + current_hash = None + + # Get the latest commit hash + latest_hash = _get_latest_commit_hash() + + if latest_hash and (current_hash is None or current_hash != latest_hash): + print(f"Updating database from {current_hash or 'unknown'} to {latest_hash}", file=sys.stderr) + _download_database(db_path, ssl_certificate_location, commit_hash=latest_hash) + return True + return False + except Exception as e: + print(f"Failed to check for updates: {e}", file=sys.stderr) + return False + + def _load_catalog(db_path): key = str(db_path) if key in _catalog_cache: @@ -266,6 +319,10 @@ def __init__(self, shelf, book, page, *, db_path = Path(db_path) _ensure_database(db_path, auto_download, update_database, ssl_certificate_location) + + # Check for updates if requested + if update_database: + _check_for_updates(db_path, ssl_certificate_location) catalog = _load_catalog(db_path) key = (shelf, book, page) diff --git a/tests/test_refractiveindex.py b/tests/test_refractiveindex.py index d520a27..fec79ba 100644 --- a/tests/test_refractiveindex.py +++ b/tests/test_refractiveindex.py @@ -340,3 +340,25 @@ def test_epsilon_n_ik_consistency(self): # Real parts should be equal, imaginary parts opposite sign self.assertAlmostEqual(eps_minus.real, eps_plus.real) self.assertAlmostEqual(eps_minus.imag, -eps_plus.imag) + + +class TestDatabaseUpdates(unittest.TestCase): + """Tests for database update functionality.""" + + def test_database_version_file_exists(self): + """Test that database version file exists after material loading.""" + from pathlib import Path + version_file = Path.home() / '.refractiveindex.info-database' / '.version' + + # Load a material (this should create/update the version file) + m = ri.RefractiveIndexMaterial(shelf='main', book='SiO2', page='Malitson') + + # Check that version file exists + self.assertTrue(version_file.exists()) + + # Check that it contains some content (hash) + with open(version_file, 'r') as f: + hash_content = f.read().strip() + + # Should not be empty + self.assertGreater(len(hash_content), 0)