-
Notifications
You must be signed in to change notification settings - Fork 201
/
build_pkg
373 lines (335 loc) · 18.3 KB
/
build_pkg
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Name: build_pkg
# Purpose: Module to create package, executable, source archieve and upload to sourceforge site.
# Authors: Sundar
# Licence: This file is a part of multibootusb package. You can redistribute it or modify
# under the terms of GNU General Public License, v.2 or above
"""
This is an internal script to make automation of building binary/ source packages and uploading to Sourceforge.
This may not work for you without modification in to variables and paths. Please amend wherever required.
Originally written for cryptully and modified by me for multibootusb.
Released under General Public Licence (GPL).
Author: Sundar
"""
import os
import shutil
import subprocess
import sys
import platform
import re
import json
def load_path_setting(src_json_file):
if not os.path.exists(src_json_file):
return None
with open(src_json_file) as f:
platform2settings = json.load(f)
return platform2settings[platform.system()]
####################################################################################################
# Change the below variables to suit your needs
# or create 'build_pkg_path.json' file.
if platform.system() == "Windows":
d = load_path_setting('build_pkg_path.json')
if d is None:
d = {
'pyinstaller_path': 'D:\\multibootusb\\pyinstaller\\pyinstaller.py',
'release_dir': 'D:\\multibootusb\\release',
'nsis': '\"C:\\Program Files (x86)\\NSIS\\makensis.exe\"'
}
pyinstaller_path, release_dir, nsis = [
d[x] for x in ['pyinstaller_path', 'release_dir', 'nsis']]
else:
from os.path import expanduser
home = expanduser("~")
# pyinstaller_path = "/home/sundar/Documents/pyInstaller/pyinstaller.py"
pyinstaller_path = "/media/sundar/Data/multibootusb/pyinstaller/pyinstaller.py"
release_dir = "/media/sundar/Data/multibootusb/release"
debian_bin_path = '/usr/bin/multibootusb'
rpm_bin_path = '/usr/local/bin/multibootusb'
# Let's keep the following block disabled until somebody tests it out.
#
# d = load_path_setting('build_pkg_path.json')
# if d is None:
# d = {
# 'pyinstaller_path': '/media/sundar/Data/multibootusb/pyinstaller/pyinstaller.py',
# 'release_dir': '/media/sundar/Data/multibootusb/release',
# 'debian_bin_path': '/usr/bin/multibootusb',
# 'rpm_bin_path': '/usr/local/bin/multibootusb'
# }
# pyinstaller_path, release_dir, debian_bin_path, rpm_bin_path = [
# d[x] for x in ['pyinstaller_path', 'release_dir',
# 'debian_bin_path', 'rpm_bin_path']]
sourceforge_release_path = "[email protected]:/home/frs/project/multibootusb/"
####################################################################################################
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def coorect_bin_path(_file_path, search_bin_path, replace_bin_path):
import shutil
# shutil.move(_file_path, _file_path + "~")
# destination = open(_file_path, "w")
# source = open(_file_path + "~", "r").read()
with open(_file_path + "~", 'r') as _infile:
data = _infile.read()
if re.search(search_bin_path, data, re.I):
_data = re.search(search_bin_path, data, re.I).group()
_mod_text = re.sub(search_bin_path, replace_bin_path, data)
print('_mod_text', _mod_text)
with open(_file_path, 'w') as _out_file:
_out_file.write(_mod_text)
else:
with open(_file_path, 'w') as _out_file:
_out_file.write(data)
class pkg():
def __init__(self, name):
self.pkg_name = name
self.version = open(os.path.join("data", "version.txt"), 'r').read().strip()
self.release_upload_dir = os.path.join(release_dir, self.version)
def build_package(self):
self.clean_dir()
if not os.path.exists(self.release_upload_dir):
os.mkdir(self.release_upload_dir)
if not os.path.exists(os.path.join(self.release_upload_dir, "Linux")):
os.mkdir(os.path.join(self.release_upload_dir, "Linux"))
if not os.path.exists(os.path.join(self.release_upload_dir, "Windows")):
os.mkdir(os.path.join(self.release_upload_dir, "Windows"))
if not os.path.exists(os.path.join(self.release_upload_dir, "Source")):
os.mkdir(os.path.join(self.release_upload_dir, "Source"))
if self.pkg_name == "deb":
print('Modifying policy file...')
coorect_bin_path('org.debian.pkexec.run-multibootusb.policy', rpm_bin_path, debian_bin_path)
print("Ensure thta you have python-stdeb package installed!")
stdcfg = """[DEFAULT]
Package: multibootusb
Depends3: python3-pyqt5, parted, util-linux, mtools, python3-dbus, python3-pyudev, p7zip-full, python3-six
Build-Depends: python3-all
Section: system
XS-Python-Version: = 3
Debian-Version: 1"""
with open("stdeb.cfg", "w") as f:
f.write(stdcfg)
if subprocess.call('/usr/bin/python3 setup.py --command-packages=stdeb.command bdist_deb', shell=True) == 0 and \
os.path.exists(os.path.join("deb_dist", "python3-multibootusb_" + self.version + "-1_all.deb")):
try:
shutil.copy2(os.path.join("deb_dist", "python3-multibootusb_" + self.version + "-1_all.deb"),
os.path.join(self.release_upload_dir, "Linux"), follow_symlinks=True) #,follow_symlinks=False
except:
os.system('cp -rf ' + os.path.join("deb_dist", "python3-multibootusb_" + self.version + "-1_all.deb") + ' ' +
os.path.join(self.release_upload_dir, "Linux"))
if os.path.exists("stdeb.cfg"):
os.remove("stdeb.cfg")
print("\n\n\nDebian package has been created and can be found here::")
print((os.path.join("deb_dist", "python3-multibootusb_" + self.version + "-1_all.deb\n\n\n")))
result = True
elif self.pkg_name == 'rpm' or self.pkg_name == 'suse' or self.pkg_name == 'mageia':
print('Modifying policy file for rpm packages...')
coorect_bin_path('org.debian.pkexec.run-multibootusb.policy', debian_bin_path, rpm_bin_path)
if self.pkg_name == 'suse':
require = "python3-qt5, parted, util-linux, mtools, dbus-1-python3, python3-pyudev, p7zip, python3-six"
elif self.pkg_name == 'mageia':
require = "python3-qt5, parted, util-linux, mtools, python3-dbus, python3-pyudev, p7zip, python3-six"
else:
require = "python3-PyQt5, parted, util-linux, mtools, python3-dbus, python3-pyudev, p7zip, p7zip-plugins, python3-six"
setup_cfg = ("[bdist_rpm]\n"
"Group = Applications/System\n"
"Vendor = Sundar <[email protected]>\n"
"Requires = " + require)
with open("setup.cfg", "w") as f:
f.write(setup_cfg)
if subprocess.call('/usr/bin/python3 setup.py bdist_rpm', shell=True) == 0 and \
os.path.exists(os.path.join("dist", "multibootusb-" + self.version + "-1.noarch.rpm")):
if self.pkg_name == 'suse':
package = "multibootusb-" + self.version + "-1suse.noarch.rpm"
elif self.pkg_name == 'mageia':
package = "multibootusb-" + self.version + "-1mageia.noarch.rpm"
else:
package = "multibootusb-" + self.version + "-1.noarch.rpm"
try:
shutil.copy2(os.path.join("dist", "multibootusb-" + self.version + "-1.noarch.rpm"),
os.path.join(self.release_upload_dir, "Linux", package))
except:
os.system('cp -rf ' + os.path.join("dist", "multibootusb-" + self.version + "-1.noarch.rpm") + ' ' +
os.path.join(self.release_upload_dir, "Linux", package))
if os.path.exists("setup.cfg"):
os.remove("setup.cfg")
print("\n\n\nRPM package has been created and can be found here::")
print((os.path.join("dist", "multibootusb-" + self.version + "-1.noarch.rpm\n\n\n")))
result = True
elif self.pkg_name == "exe":
if not platform.system() == "Windows":
print("You can generate windows executable from windows host only.")
else:
# subprocess.call('python ' + pyinstaller_path + ' --upx-dir C:\\upx multibootusb.spec', shell=True) == 0 and \
# os.path.exists(os.path.join("dist", 'multibootusb-' + self.version + ".exe")):
if subprocess.call('python ' + pyinstaller_path + ' --uac-admin --onefile onefile-multibootusb.spec', shell=True) == 0 and \
os.path.exists(os.path.join("dist", 'multibootusb-' + self.version + ".exe")):
shutil.copy2(os.path.join("dist", 'multibootusb-' + self.version + ".exe"),
os.path.join(self.release_upload_dir, "Windows"))
print("\n\n\nWindows binary has been created and can be found here::")
print((os.path.join("dist", "multibootusb-" + self.version + ".exe\n\n\n")))
result = True
elif self.pkg_name == "setup_exe":
import re
if not platform.system() == "Windows":
print("You can generate windows setup file from windows host only.")
else:
# subprocess.call('python ' + pyinstaller_path + ' --upx-dir C:\\upx multibootusb.spec', shell=True) == 0 and \
# os.path.exists(os.path.join("dist", 'multibootusb-' + self.version + ".exe")):
# pyinstaller.exe --onefile --windowed --icon=app.ico app.py
if subprocess.call('python ' + pyinstaller_path + ' --onedir --uac-admin --windowed --icon=' +
os.path.join("data", "tools", "multibootusb.ico") + ' multibootusb', shell=True) == 0:
print('Successfully created one dir distribution package...\nCopying data directory...')
shutil.copytree('data', os.path.join('dist', 'multibootusb', 'data'))
if os.path.exists(os.path.join('dist', 'multibootusb', 'data')):
print('Data directory successfully copied..')
with open("multibootusb.nsi", "rt") as fin:
with open("multibootusb_new.nsi", "wt") as fout:
for line in fin:
if line.startswith('Name'):
fout.write('Name \"multibootusb ' + self.version + '\"')
elif line.startswith('OutFile'):
fout.write('OutFile \"multibootusb-' + self.version + '-setup.exe\"')
else:
fout.write(line)
if subprocess.call(nsis + ' multibootusb_new.nsi', shell=True) == 0:
os.remove('multibootusb_new.nsi')
shutil.copy2('multibootusb-' + self.version + '-setup.exe',
os.path.join(self.release_upload_dir, "Windows"))
print('Successfully created the setup.exe package...')
elif self.pkg_name == 'install':
if subprocess.call("python3", "install.py", shell=True) == 0:
print("Installation is successful.")
result = True
elif self.pkg_name == 'src':
package = "multibootusb-" + self.version + ".tar.gz"
if subprocess.call('python3 setup.py sdist', shell=True) == 0 and \
os.path.exists(os.path.join("dist", "multibootusb-" + self.version + ".tar.gz")):
try:
shutil.copy2(os.path.join("dist", "multibootusb-" + self.version + ".tar.gz"),
os.path.join(self.release_upload_dir, "Source"), follow_symlinks=False)
except:
os.system('cp -rf ' + os.path.join("dist", package) + ' ' + os.path.join(self.release_upload_dir, "Source", package))
print("\n\n\nSource package has been created and can be found here::")
print((os.path.join("dist", "multibootusb-" + self.version + ".tar.gz\n\n\n")))
result = True
elif self.pkg_name == 'run':
subprocess.call(['python3', 'multibootusb'])
elif self.pkg_name == 'clean':
self.clean_dir()
elif self.pkg_name == "upload":
self.upload()
else:
print("Option not found.")
usage()
'''
if result:
return result
'''
def upload(self):
if platform.system() == "Windows":
print("You can upload to SF only from Linux as it needs rsync.")
else:
print("Uploading files...")
cmd = "rsync --rsh=ssh -l -p -r -t -z --stats /media/sundar/Data/multibootusb/release/" + self.version + " " + sourceforge_release_path
if os.system(cmd) == 0:
print((bcolors.OKGREEN + "\n\nVersion " + self.version + " has been successfully uploaded to SF.\n\n" + bcolors.ENDC))
else:
print((bcolors.FAIL + "\n\n\nError while uploading to SF.\n\n\n" + bcolors.ENDC))
def clean_dir(self):
if not os.path.exists('build') and not os.path.exists('dist') and not os.path.exists('deb_dist'):
print("Already clean. Nothing to do.")
else:
dir_list = ["build", "dist", "deb_dist"]
if os.path.exists('MANIFEST'):
self.deleteFile('MANIFEST')
for directory in dir_list:
if os.path.exists(directory):
shutil.rmtree(directory)
print("Cleaning pthon byte files...")
for path, subdirs, files in os.walk(os.curdir):
# print subdirs
for name in files:
if name.endswith('.pyc'):
print(("Cleaning " + os.path.join(path, name)))
os.chmod(os.path.join(path, name), 0o777)
os.unlink(os.path.join(path, name))
# os.remove(os.path.join(path,name))
def deleteDirectory(self, path):
try:
for files in os.listdir(path):
if os.path.isdir(os.path.join(path, files)):
# print (os.path.join(path, files))
os.chmod(os.path.join(path, files), 0o777)
shutil.rmtree(os.path.join(path, files))
else:
# print (os.path.join(path, files))
os.chmod(os.path.join(path, files), 0o777)
os.unlink(os.path.join(path, files))
os.remove(os.path.join(path, files))
if os.path.exists(path):
print("Path exist.")
os.rmdir(path)
shutil.rmtree(path)
else:
print("Path exist.")
except OSError as ose:
# Ignore 'no such file or directory' errors
if ose.errno != 2:
print("OS Error.")
# Useful to delete files.
def deleteFile(self, path):
try:
os.unlink(path)
except OSError as ose:
if ose.errno != 2:
print(ose)
def usage():
""" Prompt users how to use """
print ("Invalid option(s)\n"
"Possible options are ::\n"
"\033[94mexe\033[0m <-- For making Windows/ Linux standalone executable using pyinstaller\n"
"\033[94msetup_exe\033[0m<-- For making Windows setup file using pyinstaller\n"
"\033[94mdeb\033[0m <-- For making creating package for debian/ubuntu\n"
"\033[94mrpm\033[0m <-- For creating package for fedora/redhat/centos\n"
"\033[94msuse\033[0m <-- For creating package for OpenSuse\n"
"\033[94mmageia\033[0m <-- For creating package for mageia/mandriva\n"
"\033[94msrc\033[0m <-- For creating source package for other distributions\n"
"\033[94mall\033[0m <-- For creating package for all distros\n"
"\033[94mclean\033[0m <-- For Cleaning dist, build directory and other temp/python byte files\n"
"\033[94minstall\033[0m <-- Directly install from source package\n"
"\033[94mrun\033[0m <-- Directly run multibootusb from source package\n"
"\033[92mupload\033[0m <-- Upload package directory to SourceForge")
sys.exit(-1)
if __name__ == '__main__':
# Ensure to pack all menus to packaging directory
if os.path.exists(os.path.join('data', 'multibootusb', 'grub', 'menus.zip')):
os.remove(os.path.join('data', 'multibootusb', 'grub', 'menus.zip'))
shutil.make_archive(os.path.join('data', 'multibootusb', 'grub', 'menus'),
'zip', os.path.join('data', 'multibootusb', 'grub', 'menus'))
if platform.system() == 'Linux':
print('Converting line ending to Linux for proper functioning.')
os.system('dos2unix multibootusb')
argv = sys.argv
if not os.path.exists(release_dir):
print("Release directory does not exist.\nPlease mount and rerun the script.")
sys.exit(1)
elif not os.path.exists(pyinstaller_path):
print("Pyinstaller path does not exist.\nPlease correct the path and rerun the script.")
sys.exit(1)
if len(argv) == 1:
usage()
else:
argv = argv[1]
if argv == "all":
all_arug = ["clean", "exe", "deb", "rpm", "suse", "mageia", "src", "setup_exe"]
for package_name in all_arug:
print(("Creating package for argument " + package_name))
build = pkg(package_name)
build.build_package()
else:
build = pkg(argv)
build.build_package()