Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

200 lines (171 sloc) 5.54 KB
#!/usr/bin/python3
"""
This tool serves to update Riot-Web easily and quickly.
!!!! IT STILL NEEDS SOME LOVE, AT THE MOMENT I'M STILL WORKING ON IT
"""
import os
import errno
import shutil
import feedparser
import requests
import tarfile
def main():
"""
Todo: Initial setup
Todo: code rework
Todo: comments
:return:
"""
update_available, information = check_for_update()
if update_available:
update(information, 'www-data', 'www-data')
return
print("Nothing to do!")
return
def update(information, user, group) -> bool:
print(information)
print("Update time!")
do_update = input('Update now? (y/n): ')
if do_update is 'y':
print('Download ' + information[1] + ' version')
download_release(information)
print('Extracting ' + information[1] + '.tar.gz')
untar_file('/var/www/' + str(information[0]) + '.tar.gz', '/var/www/')
print('Update Symlinks')
create_symlink('/var/www/riot-' + information[1], '/var/www/riot_prod')
create_symlink('/var/www/.config/config.json', '/var/www/riot_prod/config.json')
print('Update permissions')
chmod('/var/www/riot' + information[1], 0o0755, 0o0644, True)
chown('/var/www/riot-' + information[1], user, group, True)
chown('/var/www/riot_prod/config.json', False)
else:
return False
return True
def get_latest_releases() -> dict:
feed = feedparser.parse('https://github.com/vector-im/riot-web/releases.atom')
releases = {}
for entry in feed['entries']:
version_fmt = ""
if 'rc' in entry['title_detail']['value']:
continue
for version in entry['title_detail']['value'][1:].split('.'):
version_fmt += version
download_link = 'https://github.com/vector-im/riot-web/releases/download/' \
+ entry['title_detail']['value'] + '/riot-' + entry['title_detail']['value'] + '.tar.gz'
releases.update({int(version_fmt): [
int(version_fmt),
entry['title_detail']['value'],
{'download_url': download_link}]})
return releases
def get_latest_release() -> [int, dict]:
releases = get_latest_releases()
latest_version = int()
for release in releases:
if release > latest_version:
latest_version = release
return latest_version, releases[latest_version]
def check_local_version() -> int:
dir_ver = int()
for directory in os.listdir('/var/www'):
if directory.split('-')[0] == 'riot' and len(directory.split('-')) >= 2:
version_fmt = ""
for version in directory.split('-')[1][1:].split('.'):
version_fmt += version
if dir_ver < int(version_fmt):
dir_ver = int(version_fmt)
return dir_ver
def check_for_update() -> [bool, dict]:
"""
:return:
"""
local_version = check_local_version()
latest_release, information = get_latest_release()
if latest_release > local_version:
return True, information
return False, dict
def download_release(information):
"""
Download the latest release of github
:param information:
:return:
"""
r = requests.get(information[2]['download_url'])
with open('/var/www/' + str(information[0]) + '.tar.gz', 'wb') as f:
f.write(r.content)
if r.status_code != 200:
print('An error occurred during the download.')
exit(r.status_code)
def untar_file(file_path, extract_path):
"""
Unpack the specified file
:param file_path:
:param extract_path:
:return:
"""
tar = tarfile.open(file_path, 'r:gz')
tar.extractall(path=extract_path)
tar.close()
def create_symlink(source, target):
"""
Creates or renews a symlink
:param source:
:param target:
:return:
"""
try:
os.symlink(source, target)
return True
except OSError as e:
if e.errno == errno.EEXIST:
os.remove(target)
os.symlink(source, target)
return True
else:
return False
def chown(path, user, group, recursive):
"""
In this function a recusive chown can make to a path or to a single directory or file.
:param path:
:param user:
:param group:
:param recursive:
:return:
"""
if recursive:
for root, dirs, files in os.walk(path):
for dir in dirs:
print('chown: ' + os.path.join(root, dir))
shutil.chown(os.path.join(root, dir), user, group)
for file in files:
print('chown: ' + os.path.join(root, file))
shutil.chown(os.path.join(root, file), user, group)
shutil.chown(root, user, group)
return
shutil.chown(path, user, group)
return
def chmod(path, mod_dir, mod_file, recursive):
"""
In this function a recusive chmod can make to a path or to a single directory or file.
:param path:
:param mod_dir:
:param mod_file:
:param recursive:
:return:
"""
if recursive:
for root, dirs, files in os.walk(path):
for dir in dirs:
print(os.path.join(root, dir))
os.chmod(os.path.join(root, dir), mod_dir)
for file in files:
print(os.path.join(root, file))
os.chmod(os.path.join(root, file), mod_file)
os.chmod(root, mod_dir)
return
if os.path.isfile(path):
os.chmod(path, mod_file)
return
os.chmod(path, mod_dir)
return
if __name__ == '__main__':
main()
You can’t perform that action at this time.