-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.py
executable file
·117 lines (92 loc) · 3.11 KB
/
build.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
#!/usr/bin/env python3
# deploy_it, alpha release
# Copyright 2019, Aswin Babu Karuvally
# import the serious stuff
import json
import shutil
import sys
import os
import logging
from distutils.dir_util import copy_tree
import pdb # debug
# stuff to check before starting process
def init_checks(config, file_list):
for system_file in file_list:
if not os.path.exists(system_file):
logging.critical(system_file + " does not exist, exiting...")
sys.exit(1)
# config script specific checks
if config["systemd_service"]["enable"] == True:
unit_file = config["systemd_service"]["unit_file"]
if not os.path.exists(unit_file):
logging.critical(unit_file + " does not exist, exiting...")
sys.exit(1)
if config["post_install_script"]["enable"]:
script_file = config["post_install_script"]["script_file"]
if not os.path.exists(script_file):
logging.critical(script_file + " does not exist, exiting...")
def setup_logging():
format_string = "[%(asctime)s] %(message)s"
date_format = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(
filename = os.path.join("build.log"),
level = logging.DEBUG,
format = format_string,
datefmt = date_format
)
# print logs to stderr
logging.getLogger().addHandler(logging.StreamHandler())
# build the deployment archive
def build_archive(config, file_list):
# get configuration
archive_name = config["archive"]["filename"]
archive_format = config["archive"]["format"]
source_dir = config["basics"]["source_dir"]
# basic checks
if not os.path.isdir(source_dir):
logging.warning("source directory is not accessible, exiting...")
sys.exit()
if os.path.isdir(os.path.join(source_dir, ".git")):
shutil.rmtree(os.path.join(source_dir, ".git"))
# copy files
os.mkdir(archive_name)
for file_path in file_list:
shutil.copy(file_path, archive_name)
copy_tree("src", archive_name + "/src")
# create archive
shutil.make_archive(
base_name = archive_name,
format = archive_format,
base_dir = archive_name
)
# cleanup and exit
shutil.rmtree(archive_name)
logging.info(archive_name + "." + archive_format + " is built")
# the main function
def main():
# check if the current dir is writable
if not os.access("./", os.W_OK):
print("current directory is not writable, exiting...")
sys.exit(1)
# essential stuff
setup_logging()
logging.info("initializing deploy_it builder")
file_list = [
"deploy.py",
"install_venv.json",
"uninstall.py",
"config.json"
]
if not os.path.exists("config.json"):
logging.critical("config.json does not exist, exiting...")
sys.exit(1)
# read the configution
with open("config.json") as config_file:
config = config_file.read()
config = json.loads(config)
# do the initial checks
init_checks(config, file_list)
# build the archive
build_archive(config, file_list)
if __name__ == "__main__":
main()