-
Notifications
You must be signed in to change notification settings - Fork 439
/
Copy pathiotjs-create-module.py
executable file
·99 lines (80 loc) · 3.72 KB
/
iotjs-create-module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
# Copyright 2018-present Samsung Electronics Co., Ltd. and other contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import os
import re
IOTJS_BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
TEMPLATE_BASE_DIR = os.path.join(os.path.dirname(__file__), 'module_templates')
MODULE_NAME_RE = "^[a-z0-9][a-z0-9\._]*$"
def load_templates(template_dir):
for root, dirs, files in os.walk(template_dir):
for fp in files:
yield os.path.relpath(os.path.join(root, fp), template_dir)
def replace_contents(input_file, module_name):
with open(input_file) as fp:
data = fp.read()
data = data.replace("$MODULE_NAME$", module_name)
data = data.replace("$IOTJS_PATH$", IOTJS_BASE_DIR)
return data
def create_module(output_dir, module_name, template_dir, template_files):
module_path = os.path.join(output_dir, module_name)
print("Creating module in {}".format(module_path))
if os.path.exists(module_path):
print("Module path ({}) already exists! Exiting".format(module_path))
return False
for file_name in template_files:
file_path = os.path.join(template_dir, file_name)
print("loading template file: {}".format(file_path))
contents = replace_contents(file_path, module_name)
output_path = os.path.join(module_path, file_name)
# create sub-dir if required
base_dir = os.path.dirname(output_path)
if not os.path.exists(base_dir):
os.mkdir(base_dir)
with open(output_path, "w") as fp:
fp.write(contents)
return True
def valid_module_name(value):
if not re.match(MODULE_NAME_RE, value):
msg = "Invalid module name, should match regexp: %s" % MODULE_NAME_RE
raise argparse.ArgumentTypeError(msg)
return value
if __name__ == "__main__":
import argparse
import sys
desc = "Create an IoT.js external module using a template"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument("module_name", metavar="<MODULE NAME>", nargs=1,
type=valid_module_name,
help="name of the new module ((must be in lowercase " +
"and should match regexp: %s)" % MODULE_NAME_RE)
parser.add_argument("--path", default=".",
help="directory where the module will be created " +
"(default: %(default)s)")
parser.add_argument("--template", default="basic",
choices=["basic", "shared"],
help="type of the template which should be used "
"(default: %(default)s)")
args = parser.parse_args()
template_dir = os.path.join(TEMPLATE_BASE_DIR,
"%s_module_template" % args.template)
template_files = load_templates(template_dir)
created = create_module(args.path,
args.module_name[0],
template_dir,
template_files)
if created:
module_path = os.path.join(args.path, args.module_name[0])
print("Module created in: {}".format(os.path.abspath(module_path)))