Example #1
0
def main():
    Config = ConfigParser.SafeConfigParser()

    cfg_file = "make_pr.conf"
    if os.path.isfile(cfg_file):
        Config.readfp(open(cfg_file))
        for section in Config.sections():
            for option in Config.options(section):
                cfg[option] = Config.get(section, option)

    cfg["git_mirror_dir"] = os.path.expandvars(
        os.path.expanduser(cfg["gitdir"]))

    tracking = Tracking(dbpath=cfg["db_conn"])
    prev_max_pull_id = tracking.get_max_pull_id()

    print "prev_max_pull_id: %d" % prev_max_pull_id

    repo_obj = GitRepo(repo_path=cfg["git_mirror_dir"])
    all_pull_ids = repo_obj.get_all_pull_ids()

    pull_ids_to_work = [
        elem for elem in all_pull_ids if elem > prev_max_pull_id
    ]

    print "pull_ids_to_work: %s" % pull_ids_to_work

    newcount = 0
    for pull_id in pull_ids_to_work:
        message = make_gnats_message(pull_id=pull_id,
                                     cfg=cfg,
                                     pr_template=pr_template,
                                     repo_obj=repo_obj)
        fname = "pr%d.txt" % pull_id
        pr_file = open(fname, "w")
        pr_file.write(message)
        pr_file.close()
        print "Wrote out: %s" % fname
        # shell command to send-pr:
        #    yes s |send-pr -f x.out
        subprocess.check_call("yes s |send-pr -f %s" % fname, shell=True)
        tracking.record_pr_sent(pull_id)
        newcount += 1

    if newcount > 0:
        print "Finished successfully, made %d new prs!" % newcount
    else:
        print "Finished successfully, no new prs made"

    return 0
Example #2
0
    def scan_sloc(self, repo_name, repo_path, repo_owner, skip_path, suffix):
        db_session = self.create_db_session()
        row_repo = self.load_repository(db_session, repo_name)
        if row_repo == None:
            row_repo = Repository(name=repo_name, path=repo_path, owner=repo_owner)
            db_session.add(row_repo)

        gitrepo = GitRepo(repo_path + '/.git')
        existing_revision = [ _.hash for _ in row_repo.revisions]
        log.info(f'repo_name: {row_repo.name}')
        log.info(f'repo_path: {row_repo.path}')
        log.info(f'repo_owner: {row_repo.owner}')
        log.info(f'size of existing_revision: {len(existing_revision)}')

        analysis_cache = {}
        for commit in gitrepo.get_all_commit_id():
            date = datetime.datetime.fromtimestamp(int(commit.commit_time)).strftime('%Y-%m-%d %H:%M:%S')
            hash = str(commit.id)
            log.info(f'{date}, {hash}')

            if hash not in existing_revision:
                log.info('add revision')
                existing_revision.append(hash)
                row_revision = Revision(hash=hash,
                                        commit_time=datetime.datetime.fromtimestamp(int(commit.commit_time)))
                row_repo.revisions.append(row_revision)
                gitrepo.checkout_by(hash)

                row_revision.slocs = []
                for f in self.source_scanner(repo_path, skip_path, suffix):
                    fcontent = ''
                    with open(f, 'rb') as fh:
                        fcontent = fh.read()

                    if f in analysis_cache and analysis_cache[f][1] == fcontent:
                        analysis = analysis_cache[f][0]
                        # log.info('Use cache in analysis: %s', f)
                    else:
                        analysis = pygount.source_analysis(f, group='pygount', encoding='automatic')
                        analysis_cache[f] = (analysis, fcontent)
                        log.info(f'Analysis: {f}')

                    row_revision.slocs.append(Sloc(language=analysis.language,
                                                   filename=f, source_line=analysis.code,
                                                   empty_line=analysis.empty))
            db_session.commit()

        log.info('End of scaning.')