1 |
2 |
drasko |
#! /usr/bin/env python2.6
|
2 |
|
|
# -*- mode: python; coding: utf-8; -*-
|
3 |
|
|
#
|
4 |
|
|
# Script to add/remove project to baremetal
|
5 |
|
|
# menu of main screen
|
6 |
|
|
#
|
7 |
|
|
# This script should be called from project root directory
|
8 |
|
|
#
|
9 |
|
|
import os, sys, shutil, re
|
10 |
|
|
|
11 |
|
|
PROJRELROOT = '../../'
|
12 |
|
|
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), PROJRELROOT)))
|
13 |
|
|
|
14 |
|
|
from optparse import OptionParser
|
15 |
|
|
from os.path import join
|
16 |
|
|
from shutil import copytree
|
17 |
|
|
from config.projpaths import *
|
18 |
|
|
|
19 |
|
|
def parse_cmdline_options():
|
20 |
|
|
usage = "usage: %prog [options] arg"
|
21 |
|
|
parser = OptionParser(usage)
|
22 |
|
|
|
23 |
|
|
parser.add_option("-a", "--add", action = "store_true", default = False,
|
24 |
|
|
dest = "addproject", help = "Add new project to baremetal projects")
|
25 |
|
|
parser.add_option("-d", "--del", action = "store_true", default = False,
|
26 |
|
|
dest = "delproject", help = "Delete existing project from baremetal projects")
|
27 |
|
|
parser.add_option("-i", "--desc", type = "string", dest = "projdesc",
|
28 |
|
|
help = "Description of new project to be added")
|
29 |
|
|
parser.add_option("-s", "--src", type = "string", dest = "srcpath",
|
30 |
|
|
help = "With -a, Source directory for new project to be added \
|
31 |
|
|
With -d, Source directory of baremetal project to be deleted")
|
32 |
|
|
|
33 |
|
|
(options, args) = parser.parse_args()
|
34 |
|
|
|
35 |
|
|
# Sanity checks
|
36 |
|
|
if (not options.addproject and not options.delproject) or \
|
37 |
|
|
(options.addproject and options.delproject):
|
38 |
|
|
parser.error("Only one of -a or -d needed, use -h argument for help")
|
39 |
|
|
exit()
|
40 |
|
|
|
41 |
|
|
if options.addproject:
|
42 |
|
|
add_del = 1
|
43 |
|
|
if not options.projdesc or not options.srcpath:
|
44 |
|
|
parser.error("--desc or --src missing, use -h argument for help")
|
45 |
|
|
exit()
|
46 |
|
|
|
47 |
|
|
if options.delproject:
|
48 |
|
|
add_del = 0
|
49 |
|
|
if options.projdesc or not options.srcpath:
|
50 |
|
|
parser.error("--desc provided or --src missing with -d, use -h argument for help")
|
51 |
|
|
exit()
|
52 |
|
|
|
53 |
|
|
return options.projdesc, options.srcpath, add_del
|
54 |
|
|
|
55 |
|
|
def container_cml_templ_del_symbl(projname):
|
56 |
|
|
cont_templ = "config/cml/container_ruleset.template"
|
57 |
|
|
sym = "CONT%(cn)d_BAREMETAL_PROJ_" + projname.upper()
|
58 |
|
|
|
59 |
|
|
buffer = ""
|
60 |
|
|
with open(cont_templ, 'r') as fin:
|
61 |
|
|
exist = False
|
62 |
|
|
# Prepare buffer for new cont_templ with new project symbols added
|
63 |
|
|
for line in fin:
|
64 |
|
|
parts = line.split()
|
65 |
|
|
|
66 |
|
|
# Find out where baremetal symbols start in cont_templ
|
67 |
|
|
if len(parts) > 1 and parts[0] == sym:
|
68 |
|
|
exist = True
|
69 |
|
|
continue
|
70 |
|
|
elif len(parts) == 1 and parts[0] == sym:
|
71 |
|
|
continue
|
72 |
|
|
|
73 |
|
|
buffer += line
|
74 |
|
|
if exist == False:
|
75 |
|
|
print "Baremetal project named " + projname + " does not exist"
|
76 |
|
|
exit()
|
77 |
|
|
|
78 |
|
|
# Write new cont_templ
|
79 |
|
|
with open(cont_templ, 'w+') as fout:
|
80 |
|
|
fout.write(buffer)
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
def container_cml_templ_add_symbl(projdesc, projname):
|
84 |
|
|
cont_templ = "config/cml/container_ruleset.template"
|
85 |
|
|
|
86 |
|
|
pattern = re.compile("(CONT\%\(cn\)d_BAREMETAL_PROJ_)(.*)")
|
87 |
|
|
baremetal_name_templ = "CONT%(cn)d_BAREMETAL_PROJ_"
|
88 |
|
|
new_sym = baremetal_name_templ + projname.upper()
|
89 |
|
|
|
90 |
|
|
buffer = ""
|
91 |
|
|
with open(cont_templ, 'r') as fin:
|
92 |
|
|
baremetal_sym_found = False
|
93 |
|
|
last_baremetal_proj = ""
|
94 |
|
|
|
95 |
|
|
# Prepare buffer for new cont_templ with new project symbols added
|
96 |
|
|
for line in fin:
|
97 |
|
|
parts = line.split()
|
98 |
|
|
|
99 |
|
|
# Find out where baremetal symbols start in cont_templ
|
100 |
|
|
if len(parts) > 1 and re.match(pattern, parts[0]):
|
101 |
|
|
baremetal_sym_found = True
|
102 |
|
|
|
103 |
|
|
# Find the name of last baremetal project already present in list
|
104 |
|
|
last_baremetal_proj = parts[0][len(baremetal_name_templ):]
|
105 |
|
|
|
106 |
|
|
# We are done with baremetal symbols, add new symbol to buffer
|
107 |
|
|
elif baremetal_sym_found == True:
|
108 |
|
|
baremetal_sym_found = False
|
109 |
|
|
sym_def = new_sym + "\t\'" + projdesc + "\'\n"
|
110 |
|
|
buffer += sym_def
|
111 |
|
|
|
112 |
|
|
# Search for baremetal menu and add new project symbol
|
113 |
|
|
elif len(parts) == 1 and \
|
114 |
|
|
parts[0] == baremetal_name_templ + last_baremetal_proj:
|
115 |
|
|
sym_reference = "\t" + new_sym + "\n"
|
116 |
|
|
line += sym_reference
|
117 |
|
|
|
118 |
|
|
buffer += line
|
119 |
|
|
|
120 |
|
|
# Write new cont_templ
|
121 |
|
|
with open(cont_templ, 'w+') as fout:
|
122 |
|
|
fout.write(buffer)
|
123 |
|
|
|
124 |
|
|
def add_project(projdesc, srcdir, projname):
|
125 |
|
|
container_cml_templ_add_symbl(projdesc, projname)
|
126 |
|
|
|
127 |
|
|
baremetal_dir = "conts/baremetal"
|
128 |
|
|
dest_dir = join(baremetal_dir, projname)
|
129 |
|
|
|
130 |
|
|
print "Copying source files from " + srcdir + " to " + dest_dir
|
131 |
|
|
shutil.copytree(srcdir, dest_dir)
|
132 |
|
|
print "Done, New baremetal project " + projname + \
|
133 |
|
|
" is ready to be used."
|
134 |
|
|
|
135 |
|
|
def del_project(srcdir, projname):
|
136 |
|
|
container_cml_templ_del_symbl(projname)
|
137 |
|
|
|
138 |
|
|
baremetal_dir = "conts/baremetal"
|
139 |
|
|
src_dir = join(baremetal_dir, projname)
|
140 |
|
|
|
141 |
|
|
print "Deleting source files from " + src_dir
|
142 |
|
|
shutil.rmtree(src_dir, "ignore_errors")
|
143 |
|
|
print "Done.."
|
144 |
|
|
|
145 |
|
|
def main():
|
146 |
|
|
projdesc, srcdir, add_del = parse_cmdline_options()
|
147 |
|
|
|
148 |
|
|
# Get the base directory
|
149 |
|
|
projpath, projname = os.path.split(srcdir)
|
150 |
|
|
|
151 |
|
|
# Python's basename() doesnot work fine if path ends with /,
|
152 |
|
|
# so we need to manually correct this
|
153 |
|
|
if projname == "":
|
154 |
|
|
projpath, projname = os.path.split(projpath)
|
155 |
|
|
|
156 |
|
|
if add_del == 1:
|
157 |
|
|
add_project(projdesc, srcdir, projname)
|
158 |
|
|
else:
|
159 |
|
|
del_project(srcdir, projname)
|
160 |
|
|
|
161 |
|
|
# Delete the config.cml file, so that user can see new projects
|
162 |
|
|
os.system("rm -f " + CML2_CONFIG_FILE)
|
163 |
|
|
|
164 |
|
|
if __name__ == "__main__":
|
165 |
|
|
main()
|
166 |
|
|
|