-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchive.py
138 lines (111 loc) · 3.63 KB
/
archive.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
134
135
136
137
138
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Copyright © 2014 by Adam Hellberg <[email protected]>.
#
import os
import re
import sys
import fnmatch
import zipfile
import glob
from subprocess import call
SCRIPT = sys.argv[0]
TOC_VERSION_PATTERN = re.compile("^## Version: (\d+\.\d+\.\d+(?:-[a-z0-9]+)?)$")
def log(msg, *args):
print '{0}: {1}'.format(SCRIPT, msg.format(*args))
if len(sys.argv) < 3:
log('expected args: name, build number')
sys.exit(1)
name = None
build = None
try:
name = str(sys.argv[1]).strip()
build = int(sys.argv[2].strip())
except ValueError:
log('expected build number argument of type integer')
sys.exit(1)
TOC_FILENAME = "{0}.toc".format(name)
def clean_directory(dst):
log('Cleaning directory: {0}', dst)
if os.path.isdir(dst):
for f in os.listdir(dst):
fp = os.path.join(dst, f)
try:
if os.path.isfile(fp):
os.unlink(fp)
except Exception, e:
log('EXCEPTION: Failed to delete {0}: {1}', fp, e)
elif os.path.isfile(dst):
os.unlink(dst)
os.makedirs(dst)
else:
os.makedirs(dst)
log('Directory cleanup completed: {0}', dst)
build_dirs = ['build', 'lua', os.path.join('lua', 'locales')]
for build_dir in build_dirs:
clean_directory(build_dir)
additional_files = ["LICENSE", "README.md", TOC_FILENAME, 'load_locales.xml']
ignored = ['/.*', SCRIPT, '*.moon', '*.zip']
def is_file_ignored(file):
for pattern in ignored:
if os.name == 'nt':
pattern = pattern.replace('/', '\\')
if re.search(fnmatch.translate(pattern), file):
return True
return False
def compile_moon(path, target):
files = glob.glob(path)
log('Compiling MoonScript files in {0} to {1}', path, target)
ret = call(['moonc', '-t', target] + files)
if ret != 0:
log('FAILURE: moonc compile of {0} exited with non-zero return code', path)
sys.exit(1)
def lint_addon():
files = glob.glob('*.moon')
ret = call(["moonc", "-l"] + files)
if ret != 0:
log('FAILURE: moonc linter exited with non-zero return code')
sys.exit(1)
def compile_addon():
lint_addon()
compile_moon('*.moon', 'lua')
compile_moon(os.path.join('locales', '*.moon'), 'lua')
luafiles = glob.glob(os.path.join('lua', '**', '*.lua'))
ret = call(["luac", "-p"] + luafiles)
if ret != 0:
log('FAILURE: luac parse exited with non-zero return code')
sys.exit(1)
def zip_file(zf, file):
absname = os.path.abspath(file)
if not is_file_ignored(absname):
arcname = os.path.join(name, file)
log('{0} -> {1}', file, arcname)
zf.write(absname, arcname)
def zip_dir(zf, path):
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
zip_file(zf, os.path.join(dirpath, filename))
def make_zip(sources, zf):
for src in sources:
if os.path.isdir(src):
zip_dir(zf, src)
else:
zip_file(zf, src)
def get_version():
version = "UNKNOWN"
for line in open(TOC_FILENAME).read().splitlines():
match = TOC_VERSION_PATTERN.match(line)
if match:
version = match.group(1)
break
return version
version = get_version()
zipname = os.path.join('build', '{0}_v{1}_b{2}.zip'.format(name, version, build))
compile_addon()
ignored.append(zipname)
log('Building zip file at {0}', zipname)
with zipfile.ZipFile(zipname, 'w', zipfile.ZIP_DEFLATED) as zf:
make_zip(['libs', 'lua'] + additional_files, zf)
zf.close()
log('DONE! Zip successfully created: {0}!', zipname)