-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
185 lines (155 loc) · 7.48 KB
/
models.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from django.db import models
from django.db.models.base import ModelBase
from django.contrib.auth.models import User
from django.conf import settings
from django.db.models.signals import post_save
from django.dispatch import receiver
from gitannex.signals import receiver_subclasses, filesync_done
from mmedia.models import MMedia, Audio
import os
import datetime
import subprocess
import logging
logger = logging.getLogger(__name__)
gitannex_dir = settings.GITANNEX_DIR
def _createRepository(repositoryName, remoteRepositoryURLOrPath):
logger.info('git config --global user.name "admin"')
cmd = 'git config --global user.name "admin"'
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir, repositoryName))
pipe.wait()
logger.info('git config --global user.email "[email protected]"')
cmd = 'git config --global user.email "[email protected]"'
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir, repositoryName))
pipe.wait()
logger.info('git init')
cmd = 'git init'
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir, repositoryName))
pipe.wait()
logger.info('git annex init ' + settings.PORTAL_NAME)
cmd = 'git annex init ' + settings.PORTAL_NAME
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir, repositoryName))
pipe.wait()
# TODO: Manage repositories dinamically
logger.info('git remote add baoba ' + remoteRepositoryURLOrPath)
cmd = 'git remote add baoba ' + remoteRepositoryURLOrPath
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir, repositoryName))
pipe.wait()
def _cloneRepository(repositoryURLOrPath, repositoryName):
cmd = 'git config --global user.name "admin"'
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir))
pipe.wait()
cmd = 'git config --global user.email "[email protected]"'
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir))
pipe.wait()
cmd = 'git clone ' + repositoryURLOrPath + repositoryName
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir))
pipe.wait()
cmd = 'git annex init ' + settings.PORTAL_NAME
pipe = subprocess.Popen(cmd, shell=True, cwd=os.path.join(settings.MEDIA_ROOT, gitannex_dir, repositoryName))
pipe.wait()
def _selectRepositoryByPath():
# Controlla il path del file ed estrai il nome del Repository
return
def _getAvailableFolders(path):
folderList = [( name , name ) for name in os.listdir(os.path.join(path, gitannex_dir)) \
if os.path.isdir(os.path.join(path, gitannex_dir, name))]
return folderList
def gitCommit(fileTitle, authorName, authorEmail, repoDir):
logger.info('git commit --author="' + authorName + ' <' + authorEmail +'>" -m "' + fileTitle + '"')
cmd = 'git commit --author="' + authorName + ' <' + authorEmail +'>" -m "' + fileTitle + '"'
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitPush(repoDir):
logger.info('git push ')
cmd = 'git push '
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitPull(repoDir):
logger.info('git pull ')
cmd = 'git pull '
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitStatus(fileName, repoDir):
# Dovrebbe restituire oltre allo status un flag per avviare o no il sync
cmd = 'git status'
def gitGetSHA(repoDir):
logger.info('git rev-parse HEAD')
cmd = 'git rev-parse HEAD'
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
output,error = pipe.communicate()
logger.debug('>>> Revision is: ' + output)
return output
def gitAnnexAdd(fileName, repoDir):
logger.info('git annex add ' + fileName)
cmd = 'git annex add ' + fileName
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitAnnexMerge(repoDir):
logger.info('git annex merge ')
cmd = 'git annex merge '
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitAnnexCopyTo(repoDir):
# TODO: Next release with dynamic "origin"
logger.info('git annex copy --fast --to origin ')
cmd = 'git annex copy --fast --to origin'
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitAnnexGet(repoDir):
# TODO: Next release with possibility to choice what to get
logger.info('git annex get .')
cmd = 'git annex get .'
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
# Connecting to MMedia signal
@receiver_subclasses(post_save, MMedia, "mmedia_post_save")
def gitMMediaPostSave(instance, **kwargs):
logger.debug(instance.mediatype)
logger.debug(type(instance))
logger.debug(instance.path_relative())
path = instance.path_relative().split(os.sep)
if gitannex_dir in path:
repositoryName = path[path.index(gitannex_dir) + 1]
gitAnnexRep = GitAnnexRepository.objects.get(repositoryName__iexact=repositoryName)
gitAnnexAdd(os.path.basename(instance.fileref.name), os.path.dirname(instance.fileref.path))
gitCommit(instance.title, instance.author.username, instance.author.email, os.path.dirname(instance.fileref.path))
def runScheduledJobs():
allRep = GitAnnexRepository.objects.all()
for rep in allRep:
if rep.enableSync:
# TODO: Manage time of syncing
# if rep.syncStartTime >= datetime.datetime.now():
rep.syncRepository()
class GitAnnexRepository(models.Model):
# Forse dovrei mettere qualcosa nella view. Esattamente.. Quando creo un repository questo puo' essere locale o remoto.
# Quindi devo poter scegliere tra una cartella locale (eventualmente crearla), o inserite un URL per effetuare il
# clone (via ssh).
# Nella view va messo un if che a seconda chiama create o cloneRepository a seconda della scelta.
repositoryName = models.CharField(max_length=60, choices=_getAvailableFolders(settings.MEDIA_ROOT))
repositoryURLOrPath = models.CharField(max_length=200)
syncStartTime = models.DateField()
enableSync = models.BooleanField()
remoteRepositoryURLOrPath = models.CharField(max_length=200)
# lastSyncSHA = models.CharField(max_length=100)
def createRepository(self):
# Dovrebbe scegliere tra remoto e locale?
_createRepository(self.repositoryName, self.remoteRepositoryURLOrPath)
def cloneRepository(self):
_cloneRepository(self.repositoryURLOrPath, self.repositoryName)
def syncRepository(self):
gitPull(self.repositoryURLOrPath)
gitAnnexMerge(self.repositoryURLOrPath)
gitPush(self.repositoryURLOrPath)
gitAnnexCopyTo(self.repositoryURLOrPath)
# TODO: Next release with possibility to choice what to get
gitAnnexGet(self.repositoryURLOrPath)
# TODO: Next release with selective sync since a given revision (using git SHA)
# self.lastSyncSHA = gitGetSHA(self.repositoryURLOrPath)
# Signal to all that files are (should be) synced
logger.debug(">>> BEFORE filesync_done")
filesync_done.send(sender=self, repositoryName=self.repositoryName, \
repositoryDir=self.repositoryURLOrPath)
logger.debug(">>> AFTER filesync_done")
def save(self, *args, **kwargs):
self.createRepository()
super(GitAnnexRepository, self).save(*args, **kwargs)