#!python3 #psychowoife import re import os import sys import getopt import hashlib import shutil import pysftp import subprocess ##globals version = '2020.05.20' git_ver_pattern = r'-(?P\d{5})-' #start game game_start_pat = ' -u -p -f -g -k [-d] [-v]\n') print('arguments:') print('\t-f --file=\t\t.xls/.xlsx/.xlsm file to update UIDs') print('\t-p --path=\t\tpath to protocol files') print('optional:') print('\t-d --debug\t\t') print('\t-v --version\t\tprints version of script') ##prints error message and exits with error code def err(str, type): error = 'ERROR: ' if type == 0: print(error + str + ': argument missing') elif type == 1: print(error + str + ': file not found') elif type == 2: print(error + 'wrong parameter') elif type == 3: print(error + 'to few arguments') elif type == 4: print(error + 'unknown arguments') elif type == 5: print(error + ': path not found') usage() sys.exit(1) ##print version def print_version(): print(version) ##parses command line arguments and returns dictionary contaning all arguments def parse_args(): #try parsing arguments according to defined format try: opts, args = getopt.getopt(sys.argv[1:], 'a:u:p:f:g:l:dvh', ['host=', 'user=', 'passwd=', 'host_file=', 'git_path=', 'sftp_path', 'debug', 'version', 'help']) except getopt.GetoptError: err('',2) #parsing failed debug = False sftp_host = '' sftp_user = '' sftp_passwd = '' sftp_path = '' known_hosts = '' git_path = '' #check if arguments even present if not opts: err('', 3) #get each value for given argument for opt, arg in opts: if opt in ('-h', '--help'): usage() sys.exit(1) if opt in ('-v', '--version'): print_version() sys.exit(0) elif opt in ('-a', '--host'): sftp_host = arg elif opt in ('-u', '--user'): sftp_user = arg elif opt in ('-p', '--passwd'): sftp_passwd = arg elif opt in ('-f', '--host_file'): if not os.path.isfile(arg): err(arg, 1) known_hosts = arg elif opt in ('-g', '--git_path'): if not os.path.isdir(arg): err(arg, 5) git_path = arg elif opt in ('-l', '--sftp_path'): git_path = arg elif opt in ('-d', '--debug'): debug = True else: err(arg, 4) #exit script if false parameter is detected #if not path: # err('path', 0) param = dict() param['sftp_host'] = sftp_host param['sftp_user'] = sftp_user param['sftp_passwd'] = sftp_passwd param['known_hosts'] = known_hosts param['git_path'] = git_path param['debug'] = debug return param ##calculate md5 chesksum of file def calc_md5(filename): hash_md5 = hashlib.md5() with open(filename, "rb") as file: for chunk in iter(lambda: file.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() def get_last_build(myhost, myname, mypasswd, known_hosts='known_hosts', bin_path='/jenkins/owrt'): cnopts = pysftp.CnOpts() cnopts.hostkeys.load(known_hosts) sftp = pysftp.Connection(host=myhost, username=myname, password=mypasswd, cnopts=cnopts) #print('Conn success') sftp.cwd(bin_path) directory_structure = sftp.listdir_attr() sftp.close() files = [] for attr in directory_structure: files.append(attr.filename) files.sort() return files[-1] def build_needed(current, last_build_version): if current > last_build_version: return True else: return False #list = [] #list.append(current) #list.append(last_build_version) #list.sort() #if list def main(): params = parse_args() last = get_last_build(params['sftp_host'], params['sftp_user'], params['sftp_passwd'], known_hosts=params['known_hosts'], bin_path=params['sftp_path']) os.chdir(params['git_path']) out = subprocess.check_output(['git', 'describe']).decode(sys.stdout.encoding).strip() match = re.search(git_ver_pattern, out) current = '00000' if match: current = match.group('version') #print(last) #print(current) print(build_needed('r'+current, last)) if __name__ == "__main__": main()