Exemplo n.º 1
0
    def echo(msg: str, frm: str = 'KDFS', err='', end='\n', is_err=False):
        currentDatetime = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        if type(frm) is str: frm = frm.upper()
        else: frm = str(frm)
        log = ''
        log_mode = 'INFO'
        log_msg = msg

        if type(err) is not str: err = str(err)

        if err == '' and not is_err:
            print("{} [{}]\t{}".format(
                colored(f"(INFO:{frm[0:7]: ^7})", 'blue', attrs=['bold']),
                currentDatetime, msg),
                  end=end,
                  file=sys.stdout)
        else:
            # append error message to msg, if exist
            logo_mode = 'ERROR'
            if err != '' and err != None:
                msg = "{} ({})".format(msg, colored(err, 'red'))
                log_msg = "{} ({})".format(msg, err)

            print("{} [{}]\t{}".format(
                colored(f"(ERR :{frm[0:7]: ^7})", 'red', attrs=['bold']),
                currentDatetime, msg),
                  file=sys.stderr)
        # log system
        from server.ServerUtils import ServerUtils as utils
        # append new log to logs file
        with open(utils.LOGS_PATH, 'a') as f:
            f.write("{} {} [{}] {}\n".format(currentDatetime, log_mode,
                                             f"{frm[0:10]: ^10}", msg))
Exemplo n.º 2
0
 def __init__(self, cfg, build_number, job_cfg):
     self.cfg = cfg
     self.build_number = build_number
     self.job_folder = cfg['jobs_directory'] + "/" + build_number
     self.job_cfg = job_cfg
     print(colored("Preparing build directories...", "cyan"))
     if not exists(cfg['jobs_directory']):
         mkdir(cfg['jobs_directory'])
     if exists(self.job_folder):
         print(colored("Build already exists! Overriding.", "yellow"))
         rmtree(self.job_folder)
     mkdir(self.job_folder)
     mkdir(self.job_folder + "/plugins")
Exemplo n.º 3
0
 def prepare_server(self):
     api = PaperAPI(self.job_cfg['version'])
     print(colored("Resolving build info from PaperAPI...", "cyan"))
     if self.job_cfg['paper_build'] == "latest":
         url = api.latest()
     else:
         url = api.build(self.job_cfg['paper_build'])
     print(
         colored("Downloading the Paper JAR... This may take a while.",
                 "cyan"))
     urlretrieve(url, self.job_folder + "/paper.jar")
     print(colored("Copying files...", "cyan"))
     copyfile("libs/eula.txt", self.job_folder + "/eula.txt")
     copyfile(self.job_cfg['properties'],
              self.job_folder + "/server.properties")
Exemplo n.º 4
0
def __interactive_deletion(repetitions):
    """Asks the user if wants to delete some repetitions"""
    saved_space = 0
    if any(repetitions):
        for key in repetitions:
            print(colored('Found this repetitions:', 'green'))
            print(colored(repetitions[key], 'green'))
            for repetition in repetitions[key]:
                print(colored('Do you want to delete the file ' + repetition + ' ?', 'yellow'))
                var = input(colored('[y/n]', 'yellow'))
                if var == 'y':
                    saved_space += crf_api.__get_size(repetition)
                    os.remove(repetition)
            if len(repetitions[key]) <= 1:
                repetitions.pop(key, None)
    print(colored('Saved: ' + str(saved_space/1024) + ' Kb', 'blue'))
Exemplo n.º 5
0
 def finalize(self):
     if self.cfg['windows_mode'] is True:
         with open(self.job_folder + "/start.bat", "w") as f:
             f.write("java -jar paper.jar\npause")
     print(colored("Packing artifacts...", "cyan"))
     make_archive(
         self.cfg['jobs_directory'] + "/artifact" + self.build_number,
         "zip", self.job_folder)
Exemplo n.º 6
0
 def resolve_subjects(self):
     print(colored("Resolving testing subjects...", "cyan"))
     for i in self.job_cfg['subject']:
         print(colored("Getting " + i + "...", "magenta"))
         if self.job_cfg['subject'][i][
                 "jenkins"] is not False and self.job_cfg['subject'][i][
                     "jenkins"][0] is True:
             api = JenkinsAPI(self.job_cfg['subject'][i]["url"])
             url = api.latest(self.job_cfg['subject'][i]["jenkins"][-1])
             urlretrieve(url, self.job_folder + "/plugins/" + i + ".jar")
         elif self.job_cfg['subject'][i][
                 "github"] is not False and self.job_cfg['subject'][i][
                     "github"][0] is True:
             api = GitHubAPI(self.job_cfg['subject'][i]["url"])
             if self.job_cfg['subject'][i]["github"][-1] is None:
                 url = api.latest(None)
             else:
                 url = api.latest(self.job_cfg['subject'][i]["github"][-1])
             urlretrieve(url, self.job_folder + "/plugins/" + i + ".jar")
         else:
             urlretrieve(self.job_cfg['subject'][i]["url"],
                         self.job_folder + "/plugins/" + i + ".jar")
Exemplo n.º 7
0
def __save_repetitions(reps, directory):
    """Creates the crf_log.txt file with all the duplicated files"""
    log_file = open('crf_log.txt', 'w+')
    log_file.write('Searched for repetitions in ' + directory + ' at: ')
    log_file.write(strftime("%Y-%m-%d %H:%M:%S") + '\n\n')
    log_file.write('<------------------------------------------------------------------>\n')
    if len(reps) > 0:
        for value in reps.values():
            for val in value:
                log_file.write(val + '\n')
            log_file.write('<------------------------------------------------------------------>\n')
    else:
        log_file.write('No file repetitions found.\n')
    print(colored('Found ' + str(len(reps)) + ' repetitions', 'red'))
    log_file.close()
Exemplo n.º 8
0

if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('directory', help='Directory to look for repetitions.')
    parser.add_argument('-a', '--approximate', help='It uses an approximate version of the' \
                        ' algorithm, it is faster but less reliable.', action='store_true')
    parser.add_argument('-i', '--interactive', help='If there is a repeated file, this option' \
                        ' lets you choose to delete one.', action='store_true')
    parser.add_argument('-f', '--fast', help='Delete the first repetition found without asking',
                        action='store_true')
    args = parser.parse_args()

    print('Calculating repeated files')
    repetitions = crf_api.check_repeated_files(args.directory, args.approximate)
    print('Saving results in crf_log.txt')
    __save_repetitions(repetitions, args.directory)

    if args.interactive:
        __interactive_deletion(repetitions)
    elif args.fast:
        print(colored('Deleting files...', 'red'))
        saved_space = 0
        for k in repetitions:
            repetitions[k].pop()
            for file in repetitions[k]:
                saved_space += crf_api.__get_size(file)
                print(colored('Deleted: ' + str(file), 'red'))
                os.remove(file)
Exemplo n.º 9
0
import json
from os.path import exists
from libs.jobs import Deploy
from libs.termcolor import colored

if __name__ == '__main__':
    with open('config.json') as config_json:
        cfg = json.load(config_json)
    print("PaperPacker " + cfg['version'] + "\n")
    if not exists(cfg['job_path']):
        print(colored("No job configs!", "red"))
        exit(-1)
    if not exists("builds.txt"):
        with open("builds.txt", "w") as bf:
            bf.write("1")
        build_number = "1"
    else:
        with open("builds.txt", "r") as rf:
            build_number = str(int(rf.read()) + 1)
            with open("builds.txt", "w") as bf:
                bf.write(build_number)
    try:
        with open(cfg['job_path']) as job_json:
            job = json.load(job_json)
    except json.JSONDecodeError:
        print(colored("Your job config isn't valid!", "red"))
        exit(-1)
    # init done, jobs next
    # noinspection PyUnboundLocalVariable
    inst = Deploy(cfg, build_number, job)
    inst.prepare_server()