TNO Intern

Commit 624574a6 authored by Arjo Segers's avatar Arjo Segers
Browse files

Use uv package manager to setup virtual environment.

parent d6532e19
Loading
Loading
Loading
Loading

bin/cso

100755 → 100644
+109 −95
Original line number Diff line number Diff line
#! /usr/bin/env python3

"""
CSO - CAMS Satellite Operator

Pre-proocessor script.
For arguments, see:
  cso --help

History

2020-05, Arjo Segers
  Initital version.

2023-08, Arjo Segers
  Define 'CSO_PREFIX' environment variable for use in rcfile settings.

2023-11, Arjo Segers
  Reformatted using 'black'.
  
2023-08, Arjo Segers
  Define environment variables 'CSO_RCFILE' and 'CSO_RCDIR'.
#! /bin/bash

#
# CSO - CAMS Satellite Operator
#
# Driver script to setup python virtural environment, managed by UV.
#
#
# CHANGES
#
# 2029-09, Arjo Segers
#   Renewed from original python script to use uv package manager.
#

# script name:
name=`basename $0`

## info ...
#echo "${name} - "
#echo "${name} - ** CSO **"
#echo "${name} - "

#
# location
#

# full path to this script:
script=`realpath $0`
# 'bin' directory:
bindir=`dirname ${script}`
# installation prefix:
prefix=`dirname ${bindir}`

## info ..
#echo "${name} - locations:"
#echo "${name} -   script: ${script}"
#echo "${name} -   bindir: ${bindir}"
#echo "${name} -   prefix: ${prefix}"
#echo "${name} - "


# 
# uv package mananager
#

## info ...
#echo "${name} - check if uv package manager exist ..."
# already present?
if ( uv --version > /dev/null ); then
  # call directly:
  uv='uv'
else
  # destination for uv installation:
  local="${HOME}/.local"
  # info ...
  echo "${name} -   install uv package manager in ${local}/bin ..."

  # from:
  #   https://docs.astral.sh/uv/
  # local copy of installation script:
  script="install-uv.sh"
  # download:
  curl --location --silent --show-error --fail \
        --output ${script} \
        https://astral.sh/uv/install.sh
  # make executable:
  chmod u+x ${script}
  # settings:
  export UV_NO_MODIFY_PATH=1  # no changes to ~/.bashrc etc
  # run:
  ./${script}
  # cleanup:
  rm -f ${script}
  
  # store location:
  uv="${local}/bin/uv"
fi


#
# run
#

"""
# set environment:
export CSO_PREFIX=${prefix}

## info ..
#echo "${name} - activate virtural environment ..."
# setup venv:
source ${prefix}/.venv/bin/activate

# -------------------------------------------------
# modules
# -------------------------------------------------
## info ..
#echo "${name} - start 'cso' ..."

# modules:
import sys
import os
import logging
# run 'cso' script, the 'pyproject.toml' file  defines
# that this should import the `cso` module  and run method `main`:
#
#    [project.scripts]
#    cso = "cso:main"
#
# run:
${uv} run cso $*

# prefix of CSO installation:
prefix = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), os.pardir))
# extend path:
sys.path.insert(0, os.path.join(prefix, "src"))
sys.path.insert(0, os.path.join(prefix, "src", "utopya"))
sys.path.insert(0, os.path.join(prefix, "src", "cso"))
## info ..
#echo "${name} - leaving virtual environment ..."
# done:
deactivate

# tools:
import utopya

#
# end
#

# -------------------------------------------------
# begin
# -------------------------------------------------
## info ...
#echo "${name} - "
#echo "${name} - ** end **"
#echo "${name} - "
# set environment:
os.environ["CSO_PREFIX"] = prefix
# dummy values to ensure that settings could be read first to setup logging:
os.environ['CSO_RCFILE'] = 'None'
os.environ['CSO_RCDIR'] = 'None'

# init script:
utos = utopya.UtopyaRunScriptRc()

# setup standard command line arguments, and also enable rcfile arguments:
utos.ArgumentsSetup(description="CAMS Satellite Operator", rcbase="cso")

# evaluate known arguments, store the other ;
# might show help text and exit:
args, xargs = utos.ArgumentsParse()

# store full path to settings:
os.environ['CSO_RCFILE'] = os.path.abspath( args.rcfile )
# location of settings file:
os.environ['CSO_RCDIR'] = os.path.dirname( os.path.abspath( args.rcfile ) )

# start, shout info:
logging.info(f"")
logging.info(f"** CSO - CAMS Satellite Operator **")
logging.info(f"")
logging.info(f"installation prefix  : {prefix}")
logging.info(f"input rcfile         : {args.rcfile}")
logging.info(f"settings base        : {args.rcbase}")
logging.info(f"")
logging.info(f"environment:")
for key in ["CSO_PREFIX","CSO_RCFILE","CSO_RCDIR"]:
    logging.info(f"  {key} = {os.environ[key]}")
# endfor
logging.info(f"")

# info ...
logging.info(f"start job tree ...")
# import class as defined in settings by 'cso.class' key;
# should be (derived from) 'utopya.JobStep' :
jbs_cls = utos.ImportClass("class")
# init job step object, use the 'rcbase' as name:
jbs = jbs_cls(args.rcbase, args.rcfile)
# start first:
jbs.Start(single=args.single)

# info:
logging.info(f"")
logging.info(f"** end **")
logging.info(f"")


# -------------------------------------------------
# end
# -------------------------------------------------
+2 −1
Original line number Diff line number Diff line
@@ -59,4 +59,5 @@ docs = [
]

[project.scripts]
cso = "cso.scripts.cli:main"
cso = "cso:main"
+62 −0
Original line number Diff line number Diff line
@@ -166,6 +166,68 @@ from .cso_catalogue import *
from .cso_tools import *


# -------------------------------------------------
# main
# -------------------------------------------------

def main():

    # modules:
    import sys
    import os

    # info ...
    #print(f"version: {__version__}")
    
    # testing ...
    #print(f"arguments:")
    #for arg in sys.argv:
    #s    print(f"  {arg}")

    # init script:
    utos = utopya.UtopyaRunScriptRc()

    # setup standard command line arguments, and also enable rcfile arguments:
    utos.ArgumentsSetup(description="CAMS Satellite Operator", rcbase="cso")

    # evaluate known arguments, store the other ;
    # might show help text and exit:
    args, xargs = utos.ArgumentsParse()

    # installation prefix:
    prefix = os.environ["CSO_PREFIX"]
    # store full path to settings:
    os.environ['CSO_RCFILE'] = os.path.abspath( args.rcfile )
    # location of settings file:
    os.environ['CSO_RCDIR'] = os.path.dirname( os.path.abspath( args.rcfile ) )

    # start, show info:
    logging.info(f"")
    logging.info(f"** CSO - CAMS Satellite Operator **")
    logging.info(f"")
    logging.info(f"installation prefix  : {prefix}")
    logging.info(f"input rcfile         : {args.rcfile}")
    logging.info(f"settings base        : {args.rcbase}")
    logging.info(f"")
    logging.info(f"environment:")
    for key in ["CSO_PREFIX","CSO_RCFILE","CSO_RCDIR"]:
        logging.info(f"  {key} = {os.environ[key]}")
    # endfor
    logging.info(f"")

    # info ...
    logging.info(f"start job tree ...")
    # import class as defined in settings by 'cso.class' key;
    # should be (derived from) 'utopya.JobStep' :
    jbs_cls = utos.ImportClass("class")
    # init job step object, use the 'rcbase' as name:
    jbs = jbs_cls(args.rcbase, args.rcfile)
    # start first:
    jbs.Start(single=args.single)

#endif


# -------------------------------------------------
# end
# -------------------------------------------------

src/cso/scripts/__init__.py

deleted100644 → 0
+0 −0

Empty file deleted.

src/cso/scripts/cli.py

deleted100644 → 0
+0 −97
Original line number Diff line number Diff line
import subprocess
from typing import Optional

import typer
import sys
import os
import webbrowser

from cso import __package__, __version__

# import utopya

# prefix of CSO installation:
prefix = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), os.pardir))
# extend path:
sys.path.insert(0, os.path.join(prefix, "src/cso"))
sys.path.insert(0, os.path.join(prefix, "src/utopya"))

main = typer.Typer()


def version_callback(value: bool):
    if not value:
        return
    typer.echo(f"🛰️  {__package__} {__version__}")
    raise typer.Exit()


@main.callback()
def callback(
    version: Optional[bool] = typer.Option(None, "--version", "-V", callback=version_callback),
):
    """🛰️  CAMS Satellite Operator (CSO) Command Line Interface"""


@main.command()
def run(rc_file: str = typer.Argument(..., help="Provide .rc file")):
    """Run cso based on settings provided in .rc file (e.g., cso run config/tutorial/tutorial.rc)"""
    raise NotImplementedError
    # # set environment:
    # os.environ["CSO_PREFIX"] = prefix
    # # dummy values to ensure that settings could be read first to setup logging:
    # os.environ["CSO_RCFILE"] = "None"
    # os.environ["CSO_RCDIR"] = "None"

    # # init script:
    # utos = utopya.UtopyaRunScriptRc()

    # # setup standard command line arguments, and also enable rcfile arguments:
    # utos.ArgumentsSetup(description="CAMS Satellite Operator", rcbase="cso")

    # # evaluate known arguments, store the other ;
    # # might show help text and exit:
    # args, xargs = utos.ArgumentsParse()

    # # store full path to settings:
    # os.environ["CSO_RCFILE"] = os.path.abspath(args.rcfile)
    # # location of settings file:
    # os.environ["CSO_RCDIR"] = os.path.dirname(os.path.abspath(args.rcfile))

    # # start, shout info:
    # logging.info(f"")
    # logging.info(f"** CSO - CAMS Satellite Operator **")
    # logging.info(f"")
    # logging.info(f"installation prefix  : {prefix}")
    # logging.info(f"input rcfile         : {args.rcfile}")
    # logging.info(f"settings base        : {args.rcbase}")
    # logging.info(f"")
    # logging.info(f"environment:")
    # for key in ["CSO_PREFIX", "CSO_RCFILE", "CSO_RCDIR"]:
    #     logging.info(f"  {key} = {os.environ[key]}")
    # # endfor
    # logging.info(f"")

    # # info ...
    # logging.info(f"start job tree ...")
    # # import class as defined in settings by 'cso.class' key;
    # # should be (derived from) 'utopya.JobStep' :
    # jbs_cls = utos.ImportClass("class")
    # # init job step object, use the 'rcbase' as name:
    # jbs = jbs_cls(args.rcbase, args.rcfile)
    # # start first:
    # jbs.Start(single=args.single)

    # # info:
    # logging.info(f"")
    # logging.info(f"** end **")
    # logging.info(f"")


# -------------------------------------------------
# end
# -------------------------------------------------


if __name__ == "__main__":
    main()