Skip to content

Commit

Permalink
Use subprocess module for updating
Browse files Browse the repository at this point in the history
  • Loading branch information
joenano committed Oct 21, 2021
1 parent a43d906 commit 64b1d91
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
37 changes: 15 additions & 22 deletions scripts/rpscrape.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from utils.completer import Completer
from utils.race import Race, VoidRaceError
from utils.settings import Settings
from utils.update import Update

from utils.async_funcs import get_documents, get_jsons
from utils.course import course_name, courses
Expand All @@ -19,6 +20,20 @@
settings = Settings()


def check_for_update():
update = Update()

if update.available():
x = input('Update available. Do you want to update? Y/N ')

if x.lower() == 'y':
update.pull_latest()

if update.up_to_date():
print('Updated successfully.')
sys.exit()


def get_race_urls(tracks, years, code):
urls = set()
courses = []
Expand Down Expand Up @@ -83,28 +98,6 @@ def scrape_races(races, folder_name, file_name, code):
print(f'Finished scraping.\n{file_name}.csv saved in rpscrape/data/{folder_name}/{code}')


def check_for_update():
try:
from git import Repo, cmd

if 'local out of date' in cmd.Git('..').execute(['git', 'remote', 'show', 'origin']).lower():
x = input('Update available. Do you want to update? Y/N ')

if x.lower() == 'y':
Repo('..').remote(name='origin').pull()

if 'up to date' in cmd.Git('..').execute(['git', 'remote', 'show', 'origin']).lower():
print('Updated successfully.')
sys.exit()
else:
print('Failed to update.')
sys.exit()
except ModuleNotFoundError:
print('gitpython module not found.\n\n'
'Install: "pip3 install gitpython" or disable auto update in settings.')
sys.exit()


def main():
if not settings:
sys.exit()
Expand Down
27 changes: 27 additions & 0 deletions scripts/utils/update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import subprocess

from pathlib import Path

class Update:

def __init__(self):
self.root_dir = Path.cwd().split('/utils')[0]

def available(self):
if 'local out of date' in self.get_status().lower():
return True
return False

def get_status(self):
command = ['git', 'remote', 'show', 'origin']
status = subprocess.check_output(command, cwd=self.root_dir)
return status.decode('utf-8')

def pull_latest(self):
command = ['git', 'pull', 'origin', 'master']
status = subprocess.check_output(command, cwd=self.root_dir)
return status.decode('utf-8')

def up_to_date(self):
if 'up-to-date' in self.get_status():
return True

0 comments on commit 64b1d91

Please sign in to comment.