###########################################################################################
# Helper classes for the different CI commands
###########################################################################################
import sys, os
import shutil

class SearchBuild:
    def __init__(self):
        self.artifacts = list()

    # search for files with a given file-ending, and call the method
    # doit() for every occurance
    def search(self, ending, folder):
        for root, dirs, files in os.walk(folder):
            for file in files:
                if file.endswith(ending):
                    self.artifacts += self.doit(os.path.join(root, file))
        print("%u artifact(s) created" % len(self.artifacts))

    def save(self, basepath, destination):
        for filename in self.artifacts:
            print("filename: %s" % filename)
            dirname = os.path.relpath(os.path.dirname(filename), basepath)
            destdir = os.path.join(destination, dirname)
            print("dest dir: %s\n" % destdir)
            if not os.path.exists(destdir):
                os.mkdir(destdir)
            shutil.copy2(filename, destdir)
            print("%s => %s" % (filename, destdir))

    # dummy rule
    def doit(self, filename):
        artifacts = list()
        return artifacts
