mirror of
https://github.com/Xevion/recommit.git
synced 2025-12-06 13:16:07 -06:00
Add GitPython repository manipulation code, allow python to commit changes to database
This commit is contained in:
79
main.py
79
main.py
@@ -1,9 +1,12 @@
|
|||||||
|
import difflib
|
||||||
import logging
|
import logging
|
||||||
import os.path
|
import os.path
|
||||||
from logging.handlers import TimedRotatingFileHandler
|
from logging.handlers import TimedRotatingFileHandler
|
||||||
from typing import List
|
from typing import List, Optional
|
||||||
|
|
||||||
from rich.logging import RichHandler
|
import git
|
||||||
|
import pytz
|
||||||
|
from decouple import config
|
||||||
|
|
||||||
from database import Database
|
from database import Database
|
||||||
from models import Commit
|
from models import Commit
|
||||||
@@ -13,10 +16,12 @@ CURRENT_DIR: str = os.path.dirname(os.path.abspath(__name__))
|
|||||||
LOGS_DIR: str = os.path.join(CURRENT_DIR, 'logs')
|
LOGS_DIR: str = os.path.join(CURRENT_DIR, 'logs')
|
||||||
if not os.path.exists(LOGS_DIR): os.makedirs(LOGS_DIR)
|
if not os.path.exists(LOGS_DIR): os.makedirs(LOGS_DIR)
|
||||||
|
|
||||||
logging.basicConfig(level=logging.WARNING, format='%(message)s', datefmt="[%X]", handlers=[
|
logging.basicConfig(level=logging.WARNING,
|
||||||
RichHandler(),
|
format='[%(asctime)s] [%(levelname)s] [%(threadName)s] %(message)s',
|
||||||
TimedRotatingFileHandler(filename=os.path.join(LOGS_DIR, 'recommit.log'), backupCount=25)
|
handlers=[
|
||||||
])
|
logging.StreamHandler(),
|
||||||
|
TimedRotatingFileHandler(filename=os.path.join(LOGS_DIR, 'recommit.log'), backupCount=25)
|
||||||
|
])
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
logger.setLevel(logging.DEBUG)
|
logger.setLevel(logging.DEBUG)
|
||||||
@@ -27,25 +32,71 @@ def main() -> None:
|
|||||||
"""The main method for this application. When executed, it will use all available sources and create commits to act as contributions."""
|
"""The main method for this application. When executed, it will use all available sources and create commits to act as contributions."""
|
||||||
logger.info('Starting recommit.')
|
logger.info('Starting recommit.')
|
||||||
|
|
||||||
sources: List[CommitSource] = [Gitlab()]
|
source_instances: List[CommitSource] = [Gitlab()]
|
||||||
|
# source_instances.clear()
|
||||||
commits: List[Commit] = []
|
commits: List[Commit] = []
|
||||||
db = Database()
|
db = Database()
|
||||||
|
|
||||||
logger.debug(f'{len(sources)} sources available.')
|
logger.debug(f'{len(source_instances)} sources available.')
|
||||||
logger.info('Ready.')
|
logger.info('Ready.')
|
||||||
|
|
||||||
for source in sources:
|
for source in source_instances:
|
||||||
new_commits: List[Commit] = source.fetch(db.check_exists)
|
new_commits: List[Commit] = source.fetch(db.check_exists)
|
||||||
commits.extend(new_commits)
|
commits.extend(new_commits)
|
||||||
|
|
||||||
logger.debug(f'{len(new_commits)} new commits from {source.name.upper()}.')
|
logger.debug(f'{len(new_commits)} new commits from {source.name.upper()}.')
|
||||||
|
|
||||||
logger.info(f'{len(commits)} commits found.')
|
logger.info(f'All done fetching. {len(commits)} commits found.')
|
||||||
|
|
||||||
# TODO: Fetch all commits from the available sources
|
repository_path = config("REPOSITORY_PATH")
|
||||||
# TODO: Check that the commit has been written
|
repository_path = os.path.abspath(repository_path)
|
||||||
# TODO: Write commits into the git log
|
if not os.path.exists(repository_path):
|
||||||
# TODO: Push to GitHub
|
logger.error('Unable to locate repository.')
|
||||||
|
|
||||||
|
repo = git.Repo(repository_path)
|
||||||
|
|
||||||
|
timezone_configuration: Optional[str] = None
|
||||||
|
try:
|
||||||
|
timezone_configuration = config('TIMEZONE', default=None)
|
||||||
|
timezone = pytz.timezone(timezone_configuration or 'UTC')
|
||||||
|
except pytz.UnknownTimeZoneError as e:
|
||||||
|
logger.error('Failed to find the timezone specified.', exc_info=e)
|
||||||
|
closest = difflib.get_close_matches(timezone_configuration, pytz.common_timezones, n=10, cutoff=0.15)
|
||||||
|
logger.info('Did you mean any of these valid timezones? ' + str(closest))
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.debug('Processing new commits...')
|
||||||
|
|
||||||
|
successful: int = 0
|
||||||
|
for commit in commits:
|
||||||
|
commit_date_string = commit.timestamp.astimezone(timezone).strftime("%Y-%m-%d %H:%M:%S %z")
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.debug('Updating metafile with latest commit id.')
|
||||||
|
meta_filepath = os.path.join(repository_path, 'meta')
|
||||||
|
with open(meta_filepath, 'w') as meta_file:
|
||||||
|
meta_file.write(str(commit.id))
|
||||||
|
|
||||||
|
logger.debug('Constructing new Git commit.')
|
||||||
|
repo.index.add(meta_filepath)
|
||||||
|
repo_commit: git.Commit = repo.index.commit(str(commit.id), commit_date=commit_date_string, author_date=commit_date_string)
|
||||||
|
|
||||||
|
logger.debug('Adding new commit to database.')
|
||||||
|
db.add_commit(commit, commit_hash=repo_commit.hexsha)
|
||||||
|
|
||||||
|
successful += 1
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f'Failed while inserting new commit ({commit.id}) into database.', commit, exc_info=e)
|
||||||
|
continue
|
||||||
|
|
||||||
|
logger.debug(f'Processed {commit.id} as {repo_commit.hexsha}')
|
||||||
|
break
|
||||||
|
|
||||||
|
logger.info(f'Finished processing commits ({successful}/{len(commits)}).')
|
||||||
|
logger.info('Pushing to origin...')
|
||||||
|
origin = repo.remote(name='origin')
|
||||||
|
origin.push()
|
||||||
|
logger.info('Done.')
|
||||||
|
|
||||||
logger.info('Shutting down.')
|
logger.info('Shutting down.')
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user