Skip to content

Commit c74550b

Browse files
committed
fix: the module creation script was broken, trying to add an intermediate 'src/' directory everywhere
1 parent d639f56 commit c74550b

1 file changed

Lines changed: 117 additions & 132 deletions

File tree

‎shell/createmodules/create.py‎

Lines changed: 117 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -3,138 +3,123 @@
33
import argparse
44
import constant as cs
55

6-
def buildStructure(rootDirectory, content):
7-
'''
8-
Builds the defined directory structure under the given root
9-
10-
Parameters:
11-
rootDirectory (string): The parent directory under which the content should be created
12-
content (dict): the definition of the directories/files that should be created under the root
13-
'''
14-
15-
contentType = content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_TYPE"]]
16-
17-
if contentType == cs.DIRECTORY:
18-
rootDirectory = createDirectory(rootDirectory, content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_NAME"]])
19-
elif contentType == cs.FILE:
20-
createFile(rootDirectory, content)
21-
22-
if cs.MODULE_STRUCTURE_KEYS["DIR_CONTENTS"] not in content:
23-
return
24-
25-
for subContent in content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENTS"]]:
26-
buildStructure(rootDirectory, subContent)
27-
28-
def createDirectory(rootDirectory, directoryName):
29-
'''
30-
Creates a given directory under the given root
31-
32-
Parameters:
33-
rootDirectory (string): The parent directory under which the sub directory should be created
34-
content (dict): the definition of the directories/files that should be created under the root
35-
36-
Returns:
37-
string: path of the current root directory
38-
'''
39-
path = os.path.join(rootDirectory, "src", directoryName)
40-
os.mkdir(path)
41-
42-
return path
43-
44-
def isTemplateFile(content):
45-
'''
46-
Checks if a given file should be copied from a template file
47-
48-
Parameters:
49-
content (dict): the definition of the directories/files that should be created under the root
50-
51-
Returns:
52-
boolean: true if template file is available else false
53-
'''
54-
isTemplateKey = cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_IS_TEMPLATE"]
55-
56-
return (isTemplateKey in content and content[isTemplateKey])
57-
58-
def readTemplateFile(fileName):
59-
'''
60-
Reads the contents of the given template file
61-
62-
Parameters:
63-
fileName (string): file name to be created
64-
65-
Returns:
66-
string: contents of the corresponding template file
67-
'''
68-
templateFileName = cs.TEMPLATE_FILE_NAME_PREFIX + fileName
69-
path = os.path.join(cs.MODULE_CREATION_SHELL_DIR, templateFileName)
70-
71-
return open(path, cs.FILE_READ_PERMISSION).read()
72-
73-
def writeContentsToFile(file, content, rootDirectory):
74-
'''
75-
Copies content from template file and writes to given file
76-
77-
Parameters:
78-
file (File object): the new file to which the contents of the template are copied
79-
content (dict): the definition of the directories/files that should be created under the root
80-
rootDirectory (string): name of the directory under which the file is created
81-
82-
Returns:
83-
boolean: true if template file is available else false
84-
'''
85-
moduleName = rootDirectory.split(os.path.sep)[-1]
86-
87-
templateFileContents = readTemplateFile(content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_NAME"]])
88-
file.write(templateFileContents.replace(cs.MODULE_NAME_PLACEHOLDER, moduleName))
89-
90-
91-
def createFile(rootDirectory, content):
92-
'''
93-
Creates a file under the given root
94-
95-
Parameters:
96-
rootDirectory (string): The parent directory under which the file should be created
97-
content (dict): the definition of the directories/files that should be created under the root
98-
'''
99-
fileName = content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_NAME"]]
100-
path = os.path.join(rootDirectory, fileName)
101-
file = open(path, "w+")
102-
103-
if isTemplateFile(content):
104-
writeContentsToFile(file, content, rootDirectory)
105-
106-
def getModuleName():
107-
'''
108-
Fetches the argument name passed to the script
109-
110-
Returns:
111-
string: name of the module to be created
112-
'''
113-
parser = argparse.ArgumentParser()
114-
parser.add_argument("name")
115-
directory = parser.parse_args()
116-
117-
return directory.name
118-
119-
def getDirectoryStructure():
120-
'''
121-
Reads the directory structure from file and builds a dictionary
122-
123-
Returns
124-
dict: dictionary with the directory structure to be created
125-
'''
126-
jsonFile = os.getcwd() + os.path.sep + cs.MODULE_CREATION_SHELL_DIR + os.path.sep + cs.MODULE_STRUCTURE_FILE_NAME
127-
f = open(jsonFile)
128-
directoryStructure = json.load(f)
129-
f.close()
130-
131-
return directoryStructure
6+
7+
def build_structure(root_directory, content):
8+
"""
9+
Builds the defined directory structure under the given root
10+
11+
Parameters:
12+
root_directory (string): The parent directory under which the content should be created
13+
content (dict): the definition of the directories/files that should be created under the root
14+
"""
15+
16+
content_type = content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_TYPE"]]
17+
18+
if content_type == cs.DIRECTORY:
19+
root_directory = create_directory(root_directory, content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_NAME"]])
20+
elif content_type == cs.FILE:
21+
create_file(root_directory, content)
22+
23+
if cs.MODULE_STRUCTURE_KEYS["DIR_CONTENTS"] not in content:
24+
return
25+
26+
for subContent in content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENTS"]]:
27+
build_structure(root_directory, subContent)
28+
29+
30+
def create_directory(root_directory, directory_name):
31+
"""
32+
Creates a given directory under the given root
33+
34+
Parameters:
35+
root_directory (string): The parent directory under which the subdirectory should be created
36+
directory_name (string): name of the directory that should be created under the root
37+
38+
Returns:
39+
string: path of the current root directory
40+
"""
41+
path = os.path.join(root_directory, directory_name)
42+
os.mkdir(path)
43+
return path
44+
45+
46+
def is_template_file(content):
47+
"""
48+
Checks if a given file should be copied from a template file
49+
50+
Parameters:
51+
content (dict): the definition of the directories/files that should be created under the root
52+
53+
Returns:
54+
boolean: true if template file is available else false
55+
"""
56+
is_template_key = cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_IS_TEMPLATE"]
57+
return is_template_key in content and content[is_template_key]
58+
59+
60+
def read_template_file(file_name):
61+
"""
62+
Reads the contents of the given template file
63+
64+
Parameters:
65+
file_name (string): file name to be created
66+
67+
Returns:
68+
string: contents of the corresponding template file
69+
"""
70+
template_file_name = cs.TEMPLATE_FILE_NAME_PREFIX + file_name
71+
with open(os.path.join(cs.MODULE_CREATION_SHELL_DIR, template_file_name)) as f:
72+
return f.read()
73+
74+
75+
def write_contents_to_file(file, content, root_directory):
76+
"""
77+
Copies content from template file and writes to given file
78+
79+
Parameters:
80+
file (File object): the new file to which the contents of the template are copied
81+
content (dict): the definition of the directories/files that should be created under the root
82+
root_directory (string): name of the directory under which the file is created
83+
84+
Returns:
85+
boolean: true if template file is available else false
86+
"""
87+
module_name = root_directory.split(os.path.sep)[-1]
88+
89+
template_file_contents = read_template_file(content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_NAME"]])
90+
file.write(template_file_contents.replace(cs.MODULE_NAME_PLACEHOLDER, module_name))
91+
92+
93+
def create_file(root_directory, content):
94+
"""
95+
Creates a file under the given root
96+
97+
Parameters:
98+
root_directory (string): The parent directory under which the file should be created
99+
content (dict): the definition of the directories/files that should be created under the root
100+
"""
101+
file_name = content[cs.MODULE_STRUCTURE_KEYS["DIR_CONTENT_NAME"]]
102+
path = os.path.join(root_directory, file_name)
103+
file = open(path, "w+")
104+
105+
if is_template_file(content):
106+
write_contents_to_file(file, content, root_directory)
107+
132108

133109
if __name__ == "__main__":
134-
moduleName = getModuleName()
135-
directoryStructure = getDirectoryStructure()
136-
137-
rootDir = createDirectory(os.getcwd(), moduleName)
110+
parser = argparse.ArgumentParser()
111+
parser.add_argument("name")
112+
directory = parser.parse_args()
113+
114+
with open(os.path.join(os.getcwd(), cs.MODULE_CREATION_SHELL_DIR, cs.MODULE_STRUCTURE_FILE_NAME)) as f:
115+
directory_structure = json.load(f)
116+
root_dir = create_directory(os.path.join(os.getcwd(), "src"), directory.name)
117+
118+
for content in directory_structure:
119+
build_structure(root_dir, content)
120+
121+
print(f"""\nINFO: to compile your module, add
122+
123+
add_subdirectory({directory.name})
138124
139-
for content in directoryStructure:
140-
buildStructure(rootDir, content)
125+
To modules/src/CMakeLists.txt""")

0 commit comments

Comments
 (0)