forked from demisto/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage_creator.py
More file actions
executable file
·139 lines (105 loc) · 4.89 KB
/
package_creator.py
File metadata and controls
executable file
·139 lines (105 loc) · 4.89 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python
import os
import sys
import glob
import yaml
import base64
import argparse
import re
DIR_TO_PREFIX = {
'Integrations': 'integration',
'Scripts': 'script'
}
TYPE_TO_EXTENSION = {
'python': '.py',
'javascript': '.js'
}
IMAGE_PREFIX = 'data:image/png;base64,'
def merge_script_package_to_yml(package_path, dir_name, dest_path=""):
"""Merge the various components to create an output yml file
Args:
package_path (str): Directory containing the various files
dir_name (str): Parent directory containing package (Scripts/Integrations)
dest_path (str, optional): Defaults to "". Destination output
Returns:
output path, script path, image path
"""
output_filename = '{}-{}.yml'.format(DIR_TO_PREFIX[dir_name], os.path.basename(os.path.dirname(package_path)))
if dest_path:
output_path = os.path.join(dest_path, output_filename)
else:
output_path = os.path.join(dir_name, output_filename)
yml_path = glob.glob(package_path + '*.yml')[0]
with open(yml_path, 'r') as yml_file:
yml_data = yaml.safe_load(yml_file)
if dir_name == 'Scripts':
script_type = TYPE_TO_EXTENSION[yml_data['type']]
elif dir_name == 'Integrations':
script_type = TYPE_TO_EXTENSION[yml_data['script']['type']]
with open(yml_path, 'r') as yml_file:
yml_text = yml_file.read()
yml_text, script_path = insert_script_to_yml(package_path, script_type, yml_text, dir_name, yml_data)
yml_text, image_path = insert_image_to_yml(dir_name, package_path, yml_data, yml_text)
with open(output_path, 'w') as f:
f.write(yml_text)
return output_path, yml_path, script_path, image_path
def insert_image_to_yml(dir_name, package_path, yml_data, yml_text):
image_path = glob.glob(package_path + '*png')
found_img_path = None
if dir_name == 'Integrations' and image_path:
found_img_path = image_path[0]
with open(found_img_path, 'rb') as image_file:
image_data = image_file.read()
if yml_data.get('image'):
yml_text = yml_text.replace(yml_data['image'], IMAGE_PREFIX + base64.b64encode(image_data))
else:
yml_text = 'image: ' + IMAGE_PREFIX + base64.b64encode(image_data) + '\n' + yml_text
return yml_text, found_img_path
def insert_script_to_yml(package_path, script_type, yml_text, dir_name, yml_data):
ignore_regex = r'CommonServerPython\.py|CommonServerUserPython\.py|demistomock\.py|test_.*\.py|_test\.py'
script_path = list(filter(lambda x: not re.search(ignore_regex, x),
glob.glob(package_path + '*' + script_type)))[0]
with open(script_path, 'r') as script_file:
script_code = script_file.read()
script_code = clean_python_code(script_code)
lines = ['|-']
lines.extend(' {}'.format(line) for line in script_code.split('\n'))
script_code = '\n'.join(lines)
if dir_name == 'Scripts':
if yml_data.get('script'):
yml_text = yml_text.replace(yml_data.get('script'), script_code)
else:
yml_text = yml_text.replace("script: ''", "script: " + script_code)
elif dir_name == 'Integrations':
if yml_data.get('script', {}).get('script'):
yml_text = yml_text.replace(yml_data.get('script', {}).get('script'), script_code)
else:
yml_text = yml_text.replace("script: ''", "script: " + script_code)
return yml_text, script_path
def clean_python_code(script_code):
script_code = script_code.replace("import demistomock as demisto", "")
script_code = script_code.replace("from CommonServerPython import *", "")
script_code = script_code.replace("from CommonServerUserPython import *", "")
return script_code
def get_package_path():
parser = argparse.ArgumentParser(description='Utility merging package yml with its code into one yml file')
parser.add_argument('-p', '--packagePath', help='Path to the package', required=True)
parser.add_argument('-d', '--destPath', help='Destination directory path for the result yml', default="")
options = parser.parse_args()
package_path = options.packagePath
dest_path = options.destPath
if package_path[-1] != '/':
package_path = package_path + '/'
directory_name = ""
for dir_name in DIR_TO_PREFIX.keys():
if dir_name in package_path:
directory_name = dir_name
if not directory_name:
print "You have failed to provide a legal file path, a legal file path " \
"should contain either Integrations or Scripts directories"
sys.exit(1)
return package_path, directory_name, dest_path
if __name__ == "__main__":
package_path, dir_name, dest_path = get_package_path()
output, yml, script, image = merge_script_package_to_yml(package_path, dir_name, dest_path)
print("Done creating: {}, from: {}, {}, {}".format(output, yml, script, image))