import filecmp import os
import platform import shutil
okgreen = "\033[1;32;40m"
failred = "\033[1;31;40m"
highlight = "\033[1;34;40m"
endc = "\033[0m"
OK = okgreen + " OK" + endc
ERROR = failred + "ERROR: " + endc
crate_root = os.path.dirname(os.getcwd()) + "/"
binary = "raspberry-web"
database = "raspberry-web.sqlite"
config = "configuration.toml"
service = "raspberry-web.service"
binary_source = crate_root + "target/release/"
config_source = crate_root + "config/"
database_soure = crate_root + "raspberry-web-db/"
systemd_source = crate_root + "config/"
binary_target = "/usr/local/bin/"
config_target = "/usr/local/raspberry-web/"
database_target = "/usr/local/raspberry-web/database/"
systemd_target = "/etc/systemd/system/"
def mkdir_if_not_exists(path):
if not os.path.exists(path):
os.makedirs(path)
print("Created directory" + highlight + path + endc + OK)
else:
print(highlight + path + endc + " is already present" + OK)
def cp_if_dst_different(source, target):
if os.path.isfile(target):
if not filecmp.cmp(source, target, shallow=False):
shutil.copy2(source, target)
print("Added new version of file " + highlight + target + endc + OK)
return True
else:
print(highlight + target + endc + " already present in same version" + OK)
return False
else:
shutil.copy2(source, target)
print("Added file " + highlight + target + endc + OK)
return True
def cp_if_dst_nonexistant(source, target):
if not os.path.isfile(target):
shutil.copy2(source, target)
print("Added file " + highlight + target + endc + OK)
return True
else:
print(highlight + target + endc + " already present" + OK)
return False
def check_file_exists_or_exit(path):
if os.path.isfile(path):
print("Found source file " + highlight + path + endc + OK)
else:
print(ERROR + highlight + path + endc + " not present.")
exit()
supported_os = ['Linux', 'Darwin']
if __name__ == "__main__":
system = platform.system()
if system not in supported_os:
print(ERROR + highlight + system + endc + " is not supported.")
exit()
print("Checking source files...")
soure_files = [
binary_source+binary,
config_source+config,
database_soure+database,
systemd_source+service]
for path in soure_files:
check_file_exists_or_exit(path)
print()
print("Checking target directories...")
for path in [config_target, database_target]:
mkdir_if_not_exists(path)
print()
print("Copying files...")
binary_copied = cp_if_dst_different(binary_source+binary, binary_target+binary)
if binary_copied:
os.chmod(binary_target+binary, 0o755)
config_copied = cp_if_dst_nonexistant(config_source+config, config_target+config)
if config_copied:
os.chmod(config_target+config, 0o644)
database_copied = cp_if_dst_nonexistant(database_soure+database, database_target+database)
if database_copied:
os.chmod(database_target+database, 0o644)
if system == 'Linux':
service_copied = cp_if_dst_nonexistant(systemd_source+service, systemd_target+service)
if service_copied:
os.chmod(systemd_target+service, 0o644)
else:
print("Not adding " + highlight + service + endc + " to " + highlight + systemd_target \
+ endc + " since we are running " + highlight + system + endc + OK)
print()
print(okgreen + "Done." + endc)