From 2aa5f6bf4c321ba02d8340bc5005dc1c371b5f52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jean-Marie=20Cl=C3=A9ment?= Date: Mon, 10 Feb 2014 13:08:14 +0100 Subject: [PATCH] GitFile stores hashes for object and commit --- README.md | 2 +- Requirements.txt | 1 + generator/management/commands/importsongs.py | 17 ++- ...rsion__add_field_gitfile_commit_hash__a.py | 139 ++++++++++++++++++ generator/models.py | 6 +- 5 files changed, 156 insertions(+), 9 deletions(-) create mode 100644 generator/migrations/0004_auto__del_field_gitfile_file_version__add_field_gitfile_commit_hash__a.py diff --git a/README.md b/README.md index 7bd8dab..7c6c0cf 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Web interface for songbook generation (see https://github.com/crep4ever/songbook ## Installation -Be sure you have Python and pip installed. Then run `pip install Requirements.txt`. +Be sure you have Python and pip installed. Then run `pip install -r Requirements.txt`. In order to get the compilation engine, do `git submodule init` and `git submodule update`. diff --git a/Requirements.txt b/Requirements.txt index e8b4200..dcb3867 100644 --- a/Requirements.txt +++ b/Requirements.txt @@ -2,3 +2,4 @@ Django==1.6.1 South==0.8.4 Unidecode==0.04.14 jsonfield==0.9.20 +GitPython==0.3.2.RC1 diff --git a/generator/management/commands/importsongs.py b/generator/management/commands/importsongs.py index 7a6283a..b100839 100644 --- a/generator/management/commands/importsongs.py +++ b/generator/management/commands/importsongs.py @@ -4,7 +4,7 @@ sys.path.append(settings.SONG_PROCESSOR_DIR) from utils.plastex import parsetex import pprint -import sh +import git import os from generator import models from django.db import transaction @@ -22,8 +22,8 @@ class Command(BaseCommand): def handle(self, *args, **options): - git = sh.git.bake(_cwd=settings.SONGS_LIBRARY_DIR) - filerev = git("rev-parse", "HEAD") + repo = git.Repo(settings.SONGS_LIBRARY_DIR) + gitcmd = repo.git for root, dirs, filenames in os.walk(settings.SONGS_LIBRARY_DIR, topdown=True, @@ -32,7 +32,7 @@ def handle(self, *args, **options): for filename in filenames: if filename.lower().endswith(".sg"): - filepath = os.path.join(root, filename) + filepath = os.path.realpath(os.path.join(root, filename)) filepath_rel = os.path.relpath(filepath, settings.SONGS_LIBRARY_DIR) try: data = parsetex(filepath) @@ -77,8 +77,13 @@ def handle(self, *args, **options): + song_model.title) gitfile = models.GitFile() - gitfile.file_path = filepath_rel - gitfile.file_version = filerev + + gitfile.object_hash = gitcmd.hash_object(filepath) + + gitfile.commit_hash = gitcmd.log("-1", + "--pretty=format:%H", + "--", filepath) + gitfile.file_path = filepath song_model.file = gitfile diff --git a/generator/migrations/0004_auto__del_field_gitfile_file_version__add_field_gitfile_commit_hash__a.py b/generator/migrations/0004_auto__del_field_gitfile_file_version__add_field_gitfile_commit_hash__a.py new file mode 100644 index 0000000..6ddf8ad --- /dev/null +++ b/generator/migrations/0004_auto__del_field_gitfile_file_version__add_field_gitfile_commit_hash__a.py @@ -0,0 +1,139 @@ +# -*- coding: utf-8 -*- +from south.utils import datetime_utils as datetime +from south.db import db +from south.v2 import SchemaMigration +from django.db import models + + +class Migration(SchemaMigration): + + def forwards(self, orm): + # Deleting field 'GitFile.file_version' + db.delete_column(u'generator_gitfile', 'file_version') + + # Adding field 'GitFile.commit_hash' + db.add_column(u'generator_gitfile', 'commit_hash', + self.gf('django.db.models.fields.CharField')(default=0, max_length=20), + keep_default=False) + + # Adding field 'GitFile.object_hash' + db.add_column(u'generator_gitfile', 'object_hash', + self.gf('django.db.models.fields.CharField')(default=0, max_length=20), + keep_default=False) + + + def backwards(self, orm): + # Adding field 'GitFile.file_version' + db.add_column(u'generator_gitfile', 'file_version', + self.gf('django.db.models.fields.CharField')(default=0, max_length=20), + keep_default=False) + + # Deleting field 'GitFile.commit_hash' + db.delete_column(u'generator_gitfile', 'commit_hash') + + # Deleting field 'GitFile.object_hash' + db.delete_column(u'generator_gitfile', 'object_hash') + + + models = { + u'auth.group': { + 'Meta': {'object_name': 'Group'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}), + 'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': u"orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}) + }, + u'auth.permission': { + 'Meta': {'ordering': "(u'content_type__app_label', u'content_type__model', u'codename')", 'unique_together': "((u'content_type', u'codename'),)", 'object_name': 'Permission'}, + 'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '50'}) + }, + u'auth.user': { + 'Meta': {'object_name': 'User'}, + 'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}), + 'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'groups': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Group']"}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}), + 'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}), + 'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}), + 'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}), + 'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "u'user_set'", 'blank': 'True', 'to': u"orm['auth.Permission']"}), + 'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'}) + }, + u'contenttypes.contenttype': { + 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"}, + 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'generator.artist': { + 'Meta': {'ordering': "['name']", 'object_name': 'Artist'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '100'}) + }, + u'generator.gitfile': { + 'Meta': {'object_name': 'GitFile'}, + 'commit_hash': ('django.db.models.fields.CharField', [], {'max_length': '20'}), + 'file_path': ('django.db.models.fields.CharField', [], {'max_length': '500'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'object_hash': ('django.db.models.fields.CharField', [], {'max_length': '20'}) + }, + u'generator.itemsinsongbook': { + 'Meta': {'object_name': 'ItemsInSongbook'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'item_id': ('django.db.models.fields.PositiveIntegerField', [], {}), + 'item_type': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['contenttypes.ContentType']"}), + 'rank': ('django.db.models.fields.IntegerField', [], {}), + 'songbook': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['generator.Songbook']"}) + }, + u'generator.profile': { + 'Meta': {'object_name': 'Profile'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'songbooks': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'songbooks'", 'blank': 'True', 'through': u"orm['generator.SongbooksByUser']", 'to': u"orm['generator.Songbook']"}), + 'user': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['auth.User']", 'unique': 'True'}) + }, + u'generator.section': { + 'Meta': {'object_name': 'Section'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '200'}) + }, + u'generator.song': { + 'Meta': {'ordering': "['title']", 'object_name': 'Song'}, + 'artist': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'songs'", 'to': u"orm['generator.Artist']"}), + 'capo': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}), + 'file': ('django.db.models.fields.related.OneToOneField', [], {'to': u"orm['generator.GitFile']", 'unique': 'True', 'null': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'language': ('django.db.models.fields.CharField', [], {'max_length': '7', 'null': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '100'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '100'}) + }, + u'generator.songbook': { + 'Meta': {'object_name': 'Songbook'}, + 'bookoptions': ('jsonfield.fields.JSONField', [], {}), + 'booktype': ('django.db.models.fields.CharField', [], {'default': "'chrd'", 'max_length': '4'}), + 'description': ('django.db.models.fields.TextField', [], {'blank': 'True'}), + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_public': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'items': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'items'", 'blank': 'True', 'through': u"orm['generator.ItemsInSongbook']", 'to': u"orm['contenttypes.ContentType']"}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '100'}), + 'template': ('django.db.models.fields.CharField', [], {'default': "'patacrep.tmpl'", 'max_length': '100'}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '100'}), + 'users': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'related_name': "'users'", 'blank': 'True', 'through': u"orm['generator.SongbooksByUser']", 'to': u"orm['generator.Profile']"}) + }, + u'generator.songbooksbyuser': { + 'Meta': {'object_name': 'SongbooksByUser'}, + u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'is_owner': ('django.db.models.fields.BooleanField', [], {'default': 'False'}), + 'songbook': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['generator.Songbook']"}), + 'user': ('django.db.models.fields.related.ForeignKey', [], {'to': u"orm['generator.Profile']"}) + } + } + + complete_apps = ['generator'] \ No newline at end of file diff --git a/generator/models.py b/generator/models.py index 4c81973..631f9b7 100644 --- a/generator/models.py +++ b/generator/models.py @@ -144,12 +144,14 @@ class GitFile(models.Model): """Hold the information about the file object in a git repository. Attributes: file_path string path of the file in the songs repository - file_version string version of the file as currently known in db + commit_hash string hash of the commit the file was imported from + object_hash string hash of the file object """ # We use a CharField here, not FileField, we take care of the file. file_path = models.CharField(max_length=500) - file_version = models.CharField(max_length=20) + commit_hash = models.CharField(max_length=20) + object_hash = models.CharField(max_length=20) def __unicode__(self): return "{0}:{1}".format(self.file_version, self.file_path)