refined git opertaions
This commit is contained in:
parent
f0d04032b9
commit
6d0b30ad4c
@ -5,13 +5,15 @@ import subprocess
|
|||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
from .utilities import (
|
from .utilities import (
|
||||||
copy_content
|
copy_content,
|
||||||
|
run_git_command
|
||||||
)
|
)
|
||||||
|
|
||||||
from .dialogue_box import (
|
from .dialogue_box import (
|
||||||
draw_dialogue_box
|
draw_dialogue_box
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def github_init(deploy_location, git_commands):
|
def github_init(deploy_location, git_commands):
|
||||||
user_details_defined = git_check_user_details(deploy_location, git_commands)
|
user_details_defined = git_check_user_details(deploy_location, git_commands)
|
||||||
if not user_details_defined:
|
if not user_details_defined:
|
||||||
@ -20,13 +22,12 @@ def github_init(deploy_location, git_commands):
|
|||||||
username, password = git_get_username_password()
|
username, password = git_get_username_password()
|
||||||
remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git'
|
remote_url = f'https://{username}:{password}@github.com/{username}/{username}.github.io.git'
|
||||||
|
|
||||||
subprocess.run(git_commands["git_init"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
run_git_command('git_init', deploy_location)
|
||||||
subprocess.run(git_commands["git_add"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
run_git_command('git_add', deploy_location)
|
||||||
subprocess.run(git_commands["git_commit"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
run_git_command('git_commit', deploy_location)
|
||||||
subprocess.run(git_commands["git_branch"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
run_git_command('git_branch', deploy_location)
|
||||||
subprocess.run((git_commands["git_add_url"]).append(remote_url), cwd=deploy_location, check=True, text=True,
|
run_git_command('git_add_url', deploy_location, [remote_url])
|
||||||
capture_output=True)
|
run_git_command('git_push', deploy_location)
|
||||||
subprocess.run(git_commands["git_push"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
|
||||||
|
|
||||||
|
|
||||||
def git_set_user_details(deploy_location, git_commands):
|
def git_set_user_details(deploy_location, git_commands):
|
||||||
@ -40,10 +41,9 @@ def git_set_user_details(deploy_location, git_commands):
|
|||||||
)
|
)
|
||||||
if input_confirmation:
|
if input_confirmation:
|
||||||
break
|
break
|
||||||
subprocess.run(git_commands["git_config_email"] + [email], cwd=deploy_location, check=True, text=True,
|
|
||||||
capture_output=True)
|
run_git_command('git_config_email', deploy_location, [email])
|
||||||
subprocess.run(git_commands["git_config_name"] + [name], cwd=deploy_location, check=True,
|
run_git_command('git_config_name', deploy_location, [name])
|
||||||
text=True, capture_output=True)
|
|
||||||
|
|
||||||
|
|
||||||
def git_existing_repo_setup(deploy_location, git_commands):
|
def git_existing_repo_setup(deploy_location, git_commands):
|
||||||
@ -61,18 +61,16 @@ def git_existing_repo_setup(deploy_location, git_commands):
|
|||||||
repo_url = repo_url + '.git'
|
repo_url = repo_url + '.git'
|
||||||
|
|
||||||
dist_folder_name = ((repo_url.split('/')).pop()).removesuffix('.git')
|
dist_folder_name = ((repo_url.split('/')).pop()).removesuffix('.git')
|
||||||
subprocess.run(git_commands["git_clone"] + [repo_url], cwd=settings.DEPLOY_CONFIG["DEPLOY_LOCATION"], check=True,
|
run_git_command('git_clone', settings.DEPLOY_CONFIG["DEPLOY_LOCATION"], [repo_url])
|
||||||
text=True, capture_output=True)
|
|
||||||
git_update_viewable_ui(deploy_location, dist_folder_name)
|
git_update_viewable_ui(deploy_location, dist_folder_name)
|
||||||
|
|
||||||
|
|
||||||
def git_check_user_details(deploy_location, git_commands):
|
def git_check_user_details(deploy_location, git_commands):
|
||||||
try:
|
config_email = run_git_command('git_config_name', deploy_location)
|
||||||
subprocess.run(git_commands["git_config_name"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
config_name = run_git_command('git_config_email', deploy_location)
|
||||||
subprocess.run(git_commands["git_config_email"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
if not config_email or not config_name:
|
||||||
return True
|
|
||||||
except:
|
|
||||||
return False
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def git_update_viewable_ui(deploy_location, dist_folder_name, build_frontend=False):
|
def git_update_viewable_ui(deploy_location, dist_folder_name, build_frontend=False):
|
||||||
@ -137,22 +135,33 @@ def github_pages_deploy(deploy_location, git_commands):
|
|||||||
user_details_defined = git_check_user_details(deploy_location, git_commands)
|
user_details_defined = git_check_user_details(deploy_location, git_commands)
|
||||||
if not user_details_defined:
|
if not user_details_defined:
|
||||||
git_set_user_details(deploy_location, git_commands)
|
git_set_user_details(deploy_location, git_commands)
|
||||||
subprocess.run(git_commands["git_pull"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
|
||||||
print("completed git pull")
|
user_email = run_git_command("git_config_email", deploy_location)
|
||||||
origin_url_subprocess = subprocess.run(git_commands["git_get_origin_url"], cwd=deploy_location, check=True,
|
user_name = run_git_command("git_config_name", deploy_location)
|
||||||
text=True, capture_output=True)
|
repo_name = run_git_command("git_get_origin_url", deploy_location).split("/").pop()
|
||||||
origin_url = origin_url_subprocess.stdout.strip()
|
|
||||||
print("Got origin as "+str(origin_url))
|
deploy_confirmation = draw_dialogue_box(
|
||||||
parsed_url = urllib.parse.urlparse(origin_url)
|
'Github Deploy',
|
||||||
print(parsed_url)
|
f'Deploying as {user_name} with email {user_email} to {repo_name}',
|
||||||
if not '@' in parsed_url.netloc:
|
'confirmation'
|
||||||
username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox')
|
)
|
||||||
password = draw_dialogue_box('Github Deploy', 'Enter your github token', 'password')
|
|
||||||
netloc = f"{username}:{password}@{parsed_url.hostname}"
|
if deploy_confirmation:
|
||||||
new_url = urllib.parse.urlunparse(parsed_url._replace(netloc=netloc))
|
run_git_command('git_pull', deploy_location)
|
||||||
subprocess.run(git_commands["git_set_origin_url"] + [new_url], cwd=deploy_location, check=True, text=True,
|
print("completed git pull")
|
||||||
capture_output=True)
|
origin_url = run_git_command('git_get_origin_url', deploy_location)
|
||||||
print("Origin URL changed")
|
print("Got origin as "+str(origin_url))
|
||||||
subprocess.run(git_commands["git_add"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
parsed_url = urllib.parse.urlparse(origin_url)
|
||||||
subprocess.run(git_commands["git_commit"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
print(parsed_url)
|
||||||
subprocess.run(git_commands["git_push"], cwd=deploy_location, check=True, text=True, capture_output=True)
|
if not '@' in parsed_url.netloc:
|
||||||
|
username = draw_dialogue_box('Github Deploy', 'Enter your username', 'textbox')
|
||||||
|
password = draw_dialogue_box('Github Deploy', 'Enter your github token', 'password')
|
||||||
|
netloc = f"{username}:{password}@{parsed_url.hostname}"
|
||||||
|
new_url = urllib.parse.urlunparse(parsed_url._replace(netloc=netloc))
|
||||||
|
run_git_command('git_set_origin_url', deploy_location, [new_url])
|
||||||
|
print("Origin URL changed")
|
||||||
|
run_git_command('git_add', deploy_location)
|
||||||
|
run_git_command('git_commit', deploy_location)
|
||||||
|
run_git_command('git_push', deploy_location)
|
||||||
|
else:
|
||||||
|
raise subprocess.CalledProcessError(1, 'failed', 'Deployment Cancelled')
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
|
import subprocess
|
||||||
|
|
||||||
|
|
||||||
def copy_content(source, destination, content_type, copy_type='merge'):
|
def copy_content(source, destination, content_type, copy_type='merge'):
|
||||||
@ -17,4 +18,30 @@ def copy_content(source, destination, content_type, copy_type='merge'):
|
|||||||
shutil.copy(source, destination)
|
shutil.copy(source, destination)
|
||||||
print(f'{content_type} copied successfully from {source} to {destination}')
|
print(f'{content_type} copied successfully from {source} to {destination}')
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f'Error occurred: {e}')
|
print(f'Error occurred: {e}')
|
||||||
|
|
||||||
|
|
||||||
|
def run_git_command(operation, deploy_location, parameter=None):
|
||||||
|
if not parameter:
|
||||||
|
parameter=[]
|
||||||
|
git_commands = {
|
||||||
|
"git_init": ['git', 'init'],
|
||||||
|
"git_add": ['git', 'add', '.'],
|
||||||
|
"git_pull": ['git', 'pull'],
|
||||||
|
"git_config_email": ['git', 'config', '--local', 'user.email'],
|
||||||
|
"git_config_name": ['git', 'config', '--local', 'user.name'],
|
||||||
|
"git_commit": ['git', 'commit', '-m', 'Update website'],
|
||||||
|
"git_branch": ['git', 'branch', '-m', 'main'],
|
||||||
|
"git_get_origin_url": ['git', 'remote', 'get-url', 'origin'],
|
||||||
|
"git_set_origin_url": ['git', 'remote', 'set-url', 'origin'],
|
||||||
|
"git_add_url": ['git', 'remote', 'add', 'origin'],
|
||||||
|
"git_push": ['git', 'push', '-u', 'origin', 'main'],
|
||||||
|
"git_clone": ['git', 'clone']
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
subprocess_operation = subprocess.run(git_commands[operation] + parameter, cwd=deploy_location, check=True, text=True, capture_output=True)
|
||||||
|
subprocess_output = subprocess_operation.stdout.strip()
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
subprocess_output = None
|
||||||
|
|
||||||
|
return subprocess_output
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user