Commit 4b3bcc31 authored by PENGUEN Julien's avatar PENGUEN Julien

Initial commit

parents
Pipeline #758 failed with stages
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.so
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar
*.tar
*.zip
# Logs and databases #
######################
*.log
*.sql
*.sqlite
# OS generated files #
######################
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
# Sphinx documentation #
########################
docs/_build/
# Python stuff #
################
eggs/
.eggs/
*.egg-info/
*.egg
__pycache__/
*.pyc
.pytest_cache/
*~
ETC42 python module
===================
FROM ubuntu:16.04
FROM java:8
MAINTAINER Julien Penguen<julien.penguen@lam.fr>
ARG uidval
ARG username
ARG etc42_jar
RUN mkdir /var/etc42
RUN useradd -r -u $uidval -m $username
USER $username
WORKDIR /home/$username
ADD $etc42_jar /var/etc42/
# write your program options here e.g: option = string
etc42_url = http://cesam.lam.fr/Apps/ETC/webstart/
etc42_version = 1.0.0.11
import os
import docker
import wget
import argparse
# -------------------------#
# get_path function
def get_path(file_name):
"""Get the full path of file in configuration directory.
:param file_name: Name of the file.
:return: Full path of configuration file.
:Example:
get_path("my_conf.conf") # -> /python/package/path/my_conf.conf
"""
return os.path.join(os.path.dirname(__file__), file_name)
# -------------------------- #
# get_directory_path function
def get_directory_path():
"""
Get the full path of directory python package.
:return: path of directory file.
:Example:
get_directory_path() # -> /python/package/path
"""
return os.path.dirname(__file__)
# -------------------------- #
# get_args_from_file function
def get_args_from_file(file_name, args):
"""
Get arguments value from configuration file.
:param file_name: name of the configuration file
:param args: object (ex. retrieve from argparse module)
Get arguments value from configuration file. Value has to be formatted as
``option = string``. To comment use ``#``.
The function modifies every ``None`` args attribue.
The function add new args attribute.
:Example:
In ``my_conf.conf`` file:
arg1 = 2
arg2 = foo
class MyCls():
arg1=1
args = MyCls()
get_args_from_file("my_conf.conf",args)
args.arg1 # -> 1
args.arg2 # -> foo
"""
with open(file_name, 'r') as ff:
lines = ff.readlines()
for line in lines:
try:
key, value = line.replace('\n', '').split('#')[0].split("=")
except ValueError:
continue
try:
if getattr(args, key.strip()) is None:
setattr(args, key.strip(), value.strip())
except AttributeError:
setattr(args, key.strip(), value.strip())
# --------------------- #
# get_jar_etc42 function
def get_jar_etc42(etc42_jar, etc42_url):
"""
Upload the etc42 jar
:param etc42_jar: Name of the jar version of etc42.
:param etc42_url: Url of etc42.
:return: True or False, success or failure of uploading operation.
:Example:
get_jar_etc42("etc42-1.0.0.11.jar",etc42_url) # -> True (uploading with success etc42-1.0.0.11.jar)
# -> False (Failure)
"""
print('- Download of {} in progress ...'.format(etc42_jar))
try:
print('=> Connection to cesam.lam.fr')
url = etc42_url + etc42_jar
wget.download(url, get_path(etc42_jar))
print('')
print('=> Full {} download \n'.format(etc42_jar))
return True
except Exception as e:
print('=> Error while downloading : ' + str(e))
return False
# ------------------------ #
# get_image_etc42 function
def get_image_etc42(client, etc42_version):
"""
Get the docker image etc42 image_etc42-(etc42_version)
:param client: Client docker.
:param etc42_version: Number of etc42 version.
:return: True or False, if docker image etc42 exist or not.
:Example:
get_image_etc42(client,"1.0.0.11") # -> True (image_etc42-1.0.0.11 exist)
# -> False (image_etc42-1.0.0.11 not exist)
"""
image_name = 'image_etc42-' + etc42_version + ':latest'
try:
result = client.images.get(image_name)
return True
except Exception as e:
return False
# ------------- #
# build function
def build(client, etc42_version, etc42_jar):
"""
Build the docker image etc42 image_etc42-(etc42_version)
:param client: Client docker.
:param etc42_version: Number of etc42 version.
:param etc42_jar: etc42 jar.
:return: True or False, success or failure of building operation.
:Example:
get_image_etc42(client,"1.0.0.11") # -> True (Building with success image_etc42-1.0.0.11)
# -> False (Failure)
"""
print('- Creating the etc42-{} docker image in progress ...'.format(etc42_version))
try:
image, log = client.images.build(
path = get_directory_path(),
dockerfile = 'Dockerfile',
tag = 'image_etc42-' + etc42_version,
buildargs = {
'username' : os.environ['USER'],
'uidval' : str(os.getuid()),
'etc42_jar' : etc42_jar,
},
rm = True
)
print('=> creating the etc42-{} docker image successfully \n'.format(etc42_version))
return True
except Exception as e:
print('type error: ' + str(e))
return False
# ----------- #
# run function
def run(client, etc42_version, etc42_jar):
"""
run the docker container with docker image etc42
:param client: Client docker.
:param etc42_version: Number of etc42 version.
:param etc42_jar: etc42 jar.
"""
print('- Container construction and launch in progress ... \n')
if 'SAMP_HUB' in os.environ:
val_samp_hub = os.environ['SAMP_HUB']
else:
val_samp_hub = os.environ['HOME'] + os.environ['USER'] + "/.samp"
client.containers.run(
image = 'image_etc42-' + etc42_version,
name = 'container_etc42-' + etc42_version,
network = 'host',
environment = {
'DISPLAY' : os.environ['DISPLAY'],
'USER' : os.environ['USER'],
'HOME' : os.environ['HOME'],
'SAMP_HUB' : val_samp_hub
},
volumes = {
'/tmp/.X11-unix' : {'bind' : '/tmp/.X11-unix', 'mode' : 'rw'},
os.environ['HOME'] : {'bind' : os.environ['HOME'], 'mode' : 'rw'}
},
command = 'java -jar /var/etc42/' + etc42_jar,
remove = 'true')
# ------------ #
# main function
def main():
"""
etc42 entry point.
Parse arguments and call the build()
and run() functions
"""
# ----------------------------- #
# --- section etc42 version --- #
# ----------------------------- #
parser = argparse.ArgumentParser()
args = parser.parse_args()
get_args_from_file(get_path('etc42.conf'), args)
etc42_url = args.etc42_url
etc42_version = args.etc42_version
etc42_jar = 'ETC-Jar-' + etc42_version + '.jar'
# --------------------------------------- #
print('')
print('====================================')
print('====================================')
print(' Universal Exposure Time Calculator ')
print('')
print(' ETC42 ')
print('====================================')
print('====================================')
print('')
print('- etc42 version: {}'.format(etc42_version))
print('- etc42 authors: The LAM ETC42 developers')
print('- etc42 license: GPLv3')
print('')
print('====================================')
print('')
# ----------------------------------------------- #
# --- section etc42 docker image management --- #
# ----------------------------------------------- #
client = docker.from_env()
print('- Searching for the current etc42-{} docker image ...'.format(etc42_version))
if get_image_etc42(client, etc42_version) is False:
print('=> The etc42-{} docker image does not exist \n'.format(etc42_version))
# --- etc42-[version].jar download --- #
print('- Searching for the current {} ...'.format(etc42_jar))
if os.path.isfile(get_path(etc42_jar)):
print('=> The {} already exist \n'.format(etc42_jar))
result_telechargement = True
else:
print('=> The {} does not exist \n'.format(etc42_jar))
result_telechargement = get_jar_etc42(etc42_jar, etc42_url)
# ------------------------------------ #
# --- etc42-[version] image construction ----- #
if result_telechargement:
result_image = build(client, etc42_version, etc42_jar)
else:
print('=> Error while downloading of {}'.format(etc42_jar))
else:
print('=> The etc42-{} docker image already exist \n'.format(etc42_version))
result_image = True
# ------------------------------------------- #
# --------------------------------------------------- #
# --- section etc42 docker container management --- #
# --------------------------------------------------- #
if result_image :
run(client, etc42_version, etc42_jar) # container launching
else:
print('=> Error while building of {} docker image'.format(etc42_jar))
# --------------------------------------- #
print('=> End of etc42 program')
print('')
[aliases]
test=pytest
import sys
__minimum_python_version__ = '3.5.2'
if sys.version_info < tuple((int(val) for val in __minimum_python_version__.split('.'))):
sys.stderr.write("ERROR: etc42 requires Python {} or later\n".format(
__minimum_python_version__))
sys.exit(1)
import os
from setuptools import setup, find_packages
NAME = 'etc42' #PEP 508
VERSION = '0.0.0' #PEP 440
# Add any necessary entry points
entry_points = {}
# Command-line scripts
entry_points['console_scripts'] = [
'etc42 = etc42.etc42.etc42:main'
]
setup(name=NAME,
version=VERSION,
description='ETC42 Universal Exposure Time Calculator',
long_description=open(os.path.join(os.path.dirname(__file__),
'README.md')).read(),
url='https://lam.fr/',
author='The LAM ETC42 developers',
license='GPLv3',
packages=find_packages(),
requires=['docker'],
setup_requires=[],
install_requires=[],
classifiers=[
'Development Status :: 1 - Planning',
'Intended Audience :: Science/Research',
'Operating System :: OS Independent',
'Programming Language :: Python',
'License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)', # QUESTION : same as above
'Topic :: Scientific/Engineering :: Astronomy',
'Topic :: Scientific/Engineering :: Physics'
],
include_package_data=True,
entry_points=entry_points,
python_requires='>=' + __minimum_python_version__,
tests_require=['pytest']
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment