track the names of files associated with projects

This commit is contained in:
Doug Hellmann 2014-10-30 13:08:20 -04:00
parent 82f3244737
commit 337c5c53d3
3 changed files with 76 additions and 2 deletions

View File

@ -0,0 +1,29 @@
"""add file table
Revision ID: 5123eb59e1bb
Revises: 575c6e7ef2ea
Create Date: 2014-10-30 12:54:15.087307
"""
# revision identifiers, used by Alembic.
revision = '5123eb59e1bb'
down_revision = '575c6e7ef2ea'
from alembic import op
import sqlalchemy as sa
def upgrade():
op.create_table(
'file',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('project_id', sa.Integer,
sa.ForeignKey('project.id', name='fk_file_project_id')),
sa.Column('name', sa.String(), nullable=False),
sa.Column('path', sa.String()),
)
def downgrade():
op.drop_table('file')

View File

@ -1,5 +1,6 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship, backref
Base = declarative_base()
@ -9,3 +10,13 @@ class Project(Base):
id = Column(Integer, primary_key=True)
name = Column(String, unique=True, nullable=False)
path = Column(String)
class File(Base):
__tablename__ = 'file'
id = Column(Integer, primary_key=True)
project_id = Column(Integer, ForeignKey('project.id'))
name = Column(String, nullable=False)
path = Column(String)
project = relationship("Project", backref=backref('files', order_by=name))

View File

@ -1,6 +1,9 @@
import io
import logging
import os
import subprocess
from aeromancer.db.models import Project
from aeromancer.db.models import Project, File
from sqlalchemy.orm.exc import NoResultFound
@ -19,6 +22,8 @@ def add_or_update(session, name, path):
proj_obj = Project(name=name, path=path)
LOG.info('adding project %s from %s', name, path)
session.add(proj_obj)
update_project_files(session, proj_obj)
return proj_obj
@ -31,3 +36,32 @@ def remove(session, name):
except NoResultFound:
return
session.delete(proj_obj)
def update_project_files(session, proj_obj):
"""Update the files stored for each project"""
# Delete any existing files in case the list of files being
# managed has changed. This naive, and we can do better, but as a
# first version it's OK.
for file_obj in proj_obj.files:
session.delete(file_obj)
# Now load the files currently being managed by git.
before = os.getcwd()
os.chdir(proj_obj.path)
try:
cmd = subprocess.Popen(['git', 'ls-files', '-z'],
stdout=subprocess.PIPE)
output = cmd.communicate()[0]
filenames = output.split('\0')
finally:
os.chdir(before)
for filename in filenames:
fullname = os.path.join(proj_obj.path, filename)
if not os.path.isfile(fullname):
continue
with io.open(fullname, mode='r', encoding='utf-8') as f:
body = f.read()
lines = body.splitlines()
LOG.info('%s has %s lines', filename, len(lines))
session.add(File(project=proj_obj, name=filename, path=fullname))