Improvements for pipeline helper code /u/ProgrammerStuckInTX Python Education

I’m mostly an embedded C programmer, and I’m working on an Azure DevOps pipeline. The pipeline takes Simulink models and generates source files (.c & .h) which then become part of a downstream git repo. I wrote a little python helper get the branch name from the Simulink project then puts the downstream source code repo on the same branch.

What I’ve written seems to work, just wondering how I could improve it.

import argparse import os import subprocess import sys orig_dir = '' def exit_gracefully(msg: str, code: int) -> None: print(msg) os.chdir(orig_dir) sys.exit(code) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Set Downstream Repo Branch') parser.add_argument('--us_repo', type=str, help='Path to Upstream Repo') parser.add_argument('--ds_repo', type=str, help='Path to Downstream Repo') args, unknown = parser.parse_known_args() orig_dir = os.getcwd() os.chdir(args.us_repo) print(os.getcwd()) results = subprocess.run(["git", "branch", "--show-current"], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to get branch info for repo: {args.us_repo}', 1) branch_name = results.stdout.strip() os.chdir(args.ds_repo) results = subprocess.run(["git", "branch", "-a", "-v"], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to get branch info for repo: {args.ds_repo}', 1) branch_exists = branch_name in results.stdout if branch_exists == True: results = subprocess.run(['git', 'checkout', branch_name], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to set downstream branch to {branch_name}', 1) else: # if the downstream branch does not exist always create it off of the dev branch, which should always exist results = subprocess.run(['git', 'checkout', 'dev'], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully('Error! Unable to checkout downstream repo dev', 1) results = subprocess.run(['git', 'checkout', '-b', branch_name], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to create branch {branch_name} for downstream repo: {args.ds_repo}', 1) results = subprocess.run(['git', 'push', '--set-upstream', 'origin', branch_name], capture_output=True, universal_newlines=True)) if results.stderr != '': exit_gracefully(f'Error! Unable to set upstream branch for repo {args.ds_repo}', 1) exit_gracefully(f'Repo: {args.ds_repo} branch set to {branch_name} ', 0) 

submitted by /u/ProgrammerStuckInTX
[link] [comments]

​r/learnpython I’m mostly an embedded C programmer, and I’m working on an Azure DevOps pipeline. The pipeline takes Simulink models and generates source files (.c & .h) which then become part of a downstream git repo. I wrote a little python helper get the branch name from the Simulink project then puts the downstream source code repo on the same branch. What I’ve written seems to work, just wondering how I could improve it. import argparse import os import subprocess import sys orig_dir = ” def exit_gracefully(msg: str, code: int) -> None: print(msg) os.chdir(orig_dir) sys.exit(code) if __name__ == “__main__”: parser = argparse.ArgumentParser(description=’Set Downstream Repo Branch’) parser.add_argument(‘–us_repo’, type=str, help=’Path to Upstream Repo’) parser.add_argument(‘–ds_repo’, type=str, help=’Path to Downstream Repo’) args, unknown = parser.parse_known_args() orig_dir = os.getcwd() os.chdir(args.us_repo) print(os.getcwd()) results = subprocess.run([“git”, “branch”, “–show-current”], capture_output=True, universal_newlines=True) if results.stderr != ”: exit_gracefully(f’Error! Unable to get branch info for repo: {args.us_repo}’, 1) branch_name = results.stdout.strip() os.chdir(args.ds_repo) results = subprocess.run([“git”, “branch”, “-a”, “-v”], capture_output=True, universal_newlines=True) if results.stderr != ”: exit_gracefully(f’Error! Unable to get branch info for repo: {args.ds_repo}’, 1) branch_exists = branch_name in results.stdout if branch_exists == True: results = subprocess.run([‘git’, ‘checkout’, branch_name], capture_output=True, universal_newlines=True) if results.stderr != ”: exit_gracefully(f’Error! Unable to set downstream branch to {branch_name}’, 1) else: # if the downstream branch does not exist always create it off of the dev branch, which should always exist results = subprocess.run([‘git’, ‘checkout’, ‘dev’], capture_output=True, universal_newlines=True) if results.stderr != ”: exit_gracefully(‘Error! Unable to checkout downstream repo dev’, 1) results = subprocess.run([‘git’, ‘checkout’, ‘-b’, branch_name], capture_output=True, universal_newlines=True) if results.stderr != ”: exit_gracefully(f’Error! Unable to create branch {branch_name} for downstream repo: {args.ds_repo}’, 1) results = subprocess.run([‘git’, ‘push’, ‘–set-upstream’, ‘origin’, branch_name], capture_output=True, universal_newlines=True)) if results.stderr != ”: exit_gracefully(f’Error! Unable to set upstream branch for repo {args.ds_repo}’, 1) exit_gracefully(f’Repo: {args.ds_repo} branch set to {branch_name} ‘, 0) submitted by /u/ProgrammerStuckInTX [link] [comments] 

I’m mostly an embedded C programmer, and I’m working on an Azure DevOps pipeline. The pipeline takes Simulink models and generates source files (.c & .h) which then become part of a downstream git repo. I wrote a little python helper get the branch name from the Simulink project then puts the downstream source code repo on the same branch.

What I’ve written seems to work, just wondering how I could improve it.

import argparse import os import subprocess import sys orig_dir = '' def exit_gracefully(msg: str, code: int) -> None: print(msg) os.chdir(orig_dir) sys.exit(code) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Set Downstream Repo Branch') parser.add_argument('--us_repo', type=str, help='Path to Upstream Repo') parser.add_argument('--ds_repo', type=str, help='Path to Downstream Repo') args, unknown = parser.parse_known_args() orig_dir = os.getcwd() os.chdir(args.us_repo) print(os.getcwd()) results = subprocess.run(["git", "branch", "--show-current"], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to get branch info for repo: {args.us_repo}', 1) branch_name = results.stdout.strip() os.chdir(args.ds_repo) results = subprocess.run(["git", "branch", "-a", "-v"], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to get branch info for repo: {args.ds_repo}', 1) branch_exists = branch_name in results.stdout if branch_exists == True: results = subprocess.run(['git', 'checkout', branch_name], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to set downstream branch to {branch_name}', 1) else: # if the downstream branch does not exist always create it off of the dev branch, which should always exist results = subprocess.run(['git', 'checkout', 'dev'], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully('Error! Unable to checkout downstream repo dev', 1) results = subprocess.run(['git', 'checkout', '-b', branch_name], capture_output=True, universal_newlines=True) if results.stderr != '': exit_gracefully(f'Error! Unable to create branch {branch_name} for downstream repo: {args.ds_repo}', 1) results = subprocess.run(['git', 'push', '--set-upstream', 'origin', branch_name], capture_output=True, universal_newlines=True)) if results.stderr != '': exit_gracefully(f'Error! Unable to set upstream branch for repo {args.ds_repo}', 1) exit_gracefully(f'Repo: {args.ds_repo} branch set to {branch_name} ', 0) 

submitted by /u/ProgrammerStuckInTX
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *