cloudFPGA (cF) API  1.0
The documentation of the source code of cloudFPGA (cF)
gen_env.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # /*******************************************************************************
3 # * Copyright 2016 -- 2022 IBM Corporation
4 # *
5 # * Licensed under the Apache License, Version 2.0 (the "License");
6 # * you may not use this file except in compliance with the License.
7 # * You may obtain a copy of the License at
8 # *
9 # * http://www.apache.org/licenses/LICENSE-2.0
10 # *
11 # * Unless required by applicable law or agreed to in writing, software
12 # * distributed under the License is distributed on an "AS IS" BASIS,
13 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # * See the License for the specific language governing permissions and
15 # * limitations under the License.
16 # *******************************************************************************/
17 
18 # *
19 # * cloudFPGA
20 # * =============================================
21 # * Created: FEB 2020
22 # * Authors: FAB, WEI, NGL
23 # *
24 # * Description:
25 # * Python file to parse cFp.json
26 # *
27 
28 import json
29 import os
30 import re
31 
32 __cfp_json_path__ = "/../cFp.json"
33 __env_file_name__ = "/this_machine_env.sh"
34 __to_be_defined_key__ = 'to-be-defined'
35 __lignin_key__ = 'lignin-conf'
36 __cfenv_small_name__ = 'cfenv-small'
37 __cfenv_path_from_root__ = '/env/' + __cfenv_small_name__ + '/'
38 __cfenv_req_packages__ = 'docopt==0.6.2 requests==2.26.0'
39 
40 __mandatory_keys__ = ['cFpMOD', 'usedRoleDir', 'usedRoleDir2', 'cFpSRAtype', 'roleName1', 'roleName2']
41 __optional_keys__ = ['cFa', 'additional_lines', __lignin_key__]
42 
43 __match_regex__ = []
44 __replace_regex__ = []
45 
46 __match_regex__.append("##ROOTDIR##")
47 __replace_regex__.append("abs_path")
48 
49 __match_regex__.append("##MOD##")
50 __replace_regex__.append("cFpMOD")
51 
52 __match_regex__.append("##SRA##")
53 __replace_regex__.append("cFpSRAtype")
54 
55 __match_regex__.append("##DIR1##")
56 __replace_regex__.append("usedRoleDir")
57 
58 __match_regex__.append("##DIR2##")
59 __replace_regex__.append("usedRoleDir2")
60 
61 __match_regex__.append("##ROLE1##")
62 __replace_regex__.append("roleName1")
63 
64 __match_regex__.append("##ROLE2##")
65 __replace_regex__.append("roleName2")
66 
67 __match_regex__.append("##virtual_path##")
68 __replace_regex__.append("cfenvPath")
69 
70 __match_regex__.append("##python3_bin##")
71 __replace_regex__.append("sysPython3Bin")
72 
73 
74 def print_incomplete(msg=""):
75  me_abs = os.path.realpath(__file__)
76  cfp_json_file = os.path.abspath(me_abs + __cfp_json_path__)
77  print("The project describing file {} is invalid.\n{}\n".format(cfp_json_file, msg) +
78  "Please use 'cFBuild update' to fix this project setup.")
79  exit(1)
80 
81 
83  # this is not working, we need the python3 without the virutalenv
84  # sys_py_bin = os.popen('which python3').read()
85  sys_py_bin = None
86  if 'cFsysPy3_cmd_hint_0' in os.environ and os.path.isfile(os.environ['cFsysPy3_cmd_hint_0']):
87  sys_py_bin = os.environ['cFsysPy3_cmd_hint_0']
88  elif 'cFsysPy3_cmd_hint_1' in os.environ and os.path.isfile(os.environ['cFsysPy3_cmd_hint_1']):
89  sys_py_bin = os.environ['cFsysPy3_cmd_hint_1']
90  elif os.path.isfile('/usr/bin/python3.8'):
91  sys_py_bin = '/usr/bin/python3.8'
92  elif os.path.isfile('/usr/bin/python3'):
93  sys_py_bin = '/usr/bin/python3'
94  else:
95  # as fallback, better than nothing
96  # returns the virtualenv python
97  sys_py_bin = os.popen('which python3').read()
98  return sys_py_bin
99 
100 
101 def main():
102  me_abs = os.path.dirname(os.path.realpath(__file__))
103  cfp_json_file = me_abs + __cfp_json_path__
104  with open(cfp_json_file, 'r') as json_file:
105  data = json.load(json_file)
106 
107  root_abs = os.path.realpath(me_abs+"/../")
108  data['abs_path'] = root_abs
109  sys_py_bin = get_sys_python_env()
110 
111  # check for virtualenv
112  cfenv_dir = os.path.abspath(root_abs + __cfenv_path_from_root__)
113  if not os.path.isdir(cfenv_dir) or not os.path.isfile("{}/bin/activate".format(cfenv_dir)):
114  # delete it, to be sure
115  os.system("rm -rf {}".format(cfenv_dir))
116  # creating it
117  print("[INFO] the python virutalenv for this project on this machine is missing, installing it...")
118  os.system('cd {}; virtualenv -p {} {}'
119  .format(os.path.abspath(cfenv_dir + '/../'), sys_py_bin, __cfenv_small_name__))
120  os.system('/bin/bash -c "source {}/bin/activate; pip install {}"'.format(cfenv_dir, __cfenv_req_packages__))
121 
122  data['cfenvPath'] = cfenv_dir
123  data['sysPython3Bin'] = sys_py_bin
124 
125  for e in __mandatory_keys__:
126  if e not in data.keys():
127  print_incomplete("The mandatory key {} is missing.".format(e))
128 
129  env_file = me_abs + __env_file_name__
130 
131  # first, check the timestamps
132  if os.path.exists(env_file):
133  json_time = os.path.getmtime(cfp_json_file)
134  env_time = os.path.getmtime(env_file)
135 
136  if env_time >= json_time:
137  # the environment was already created...nothing to do
138  exit(0)
139 
140  with open(me_abs + "/machine_env.template", "r") as input, open(env_file, "w+") as outfile:
141  out = input.read()
142  for i in range(0, len(__match_regex__)):
143  out = re.sub(re.escape(__match_regex__[i]), data[__replace_regex__[i]], out)
144 
145  if 'additional_lines' in data.keys():
146  out += '\n\n'
147  for e in data['additional_lines']:
148  new_line = str(e) + '\n'
149  out += new_line
150  out += '\n\n'
151 
152  outfile.write(out)
153 
154  os.system("chmod +x {}".format(env_file))
155 
156 
157 if __name__ == '__main__':
158  main()
159  exit(0)
160 
def main()
Definition: gen_env.py:101
def print_incomplete(msg="")
Definition: gen_env.py:74
def get_sys_python_env()
Definition: gen_env.py:82