1
0
mirror of https://github.com/CMiksche/gitea-auto-update synced 2025-12-11 00:17:22 +01:00

refactor: follow PEP standards

Follow Python Enhancement Proposals and add PyLint to enforce them.
This commit is contained in:
Christoph Miksche
2020-03-27 16:56:05 +01:00
parent ae8d54d018
commit b92496fc0d
11 changed files with 249 additions and 199 deletions

View File

@@ -1,79 +1,91 @@
'''
Gitea Auto Updater
Copyright 2018, 2019 The Gitea-Auto-Update Authors
Copyright 2018, 2019, 2020 The Gitea-Auto-Update Authors
All rights reserved.
License: GNU General Public License
'''
import requests
import os
import sys
import logging
from shutil import which # from whichcraft import which
import requests
def is_tool(name):
"""Function to check if tool is available"""
# Check whether `name` is on PATH and marked as executable.
return which(name) is not None
def download(url, file_name):
"""Function to download a file"""
# open in binary mode
with open(file_name, "wb") as file:
# get request
response = requests.get(url)
# write to file
file.write(response.content)
def sha_check():
"""Check sha for gitea file"""
return os.system("sha256sum -c gitea.xz.sha256 > /dev/null") == 0
class Download:
"""Class for downloading gitea"""
def __init__(self, tmpDir, githubVersion, githubVersionTag, gtSystem, gtFile):
if not self.isTool("xz"):
def __init__(self, tmp_dir, github_version_tag, gt_system, gt_file):
if not is_tool("xz"):
logging.error('Download: missing dependency: xz')
quit()
sys.exit()
self.tmpDir = tmpDir
self.githubVersion = githubVersion
self.githubVersionTag = githubVersionTag
self.gtSystem = gtSystem
self.gtFile = gtFile
self.tmp_dir = tmp_dir
self.github_version_tag = github_version_tag
self.github_version = self.github_version_tag[1:]
self.gt_system = gt_system
self.gt_file = gt_file
self.downloadGiteaFiles()
self.checkAndExtract()
self.download_gitea_files()
self.check_and_extract()
def isTool(self, name):
# Function to check if tool is available
##Check whether `name` is on PATH and marked as executable.
return which(name) is not None
def download(self, url, fileName):
# Function to download a file
# open in binary mode
with open(fileName, "wb") as file:
# get request
response = requests.get(url)
# write to file
file.write(response.content)
def downloadGiteaFiles(self):
def download_gitea_files(self):
"""Download gitea files"""
# Set download url
gtDownload = 'https://github.com/go-gitea/gitea/releases/download/' + self.githubVersionTag + '/gitea-' + self.githubVersion + '-' + self.gtSystem + '.xz'
logging.info('Download: Gitea file: %s', gtDownload)
shaDownload = gtDownload + '.sha256'
logging.info('Download: SHA file: %s', shaDownload)
gt_download = 'https://github.com/go-gitea/gitea/releases/download/' \
+ self.github_version_tag + '/gitea-' + self.github_version \
+ '-' + self.gt_system + '.xz'
logging.info('Download: Gitea file: %s', gt_download)
sha_download = gt_download + '.sha256'
logging.info('Download: SHA file: %s', sha_download)
# Download file
logging.info('Download: downloading sha256 hashsum')
self.download(shaDownload, self.tmpDir + 'gitea.xz.sha256')
logging.info('Download: downloading %s', self.githubVersionTag + 'gitea.xz')
self.tmpXz = self.tmpDir +'gitea-' + self.githubVersion + '-' + self.gtSystem + '.xz'
self.download(gtDownload, self.tmpXz)
download(sha_download, self.tmp_dir + 'gitea.xz.sha256')
logging.info('Download: downloading %s', self.github_version_tag + 'gitea.xz')
self.tmp_xz = self.tmp_dir + 'gitea-' + self.github_version + '-' + self.gt_system + '.xz'
download(gt_download, self.tmp_xz)
def shaCheck(self):
return os.system("sha256sum -c gitea.xz.sha256 > /dev/null") == 0
def extractFile(self):
def extract_file(self):
"""Extract gitea file"""
logging.info('Download: sha ok, extracting file to location')
# extracting download file
cmd = "xz -d " + self.tmpXz
cmd = "xz -d " + self.tmp_xz
os.system(cmd)
# moving temp file to gtfile location
cmd = 'mv ' + self.tmpDir + 'gitea-' + self.githubVersion + '-' + self.gtSystem + ' ' + self.gtFile
cmd = 'mv ' + self.tmp_dir + 'gitea-' + self.github_version \
+ '-' + self.gt_system + ' ' + self.gt_file
os.system(cmd)
cmd = 'chmod +x ' + self.gtFile
cmd = 'chmod +x ' + self.gt_file
os.system(cmd)
def checkAndExtract(self):
os.chdir(self.tmpDir)
if self.shaCheck():
self.extractFile()
def check_and_extract(self):
"""Check file and extract"""
os.chdir(self.tmp_dir)
if sha_check():
self.extract_file()
else:
logging.error('Download: error: sha256sum failed')
quit()
sys.exit()