def run(self): settings = self.project.settings logger.info("Getting file revisions...") file_revisions = self.project.get_disk_file_revisions() logger.info("%d file revisions" % len(file_revisions)) snapshot = self.project.DiskSnapshot({'created_at': time.time()}) try: code_environment = CodeEnvironment(self.project, file_revisions, global_settings=self.settings, project_settings=settings) code_environment.analyze(snapshot=snapshot, save_if_empty=False) except KeyboardInterrupt: raise
def analyze_and_generate_diffs(self,branch,snapshots,diff_list): analyzed_snapshots = {} new_analyzed_snapshots = {} if 'project_settings' in self.opts: project_settings = self.opts['project_settings'] else: project_settings = self.project.settings #we look for a .checkmate.yml file in the main branch of the project #if we find it, we either replace the project settings, update them #or do nothing, depending on the desired configuration. try: git_settings = self.project.git.get_settings() if git_settings is not None: if git_settings.get('overwrite_project_settings',False): project_settings = git_settings elif not project_settings.get('ignore_git_settings',False): project_settings.update(git_settings) except ValueError: logger.warning("Error when loading checkmate settings from git...") git_settings = {} code_environment = CodeEnvironment(self.project, global_settings=self.settings, project_settings=project_settings) code_environment.env['branch'] = branch code_environment.env['project'] = self.project for git_snapshot in snapshots: try: query = {'sha' : git_snapshot.sha, 'project' : self.project} git_snapshot = self.backend.get(GitSnapshot,query,include = ('snapshot',)) snapshot = git_snapshot.snapshot #we check if the snapshot is analyzed and if the analysis configuration matches if snapshot.analyzed and snapshot.configuration == self.project.configuration: analyze_snapshot = False else: analyze_snapshot = True snapshot.configuration = self.project.configuration except GitSnapshot.DoesNotExist: analyze_snapshot = True snapshot = Snapshot() snapshot.configuration = self.project.configuration except GitSnapshot.MultipleDocumentsReturned: logger.error("Multiple snapshots returned, deleting...") with self.backend.transaction(): self.backend.filter(GitSnapshot,query).delete() analyze_snapshot = True if analyze_snapshot: try: file_revisions = self.project.git.get_file_revisions(git_snapshot.sha) except: logger.error("Cannot fetch file revisions for snapshot %s in project %s" % (git_snapshot.sha,self.project.pk) ) continue code_environment.file_revisions = file_revisions git_snapshot.snapshot = code_environment.analyze(file_revisions, save_if_empty = True, snapshot=snapshot) new_analyzed_snapshots[git_snapshot.sha] = git_snapshot with self.backend.transaction(): git_snapshot.snapshot.hash = git_snapshot.sha self.backend.save(git_snapshot.snapshot) git_snapshot.project = self.project self.backend.save(git_snapshot) analyzed_snapshots[git_snapshot.sha] = git_snapshot snapshot_pairs = [] for diff_params in diff_list: try: snapshot_pairs.append([analyzed_snapshots[diff_params['snapshot_a']] ,analyzed_snapshots[diff_params['snapshot_b']]]) except KeyError: continue diffs = [] for git_snapshot_a,git_snapshot_b in snapshot_pairs: diff = None try: query = {'snapshot_a' : git_snapshot_a.snapshot, 'snapshot_b' : git_snapshot_b.snapshot} diff = self.backend.get(Diff,query) except Diff.DoesNotExist: logger.debug("Generating a diff between snapshots %s and %s" % (git_snapshot_a.sha, git_snapshot_b.sha)) #if the configuration of the diff does not match the project configuration, we regenerate it if diff is None or diff.configuration != self.project.configuration: if diff is not None: diff.configuration = self.project.configuration diff,diff_file_revisions,diff_issue_occurrences = code_environment.diff_snapshots( git_snapshot_a.snapshot, git_snapshot_b.snapshot, save = True, diff = diff) diffs.append(diff) return analyzed_snapshots,diffs