Created
April 24, 2023 08:19
-
-
Save pkutaj/25166b3d94e26c35b46ec1413ac0b6a7 to your computer and use it in GitHub Desktop.
2023-04-21-How-to-Use-Context-Manager-Decorator-To-Create-Custom-With-Statements-in-Python2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import git | |
from contextlib import contextmanager | |
TFMODULESPATH = os.environ['TFMODULESPATH'] | |
def make_remote_change(f: callable) -> callable: | |
def wrap(*args, **kwargs): | |
with push_and_pop_path(TFMODULESPATH): | |
prepare_repo_for_change() | |
ticket_nr = input("ticket# (e.g. ZD34613): ") | |
branch_name = f"infra_support/{ticket_nr}" | |
checkout_new_branch(branch_name, TFMODULESPATH) | |
f(*args, **kwargs) | |
with push_and_pop_path(TFMODULESPATH): | |
commit_and_push( | |
file_name="*", commit_msg=f"[{ticket_nr}] {f.__name__}") | |
open_pr(branch_name) | |
return wrap | |
@contextmanager | |
def push_and_pop_path(new_path): | |
try: | |
old_path = os.getcwd() | |
os.chdir(new_path) | |
yield | |
finally: | |
os.chdir(old_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment