-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlocalrepository.py
More file actions
81 lines (72 loc) · 2.63 KB
/
localrepository.py
File metadata and controls
81 lines (72 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
file: localrepository.py
author: Ben Grawi <bjg1568@rit.edu>
date: October 2013
description: Holds the repository abstraction class
"""
from git import *
from commit import *
from datetime import datetime
import os
import logging
class LocalRepository():
"""
Repository():
description: Abstracts the actions done on a repository
"""
repo = None
adapter = None
start_date = None
def __init__(self, repo):
"""
__init__(path): String -> NoneType
description: Abstracts the actions done on a repository
"""
self.repo = repo
# Temporary until other Repo types are added
self.adapter = Git
self.commits = {}
def sync(self):
"""
sync():
description: Simply wraps the syncing functions together
"""
# TODO: Error checking.
firstSync = self.syncRepoFiles()
self.syncCommits(firstSync)
# Set the date AFTER it has been ingested and synced.
self.repo.ingestion_date = self.start_date
def syncRepoFiles(self):
"""
syncRepoFiles() -> Boolean
description: Downloads the current repo locally, and sets the path and
injestion date accordingly
returns: Boolean - if this is the first sync
"""
# Cache the start date to set later
self.start_date = str(datetime.now().replace(microsecond=0))
path = os.path.dirname(__file__) + self.adapter.REPO_DIRECTORY + self.repo.id
# See if repo has already been downloaded, if it is fetch, if not clone
if os.path.isdir(path):
self.adapter.fetch(self.adapter, self.repo)
firstSync = False
else:
self.adapter.clone(self.adapter, self.repo)
firstSync = True
return firstSync
def syncCommits(self, firstSync):
"""
syncCommits():
description: Makes each commit dictonary into an object and then
inserts them into the database
arguments: firstSync Boolean: whether to sync all commits or after the
ingestion date
"""
commits = self.adapter.log(self.adapter, self.repo, firstSync)
commitsSession = Session()
logging.info('Saving commits to the database...')
for commitDict in commits:
commitDict['repository_id'] = self.repo.id
commitsSession.merge(Commit(commitDict))
commitsSession.commit()
logging.info('Done saving commits to the database.')