Commits

Ed Brannin committed 616e761

Much ado about nothing

Comments (0)

Files changed (3)

app_sizer/backup_import.py

 com.apple.stocks
 com.apple.videos
 com.apple.weather
+com.apple.youtube
 """.split()
 
 def import_backups(path=None, name=None):
-    from .chart.models import App, Backup
+    from .chart.models import App, Backup, BackupApp
     # for i, app in tqdm(enumerate(scan_all(path=path))):
     for name in backup_names():
         # print "Looking at backup {}".format(name)
         backup = Backup.get_by_path(path)
         if backup is None:
             backup = Backup()
-        backup.path = path
+        #elif backup.mtime == mtime:
+            # Nothing new to see here
+        #    continue
+        # backup.path = path
         backup.name = this_device_name
         backup.plist = plist
         backup.mtime = mtime
         # We don't need to do any special housekeeping for existing apps;
         # sqlalchemy handles that for us.
 
+        bundle_ids = set([b for b in apps if b not in STOCK_APPS])
+
         # ... or does it?  Clear the list.
         for ba in backup.installed_apps:
+            # TODO: Start tracking history of removed apps somehow.
+            #if ba.app.bundle_id not in bundle_ids:
+            #    ba.status = "REMOVED"
             ba.delete()
         backup.save()
 
-        for bundle_id in [b for b in apps_in(plist) if b not in STOCK_APPS]:
+        # Temporarily useless
+        already_there_apps = set([b.app.bundle_id for b in backup.installed_apps])
+        # print "Already there: ", already_there_apps
+
+        for bundle_id in bundle_ids:
             #print "Looking at {}".format(bundle_id)
             app = App.get_by_bundle_id(bundle_id)
-            if app is not None:
-                backup.apps.append(app)
+            if app is not None and bundle_id not in already_there_apps:
+                backup.installed_apps.append(BackupApp(app=app, backup=backup, status="NEW", path=""))
             else:
                 print "Unable to find App for bundle_id: {}".format(bundle_id)
         backup.save()

app_sizer/chart/models.py

 # Why does this work but a declarative class doesn't?  No idea.
 # http://xsnippet.org/359350/
 # http://stackoverflow.com/q/5756559/25625
-class BackupApp(db.Model, CRUDMixin):
+class BackupApp(db.Model, CRUDMixin, TimestampMixin):
     __tablename__ = 'backups_apps'
     backup_id = db.Column(db.Integer, db.ForeignKey('backups.id'), nullable=False, default=1)
     app_id = db.Column(db.Integer, db.ForeignKey('apps.id'), nullable=False, default=1)
     status = db.Column(db.String)
+    path = db.Column(db.String)
 
     app = db.relationship(App, backref="installed_apps")
-    backup = db.relationship(Backup, backref="installed_apps")
+    backup = db.relationship(Backup, backref="installed_apps", lazy='joined')
 
-    def __init__(self, app=None, backup=None, status=None):
+    def __init__(self, app=None, backup=None, status=None, path=""):
         self.app = app
         self.backup = backup
         self.status = status
+        self.path = path
 
 Backup.apps = association_proxy("installed_apps", "app")
 App.backups = association_proxy("installed_apps", "backup")

migrations/versions/3fc24eab3c89_backup_apps_timestamps_and_path.py

+"""backup_apps timestamps and path
+
+Revision ID: 3fc24eab3c89
+Revises: 43ab57ed5d00
+Create Date: 2014-06-20 21:33:31.641259
+
+"""
+
+# revision identifiers, used by Alembic.
+revision = '3fc24eab3c89'
+down_revision = '43ab57ed5d00'
+
+from alembic import op
+import sqlalchemy as sa
+import datetime
+
+def upgrade():
+    op.add_column('backups_apps', sa.Column('created_at', sa.DateTime(), nullable=True, default=datetime.datetime.now))
+    op.add_column('backups_apps', sa.Column('updated_at', sa.DateTime(), nullable=True, default=datetime.datetime.now, onupdate=datetime.datetime.now))
+    op.add_column('backups_apps', sa.Column('path', sa.String(), nullable=True))
+
+
+def downgrade():
+    op.drop_column('backups_apps', 'created_at')
+    op.drop_column('backups_apps', 'updated_at')
+    op.drop_column('backups_apps', 'path')