Commit ad6d274a4a73f7cb55ccc25f46d18cca5051b7f7

Authored by Etienne Pallier
1 parent 1cffbf1c
Exists in dev

Restructuration du projet - phase 2 :

pyros_django/ => src/core/pyros_django/

il a fallu faire des changements un peu partout... pour que tout
remarche
.pydevproject
1 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 2 <?eclipse-pydev version="1.0"?><pydev_project>
3   -
  3 +
  4 +
4 5  
5 6  
6 7  
7 8  
8 9 <pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
9   -
  10 +
  11 +
10 12  
11 13  
12 14  
13 15  
14 16 <pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
15   -
  17 +
  18 +
16 19  
17 20  
18 21  
19 22  
20 23 <pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
21   -
22   - <path>/${PROJECT_DIR_NAME}/pyros_django</path>
23   -
  24 + <path>/${PROJECT_DIR_NAME}/src</path>
24 25 <path>/${PROJECT_DIR_NAME}/simulators</path>
25   -
26 26 </pydev_pathproperty>
27   -
  27 +
  28 +
28 29  
29 30  
30 31  
31 32  
32 33 <pydev_variables_property name="org.python.pydev.PROJECT_VARIABLE_SUBSTITUTION">
33   -
  34 +
  35 +
34 36  
35 37  
36 38  
37 39  
38 40 <key>DJANGO_MANAGE_LOCATION</key>
39   -
  41 +
  42 +
40 43  
41 44  
42 45  
43 46  
44   - <value>pyros_django/manage.py</value>
45   -
  47 + <value>src/core/pyros_django/manage.py</value>
  48 +
  49 +
46 50  
47 51  
48 52  
49 53  
50 54 <key>DJANGO_SETTINGS_MODULE</key>
51   -
  55 +
  56 +
52 57  
53 58  
54 59  
55 60  
56 61 <value>pyros.settings</value>
57   -
  62 +
  63 +
58 64  
59 65  
60 66  
61 67  
62 68 </pydev_variables_property>
63   -
  69 +
  70 +
64 71  
65 72  
66 73  
... ...
devices_channel/README.md
1 1  
2   -VERSION: 0.30.3
  2 +VERSION: 0.30.4
3 3  
4   -Date: 13/06/2019
  4 +Date: 14/06/2019
5 5  
6 6 By: epallier@irap.omp.eu
7 7  
8   -Comment : Restructuration du projet - phase 1 : src/ => pyros_django/
  8 +Comment : Restructuration du projet - phase 2 : pyros_django/ => src/core/pyros_django/
9 9  
10 10  
11 11  
... ... @@ -13,7 +13,10 @@ Comment : Restructuration du projet - phase 1 : src/ =&gt; pyros_django/
13 13 ********************************************************************************************
14 14 HISTORY
15 15  
16   -11/06/2019
  16 +14/06/2019 (v0.30.3)
  17 + - Restructuration du projet - phase 1 : src/ => pyros_django/
  18 +
  19 +11/06/2019 (v0.30.2)
17 20 - Ajoutรฉ options "test" et "testall" dans pyros.py pour exรฉcution des (anciens) tests unitaires
18 21 - Restructuring : private/ => venv/
19 22  
... ...
devices_channel/client/devices_abstract/device_controller_abstract.py
... ... @@ -22,7 +22,7 @@ import time
22 22 sys.path.append("..")
23 23 # from src_socket/client/
24 24 sys.path.append("../../..")
25   -import pyros_django.utils.celme as celme
  25 +import src.core.pyros_django.utils.celme as celme
26 26 from devices_channel.client.logs import *
27 27  
28 28  
... ...
devices_channel/client/devices_abstract/plc_controller_abstract.py
... ... @@ -20,7 +20,7 @@ import time
20 20 sys.path.append("..")
21 21 # from src_socket/client/
22 22 sys.path.append("../../..")
23   -import pyros_django.utils.celme as celme
  23 +import src.core.pyros_django.utils.celme as celme
24 24  
25 25  
26 26 # Local application imports
... ...
devices_channel/client/devices_abstract/telescope_controller_abstract.py
... ... @@ -20,7 +20,7 @@ import time
20 20 sys.path.append("..")
21 21 # from src_socket/client/
22 22 sys.path.append("../../..")
23   -import pyros_django.utils.celme as celme
  23 +import src.core.pyros_django.utils.celme as celme
24 24  
25 25  
26 26 # Local application imports
... ...
pyros.py
... ... @@ -21,8 +21,12 @@ import time
21 21  
22 22 DEBUG = False
23 23  
  24 +PYROS_DJANGO_BASE_DIR = "src/core/pyros_django"
  25 +
24 26 INIT_FIXTURE = "initial_fixture.json"
25 27  
  28 +_previous_dir = None
  29 +
26 30 AGENTS = {
27 31 #"agentX" : "agent",
28 32 "agent" : "Agent",
... ... @@ -243,7 +247,7 @@ def shell():
243 247 print(" >>> agent.run(2) (=> will run 2 iterations)")
244 248 print(" >>> cmd = agent.send_command('AgentB','eval 2+2')")
245 249 print(" >>> cmd")
246   - print(" >>> cmd1.get_updated_result()")
  250 + print(" >>> cmd.get_updated_result()")
247 251 print(" >>> ...")
248 252 print(" - See documentation, section 'Play with a pyros agent' inside the chapter 'Running pyros' for more details")
249 253 print()
... ... @@ -259,11 +263,13 @@ def shell():
259 263 print()
260 264 print("Type 'exit()' to quit")
261 265 print()
262   - os.chdir("pyros_django/")
  266 + change_dir(PYROS_DJANGO_BASE_DIR)
  267 +
263 268 # execProcess("python install.py install")
264 269 if not test_mode(): execProcessFromVenv("manage.py shell")
265 270 # Go back to the initial dir
266   - os.chdir("../")
  271 + #os.chdir("../")
  272 + change_dir("PREVIOUS")
267 273 return True
268 274  
269 275  
... ... @@ -276,7 +282,7 @@ def dbshell():
276 282 print("Type 'exit' to quit")
277 283 print()
278 284 # execProcess("python install.py install")
279   - if not test_mode(): execProcessFromVenv("pyros_django/manage.py dbshell")
  285 + if not test_mode(): execProcessFromVenv(PYROS_DJANGO_BASE_DIR+"/manage.py dbshell")
280 286 # Go back to the initial dir
281 287 return True
282 288  
... ... @@ -307,17 +313,17 @@ def test():
307 313 apps = ['common', 'scheduler', 'routine_manager', 'user_manager', 'alert_manager.tests.TestStrategyChange']
308 314 for app in apps:
309 315 _loaddata() or die()
310   - change_dir("pyros_django")
  316 + change_dir(PYROS_DJANGO_BASE_DIR)
311 317 execProcessFromVenv('manage.py test ' + app) or die()
312   - change_dir("..")
  318 + change_dir("PREVIOUS")
313 319 # execProcess("python install.py install")
314 320 return True
315 321  
316 322 @pyros_launcher.command(help="Run ALL tests")
317 323 def testall():
318   - change_dir("pyros_django")
  324 + change_dir(PYROS_DJANGO_BASE_DIR)
319 325 execProcessFromVenvAsync("manage.py test")
320   - change_dir("..")
  326 + change_dir("PREVIOUS")
321 327 return True
322 328  
323 329  
... ... @@ -386,7 +392,7 @@ def start(agent:str, configfile:str):
386 392 if configfile:
387 393 print("With config file", configfile)
388 394 else:
389   - configfile = ''
  395 + configfile = ''
390 396 #if test_mode(): print("in test mode")
391 397 #if verbose_mode(): print("in verbose mode")
392 398  
... ... @@ -425,11 +431,11 @@ def start(agent:str, configfile:str):
425 431 # Agent "webserver"
426 432 if agent_name == "webserver":
427 433 cmd = "manage.py runserver"
428   - os.chdir("pyros_django")
  434 + os.chdir(PYROS_DJANGO_BASE_DIR)
429 435 #if not test_mode(): execProcessFromVenv("start_agent.py " + agent_name + " " + configfile)
430 436  
431 437 elif agent_name == "agentM":
432   - os.chdir("pyros_django/monitoring/")
  438 + os.chdir(PYROS_DJANGO_BASE_DIR+"/monitoring/")
433 439 cmd = f"Agent{agent_name[5:]}.py"
434 440 if test_mode(): cmd += " -t"
435 441 if configfile: cmd += " {configfile}"
... ... @@ -438,7 +444,7 @@ def start(agent:str, configfile:str):
438 444 elif agent_name.startswith("agent"):
439 445 # Run agent without actual commands sent to devices (FOR_REAL=False)
440 446 ##agentX.run(FOR_REAL=True)
441   - os.chdir("pyros_django/agent/")
  447 + os.chdir(PYROS_DJANGO_BASE_DIR+"/agent/")
442 448 #cmd = "-m AgentX"
443 449 #cmd = f" Agent{agent_name[5:]}.py {configfile}"
444 450 cmd = f"Agent{agent_name[5:]}.py"
... ... @@ -507,39 +513,42 @@ def _update_python_packages_from_requirements():
507 513  
508 514 def _update_plantuml_diags():
509 515 res = False
510   - for dirpath, dirnames, files in os.walk('pyros_django'):
  516 + for dirpath, dirnames, files in os.walk(PYROS_DJANGO_BASE_DIR):
511 517 if os.path.basename(dirpath) == "doc":
512 518 diagrams = glob.glob(dirpath+os.sep+"*.pu")
513 519 for diag in diagrams: res = execProcessFromVenv("-m plantuml "+diag)
514 520 return res
515 521  
516 522 def _migrate():
517   - change_dir("pyros_django")
  523 + change_dir(PYROS_DJANGO_BASE_DIR)
518 524 # Migrate only migrations for the app "common"
519 525 #res = execProcessFromVenv("manage.py migrate common")
520 526 # Migrate all migrations for ALL apps
521 527 res = execProcessFromVenv("manage.py migrate")
522   - change_dir("..")
  528 + change_dir("PREVIOUS")
523 529 return res
524 530  
525 531 def _makemigrations():
526   - change_dir("pyros_django")
  532 + change_dir(PYROS_DJANGO_BASE_DIR)
527 533 #execProcessFromVenv(self.venv_bin + " manage.py makemigrations")
528 534 #res = execProcessFromVenv("manage.py makemigrations common")
529 535 res = execProcessFromVenv("manage.py makemigrations")
530   - change_dir("..")
  536 + change_dir("PREVIOUS")
531 537 return res
532 538  
533 539 #TODO: mettre la fixture en date naive (sans time zone)
534 540 def _loaddata():
535   - change_dir("pyros_django")
  541 + change_dir(PYROS_DJANGO_BASE_DIR)
536 542 #execProcessFromVenv(self.venv_bin + " manage.py loaddata misc" + os.sep + "fixtures" + os.sep + self.INIT_FIXTURE)
537 543 res = execProcessFromVenv("manage.py loaddata misc" + os.sep + "fixtures" + os.sep + INIT_FIXTURE)
538   - change_dir("..")
  544 + change_dir("PREVIOUS")
539 545 return res
540 546  
541 547  
542 548 def change_dir(path):
  549 + global _previous_dir
  550 + if path == "PREVIOUS": path=_previous_dir
  551 + _previous_dir = os.getcwd()
543 552 if DEBUG: print("Moving to : " + path)
544 553 os.chdir(path)
545 554 if DEBUG: print("Current directory : " + str(os.getcwd()))
... ...
src/__init__.py 0 โ†’ 100644
src/core/__init__.py 0 โ†’ 100644
src/core/pyros_django/agent/Agent.py
... ... @@ -3,7 +3,7 @@
3 3 VERSION = "0.5"
4 4  
5 5 DEBUG=True
6   -DEBUG=False
  6 +#DEBUG=False
7 7  
8 8 """TODO:
9 9  
... ... @@ -45,6 +45,8 @@ from django.conf import settings as djangosettings
45 45 # To avoid a "ModuleNotFoundError: No module named 'src'"
46 46 sys.path.append("..")
47 47 sys.path.append("../..")
  48 +sys.path.append("../../..")
  49 +sys.path.append("../../../..")
48 50 ##sys.path.append("src")
49 51 print("Starting with this sys.path", sys.path)
50 52  
... ... @@ -56,7 +58,8 @@ print(&quot;Starting with this sys.path&quot;, sys.path)
56 58 ##os.chdir("src")
57 59 print("Current directory : " + str(os.getcwd()))
58 60  
59   -os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pyros_django.pyros.settings")
  61 +#os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.core.pyros_django.pyros.settings")
  62 +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "src.core.pyros_django.pyros.settings")
60 63 # os.environ['SECRET_KEY'] = 'abc'
61 64 # os.environ['ENVIRONMENT'] = 'production'
62 65 import django
... ... @@ -332,8 +335,19 @@ class Agent:
332 335 MODE_ACTIVE = "ACTIVE"
333 336 MODE_IDLE = "IDLE"
334 337  
  338 + PYROS_DJANGO_BASE_DIR = "src/core/pyros_django"
335 339 DEFAULT_CONFIG_FILE_NAME = "config_unit_simulunit1.xml"
336   - CONFIG_DIR = "config"
  340 + CONFIG_DIR_NAME = "config"
  341 +
  342 + # Parameters from config file
  343 + # for /src/
  344 + #_path_data = '../../config'
  345 + # for /src/core/pyros_django/
  346 + #_path_data = '../../../../config'
  347 + # Path to config
  348 + _path_data = ''
  349 + _computer_alias = ''
  350 + _computer_description = ''
337 351  
338 352 # Current and next command to send
339 353 cmdts = None
... ... @@ -344,10 +358,6 @@ class Agent:
344 358 _current_specific_cmd = None
345 359 _current_specific_thread = None
346 360  
347   - # Parameters from config file
348   - _computer_alias = ''
349   - _computer_description = ''
350   - _path_data = '../../config'
351 361  
352 362 _iter_num = None
353 363  
... ... @@ -364,6 +374,14 @@ class Agent:
364 374 self._log = LogPyros()
365 375 self._log.set_agent_alias(self.name)
366 376  
  377 + #print(os.path.dirname(__file__))
  378 + my_parent_abs_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
  379 + #print(os.path.dirname(os.path.realpath(__file__)))
  380 + #print(djangosettings.BASE_DIR)
  381 + self.BASE_DIR = my_parent_abs_dir.split(self.PYROS_DJANGO_BASE_DIR)[0]
  382 + #print(self.BASE_DIR)
  383 + self._path_data = os.path.join(self.BASE_DIR, self.CONFIG_DIR_NAME)
  384 +
367 385 #self.set_mode(self.MODE_IDLE)
368 386 if not config_filename:
369 387 config_filename = self.DEFAULT_CONFIG_FILE_NAME
... ... @@ -374,14 +392,14 @@ class Agent:
374 392 # => give it an absolute path (and remove "src/agent/" from it)
375 393 if config_filename == os.path.basename(config_filename):
376 394 # Build abs path including current agent dir
377   - config_filename = os.path.abspath(self.CONFIG_DIR + os.sep + config_filename)
  395 + config_filename = os.path.abspath(self.CONFIG_DIR_NAME + os.sep + config_filename)
378 396 # Remove "src/agent_name/" from abs dir :
379   - # (1) Remove "src/"
380   - config_filename = config_filename.replace(os.sep+"pyros_django"+os.sep,os.sep)
  397 + # (1) Remove "src/core/pyros_django/"
  398 + config_filename = config_filename.replace(self.PYROS_DJANGO_BASE_DIR+os.sep, os.sep)
381 399 # (2) Remove "agent_name/"
382 400 #TODO: bidouille, faire plus propre
383   - config_filename = config_filename.replace(os.sep+"agent"+os.sep,os.sep)
384   - config_filename = config_filename.replace(os.sep+"monitoring"+os.sep,os.sep)
  401 + config_filename = config_filename.replace(os.sep+"agent"+os.sep, os.sep)
  402 + config_filename = config_filename.replace(os.sep+"monitoring"+os.sep, os.sep)
385 403 self.printd("Config file used is", config_filename)
386 404 #self.printd("current path", os.getcwd())
387 405 #self.printd("this file path :", __file__)
... ... @@ -446,6 +464,7 @@ class Agent:
446 464 while self.DO_RESTART:
447 465 self.DO_RESTART = False
448 466  
  467 + self.printd("on est ici: ", os.getcwd())
449 468 self.load_config()
450 469  
451 470 if self.SIMULATOR_MODE:
... ... @@ -746,12 +765,16 @@ class Agent:
746 765 unit_subtag = 'computer'
747 766 aliases = self.config.get_aliases(unit_subtag)
748 767 for alias in aliases:
  768 + self.printd("alias", alias)
749 769 value = self.config.get_paramvalue(alias,'local','hostname')
  770 + self.printd("value", value)
750 771 if value == hostname:
  772 + self.printd("value", value)
751 773 self._computer_alias = alias
752 774 value = self.config.get_paramvalue(alias,unit_subtag,'description')
753 775 self._computer_description = value
754 776 value = self.config.get_paramvalue(alias,'path','data')
  777 + # Overrides default value
755 778 self._path_data = value
756 779 break
757 780 print(f"hostname = {hostname}")
... ...
src/core/pyros_django/monitoring/AgentM.py
... ... @@ -10,11 +10,11 @@ import sys
10 10  
11 11 sys.path.append("..")
12 12 sys.path.append("../..")
13   -from pyros_django.agent.Agent import Agent, extract_parameters
  13 +from src.core.pyros_django.agent.Agent import Agent, extract_parameters
14 14  
15 15 # PM 20190416 recycle code
16   -from pyros_django.devices.PLC import PLCController
17   -from pyros_django.monitoring.plc_checker import PlcChecker
  16 +from src.core.pyros_django.devices.PLC import PLCController
  17 +from src.core.pyros_django.monitoring.plc_checker import PlcChecker
18 18 from common.models import *
19 19  
20 20 ##log = L.setupLogger("AgentXTaskLogger", "AgentX")
... ...
src/core/pyros_django/monitoring/tasks.py
... ... @@ -2,7 +2,7 @@ from __future__ import absolute_import
2 2 from django.conf import settings
3 3 from common.models import *
4 4  
5   -from pyros_django.monitoring.plc_checker import PlcChecker
  5 +from src.core.pyros_django.monitoring.plc_checker import PlcChecker
6 6 from devices.PLC import PLCController
7 7 from utils.JDManipulator import *
8 8 import json
... ...
src/core/pyros_django/pyros/settings.py
... ... @@ -50,6 +50,7 @@ import os
50 50 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
51 51  
52 52 # Output folder for images
  53 +#TODO: c'est quoi ce dossier ? existe-t-il vraiment ???
53 54 OUTPUT_FOLDER = os.path.join(BASE_DIR, "../images_folder")
54 55  
55 56 # Quick-start development settings - unsuitable for production
... ... @@ -144,10 +145,10 @@ LOGIN_URL = &quot;/&quot;
144 145  
145 146 # DATABASE CONFIG
146 147 '''
147   -From MySQL 5.7 onwards and on fresh installs of MySQL 5.6,
148   -the default value of the sql_mode option contains STRICT_TRANS_TABLES.
149   -That option escalates warnings into errors when data are truncated upon insertion,
150   -so Django highly recommends activating a strict mode for MySQL to prevent data loss
  148 +From MySQL 5.7 onwards and on fresh installs of MySQL 5.6,
  149 +the default value of the sql_mode option contains STRICT_TRANS_TABLES.
  150 +That option escalates warnings into errors when data are truncated upon insertion,
  151 +so Django highly recommends activating a strict mode for MySQL to prevent data loss
151 152 (either STRICT_TRANS_TABLES or STRICT_ALL_TABLES)
152 153 '''
153 154 mysql_options = { 'init_command': "SET sql_mode='STRICT_TRANS_TABLES'" }
... ...
src/core/pyros_django/utils/Logger.py
1 1 from django.conf import settings
2 2 import logging
3 3  
4   -logging.basicConfig(filename='%s/../logs/pyros.log'%(settings.BASE_DIR), format='-> At : [%(asctime)s]\n\t By module [%(module)s] logger : [%(name)s] : "%(message)s"', level=logging.DEBUG)
  4 +# If django project is in /src/ :
  5 +#RELATIVE_PATH_TO_LOG_DIR = '../logs/'
  6 +#logging.basicConfig(filename='%s/../logs/pyros.log'%(settings.BASE_DIR), format='-> At : [%(asctime)s]\n\t By module [%(module)s] logger : [%(name)s] : "%(message)s"', level=logging.DEBUG)
  7 +# If django project is in /src/core/pyros_django/ :
  8 +RELATIVE_PATH_TO_LOG_DIR = '../../../logs'
  9 +logging.basicConfig(filename = f'{settings.BASE_DIR}/{RELATIVE_PATH_TO_LOG_DIR}/pyros.log',
  10 + format = '-> At : [%(asctime)s]\n\t By module [%(module)s] logger : [%(name)s] : "%(message)s"',
  11 + level = logging.DEBUG)
  12 +#logging.basicConfig(filename='%s/../../../logs/pyros.log'%(settings.BASE_DIR), format='-> At : [%(asctime)s]\n\t By module [%(module)s] logger : [%(name)s] : "%(message)s"', level=logging.DEBUG)
5 13  
6 14 def setupLogger(logger_name, log_file, level=logging.INFO):
7 15 l = logging.getLogger(logger_name)
8 16 formatter = logging.Formatter('[%(message)s] at %(filename)s : %(lineno)s')
9   - fileHandler = logging.FileHandler('%s/../logs/%s.log'%(settings.BASE_DIR, log_file), mode='w')
  17 + #fileHandler = logging.FileHandler('%s/../logs/%s.log'%(settings.BASE_DIR, log_file), mode='w')
  18 + fileHandler = logging.FileHandler(f'{settings.BASE_DIR}/{RELATIVE_PATH_TO_LOG_DIR}/{log_file}.log', mode='w')
10 19 fileHandler.setFormatter(formatter)
11 20 # streamHandler = logging.StreamHandler()
12 21 # streamHandler.setFormatter(formatter)
... ...
start_agent.py
... ... @@ -85,24 +85,24 @@ if agent_name not in AGENTS:
85 85 sys.exit(1)
86 86  
87 87 if agent_name == "majordome":
88   - from pyros_django.majordome.tasks import Majordome
  88 + from src.core.pyros_django.majordome.tasks import Majordome
89 89 Majordome().run(FOR_REAL=True)
90 90 sys.exit(0)
91 91  
92 92 if agent_name == "monitoring":
93   - from pyros_django.monitoring.tasks import Monitoring
  93 + from src.core.pyros_django.monitoring.tasks import Monitoring
94 94 Monitoring().run()
95 95 sys.exit(0)
96 96  
97 97 if agent_name == "agentA":
98   - from pyros_django.agent.AgentA import AgentA
  98 + from src.core.pyros_django.agent.AgentA import AgentA
99 99 agentA = AgentA(name="agentA", config_filename=configfile)
100 100 # Run agent without actual commands sent to devices (FOR_REAL=False)
101 101 agentA.run(FOR_REAL=True)
102 102 sys.exit(0)
103 103  
104 104 if agent_name == "agentM":
105   - from pyros_django.monitoring.AgentM import AgentM
  105 + from src.core.pyros_django.monitoring.AgentM import AgentM
106 106 agentM = AgentM(name="agentM", config_filename=configfile)
107 107 # Run agent without actual commands sent to devices (FOR_REAL=False)
108 108 agentM.run(FOR_REAL=True)
... ... @@ -116,7 +116,7 @@ agent.run(FOR_REAL=True)
116 116  
117 117 # Default agent is AgentX
118 118  
119   -from pyros_django.agent.AgentX import AgentX
  119 +from src.core.pyros_django.agent.AgentX import AgentX
120 120 # AgentX().run(FOR_REAL=False)
121 121 agentX = AgentX(name="agentX", config_filename=configfile)
122 122  
... ...