Skip to content

Commit

Permalink
auto-build docs and auto-update version merge
Browse files Browse the repository at this point in the history
  • Loading branch information
jmineau authored Aug 5, 2024
2 parents 5c4c282 + c723df4 commit 79edc5c
Show file tree
Hide file tree
Showing 10 changed files with 219 additions and 67 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/update_version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Update package version

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
permissions:
contents: write
jobs:
update_version:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.head.ref }}

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Run _version.py
run: python lair/_version.py

- name: Commit changes
run: |
git config --local user.name "GitHub Actions"
git config --local user.email "[email protected]"
git add lair/__init__.py
git commit -m "auto-update package version"
git push
20 changes: 0 additions & 20 deletions docs/Makefile

This file was deleted.

3 changes: 1 addition & 2 deletions docs/cron
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
0 0 * * * ~/lair/docs/dev-docs.sh
0 0 * * * ~/.lair-docs/docs/latest-docs.sh
0 0 * * * ~/software/python/miniconda3/envs/lair-dev/bin/python ~/.lair-docs/docs/make_docs.py --version dev latest
16 changes: 0 additions & 16 deletions docs/dev-docs.sh

This file was deleted.

24 changes: 0 additions & 24 deletions docs/latest-docs.sh

This file was deleted.

117 changes: 117 additions & 0 deletions docs/make_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""
Build the documentation for the project.
"""

import argparse
import os
from pathlib import Path
import subprocess
import requests

# Load the git module on CHPC
subprocess.run('module load git', shell=True)

# Parse args
parser = argparse.ArgumentParser()
parser.add_argument(
'--version',
nargs='*', # Accepts zero or more arguments
default='all', # Default to 'all' if no version is specified
help='The version of the documentation to build. Specify one or more versions, or "all" for all versions. Default: all',
type=str
)

args = parser.parse_args()


def sphinx_build(version: str, source: str | Path, build: str | Path):
"""
Build the documentation for a specific version of the project.
Parameters
----------
version : str
The version of the project to build the documentation for.
source : str
The path to the source documentation.
build : str
The path to the built documentation.
latest : str, optional
The path to the latest documentation. Default: None
"""

# Set the version in the environment
os.environ['LAIR_DOCS_VERSION'] = version

# Build the docs
os.system(f'sphinx-build {source} {build}')


def checkout_version(version: str):
"""
Checkout the specified version of the project.
Parameters
----------
version : str
The version of the project to checkout.
"""
target = 'main' if version == 'latest' else f'v{version}'
try:
subprocess.run(['git', 'checkout', target], check=True)
print(f"Checked out {target} successfully.")
except subprocess.CalledProcessError as e:
print(f"An error occurred while checking out {target}: {e}")


host = Path('~/public_html/lair').expanduser()
dev = Path('~/lair/docs').expanduser()
docs = Path('~/.lair-docs/docs').expanduser()
source = docs / 'source'

# Get list of versions from switcher
switcher_url = 'https://raw.githubusercontent.com/jmineau/lair/main/docs/versions.json'
switcher = requests.get(switcher_url).json()

# Determine which versions to build
to_build: list = args.version
if to_build == 'all':
to_build = [v['version'] for v in switcher]

print('to_build:', to_build)

# Change to the docs directory
os.chdir(docs)

# Pull the latest changes from the git repository
subprocess.run(['git', 'pull'], check=True)

# Iterate over the versions in the switcher
for v in switcher:
version = v['version']
name = v.get('name', '')

if version == 'dev' and 'dev' in to_build:
print(f"Building version {version} ({name})")
# Build the dev version from my local clone
sphinx_build('dev', source=dev / 'source', build=host / 'dev')

elif 'latest' in name:
if 'latest' not in to_build and version not in to_build:
# Skip building the latest version if it's not specified
continue
print(f"Building version {version} ({name})")
# Build the latest version from the head of main branch
checkout_version('latest')
sphinx_build(version, source=source, build=host / 'latest')
else:
if version not in to_build and name != '' and name not in to_build:
# Skip building the version if it's not specified
continue
print(f"Building version {version} ({name})")
# Checkout the specified version
checkout_version(version)
sphinx_build(version, source=source, build=host / version)

# Checkout the main branch
checkout_version('latest')
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
'navbar_start': ['navbar-logo', 'version-switcher'],
# 'show_version_warning_banner': True,
'switcher': {
'json_url': 'https://raw.githubusercontent.com/jmineau/lair/main/docs/switcher.json',
'json_url': 'https://raw.githubusercontent.com/jmineau/lair/main/docs/versions.json',
'version_match': version
}
}
Expand Down
6 changes: 3 additions & 3 deletions docs/switcher.json → docs/versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"url": "https://jamesmineau.chpc.utah.edu/lair/dev"
},
{
"name": "alpha",
"version": "alpha",
"url": "https://jamesmineau.chpc.utah.edu/lair",
"name": "alpha (latest)",
"version": "2024.08",
"url": "https://jamesmineau.chpc.utah.edu/lair/latest",
"preferred": true
}
]
2 changes: 1 addition & 1 deletion lair/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
from . import utils


__version__ = '2024.07.0'
__version__ = '2024.08.1'
63 changes: 63 additions & 0 deletions lair/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""
Automatically update the version of the package.
To be used with pre-commit hooks.
CalVer format: YYYY.MM.PATCH
Publishing versions in May, August, and December.
"""
import datetime
import os
import re

init_file = os.path.join(os.path.dirname(__file__), '__init__.py')

# Get the current version
with open(init_file, 'r') as f:
current_version = re.search(r'__version__ = [\'\"](\d{4}\.\d{2}\.\d+)[\'\"]',
f.read()).group(1)
cur_year, cur_month, cur_patch = current_version.split('.')

is_major_change = False

def get_version() -> str:
global is_major_change
now = datetime.datetime.now()
year = now.year
month = now.month

if month <= 5:
month = 5
elif month > 5 and month <= 8:
month = 8
else:
month = 12

# If the month has changed, reset the patch
if month != int(cur_month):
is_major_change = True
return f'{year}.{month:02d}.0'

patch = int(cur_patch) + 1

return f'{year}.{month:02d}.{patch}'


def update(version: str):
with open(init_file, 'r') as f:
lines = f.readlines()

updated_lines = []
for line in lines:
if line.startswith('__version__'):
updated_lines.append(f"__version__ = '{version}'\n")
else:
updated_lines.append(line)

with open(init_file, 'w') as f:
f.writelines(updated_lines)


if __name__ == "__main__":
version = get_version()
update(version)
print(f'Updated version to {version}')

0 comments on commit 79edc5c

Please sign in to comment.