-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmakepackage.py
133 lines (101 loc) · 3.73 KB
/
makepackage.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
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
# copyright Allegorithmic. All rights reserved.
import os
import sys
import fnmatch
import json
from zipfile import ZipFile
class IgnoreFileFilter(object):
def __init__(self, filename):
self.__globs = []
self.__dirs_to_ignore = []
if os.path.exists(filename):
with open(filename, 'rt') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
if line == '' or line.startswith('#'):
continue
if line.endswith('/'):
self.__dirs_to_ignore.append(line[:-1])
else:
self.__globs.append(line)
def filter(self, filepath):
# Ignore filter config files.
if filepath.endswith('.sdpackageignore'):
return False
# Check that the file is not included in any glob.
if self.__globs:
filename = os.path.basename(filepath)
for pattern in self.__globs:
if fnmatch.fnmatch(filename, pattern):
return False
# Check that the file is not inside any ignored directory.
if self.__dirs_to_ignore:
dirname = os.path.normpath(os.path.dirname(filepath))
dirs = dirname.split(os.sep)
for pattern in self.__dirs_to_ignore:
for d in dirs:
if pattern == d:
return False
return True
def read_metadata():
if not os.path.exists('pluginInfo.json'):
print("Missing metadata file")
return None
try:
f = open('pluginInfo.json', 'rt')
return json.load(f)
except Exception as e:
print('Error while checking metadata: %s' % e)
f.close()
return None
def check_metadata(metadata):
if not 'name' in metadata:
print('"name" metadata entry is missing')
return False
return True
def walk(directory):
for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
for filename in filenames:
yield os.path.join(dirpath, filename)
def add_file_to_package(zfile, plugin_name, filepath):
print("Adding file %s to package" % filepath)
archive_filepath = os.path.join(plugin_name, filepath)
zfile.write(filepath, arcname=archive_filepath)
def main():
this_dir = os.path.abspath(os.path.dirname(__file__))
build_dir = os.path.join(this_dir, "build")
if not os.path.exists(build_dir):
try:
os.makedirs(build_dir)
except Exception as e:
print('Could not create build directory')
sys.exit(1)
package_parent_dir = os.path.abspath(os.path.join(this_dir, '..'))
package_dir_name = os.path.basename(this_dir)
# Save the current dir and switch to the package dir.
saved_dir = os.getcwd()
os.chdir(this_dir)
metadata = read_metadata()
if not metadata:
sys.exit(1)
if not check_metadata(metadata):
sys.exit(1)
plugin_name = metadata['name']
package_filepath = os.path.join(build_dir, plugin_name) + '.sdplugin'
try:
file_filter = IgnoreFileFilter('.sdpackageignore')
with ZipFile(package_filepath, 'w') as zfile:
for filepath in walk('.'):
if os.path.abspath(filepath) == os.path.join(this_dir, 'pluginInfo.json'):
add_file_to_package(zfile, plugin_name, filepath)
elif file_filter.filter(filepath):
add_file_to_package(zfile, plugin_name, filepath)
except Exception as e:
print("Error while packaging plugin: %s" % e)
sys.exit(1)
finally:
# Restore the saved directory.
os.chdir(saved_dir)
if __name__ == '__main__':
main()