AIL-framework/bin/Update.py

474 lines
20 KiB
Python
Raw Normal View History

2018-11-12 17:10:31 +01:00
#!/usr/bin/env python3
# -*-coding:UTF-8 -*
2018-11-13 14:54:56 +01:00
"""
Update AIL
============================
Update AIL clone and fork
"""
2018-11-12 17:10:31 +01:00
import configparser
import os
import sys
import argparse
2018-11-12 17:10:31 +01:00
import subprocess
sys.path.append(os.environ['AIL_BIN'])
##################################
# Import Project packages
##################################
# TODO: move other functions
from packages import git_status
UPDATER_FILENAME = os.path.join(os.environ['AIL_BIN'], 'Update.py')
UPDATER_LAST_MODIFICATION = float(os.stat(UPDATER_FILENAME).st_mtime)
2018-11-12 17:10:31 +01:00
def auto_update_enabled(cfg):
auto_update = cfg.get('Update', 'auto_update')
if auto_update == 'True' or auto_update == 'true':
return True
else:
return False
# check if files are modify locally
def check_if_files_modified():
2023-05-17 15:46:44 +02:00
# return True
2018-11-12 17:10:31 +01:00
process = subprocess.run(['git', 'ls-files' ,'-m'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
modified_files = process.stdout
if modified_files:
l_modified_files = []
for modified_file in modified_files.decode().split('\n'):
if modified_file:
if modified_file.split('/')[0] != 'configs':
l_modified_files.append(modified_file)
if l_modified_files:
print('Modified Files:')
for modified_file in l_modified_files:
print('{}{}{}'.format(TERMINAL_BLUE, modified_file, TERMINAL_DEFAULT))
print()
return False
else:
return True
2018-11-12 17:10:31 +01:00
else:
return True
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
sys.exit(1)
2018-11-12 17:10:31 +01:00
def repo_is_fork():
2023-05-17 15:46:44 +02:00
# return False
2018-11-13 14:54:56 +01:00
print('Check if this repository is a fork:')
2019-04-23 15:30:47 +02:00
process = subprocess.run(['git', 'remote', '-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-11-12 17:10:31 +01:00
if process.returncode == 0:
2019-04-23 15:30:47 +02:00
res = process.stdout.decode()
if 'origin {}'.format(AIL_REPO) in res:
print(' This repository is a {}clone of {}{}'.format(TERMINAL_BLUE, AIL_REPO, TERMINAL_DEFAULT))
return False
elif 'origin {}'.format(OLD_AIL_REPO) in res:
print(' old AIL repository, Updating remote origin...')
2020-05-22 15:49:06 +02:00
res = git_status.set_default_remote(AIL_REPO, verbose=False)
if res:
return False
else:
return True
2018-11-13 14:54:56 +01:00
else:
2019-04-23 15:30:47 +02:00
print(' This repository is a {}fork{}'.format(TERMINAL_BLUE, TERMINAL_DEFAULT))
print()
return True
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
def is_upstream_created(upstream):
process = subprocess.run(['git', 'remote', '-v'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
output = process.stdout.decode()
if upstream in output:
return True
else:
return False
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
def create_fork_upstream(upstream):
2018-11-13 14:54:56 +01:00
print('{}... Creating upstream ...{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
print('git remote add {} {}'.format(upstream, AIL_REPO))
process = subprocess.run(['git', 'remote', 'add', upstream, AIL_REPO],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-11-12 17:10:31 +01:00
if process.returncode == 0:
print(process.stdout.decode())
2018-11-13 14:54:56 +01:00
if is_upstream_created(upstream):
print('Fork upstream created')
print('{}... ...{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('Fork not created')
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
def update_fork():
2018-11-13 14:54:56 +01:00
print('{}... Updating fork ...{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
if cfg.get('Update', 'update-fork') == 'True' or cfg.get('Update', 'update-fork') == 'true':
upstream = cfg.get('Update', 'upstream')
if not is_upstream_created(upstream):
create_fork_upstream(upstream)
2018-11-13 14:54:56 +01:00
print('{}git fetch {}:{}'.format(TERMINAL_YELLOW, upstream, TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
process = subprocess.run(['git', 'fetch', upstream], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
print(process.stdout.decode())
2018-11-13 14:54:56 +01:00
print('{}git checkout master:{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
process = subprocess.run(['git', 'checkout', 'master'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
print(process.stdout.decode())
2018-11-13 14:54:56 +01:00
print('{}git merge {}/master:{}'.format(TERMINAL_YELLOW, upstream, TERMINAL_DEFAULT))
process = subprocess.run(['git', 'merge', '{}/master'.format(upstream)],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-11-12 17:10:31 +01:00
if process.returncode == 0:
print(process.stdout.decode())
2018-11-13 14:54:56 +01:00
print('{}... ...{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(1)
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}Fork Auto-Update disabled in config file{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
def get_git_current_tag(path_current_version):
2018-11-15 13:48:44 +01:00
try:
with open(path_current_version, 'r') as version_content:
2018-11-15 13:48:44 +01:00
version = version_content.read()
except FileNotFoundError:
version = 'v5.0' # TODO Replace with VERSION.json
with open(path_current_version, 'w') as version_content:
2018-11-15 13:48:44 +01:00
version_content.write(version)
2020-02-25 15:57:07 +01:00
version = version.replace(" ", "").splitlines()[0]
if version[0] != 'v':
version = 'v{}'.format(version)
return version
2018-11-12 17:10:31 +01:00
2018-11-13 14:54:56 +01:00
def get_git_upper_tags_remote(current_tag, is_fork):
2020-02-25 15:57:07 +01:00
# keep only first dot
nb_dot = current_tag.count('.')
if nb_dot > 0:
nb_dot = nb_dot - 1
2020-02-25 15:57:07 +01:00
current_tag_val = current_tag.rsplit('.', nb_dot)
current_tag_val = ''.join(current_tag_val)
2018-11-13 14:54:56 +01:00
if is_fork:
process = subprocess.run(['git', 'tag'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
list_all_tags = process.stdout.decode().splitlines()
list_upper_tags = []
if list_all_tags[-1][1:] == current_tag:
list_upper_tags.append((list_all_tags[-1], None))
2019-05-20 13:02:09 +02:00
# force update order
list_upper_tags.sort()
2018-11-13 14:54:56 +01:00
return list_upper_tags
for tag in list_all_tags:
2020-02-25 15:57:07 +01:00
if float(tag[1:]) >= float(current_tag_val):
list_upper_tags.append((tag, None))
2019-05-20 13:02:09 +02:00
# force update order
list_upper_tags.sort()
2018-11-12 17:10:31 +01:00
return list_upper_tags
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
else:
process = subprocess.run(['git', 'ls-remote', '--tags'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-11-13 14:54:56 +01:00
if process.returncode == 0:
list_all_tags = process.stdout.decode().splitlines()
last_tag = list_all_tags[-1].split('\trefs/tags/')
last_commit = last_tag[0]
last_tag = last_tag[1].split('^{}')[0]
list_upper_tags = []
if last_tag[1:] == current_tag:
list_upper_tags.append((last_tag, last_commit))
2019-05-20 13:02:09 +02:00
# force update order
list_upper_tags.sort()
2018-11-13 14:54:56 +01:00
return list_upper_tags
else:
dict_tags_commit = {}
2018-11-13 14:54:56 +01:00
for mess_tag in list_all_tags:
commit, tag = mess_tag.split('\trefs/tags/')
tag = tag.replace('^{}', '')
# remove 'v' version
2020-02-25 15:57:07 +01:00
tag = tag.replace('v', '')
# keep only first dot
nb_dot = tag.count('.')
if nb_dot > 0:
nb_dot = nb_dot - 1
tag_val = tag.rsplit('.', nb_dot)
tag_val = ''.join(tag_val)
2019-11-25 15:54:53 +01:00
# check if tag is float
try:
tag_val = float(tag_val)
2019-11-25 15:54:53 +01:00
except ValueError:
continue
2023-05-17 15:46:44 +02:00
if float(current_tag) < 5.0:
# add tag with last commit
2023-05-17 15:46:44 +02:00
if float(current_tag_val) <= float(tag_val) < float(5.0):
dict_tags_commit[tag] = commit
else:
# add tag with last commit
if float(tag_val) >= float(current_tag_val):
dict_tags_commit[tag] = commit
2020-02-25 15:57:07 +01:00
list_upper_tags = [('v{}'.format(key), dict_tags_commit[key]) for key in dict_tags_commit]
2019-05-20 13:02:09 +02:00
# force update order
list_upper_tags.sort()
2018-11-13 14:54:56 +01:00
return list_upper_tags
else:
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
def update_submodules():
print('{}git submodule update:{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
process = subprocess.run(['git', 'submodule', 'update'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
print(process.stdout.decode())
print()
else:
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
2018-11-13 14:54:56 +01:00
def update_ail(current_tag, list_upper_tags_remote, current_version_path, is_fork):
print('{}git checkout master:{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
2019-04-23 15:30:47 +02:00
process = subprocess.run(['git', 'checkout', 'master'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# process = subprocess.run(['ls'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-11-12 17:10:31 +01:00
if process.returncode == 0:
2018-11-13 14:54:56 +01:00
print(process.stdout.decode())
print()
update_submodules()
2023-05-17 15:46:44 +02:00
temp_current_tag = current_tag.replace('v', '')
if temp_current_tag.count('.') > 1:
temp_current_tag = temp_current_tag.rsplit('.', 1)
temp_current_tag = ''.join(temp_current_tag)
2018-11-12 17:10:31 +01:00
2023-05-17 15:46:44 +02:00
if float(temp_current_tag) < 5.0:
roll_back_update('2c65194b94dab95df9b8da19c88d65239f398355')
2023-05-17 15:46:44 +02:00
pulled = True
else:
print('{}git pull:{}'.format(TERMINAL_YELLOW, TERMINAL_DEFAULT))
process = subprocess.run(['git', 'pull'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
output = process.stdout.decode()
print(output)
pulled = True
else:
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
pulled = False
sys.exit(1)
2018-11-12 17:10:31 +01:00
2023-05-17 15:46:44 +02:00
if pulled:
# CHECK IF UPDATER Update
if float(os.stat(UPDATER_FILENAME).st_mtime) > UPDATER_LAST_MODIFICATION:
# request updater relaunch
print(f'{TERMINAL_RED} Relaunch Launcher {TERMINAL_DEFAULT}')
sys.exit(3)
2018-11-12 17:10:31 +01:00
if len(list_upper_tags_remote) == 1:
2018-11-15 14:26:41 +01:00
# additional update (between 2 commits on the same version)
additional_update_path = os.path.join(os.environ['AIL_HOME'], 'update', current_tag, 'additional_update.sh')
if os.path.isfile(additional_update_path):
print()
print(f'{TERMINAL_YELLOW}------------------------------------------------------------------')
2018-11-15 14:26:41 +01:00
print('- Launching Additional Update: -')
print(f'-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --{TERMINAL_DEFAULT}')
process = subprocess.run(['bash', additional_update_path],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-11-15 14:26:41 +01:00
if process.returncode == 0:
output = process.stdout.decode()
print(output)
else:
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(1)
2018-11-13 14:54:56 +01:00
print()
print(f'{TERMINAL_YELLOW}**************** AIL Successfully Updated *****************{TERMINAL_DEFAULT}')
2018-11-13 14:54:56 +01:00
print()
exit(0)
2018-11-12 17:10:31 +01:00
else:
# map version with roll back commit
list_update = []
previous_commit = list_upper_tags_remote[0][1]
for row_tuple in list_upper_tags_remote[1:]:
tag = row_tuple[0]
list_update.append((tag, previous_commit))
previous_commit = row_tuple[1]
2018-11-12 17:10:31 +01:00
for update in list_update:
launch_update_version(update[0], update[1], current_version_path, is_fork)
# Success
print(f'{TERMINAL_YELLOW}**************** AIL Successfully Updated *****************{TERMINAL_DEFAULT}')
2018-11-13 14:54:56 +01:00
print()
sys.exit(0)
2023-05-17 15:46:44 +02:00
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.decode(), TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
def launch_update_version(version, roll_back_commit, current_version_path, is_fork):
2020-02-21 13:06:37 +01:00
update_path = os.path.join(os.environ['AIL_HOME'], 'update', str(version), 'Update.sh')
2018-11-13 14:54:56 +01:00
print()
print(f'{TERMINAL_YELLOW}------------------------------------------------------------------')
print(f'- Launching Update: {TERMINAL_BLUE}{version}{TERMINAL_YELLOW} -')
print(f'-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --{TERMINAL_DEFAULT}')
if not os.path.isfile(update_path):
update_path = os.path.join(os.environ['AIL_HOME'], 'update', 'default_update', 'Update.sh')
process = subprocess.Popen(['bash', update_path, version], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
process = subprocess.Popen(['bash', update_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
2018-12-19 15:16:52 +01:00
while True:
output = process.stdout.readline().decode()
if output == '' and process.poll() is not None:
break
if output:
print(output.strip())
2018-11-12 17:10:31 +01:00
if process.returncode == 0:
# output = process.stdout.decode()
# print(output)
2018-11-12 17:10:31 +01:00
with open(current_version_path, 'w') as version_content:
version_content.write(version)
2018-11-13 14:54:56 +01:00
print('{}-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --'.format(TERMINAL_YELLOW))
print('- Successfully Updated: {}{}{} -'.format(TERMINAL_BLUE, version, TERMINAL_YELLOW))
2018-11-13 14:54:56 +01:00
print('------------------------------------------------------------------{}'.format(TERMINAL_DEFAULT))
print()
2018-11-12 17:10:31 +01:00
else:
# print(process.stdout.read().decode())
2018-12-19 15:16:52 +01:00
print('{}{}{}'.format(TERMINAL_RED, process.stderr.read().decode(), TERMINAL_DEFAULT))
2018-11-13 14:54:56 +01:00
print('------------------------------------------------------------------')
print(' {}Update Error: {}{}{}'.format(TERMINAL_RED, TERMINAL_BLUE, version, TERMINAL_DEFAULT))
print('------------------------------------------------------------------')
2018-11-12 17:10:31 +01:00
if not is_fork:
roll_back_update(roll_back_commit)
2018-11-13 14:54:56 +01:00
else:
aborting_update()
sys.exit(1)
2018-11-12 17:10:31 +01:00
def roll_back_update(roll_back_commit):
print('Rolling back to safe commit: {}{}{}'.format(TERMINAL_BLUE, roll_back_commit, TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
process = subprocess.run(['git', 'checkout', roll_back_commit], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if process.returncode == 0:
output = process.stdout
print(output)
2018-11-13 14:54:56 +01:00
sys.exit(0)
2018-11-12 17:10:31 +01:00
else:
print(TERMINAL_RED+process.stderr.decode()+TERMINAL_DEFAULT)
2018-11-13 14:54:56 +01:00
aborting_update()
2018-11-12 17:10:31 +01:00
sys.exit(1)
2018-11-13 14:54:56 +01:00
def aborting_update():
print()
print('{}Aborting ...{}'.format(TERMINAL_RED, TERMINAL_DEFAULT))
print('{}******************************************************************'.format(TERMINAL_RED))
print('* AIL Not Updated *')
print('******************************************************************{}'.format(TERMINAL_DEFAULT))
print()
2018-11-12 17:10:31 +01:00
2018-11-12 17:10:31 +01:00
if __name__ == "__main__":
TERMINAL_RED = '\033[91m'
TERMINAL_YELLOW = '\33[93m'
2018-11-13 14:54:56 +01:00
TERMINAL_BLUE = '\33[94m'
TERMINAL_BLINK = '\33[6m'
2018-11-12 17:10:31 +01:00
TERMINAL_DEFAULT = '\033[0m'
AIL_REPO = 'https://github.com/ail-project/ail-framework'
OLD_AIL_REPO = 'https://github.com/CIRCL/AIL-framework.git'
2018-11-12 17:10:31 +01:00
configfile = os.path.join(os.environ['AIL_HOME'], 'configs/update.cfg')
if not os.path.exists(configfile):
raise Exception('Unable to find the configuration file. \
Did you set environment variables? \
Or activate the virtualenv.')
cfg = configparser.ConfigParser()
cfg.read(configfile)
current_version_path = os.path.join(os.environ['AIL_HOME'], 'update/current_version')
2018-11-13 14:54:56 +01:00
print('{}******************************************************************'.format(TERMINAL_YELLOW))
2018-11-12 17:10:31 +01:00
print('* Updating AIL ... *')
2018-11-13 14:54:56 +01:00
print('******************************************************************{}'.format(TERMINAL_DEFAULT))
2018-11-12 17:10:31 +01:00
# manual updates
parser = argparse.ArgumentParser()
parser.add_argument("--manual", nargs='?', const=True, default=False)
args = parser.parse_args()
manual_update = args.manual
if auto_update_enabled(cfg) or manual_update:
2018-11-12 17:10:31 +01:00
if check_if_files_modified():
is_fork = repo_is_fork()
if is_fork:
update_fork()
current_tag = get_git_current_tag(current_version_path)
print()
print('Current Version: {}{}{}'.format(TERMINAL_YELLOW, current_tag, TERMINAL_DEFAULT))
2018-11-13 14:54:56 +01:00
print()
2020-02-25 15:57:07 +01:00
list_upper_tags_remote = get_git_upper_tags_remote(current_tag.replace('v', ''), is_fork)
# new release
2018-11-12 17:10:31 +01:00
if len(list_upper_tags_remote) > 1:
print('New Releases:')
2018-11-13 14:54:56 +01:00
if is_fork:
for upper_tag in list_upper_tags_remote:
print(' {}{}{}'.format(TERMINAL_BLUE, upper_tag[0], TERMINAL_DEFAULT))
else:
for upper_tag in list_upper_tags_remote:
print(' {}{}{}: {}'.format(TERMINAL_BLUE, upper_tag[0], TERMINAL_DEFAULT, upper_tag[1]))
2018-11-12 17:10:31 +01:00
print()
update_ail(current_tag, list_upper_tags_remote, current_version_path, is_fork)
2018-11-13 14:54:56 +01:00
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print('Please, commit your changes or stash them before you can update AIL')
aborting_update()
sys.exit(0)
2018-11-12 17:10:31 +01:00
else:
2018-11-13 14:54:56 +01:00
print(' {}AIL Auto update is disabled{}'.format(TERMINAL_RED, TERMINAL_DEFAULT))
aborting_update()
sys.exit(0)
# r = get_git_upper_tags_remote('4.2.1', False)
# print(r)