用于获取Bitbucket链接的Python脚本

时间:2017-08-16 15:30:48

标签: python git bitbucket spyder

我有一个python脚本,它将创建分支并在Bitbucket上传文件,但我试图在创建分支后从Bitbucket获取链接。我怎么能这样做?

from subprocess import PIPE
import subprocess
import os
import getpass
import pandas as pd
import shutil
git_command = ['git', 'status']
current_user =  getpass.getuser()
# Assuming every use has same location for their local repository, 
# otherwise this should be set manually
repository  = r'C:\Users\%s\bb96' % current_user
git_config = subprocess.call(['git', 'config', 'core.autocrlf', 'true'], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
#------------------------------------------------------------------------------

git_fetch = subprocess.Popen(['git', 'fetch'], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
stdout_f, stderr_f = git_fetch.communicate() 
print(stdout_f)
print(stderr_f)


'''
1.  Provide path to where all the BSTM are being stored
    Provide app_id
    Provide schema name
    Provide type of the bstms to be created
       for ongoing process -> o
       for history load -> h

2. Program creates list of branches to be created as follows:
    feature/schema-physcial_tab_name-o

3. For each new feature branch creates a directory tree:
    appid\schema\table_name\misc\o\bstm, e.g.:
    edld_bb96_rdiapid\crz_rdi_ca\table_name\misc\o\bstm

'''


def loading_to_bitbucket(bstm_path, app_id, schema, bstm_type):
# creates list of bstms
    feature_list = []
    feature_bstm_ref = {}
    bstm_list = [f for f in os.listdir(bstm_path) if os.path.isfile(os.path.join(bstm_path, f)) and f[-4:] == ".csv"]

# creates name of future branch based on table physical name 
    for bstm in bstm_list:
        directorytree = ''
        df = pd.read_csv(os.path.join(bstm_path, bstm))
        for r in range(len(df)):
            for c in range(len(df.columns.values.tolist())):
                if str(df.iloc[r,c]).strip() == "Target Physical Table Name":
                    directorytree = os.path.join(repository,app_id,schema,df.iloc[r+1,c].lower(),'misc',bstm_type,'bstm')
                    feature_list.append("feature/"+schema+"-"+df.iloc[r+1,c].lower()+"-o")
                    feature_bstm_ref["feature/"+schema+"-"+df.iloc[r+1,c].lower()+"-o"] = [bstm, directorytree]
                    break
            else:
                continue
            break   


# for each new future branch, new branch is created in bitbucket and file is loaded, 
# for existing bstm newer version os loaded only if the bstm file was updated
    for feature in feature_list:
        compare_flag = 0
        x = ''
        y = ''
        next_release = False
        #---------
        print(" ")
        print("Current iteration: " + feature)
        git_pull = subprocess.call(['git', 'pull'], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
        #git_pull = subprocess.Popen(['git', 'pull'], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
        #stdout_md, stderr_md = git_pull.communicate() 
        #print(stdout_md)
        #print(stderr_md)
        if git_pull != 0:
            print("GIT PULL didn't succeed, check your git status.")
            break
        else:
            print("GIT PULL ended successfully.")
            checkout_dev = subprocess.call(['git', 'checkout', 'dev'], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
            if checkout_dev != 0:
                print("Can\'t checkout DEV branch, check git status")
                break
            else:
                print("Checked out on DEV successfully.")
                create_branch = subprocess.Popen(['git', 'checkout', '-b', feature], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
                stdout_cb, stderr_cb = create_branch.communicate() 
                #print(str(stdout_cb))
                #print(str(stderr_cb))            
                if str(stderr_cb).find("fatal: A branch named " + "'" + feature + "'" + " already exists.") < 0 : 
                    try:
                        createdirtree = subprocess.Popen(['mkdir', feature_bstm_ref[feature][1]], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
                        stdout_md, stderr_md = createdirtree.communicate() 
                        print("Created feature branch: " + feature)
                    except:
                        print('Error while creating directory tree for ' + feature) 
                        continue   
                else:
                    try:
                        checkout_branch = subprocess.Popen(['git', 'checkout', feature], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
                        stdout_chb, stderr_chb = checkout_branch.communicate()
                        print("Checked out on branch: " + feature)
                    except:
                        print("Error")
                        break
                try:
                    x = (os.path.join(bstm_path, feature_bstm_ref[feature][0]).replace('\\', '/'))
                    y = os.path.join(feature_bstm_ref[feature][1],feature_bstm_ref[feature][0]).replace('\\', '/')
                    next_release = os.path.isfile(y) # values True/False
                    #print(next_release)
                    if next_release:
                        print("Comparing files",)
                        compare_files = subprocess.Popen(['git', 'diff', '--shortstat', x, y], stdout=PIPE, stderr=PIPE, shell=True)
                        stdout, stderr = compare_files.communicate()
                        #print(str(stdout))
                        #print(str(stderr))
                        if str(stdout).find('file changed') > 0 :
                            compare_flag = 1          
                        if compare_flag == 0:
                            try:
                                print("Nothing has changed, move to next avalaible feature branch.")
                                reset_local = subprocess.Popen(['git', 'reset', '--hard', 'origin/'+ feature], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)
                                stdout, stderr = reset_local.communicate()
                                continue
                            except:
                                print("Something went wrong.")
                                break     
                except:
                    print("comparing files didn't succeed.")
                    break
                try:
                    if compare_flag != 0:
                        print("Newer version found, new version will be loaded")
                    else:
                        print("Initial load of file.")
                    shutil.copy(os.path.join(bstm_path, feature_bstm_ref[feature][0]), feature_bstm_ref[feature][1])
                except shutil.Error:
                    print(shutil.Error)
                    print ("Unable to copy file." + feature_bstm_ref[feature][0])
                    break
                # Add the file
                try:
                    add_file = subprocess.Popen(["git", "add", "-A"],cwd = repository, stdout=PIPE, stderr=PIPE, shell=True)
                    stdout, stderr = add_file.communicate()
                    #print(str(stdout))
                    #print(str(stderr))
                except:
                    print ("git add error" + feature)
                    break
                # Compile the commit message and then commit.
                try:
                    message = str(input("Provide commit message: "))
                    #message = "Initial release " + feature_bstm_ref[feature][0][5:-4] #Assuming that each BSTM will have BSTM_ prefix
                    commit_info = subprocess.Popen(["git", "commit", "-m", message], cwd = repository,stdout=PIPE, stderr=PIPE, shell=True)
                    stdout, stderr = commit_info.communicate()
                    #print(str(stdout))
                    #print(str(stderr))
                except:
                    print ("git commit error" + feature)
                    break
                # Push to the target BSTM. If you have passphrase for your BitBucket, 
                # in this step a window will prompt out asking you to input the passphrase. 
                # After you input the passphrase, the upload will be completed for this feature.
                try:
                    push_to_remote = subprocess.Popen(["git", "push", "origin", "-u",feature], cwd = repository, stdout=PIPE, stderr=PIPE, shell=True)
                    stdout, stderr = push_to_remote.communicate()
                    #print(str(stdout))
                    #print(str(stderr))
                    print ("Git Bucket uploading succeeded for " + feature)
                except:
                    print ("git push error" + feature)
                    break
                checkout_dev = subprocess.call(['git', 'checkout', 'dev'], cwd=repository, stdout=PIPE, stderr=PIPE, shell=True)        
    print("All features from the list are checked/loaded.")


#------------------------------------------------------------------------------
bstm_path = r'M:\EDW\EDW_old\Projects\'
app_id = 'dummy'
schema = 'dummy_schema'
bstm_type = 'o'


# calling the function:
loading_to_bitbucket(bstm_path, app_id, schema, bstm_type)....

0 个答案:

没有答案
相关问题