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 python
2 
3 # /*******************************************************************************
4 # * Copyright 2016 -- 2021 IBM Corporation
5 # *
6 # * Licensed under the Apache License, Version 2.0 (the "License");
7 # * you may not use this file except in compliance with the License.
8 # * You may obtain a copy of the License at
9 # *
10 # * http://www.apache.org/licenses/LICENSE-2.0
11 # *
12 # * Unless required by applicable law or agreed to in writing, software
13 # * distributed under the License is distributed on an "AS IS" BASIS,
14 # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # * See the License for the specific language governing permissions and
16 # * limitations under the License.
17 # *******************************************************************************/
18 
19 # *
20 # * cloudFPGA
21 # * =============================================
22 # * Created: FEB 2020
23 # * Authors: FAB, WEI, NGL
24 # *
25 # * Description:
26 # * Python file to parse cFp.json
27 # *
28 
29 import json
30 import os
31 import re
32 
33 __cfp_json_path__ = "/../cFp.json"
34 __env_file_name__ = "/this_machine_env.sh"
35 
36 __mandatory_keys__ = ['cFpMOD', 'usedRoleDir', 'usedRoleDir2', 'cFpSRAtype', 'roleName1', 'roleName2']
37 __optional_keys__ = ['cFa', 'additional_lines']
38 
39 __match_regex__ = []
40 __replace_regex__ = []
41 
42 __match_regex__.append("##ROOTDIR##")
43 __replace_regex__.append("abs_path")
44 
45 __match_regex__.append("##MOD##")
46 __replace_regex__.append("cFpMOD")
47 
48 __match_regex__.append("##SRA##")
49 __replace_regex__.append("cFpSRAtype")
50 
51 __match_regex__.append("##DIR1##")
52 __replace_regex__.append("usedRoleDir")
53 
54 __match_regex__.append("##DIR2##")
55 __replace_regex__.append("usedRoleDir2")
56 
57 __match_regex__.append("##ROLE1##")
58 __replace_regex__.append("roleName1")
59 
60 __match_regex__.append("##ROLE2##")
61 __replace_regex__.append("roleName2")
62 
63 
64 def print_incomplete(msg=""):
65  me_abs = os.path.realpath(__file__)
66  cfp_json_file = me_abs + __cfp_json_path__
67  print("The project describing file {} is invalid.\n{}\n".format(cfp_json_file, msg) +
68  "Please use 'cFBuild update' to fix this project setup.")
69  exit(1)
70 
71 
72 def main():
73  me_abs = os.path.dirname(os.path.realpath(__file__))
74  cfp_json_file = me_abs + __cfp_json_path__
75  with open(cfp_json_file, 'r') as json_file:
76  data = json.load(json_file)
77 
78  root_abs = os.path.realpath(me_abs+"/../")
79  data['abs_path'] = root_abs
80 
81  for e in __mandatory_keys__:
82  if e not in data.keys():
83  print_incomplete("The mandatory key {} is missing.".format(e))
84 
85  env_file = me_abs + __env_file_name__
86 
87  # first, check the timestamps
88  if os.path.exists(env_file):
89  json_time = os.path.getmtime(cfp_json_file)
90  env_time = os.path.getmtime(env_file)
91 
92  if env_time >= json_time:
93  # the environment was already created...nothing to do
94  exit(0)
95 
96  with open(me_abs + "/machine_env.template", "r") as input, open(env_file, "w+") as outfile:
97  out = input.read()
98  for i in range(0, len(__match_regex__)):
99  out = re.sub(re.escape(__match_regex__[i]), data[__replace_regex__[i]], out)
100 
101  if 'additional_lines' in data.keys():
102  out += '\n\n'
103  for e in data['additional_lines']:
104  new_line = str(e) + '\n'
105  out += new_line
106  out += '\n\n'
107 
108  outfile.write(out)
109 
110  os.system("chmod +x {}".format(env_file))
111 
112 
113 if __name__ == '__main__':
114  main()
115  exit(0)
116 
def main()
Definition: gen_env.py:101
def print_incomplete(msg="")
Definition: gen_env.py:74