Python 3.5:将GitHub凭据传递给另一个python脚本

时间:2015-10-07 04:06:10

标签: python git github

我有一个python脚本,用于轮询GitHub存储库以获取拉取请求,还有一个额外的脚本,用于查找提交和更改的文件等于1.

除了在第二个脚本运行时我被提示输入GitHub凭据时,一切似乎都在工作。

我正在尝试将包含主脚本存储库凭据的gh变量传递给第二个脚本,以便不必再次输入凭据。

repouser变量似乎正确传递给第二个脚本。

请参阅下面的代码,并提前感谢任何指导。

main_en_pr脚本:

#! /usr/bin/python
import os
import github3
from github3 import login, GitHub, authorize
from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import codecs

sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout)



# authenticate to GIT
try:
    import readline
except ImportError:
    pass

try:
    user = input('GitHub username: ')
except KeyboardInterrupt:
    user = getuser()

password = getpass('GitHub token for {0}: '.format(user))

gh = login(user, password)

# read the contents of the config file to pull in the repo name
config = configparser.ConfigParser()
config.read('repo.ini')
repo = config.get('repos', 'repo1')
# repo = input("Please enter your repo name: ")




result = gh.repository(user, repo).pull_requests('open')


def list_all_prs():
    # open csv file and create header rows

    with open('\\\\share\\pull.csv', 'w+', newline='') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['Id', 'Login', 'Title', 'Commits', 'Changed Files'])

    # iterate through repo for pull requests based on criteria and output to csv file
    for pr in result:
        data = pr.as_dict()
        changes = (gh.repository(user, repo).pull_request(data['number'])).as_dict()
        # keep print to console statement for testing purposes
        # print(changes['id'], changes['user']['login'], changes['title'], changes['commits'], changes['changed_files'])


        with open('\\\\share\\pull.csv','a+',newline='') as f:
            csv_writer = csv.writer(f)

            csv_writer.writerow([changes['id'], changes['user']['login'], changes['title'], changes['commits'],
                                 changes['changed_files']])


list_all_prs()

exec(open("one_commit_one_file_change.py").read())

one_commit_one_file_change脚本:

#! /usr/bin/python
import os
import github3
# from github3 import login, GitHub, authorize
# from getpass import getuser, getpass
import requests
import csv
import configparser
import sys
import main_en_pr
import codecs

sys.__stdout__ = codecs.getwriter('utf8')(sys.stdout)

def one_commit_one_file_change_pr():

    #open csv file and create header rows
    with open('\\\\share\\commit_filechange.csv', 'w+') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['Login', 'Title', 'Commits', 'Changed Files','Deletions', 'Additions'])

#iterate through repo for pull requests based on criteria and output to csv file
    for pr in main_en_pr.result:
        data = pr.as_dict()
        changes = (main_en_pr.gh.repository(main_en_pr.user, main_en_pr.repo).pull_request(data['number'])).as_dict()   

        if changes['commits'] == 1 and changes['changed_files'] == 1:
        #keep print to console statement for testing purposes
        #print changes['user']['login']


            with open('\\\\share\\commit_filechange.csv', 'a+') as f:
                csv_writer = csv.writer(f)

                csv_writer.writerow([changes['user']['login'], changes['title'], changes['commits'], changes['changed_files']])

one_commit_one_file_change_pr()

1 个答案:

答案 0 :(得分:0)

执行此操作的方法是在第一个脚本中创建一个在其构造函数中执行身份验证的类。

在第二个脚本中创建上述类的对象,您将可以访问creds