Commit 1fb6bca0d4c93f71ae009f56f93e11763270d9cb

Authored by Etienne Pallier
1 parent 2614eae0
Exists in dev

refactorisations et simplifications dans pyros.py pour eviter des redondances

Showing 2 changed files with 36 additions and 49 deletions   Show diff stats
@@ -115,10 +115,7 @@ try: @@ -115,10 +115,7 @@ try:
115 except KeyError: 115 except KeyError:
116 WITH_DOCKER = False 116 WITH_DOCKER = False
117 117
118 -if type(WITH_DOCKER) is str and re.match("^y$|^Y$|^yes$|^Yes$", WITH_DOCKER.rstrip()) != None:  
119 - WITH_DOCKER = True  
120 -else:  
121 - WITH_DOCKER = False 118 +WITH_DOCKER = type(WITH_DOCKER) is str and re.match("^y$|^Y$|^yes$|^Yes$", WITH_DOCKER.rstrip()) != None
122 119
123 my_abs_path = os.path.dirname(os.path.realpath(__file__)) 120 my_abs_path = os.path.dirname(os.path.realpath(__file__))
124 # VENV_ROOT = "private" 121 # VENV_ROOT = "private"
@@ -568,8 +565,7 @@ def update(): @@ -568,8 +565,7 @@ def update():
568 def install(packages_only, database_only): 565 def install(packages_only, database_only):
569 with_packages = not database_only 566 with_packages = not database_only
570 with_database = not packages_only 567 with_database = not packages_only
571 - _install_or_update(UPDATE=False, with_packages=with_packages,  
572 - with_database=with_database) 568 + _install_or_update(UPDATE=False, with_packages=with_packages, with_database=with_database)
573 # install_or_update(UPDATE=False, with_packages=packages_only, with_database=database_only) 569 # install_or_update(UPDATE=False, with_packages=packages_only, with_database=database_only)
574 # install_or_update(UPDATE=False, packages_only=packages_only, database_only=database_only) 570 # install_or_update(UPDATE=False, packages_only=packages_only, database_only=database_only)
575 571
@@ -667,9 +663,9 @@ def _install_or_update(UPDATE: bool = False, with_packages: bool = True, with_da @@ -667,9 +663,9 @@ def _install_or_update(UPDATE: bool = False, with_packages: bool = True, with_da
667 # Upgrade pip if new version available 663 # Upgrade pip if new version available
668 os.system(VENV_PYTHON + ' -m pip install --upgrade pip') 664 os.system(VENV_PYTHON + ' -m pip install --upgrade pip')
669 # TODO: faire les apt-get intall aussi (indi, ...) 665 # TODO: faire les apt-get intall aussi (indi, ...)
670 - venv_pip_install2(GUITASTRO_PATH + '/install/requirements.txt', '-r') 666 + _venv_pip_install2(GUITASTRO_PATH + '/install/requirements.txt', '-r')
671 #venv_pip_install2(GUITASTRO_PATH + '/install/requirements_linux.txt', '-r') 667 #venv_pip_install2(GUITASTRO_PATH + '/install/requirements_linux.txt', '-r')
672 - venv_pip_install2(GUITASTRO_PATH + '/install/requirements_dev.txt', '-r') 668 + _venv_pip_install2(GUITASTRO_PATH + '/install/requirements_dev.txt', '-r')
673 ''' 669 '''
674 print("Guitastro : Generating (updating) API documentation (using Sphinx)") 670 print("Guitastro : Generating (updating) API documentation (using Sphinx)")
675 # Make html doc from RST 671 # Make html doc from RST
@@ -682,13 +678,13 @@ def _install_or_update(UPDATE: bool = False, with_packages: bool = True, with_da @@ -682,13 +678,13 @@ def _install_or_update(UPDATE: bool = False, with_packages: bool = True, with_da
682 change_dir("PREVIOUS") 678 change_dir("PREVIOUS")
683 ''' 679 '''
684 680
685 - # 5) Update PlantUML diagrams 681 + # 5) Update PlantUML diagrams (in UPDATE or INSTALL mode)
686 num += 1 682 num += 1
687 printFullTerm(Colors.BLUE, f"{num}) UPDATING UML DIAGRAMS") 683 printFullTerm(Colors.BLUE, f"{num}) UPDATING UML DIAGRAMS")
688 _update_plantuml_diags() or die() 684 _update_plantuml_diags() or die()
689 print(os.getcwd()) 685 print(os.getcwd())
690 686
691 - # 6) Update Sphinx API doc (Non Windows only) 687 + # 6) Update Sphinx API doc (Non Windows only) (in UPDATE or INSTALL mode)
692 num += 1 688 num += 1
693 printFullTerm(Colors.BLUE, f"{num}) UPDATING API DOC (with Sphinx)") 689 printFullTerm(Colors.BLUE, f"{num}) UPDATING API DOC (with Sphinx)")
694 if not IS_WINDOWS: 690 if not IS_WINDOWS:
@@ -1124,8 +1120,7 @@ def new_start(configfile: str, observatory: str, unit: str, computer_hostname: s @@ -1124,8 +1120,7 @@ def new_start(configfile: str, observatory: str, unit: str, computer_hostname: s
1124 # pip = "pip" if platform.system() == "Windows" else "pip3" 1120 # pip = "pip" if platform.system() == "Windows" else "pip3"
1125 # TODO: "python -m pip" au lieu de "pip" 1121 # TODO: "python -m pip" au lieu de "pip"
1126 pip = "pip" if IS_WINDOWS else "pip3" 1122 pip = "pip" if IS_WINDOWS else "pip3"
1127 - process = subprocess.Popen(  
1128 - pip + " install --user --upgrade pykwalify", shell=True) 1123 + process = subprocess.Popen(pip + " install --user --upgrade pykwalify", shell=True)
1129 process.wait() 1124 process.wait()
1130 if process.returncode == 0: 1125 if process.returncode == 0:
1131 # self.addExecuted(self.current_command, command) 1126 # self.addExecuted(self.current_command, command)
@@ -1306,12 +1301,9 @@ def _update_api_doc(): @@ -1306,12 +1301,9 @@ def _update_api_doc():
1306 print(os.getcwd()) 1301 print(os.getcwd())
1307 DOC_RST_PATH = "doc/doc_rst/" 1302 DOC_RST_PATH = "doc/doc_rst/"
1308 # 0) Upgrade pip if new version available 1303 # 0) Upgrade pip if new version available
1309 - if WITH_DOCKER:  
1310 - os.system(VENV_PYTHON + ' -m pip install --user --upgrade pip')  
1311 - else:  
1312 - os.system(VENV_PYTHON + ' -m pip install --upgrade pip') 1304 + _venv_pip_install2('pip', '--upgrade')
1313 # 1) install/update Sphinx requirements (only if not yet installed) 1305 # 1) install/update Sphinx requirements (only if not yet installed)
1314 - venv_pip_install2(DOC_RST_PATH+'requirements.txt', '-r') 1306 + _venv_pip_install2(DOC_RST_PATH+'requirements.txt', '-r')
1315 # 2) make html doc from RST doc 1307 # 2) make html doc from RST doc
1316 # cd doc/sourcedoc/ 1308 # cd doc/sourcedoc/
1317 change_dir(DOC_RST_PATH) 1309 change_dir(DOC_RST_PATH)
@@ -1483,20 +1475,24 @@ def notused_install_required(): @@ -1483,20 +1475,24 @@ def notused_install_required():
1483 exit(1) 1475 exit(1)
1484 1476
1485 1477
1486 -def venv_pip_install(package_name: str, options: str = ''):  
1487 - if WITH_DOCKER:  
1488 - os.system(VENV_PIP + ' install --user ' + options + ' ' + package_name)  
1489 - else:  
1490 - os.system(VENV_PIP + ' install ' + options + ' ' + package_name) 1478 +def _venv_pip_install_from_venv(PIP_MODULE: bool, package_name: str, options: str = ''):
  1479 + '''
  1480 + pip install package_name with options
  1481 + If PIP_MODULE install with 'python -m pip' else install with 'pip'
  1482 + If WITH_DOCKER => install in user mode (with --user option)
  1483 + '''
  1484 + #_pip_install(options + ' ' + package_name)
  1485 + pip_cmd = VENV_PYTHON+' -m pip' if PIP_MODULE else VENV_PIP
  1486 + usermode = '--user' if WITH_DOCKER else ''
  1487 + os.system(f'{pip_cmd} install {usermode} {options} {package_name}')
1491 1488
1492 1489
1493 -def venv_pip_install2(package_name: str, options: str = ''):  
1494 - if WITH_DOCKER:  
1495 - os.system(VENV_PYTHON + ' -m pip install --user '  
1496 - + options + ' ' + package_name)  
1497 - else:  
1498 - os.system(VENV_PYTHON + ' -m pip install '  
1499 - + options + ' ' + package_name) 1490 +def _venv_pip_install(package_name: str, options: str = ''):
  1491 + _venv_pip_install_from_venv(False, package_name, options)
  1492 +
  1493 +
  1494 +def _venv_pip_install2(package_name: str, options: str = ''):
  1495 + _venv_pip_install_from_venv(True, package_name, options)
1500 1496
1501 1497
1502 def install_venv(EVEN_IF_ALREADY_EXISTS: bool = False): 1498 def install_venv(EVEN_IF_ALREADY_EXISTS: bool = False):
@@ -1568,10 +1564,7 @@ def install_packages(): @@ -1568,10 +1564,7 @@ def install_packages():
1568 print(Colors.LOG_BLUE + "-----------------------------Upgrade pip, wheel, and setuptools" + 1564 print(Colors.LOG_BLUE + "-----------------------------Upgrade pip, wheel, and setuptools" +
1569 "-----------------------------"+END_OF_LINE + Colors.END) 1565 "-----------------------------"+END_OF_LINE + Colors.END)
1570 # Upgrade pip 1566 # Upgrade pip
1571 - if WITH_DOCKER:  
1572 - os.system(VENV_PYTHON + ' -m pip install --user --upgrade pip')  
1573 - else:  
1574 - os.system(VENV_PYTHON + ' -m pip install --upgrade pip') 1567 + _venv_pip_install2('pip', '--upgrade')
1575 ''' 1568 '''
1576 if (platform.system() == "Windows"): 1569 if (platform.system() == "Windows"):
1577 os.system(venv + '\Scripts\python -m pip install --upgrade pip') 1570 os.system(venv + '\Scripts\python -m pip install --upgrade pip')
@@ -1580,27 +1573,24 @@ def install_packages(): @@ -1580,27 +1573,24 @@ def install_packages():
1580 ''' 1573 '''
1581 1574
1582 # Pip upgrade wheel and setuptools 1575 # Pip upgrade wheel and setuptools
1583 - venv_pip_install('wheel', '--upgrade') 1576 + _venv_pip_install('wheel', '--upgrade')
1584 # os.system(VENV_PIP+' install --upgrade wheel') 1577 # os.system(VENV_PIP+' install --upgrade wheel')
1585 # Not working with python 3.8 (on 17/02/2022) 1578 # Not working with python 3.8 (on 17/02/2022)
1586 # venv_pip_install('setuptools', '--upgrade') 1579 # venv_pip_install('setuptools', '--upgrade')
1587 - if WITH_DOCKER:  
1588 - os.system(VENV_PIP+' install --user setuptools==58')  
1589 - else:  
1590 - os.system(VENV_PIP+' install setuptools==58') 1580 + _venv_pip_install('setuptools==58')
1591 1581
1592 # Pip install required packages from REQUIREMENTS file 1582 # Pip install required packages from REQUIREMENTS file
1593 print() 1583 print()
1594 1584
1595 # General normal packages 1585 # General normal packages
1596 print(Colors.LOG_BLUE + "-----------------------------Installing python packages via pip-----------------------------" + Colors.END) 1586 print(Colors.LOG_BLUE + "-----------------------------Installing python packages via pip-----------------------------" + Colors.END)
1597 - venv_pip_install('../install/'+REQUIREMENTS, '-r') 1587 + _venv_pip_install('../install/'+REQUIREMENTS, '-r')
1598 # os.system(VENV_PIP+' install -r ../install' + os.sep + REQUIREMENTS) 1588 # os.system(VENV_PIP+' install -r ../install' + os.sep + REQUIREMENTS)
1599 1589
1600 # DEV only packages 1590 # DEV only packages
1601 if DEV: 1591 if DEV:
1602 print(Colors.LOG_BLUE + "-----------------------------Installing DEV python packages via pip-----------------------------" + Colors.END) 1592 print(Colors.LOG_BLUE + "-----------------------------Installing DEV python packages via pip-----------------------------" + Colors.END)
1603 - venv_pip_install('../install/'+REQUIREMENTS_DEV, '-r') 1593 + _venv_pip_install('../install/'+REQUIREMENTS_DEV, '-r')
1604 print("FIN INSTALL PACKAGES") 1594 print("FIN INSTALL PACKAGES")
1605 return None 1595 return None
1606 1596
@@ -1657,12 +1647,9 @@ def install_observatory(observatory): @@ -1657,12 +1647,9 @@ def install_observatory(observatory):
1657 from git import Repo, GitError 1647 from git import Repo, GitError
1658 except: 1648 except:
1659 pip = "pip" if IS_WINDOWS else "pip3" 1649 pip = "pip" if IS_WINDOWS else "pip3"
1660 - if WITH_DOCKER:  
1661 - process = subprocess.Popen(  
1662 - pip + " install --user --upgrade GitPython", shell=True)  
1663 - else:  
1664 - process = subprocess.Popen(  
1665 - pip + " install --upgrade GitPython", shell=True) 1650 +
  1651 + usermode = '--user' if WITH_DOCKER else ''
  1652 + process = subprocess.Popen(pip + f" install {usermode} --upgrade GitPython", shell=True)
1666 process.wait() 1653 process.wait()
1667 if process.returncode == 0: 1654 if process.returncode == 0:
1668 # self.addExecuted(self.current_command, command) 1655 # self.addExecuted(self.current_command, command)
src/core/pyros_django/majordome/agent/A_Basic.py
1 #!/usr/bin/env python3 1 #!/usr/bin/env python3
2 2
3 3
4 -import time  
5 -import sys, argparse 4 +#import time
  5 +import sys
  6 +import argparse
6 import os 7 import os
7 8
8 ##import utils.Logger as L 9 ##import utils.Logger as L
@@ -23,7 +24,6 @@ for short_path in short_paths: @@ -23,7 +24,6 @@ for short_path in short_paths:
23 if path not in sys.path: 24 if path not in sys.path:
24 sys.path.insert(0, path) 25 sys.path.insert(0, path)
25 26
26 -  
27 #from src.core.pyros_django.common.models import AgentCmd 27 #from src.core.pyros_django.common.models import AgentCmd
28 #from src.core.pyros_django.majordome.agent.Agent import Agent, build_agent 28 #from src.core.pyros_django.majordome.agent.Agent import Agent, build_agent
29 from majordome.agent.Agent import Agent, build_agent 29 from majordome.agent.Agent import Agent, build_agent