forked from Kapiainen/Lauhdutin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_release.py
140 lines (134 loc) · 5.27 KB
/
build_release.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
139
140
import sys
import os
import zipfile
import subprocess
current_working_directory = os.getcwd()
def parse_gitignore(root_path):
print("\n Processing .gitignore...")
gitignore_path = os.path.join(current_working_directory, ".gitignore")
if os.path.isfile(gitignore_path):
gitignore = []
with open(gitignore_path, "r") as f:
gitignore = f.readlines()
folders_to_ignore = []
files_to_ignore = []
file_patterns_to_ignore = []
print(" Reading '%s'" % os.path.relpath(gitignore_path, current_working_directory))
for line in gitignore:
line = line.strip()
path = os.path.join(current_working_directory, line)
if os.name == "nt":
path = path.replace("/", "\\")
else:
path = path.replace("\\", "/")
if path.find(root_path):
print(" Jumping over:", path)
continue
if os.path.isdir(path):
print(" Folder:", line)
folders_to_ignore.append(path)
elif os.path.isfile(path):
print(" File:", line)
files_to_ignore.append(path)
elif path.find("*.") >= 0:
print(" File pattern:", line)
file_patterns_to_ignore.append(path)
else:
print(" Unsupported:", line)
print("\n Folders to ignore")
for line in folders_to_ignore:
print(" '%s'" % os.path.relpath(line, current_working_directory))
print("\n Files to ignore")
for line in files_to_ignore:
print(" '%s'" % os.path.relpath(line, current_working_directory))
return folders_to_ignore, files_to_ignore, file_patterns_to_ignore
return None, None, None
def main(root_path, releases_path, version):
release_name = "Lauhdutin"
author_name = "Kapiainen"
print("\nGenerating release: '%s - %s'" % (release_name, version))
folders_to_ignore, files_to_ignore, file_patterns_to_ignore = parse_gitignore(root_path)
print("\n Gathering stuff to include in the release...")
files_to_pack = []
for root, dirs, files in os.walk(root_path):
skip = False
for folder in folders_to_ignore:
if folder in root:
skip = True
break
if skip:
print(" Skipping folder:", root)
continue
print(" Processing folder:", root)
for file in files:
path = os.path.join(root, file)
if path in files_to_ignore:
print(" Skipping file: '%s'" % os.path.relpath(path, root_path))
else:
skip = False
for pattern in file_patterns_to_ignore:
folder_path, extension = pattern.split("*")
if path.find(folder_path) >= 0 and path.endswith(extension):
skip = True
break
if skip:
print(" Skipping file based on pattern: '%s'" % os.path.relpath(path, root_path))
else:
print(" Adding file: '%s'" % os.path.relpath(path, root_path))
if "init.lua" in path:
with open(path, "r") as f:
for line in f.readlines():
if "RUN_TESTS" in line and "=" in line:
if "true" in line:
print("\n Aborted build! Tests are enabled in '%s'!" % path)
return
else:
break
files_to_pack.append(path)
print("\n Files to pack:")
for file in files_to_pack:
print(" ", file)
try: # Check if 'zlib' module is available for 'zipfile.ZipFile' to use for compressing.
import zlib
compression_type = zipfile.ZIP_DEFLATED
print("\n Using 'zlib' module to generate a compressed archive...")
except ImportError:
print("\n 'zlib' module could not be imported!")
print(" Generating uncompressed archive instead...")
compression_type = zipfile.ZIP_STORED
readme_path = os.path.join(current_working_directory, "Readme.md")
license_path = os.path.join(current_working_directory, "License.md")
changelog_path = os.path.join(current_working_directory, "Changelog.md")
contributors_path = os.path.join(current_working_directory, "Contributors.md")
english_translation_path = os.path.join(current_working_directory, "translations", "English.txt")
with zipfile.ZipFile(os.path.join(releases_path, "%s - %s" % (release_name, version)) + ".zip", mode="w", compression=compression_type) as release_archive:
release_archive.write(readme_path, "Readme.md")
release_archive.write(license_path, "License.md")
release_archive.write(changelog_path, "Changelog.md")
release_archive.write(contributors_path, "Contributors.md")
release_archive.write(english_translation_path, os.path.join("@Resources", "Languages", "English.txt"))
for file in files_to_pack:
release_archive.write(file, os.path.relpath(file, root_path))
print("\nSuccessfully generated the release!")
try:
root_path = os.path.join(current_working_directory, "dist")
releases_path = os.path.join(current_working_directory, "Releases")
if not os.path.isdir(releases_path):
os.makedirs(releases_path)
# Compile source files
compiler = subprocess.Popen([os.path.join(current_working_directory, "compile_src.bat")], cwd=current_working_directory)
compiler.wait()
if compiler.returncode == 0:
# Update translation file
translation_updater = subprocess.Popen(["python", os.path.join(current_working_directory, "update_translations.py")], cwd=current_working_directory)
translation_updater.wait()
if translation_updater.returncode == 0:
main(root_path, releases_path, input("Enter release version: "))
else:
print("Failed to update the translation file!")
else:
print("Failed to compile source files!")
except:
import traceback
traceback.print_exc()
input("\nPress enter to exit...")