west_clientserver.py 4.63 KB
Newer Older
Marco Govoni's avatar
Marco Govoni committed
1
2
#!/usr/bin/python3

Marco Govoni's avatar
Marco Govoni committed
3
4
5
6
7
8
9
10
11
12
#
# Copyright (C) 2015-2019 M. Govoni 
# This file is distributed under the terms of the
# GNU General Public License. See the file `License'
# in the root directory of the present distribution,
# or http://www.gnu.org/copyleft/gpl.txt .
#
# This file is part of WEST.
#

Marco Govoni's avatar
Marco Govoni committed
13
14
15
from time import sleep, perf_counter as pc
from os import path, remove
from abc import ABC, abstractmethod
Marco Govoni's avatar
Marco Govoni committed
16
import json 
Marco Govoni's avatar
Marco Govoni committed
17
18
19
20
21
22
23

##############
# SUPERCLASS #
##############

class ClientServer(ABC): 
   #
Marco Govoni's avatar
Marco Govoni committed
24
   def __init__(self,client_lockfile,maxsec=21600,sleepsec=10,document={}):
Marco Govoni's avatar
Marco Govoni committed
25
26
27
28
      #
      self.client_lockfile = client_lockfile
      self.maxsec = maxsec
      self.sleepsec = sleepsec
Marco Govoni's avatar
Marco Govoni committed
29
      self.document = document 
Marco Govoni's avatar
Marco Govoni committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
      super().__init__()
   #
   @abstractmethod
   def before_sleep(self): # subclass needs to implement this method
      pass
   #
   @abstractmethod
   def after_sleep(self): # subclass needs to implement this method
      pass
   #
   @abstractmethod
   def awake_condition(self): # subclass needs to implement this method
      pass
   #
   def start(self):
      #
      # ====================================
      self.before_sleep()
      # ====================================
      #
      awake = 1 # I am awake if this is zero
      t0 = pc()
      while (pc()-t0 <= self.maxsec) :
         #
         # =================================
         self.awake_condition()
         # =================================
         #
         exists = path.exists(self.client_lockfile)
         if (not exists) : 
            awake = 0 
            break
         else : 
            sleep(self.sleepsec)
      #
      # ====================================
      self.after_sleep()
      # ====================================
      return awake

###############
# SERVERCLASS #
###############

class QboxServer(ClientServer) : 
   #
   def before_sleep(self):
       #
Marco Govoni's avatar
Marco Govoni committed
78
79
80
81
82
83
84
85
86
87
88
       command_suffix = ""
       if "response" in self.document.keys() : 
          if self.document["response"] == "RPA" :
             command_suffix += "-RPA "
          if self.document["response"] == "IPA" :
             command_suffix += "-IPA "
       if "amplitude" in self.document.keys() : 
          command_suffix += f'-amplitude {self.document["amplitude"]} '
       if "nitscf" in self.document.keys() : 
          command_suffix += f'{self.document["nitscf"]} '
       else : 
Marco Govoni's avatar
Marco Govoni committed
89
          command_suffix += '100 '
Marco Govoni's avatar
Marco Govoni committed
90
91
92
       if "nite" in self.document.keys() : 
          command_suffix += f'{self.document["nite"]}'
       #
Marco Govoni's avatar
Marco Govoni committed
93
94
95
96
97
98
99
100
101
102
103
104
105
106
       # Determine the name of the server file 
       #
       client_image = self.client_lockfile.split(".")[1] 
       self.server_inputfile = f"qb.{client_image}.in" # we assume that server_number = client_image
       #
       # List of perturbation files 
       #
       perturbation_list = []
       with open(self.client_lockfile,"r") as f:
          for cnt, line in enumerate(f):
             perturbation_list.append(line.replace("\n",""))
       #
       # Create the input file for the server 
       # 
Marco Govoni's avatar
Marco Govoni committed
107
108
109
110
       with open(self.server_inputfile,"w") as f:
          if "script" in self.document.keys() :
             for line in self.document["script"] :
                f.write(line+"\n")
Marco Govoni's avatar
Marco Govoni committed
111
          for pert in perturbation_list : 
Marco Govoni's avatar
Marco Govoni committed
112
              f.write(f"response -vext {pert} "+command_suffix+"\n")
Marco Govoni's avatar
Marco Govoni committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
       #
       # Awake server, by removing its lockfile 
       #
       if(path.exists(self.server_inputfile+".lock")) :
          remove(self.server_inputfile+".lock")

   #
   def awake_condition(self): 
       #
       # If server gets to sleeps, awake the client 
       #
       if( path.exists(self.server_inputfile+".lock")) :  
          remove(self.client_lockfile)
   #
   def after_sleep(self):
       pass 

#############
# INTERFACE #
#############

def sleep_and_wait(*args, **kwargs):
    #
    client_lockfile = args[0] # name of client lockfile 
    maxsec = 12 * 60 * 60 # 12 hours, Max sleep time (in s) 
    sleepsec = 1 # 1 second, Sleep interval (in s)
Marco Govoni's avatar
Marco Govoni committed
139
    document = {}
Marco Govoni's avatar
Marco Govoni committed
140
141
142
143
144
145
146
    #
    # change defaults 
    #
    if "maxsec" in kwargs.keys() : 
       maxsec = kwargs["maxsec"]
    if "sleepsec" in kwargs.keys() : 
       sleepsec = kwargs["sleepsec"]
Marco Govoni's avatar
Marco Govoni committed
147
148
    if "document" in kwargs.keys() :
       document = json.loads(kwargs["document"])
Marco Govoni's avatar
Marco Govoni committed
149
    #
Marco Govoni's avatar
Marco Govoni committed
150
    server = QboxServer(client_lockfile,maxsec,sleepsec,document)
Marco Govoni's avatar
Marco Govoni committed
151
152
153
154
155
156
157
158
159
160
    return_int = server.start()
    #
    return return_int
    
########
# TEST #
########

def test() :
    with open("I.1.lock","w") as f :
Marco Govoni's avatar
Marco Govoni committed
161
162
       f.write("I.1_P.1.xml")
    sleep_and_wait("I.1.lock",maxsec=60,sleepsec=2,document='{"response": "IPA", "amplitude": 0, "script" : ["set xc PBE"]}')
Marco Govoni's avatar
Marco Govoni committed
163
164
165
166
167

if __name__ == "__main__":
    # execute only if run as a script
    test()