예제 #1
0
파일: __init__.py 프로젝트: brunogola/skink
    def create_or_update(self, project):
        executer = ShellExecuter()
        project_name = self.fix_name(project.name)
        repository_path = join(self.base_dir, project_name)
        is_repo_created = self.is_repository_created(repository_path)
        if not is_repo_created and exists(repository_path):
            raise ValueError("The specified directory(%s) is not empty and is not a repository")
        if not is_repo_created:
            if not exists(self.base_dir):
                try:
                    os.mkdir(self.base_dir)
                except:
                    raise ValueError("Could not create folder %s" % self.base_dir)
                self.log("Directory successfully created.")

            self.log("Retrieving scm data for project %s in repository %s (creating new repository)" % (project_name, project.scm_repository))
            import pdb;pdb.set_trace()
            result = executer.execute("%s \"%s\" \"%s\"" % (self.retrieve_cmd, project.scm_repository, project_name), self.base_dir)
            if result.exit_code == 0:
                self.log("SCM Data retrieved successfully")
            else:
                self.log("Error retrieving SCM Data: %s" % result.run_log)
            last_commit = self.get_last_commit(repository_path)
            return ScmResult(result.exit_code == 0 and ScmResult.Created or ScmResult.Failed, repository_path, last_commit, result.run_log)
        else:
            self.log("Retrieving scm data for project %s in repository %s (updating repository)" % (project_name, project.scm_repository))
            result = executer.execute(self.update_cmd, repository_path)
            if result.exit_code == 0:
                self.log("SCM Data retrieved successfully")
            else:
                self.log("Error retrieving SCM Data: %s" % result.run_log)
            
            self.log("Retrieving last commit data for project %s in repository %s" % (project_name, project.scm_repository))
예제 #2
0
파일: __init__.py 프로젝트: brunogola/skink
    def get_last_commit(self, repository_path):
        commit_number = None
        author = None
        committer = None

        command = "git show -s --pretty=format:'%H||%an||%ae||%ai||%cn||%ce||%ci||%s'"
        
        executer = ShellExecuter()
        result = executer.execute(command, repository_path)
        
        if result.exit_code != 0:
            raise ValueError("unable to determine last commit. Error: %s" % result.run_log)
        commit_number, author_name, author_email, author_date, committer_name, committer_email, committer_date, subject = result.run_log.split("||")
        
        author_date = self.convert_to_date(author_date)
        committer_date = self.convert_to_date(committer_date)
        
        return {
                   'commit_number': commit_number,
                   'author': "%s <%s>" % (author_name, author_email),
                   'author_date': author_date,
                   'committer': "%s <%s>" % (committer_name, committer_email),
                   'committer_date': committer_date,
                   'subject': subject
               }
예제 #3
0
파일: __init__.py 프로젝트: brunogola/skink
    def _execute_svn_log(self, repository_path, rev=False):
        executer = ShellExecuter()
        
        command = "svn log -l 1 --xml"
        if rev is not False:
            command = "svn -r %d log -l 1 --xml" % rev

        result = executer.execute(command, repository_path)
        
        if result.exit_code != 0:
            raise ValueError("unable to determine commit. Error: %s" % result.run_log)
        return result
예제 #4
0
class BuildService(object):
    Success = "SUCCESS"
    Failure = "FAILURE"
    
    def __init__(self, repository=None, scm=None, executer=None, base_path=join(root_path,'skink','build')):
        if not repository:
            self.repository = ProjectRepository()
        else:
            self.repository = repository
            
        if not scm:
            self.scm = GitRepository(base_path)
        else:
            self.scm = scm
            
        if not executer:
            self.executer = ShellExecuter()
        else:
            self.executer = executer
        
        self.base_path = base_path

    def build_project(self, project_id):
        log = ["Build started at %s" % datetime.now()]
        status = BuildService.Failure
        scm_status = ScmResult.Failed
        project = self.repository.get(project_id)
        build = Build(datetime.now(), status,  scm_status, "", project)
        
        scm_creation_result = self.scm.create_or_update(project)
        build.scm_status = scm_creation_result.status
        if scm_creation_result.status == ScmResult.Failed:
            log.append(scm_creation_result.log)
            status = BuildService.Failure
        else:
            log.append("Downloaded code from %s" % project.scm_repository)
            
            execute_result = self.executer.execute(project.build_script, scm_creation_result.repository_path)
            log.append("Executed %s" % project.build_script)
            log.append("Exit Code: %s" % execute_result.exit_code)
            log.append("Run Log:")
            log.append(execute_result.run_log)
            
            status = execute_result.exit_code == 0 and BuildService.Success or BuildService.Failure
        
        build.status = status
        build.log = "\n".join(log)
        self.repository.update(project)
        
        return build
예제 #5
0
 def __init__(self, repository=None, scm=None, executer=None, base_path=join(root_path,'skink','build')):
     if not repository:
         self.repository = ProjectRepository()
     else:
         self.repository = repository
         
     if not scm:
         self.scm = GitRepository(base_path)
     else:
         self.scm = scm
         
     if not executer:
         self.executer = ShellExecuter()
     else:
         self.executer = executer
     
     self.base_path = base_path
예제 #6
0
    def __init__(self, repository=None, pipeline_repository=None, scm=None, executer=None, base_path=join(root_path, SkinkContext.current().build_path)):
        self.repository = repository
        if not repository:
            self.repository = ProjectRepository()

        self.pipeline_repository = pipeline_repository
        if not pipeline_repository:
            self.pipeline_repository = PipelineRepository()

        self.scm = scm
        if not scm:
            self.scm = GitRepository(base_path)

        self.executer = executer
        if not executer:
            self.executer = ShellExecuter()    

        self.base_path = base_path
예제 #7
0
class BuildService(object):
    Success = u"SUCCESS"
    Failure = u"FAILURE"

    def __init__(self, repository=None, pipeline_repository=None, scm=None, executer=None, base_path=join(root_path, SkinkContext.current().build_path)):
        self.repository = repository
        if not repository:
            self.repository = ProjectRepository()

        self.pipeline_repository = pipeline_repository
        if not pipeline_repository:
            self.pipeline_repository = PipelineRepository()

        self.scm = scm
        if not scm:
            self.scm = GitRepository(base_path)

        self.executer = executer
        if not executer:
            self.executer = ShellExecuter()    

        self.base_path = base_path

    def build_project(self, project_id):
        ctx = SkinkContext.current()
        
        log = ["Build started at %s" % datetime.now()]

        status = BuildService.Failure
        scm_status = ScmResult.Failed
        project = self.repository.get(project_id)
        PluginEvents.on_before_build(project)
        ctx.projects_being_built.append(project_id)
        last_build_number = project.get_last_build_number()

        build = Build()
        build.date = datetime.now()
        build.status = status
        build.scm_status = scm_status
        build.log = ""
        build.project = project
        
        scm_creation_result = self.scm.create_or_update(project)
        build.scm_status = scm_creation_result.status
        if scm_creation_result.status == ScmResult.Failed:
            log.append(scm_creation_result.log)
            status = BuildService.Failure
        else:
            log.append("Downloaded code from %s (%s)" % (project.scm_repository, scm_creation_result.status))
            
            execute_result = self.executer.execute(project.build_script, 
                                                   scm_creation_result.repository_path, 
                                                   timeout=ctx.build_timeout)

            log.append("Executed %s" % project.build_script)
            log.append("Exit Code: %s" % execute_result.exit_code)
            log.append("Run Log:")
            log.append(execute_result.run_log)

            status = execute_result.exit_code == 0 and BuildService.Success or BuildService.Failure

        for command in project.tabs:
            build_tab = BuildTab(name=command.name, command=command.command, content_type=command.content_type, build=build)
            result = self.executer.execute(command.command, scm_creation_result.repository_path)
            build_tab.log = result.run_log

        for file_locator in project.file_locators:
            file_locator_path = join(self.base_path, project.name, file_locator.locator)
            print "Finding files for locator %s" % file_locator_path
            files = glob.glob(file_locator_path)
            print "%d files found" % len(files)
            for f in files:
                print "Adding file %s" % f
                filename = split(f)[-1]
                stream = open(f, 'rb')
                content = stream.read()
                stream.close()
                build_file = BuildFile(name=filename, original_path=f, content=content, build=build)

        build.number = last_build_number + 1
        build.status = status
        build.log = "\n".join(log)
        build.commit_number = force_unicode(scm_creation_result.last_commit["commit_number"])
        build.commit_author = force_unicode(scm_creation_result.last_commit["author"])
        build.commit_committer = force_unicode(scm_creation_result.last_commit["committer"])
        build.commit_author_date = scm_creation_result.last_commit["author_date"]
        build.commit_committer_date = scm_creation_result.last_commit["committer_date"]
        build.commit_text = force_unicode(scm_creation_result.last_commit["subject"])

        self.repository.update(project, project.tabs, [locator.locator for locator in project.file_locators])
        
        ctx.projects_being_built.remove(project_id)

        if (build.status == BuildService.Success):
            PluginEvents.on_build_successful(project, build)
            self.process_pipelines_for(project)
        else:
            PluginEvents.on_build_failed(project, build)
        
        return build

    def delete_scm_repository(self, project):
        self.scm.remove_repository(project)
        
    def process_pipelines_for(self, project):
        pipelines = self.pipeline_repository.get_all_pipelines_for(project)
        for pipeline in pipelines:
            for i in range(len(pipeline.items)):
                if i < len(pipeline.items) - 1:
                    if pipeline.items[i].project.id == project.id:
                        print "Adding project %d to the queue because it's in the same pipeline as project %s" % (pipeline.items[i+1].project.id, pipeline.items[i].project.name)
                        SkinkContext.current().build_queue.append(pipeline.items[i+1].project.id)