forked from demisto/content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_creator.py
More file actions
114 lines (87 loc) · 3.8 KB
/
content_creator.py
File metadata and controls
114 lines (87 loc) · 3.8 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
import os
import sys
import yaml
import glob
import shutil
CONTENT_DIRS = ['Integrations', 'Misc', 'Playbooks', 'Reports', 'Dashboards', 'Widgets', 'Scripts']
# temp folder names
BUNDLE_PRE = 'bundle_pre'
BUNDLE_POST = 'bundle_post'
# zip files names (the extension will be added later - shutil demands file name without extension)
ZIP_PRE = 'content_yml'
ZIP_POST = 'content_new'
def is_ge_version(ver1, ver2):
# fix the version to arrays of numbers
if isinstance(ver1, str): ver1 = [int(i) for i in ver1.split('.')]
if isinstance(ver2, str): ver2 = [int(i) for i in ver2.split('.')]
for v1, v2 in zip(ver1, ver2):
if v1 > v2:
return False
# most significant values are equal
return len(ver1) <= len(ver2)
def add_tools_to_bundle(bundle):
for d in glob.glob(os.path.join('Tools', '*')):
shutil.make_archive(os.path.join(bundle, 'tools-%s' % (os.path.basename(d), )), 'zip', d)
def copy_dir_yml(dir_name, version_num, bundle_pre, bundle_post):
scan_files = glob.glob(os.path.join(dir_name, '*.yml'))
post_files = 0
for path in scan_files:
with open(path, 'r') as f:
yml_info = yaml.safe_load(f)
ver = yml_info.get('fromversion', '0')
if is_ge_version(version_num, ver):
print ' - marked as post: %s (%s)' % (ver, path, )
shutil.copyfile(path, os.path.join(bundle_post, os.path.basename(path)))
post_files += 1
else:
# add the file to both bundles
print ' - marked as pre: %s (%s)' % (ver, path, )
shutil.copyfile(path, os.path.join(bundle_pre, os.path.basename(path)))
shutil.copyfile(path, os.path.join(bundle_post, os.path.basename(path)))
print ' - total post files: %d' % (post_files, )
def copy_dir_json(dir_name, version_num, bundle_pre, bundle_post):
# handle *.json files
scan_files = glob.glob(os.path.join(dir_name, '*.json'))
for path in scan_files:
shutil.copyfile(path, os.path.join(bundle_post, os.path.basename(path)))
shutil.copyfile(path, os.path.join(bundle_pre, os.path.basename(path)))
def copy_dir_files(*args):
# handle *.json files
copy_dir_json(*args)
# handle *.yml files
copy_dir_yml(*args)
def main(circle_artifacts):
print 'starting create content artifact ...'
# version that separate post bundle from pre bundle
# e.i. any yml with "fromversion" of <version_num> or more will be only on post bundle
version_num = "3.5"
print 'creating dir for bundles ...'
for b in [BUNDLE_PRE, BUNDLE_POST]:
os.mkdir(b)
add_tools_to_bundle(b)
for d in CONTENT_DIRS:
print 'copying dir %s to bundles ...' % (d,)
copy_dir_files(d, version_num, BUNDLE_PRE, BUNDLE_POST)
print 'copying content descriptor to bundles'
for b in [BUNDLE_PRE, BUNDLE_POST]:
shutil.copyfile('content-descriptor.json', os.path.join(b, 'content-descriptor.json'))
print 'compressing bundles ...'
shutil.make_archive(ZIP_POST, 'zip', BUNDLE_POST)
shutil.make_archive(ZIP_PRE, 'zip', BUNDLE_PRE)
shutil.copyfile(ZIP_PRE + '.zip', os.path.join(circle_artifacts, ZIP_PRE + '.zip'))
shutil.copyfile(ZIP_POST + '.zip', os.path.join(circle_artifacts, ZIP_POST + '.zip'))
shutil.copyfile('release-notes.txt', os.path.join(circle_artifacts, 'release-notes.txt'))
print 'finished create content artifact'
def test_version_compare(version_num):
V = ['3.5', '2.0', '2.1', '4.7', '1.1.1', '1.5', '3.10.0', '2.7.1', '3', '3.4.9', '3.5.1']
lower = []
greater = []
for v in V:
if is_ge_version(version_num, v):
greater.append(v)
else:
lower.append(v)
print 'lower versions: %s' % (lower, )
print 'greater versions: %s' % (greater, )
if __name__ == '__main__':
main(*sys.argv[1:])