forked from 20/gitannex
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
220 lines (186 loc) · 9.27 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
"""
Modelos da aplicacao Django.
Neste arquivo sao definidos os modelos de dados da aplicacao *gitannex*.
"""
logger = logging.getLogger(__name__)
gitannex_dir = settings.GITANNEX_DIR
def _createRepository(repositoryName, remoteRepositoryURLOrPath):
"""Cria e inicializa um repositorio *git-annex*."""
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):
"""Clona e inicializa um repositorio *git-annex*."""
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):
"""Procura as pastas que podem ser inicializada como repositorio, retorna a lista das pastas."""
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):
"""Executa o *commit* no repositorio impostando os dados do author."""
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):
"""Executa o *push* do repositorio, atualizando o repositorio de origem."""
logger.info('git push ')
cmd = 'git push '
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitPull(repoDir):
"""Executa o *pull* do repositorio, atualizando o repositorio local."""
logger.info('git pull ')
cmd = 'git pull '
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitStatus(fileName, repoDir):
"""Verifica o estado atual do repositorio"""
# Dovrebbe restituire oltre allo status un flag per avviare o no il sync
cmd = 'git status'
def gitGetSHA(repoDir):
"""Resgata o codigo identificativo (SHA) da ultima revisao do repositorio, retorna o codigo."""
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):
"""Adiciona um arquivo no repositorio *git-annex*."""
logger.info('git annex add ' + fileName)
cmd = 'git annex add ' + fileName
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitAnnexMerge(repoDir):
"""Executa o *merge* do repositorio, reunindo eventuais diferencias entre o repositorio local e remoto."""
logger.info('git annex merge ')
cmd = 'git annex merge '
pipe = subprocess.Popen(cmd, shell=True, cwd=repoDir)
pipe.wait()
def gitAnnexCopyTo(repoDir):
"""Envia os conteudos binarios para o repositorio remoto."""
# 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):
"""Baixa os conteudos binarios desde o repositorio remoto."""
# 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):
"""Intercepta o sinal de *post_save* de objetos multimedia (*mmedia*) e adiciona o objeto ao repositorio."""
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():
"""Executa as operacoes programadas em todos os repositorios. """
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):
"""Classe de implementacao do modelo de repositorio *git-annex*.
Atributos:
repositoryName: nome do repositorio (campo preenchido por *_getAvailableFolders()*)
repositoryURLOrPath: apontador ao repositorio no disco ou em rede
syncStartTime = orario de inicio da sincronizacao
enableSync = flag booleano para abilitar ou disabilitar a sincronizacao
remoteRepositoryURLOrPath = apontador ao repositorio de origem
"""
# 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):
"""Cria e inicializa o repositorio."""
# Dovrebbe scegliere tra remoto e locale?
_createRepository(self.repositoryName, self.remoteRepositoryURLOrPath)
def cloneRepository(self):
"""Clona e inicializa o repositorio."""
_cloneRepository(self.repositoryURLOrPath, self.repositoryName)
def syncRepository(self):
"""Sincroniza o repositorio com sua origem."""
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)