[gnome-ostree] Rewrite Snapshot to be a class



commit b41905e09aeb846bba29ba6d31743f240b18bc10
Author: Colin Walters <walters verbum org>
Date:   Thu Jan 17 01:11:12 2013 -0500

    Rewrite Snapshot to be a class
    
    This is simpler, cleaner, and most importantly - faster, since we
    avoid creating a "componentDict" repeatedly.

 src/ostbuild/js/builtins/build.js      |   38 +++++++------
 src/ostbuild/js/builtins/checkout.js   |   10 ++--
 src/ostbuild/js/builtins/git_mirror.js |   24 +++-----
 src/ostbuild/js/snapshot.js            |   96 +++++++++++++++++++-------------
 4 files changed, 92 insertions(+), 76 deletions(-)
---
diff --git a/src/ostbuild/js/builtins/build.js b/src/ostbuild/js/builtins/build.js
index 6157c74..c67f791 100644
--- a/src/ostbuild/js/builtins/build.js
+++ b/src/ostbuild/js/builtins/build.js
@@ -65,11 +65,12 @@ const Build = new Lang.Class({
     _composeBuildroot: function(workdir, componentName, architecture, cancellable) {
         let starttime = GLib.DateTime.new_now_utc();
 
-        let buildname = Format.vprintf('%s/%s/%s', [this._snapshot['prefix'], componentName, architecture]);
+	let prefix = this._snapshot.data['prefix'];
+        let buildname = Format.vprintf('%s/%s/%s', [prefix, componentName, architecture]);
         let buildrootCachedir = this.workdir.resolve_relative_path('roots/' + buildname);
         GSystem.file_ensure_directory(buildrootCachedir, true, cancellable);
 
-        let components = this._snapshot['components']
+        let components = this._snapshot.data['components']
         let component = null;
         let buildDependencies = [];
         for (let i = 0; i < components.length; i++) {
@@ -81,9 +82,7 @@ const Build = new Lang.Class({
 
         let refToRev = {};
 
-        let prefix = this._snapshot['prefix'];
-
-        let archBuildrootName = Format.vprintf('bases/%s/%s-%s-devel', [this._snapshot['base']['name'],
+        let archBuildrootName = Format.vprintf('bases/%s/%s-%s-devel', [this._snapshot.data['base']['name'],
 									prefix,
 									architecture]);
 
@@ -281,11 +280,12 @@ const Build = new Lang.Class({
     _buildOneComponent: function(component, architecture, cancellable) {
         let basename = component['name'];
 
-        let buildname = Format.vprintf('%s/%s/%s', [this._snapshot['prefix'], basename, architecture]);
+	let prefix = this._snapshot.data['prefix'];
+        let buildname = Format.vprintf('%s/%s/%s', [prefix, basename, architecture]);
         let buildRef = 'components/' + buildname;
 
         let currentVcsVersion = component['revision'];
-        let expandedComponent = Snapshot.expandComponent(this._snapshot, component);
+        let expandedComponent = this._snapshot.getExpanded(basename);
         let previousMetadata = this._componentBuildCache[buildname];
         let wasInBuildCache = (previousMetadata != null);
 	let previousBuildVersion;
@@ -369,7 +369,7 @@ const Build = new Lang.Class({
         let checkoutdir = this.workdir.get_child('checkouts');
         let componentSrc = checkoutdir.get_child(buildname);
         GSystem.file_ensure_directory(componentSrc.get_parent(), true, cancellable);
-        let childArgs = ['ostbuild', 'checkout', '--snapshot=' + this._snapshotPath.get_path(),
+        let childArgs = ['ostbuild', 'checkout', '--snapshot=' + this._snapshot.path.get_path(),
 			 '--checkoutdir=' + componentSrc.get_path(),
 			 '--metadata-path=' + tempMetadataPath.get_path(),
 			 '--overwrite', basename];
@@ -490,9 +490,11 @@ const Build = new Lang.Class({
 								   'rev-parse', develName], cancellable);
         relatedRefs[develName] = develRevision;
 
+	let prefix = this._snapshot.data['prefix'];
+
 	for (let name in componentBuildRevs) {
 	    let rev = componentBuildRevs[name];
-            let buildRef = 'components/' + this._snapshot['prefix'] + '/' + name;
+            let buildRef = 'components/' + prefix + '/' + name;
             relatedRefs[buildRef] = rev;
 	}
 
@@ -538,7 +540,7 @@ const Build = new Lang.Class({
         GSystem.file_unlink(contentsTmpPath, cancellable);
 
         let contentsPath = composeRootdir.get_child('contents.json');
-        JsonUtil.writeJsonFileAtomic(contentsPath, this._snapshot, cancellable);
+        JsonUtil.writeJsonFileAtomic(contentsPath, this._snapshot.data, cancellable);
 
         let treename = 'trees/' + target['name'];
         
@@ -556,7 +558,7 @@ const Build = new Lang.Class({
 
     /* Build the Yocto base system. */
     _buildBase: function(architecture, cancellable) {
-        let basemeta = Snapshot.expandComponent(this._snapshot, this._snapshot['base']);
+        let basemeta = this._snapshot.getExpanded(this._snapshot.data['base']['name']);
 	let basename = basemeta['name'];
         let checkoutdir = this.workdir.get_child('checkouts').get_child(basemeta['name']);
 	GSystem.file_ensure_directory(checkoutdir.get_parent(), true, cancellable);
@@ -649,8 +651,8 @@ const Build = new Lang.Class({
 	this.libdir = Gio.File.new_for_path(GLib.getenv('OSTBUILD_LIBDIR'));
 
 	this._srcDb = new JsonDB.JsonDB(this._snapshotDir, this.prefix + '-src-snapshot');
-	[this._snapshot, this._snapshotPath] = Snapshot.load(this._srcDb, this.prefix, args.snapshot, cancellable);
-	let snapshotName = this._snapshotPath.get_basename();
+	this._snapshot = Snapshot.Snapshot.prototype.loadFromDb(this._srcDb, this.prefix, args.snapshot, cancellable);
+	let snapshotName = this._snapshot.path.get_basename();
 	
         this.forceBuildComponents = {};
         this.cachedPatchdirRevision = null;
@@ -663,11 +665,11 @@ const Build = new Lang.Class({
 			     cancellable);
 	}
 
-        let components = this._snapshot['components'];
+        let components = this._snapshot.data['components'];
 
-        let prefix = this._snapshot['prefix'];
-        let basePrefix = this._snapshot['base']['name'] + '/' + prefix;
-        let architectures = this._snapshot['architectures'];
+        let prefix = this._snapshot.data['prefix'];
+        let basePrefix = this._snapshot.data['base']['name'] + '/' + prefix;
+        let architectures = this._snapshot.data['architectures'];
 
         for (let i = 0; i < architectures.length; i++) {
             this._buildBase(architectures[i], cancellable);
@@ -702,7 +704,7 @@ const Build = new Lang.Class({
 
         for (let i = 0; i < args.components.length; i++) {
 	    let name = args.components[i];
-            let component = Snapshot.getComponent(this._snapshot, name);
+            let component = this._snapshot.getComponent(name);
             this.forceBuildComponents[name] = true;
 	}
 
diff --git a/src/ostbuild/js/builtins/checkout.js b/src/ostbuild/js/builtins/checkout.js
index 86ee300..6210ee9 100644
--- a/src/ostbuild/js/builtins/checkout.js
+++ b/src/ostbuild/js/builtins/checkout.js
@@ -130,7 +130,7 @@ const Checkout = new Lang.Class({
 	this._snapshotDir = this.workdir.get_child('snapshots');
 
 	this._srcDb = new JsonDB.JsonDB(this._snapshotDir, this.prefix + '-src-snapshot');
-	[this._snapshot, this._snapshotPath] = Snapshot.load(this._srcDb, this.prefix, args.snapshot, cancellable);
+	this._snapshot = Snapshot.Snapshot.prototype.loadFromDb(this._srcDb, this.prefix, args.snapshot, cancellable);
 
         let componentName = args.component;
 
@@ -139,7 +139,7 @@ const Checkout = new Lang.Class({
             if (args.metadata_path != null) {
 		component = JsonUtil.loadJson(Gio.File.new_for_path(args.metadata_path), cancellable);
             } else {
-		component = Snapshot.getExpanded(this._snapshot, componentName);
+		component = this._snapshot.getExpanded(componentName);
 	    }
 
 	    _checkoutOneComponent(this.mirrordir, this.patchdir, component, cancellable,
@@ -148,9 +148,9 @@ const Checkout = new Lang.Class({
 				    patchesPath: args.patches_path,
 				    overwrite: args.overwrite });
 	} else {
-	    for (let i = 0; i < this._snapshot['components'].length; i++) {
-		let componentName = this._snapshot['components'][i]['name'];
-		let component = Snapshot.getExpanded(this._snapshot, componentName);
+	    let all = this._snapshot.getAllComponentNames();
+	    for (let i = 0; i < all.length; i++) {
+		let component = this._snapshot.getExpanded(all[i]);
 		_checkoutOneComponent(this.mirrordir, this.patchdir, component, cancellable,
 				      { checkoutdir: args.checkoutdir,
 					clean: args.clean,
diff --git a/src/ostbuild/js/builtins/git_mirror.js b/src/ostbuild/js/builtins/git_mirror.js
index 6c63aab..dfbbcc6 100644
--- a/src/ostbuild/js/builtins/git_mirror.js
+++ b/src/ostbuild/js/builtins/git_mirror.js
@@ -60,33 +60,29 @@ const GitMirror = new Lang.Class({
 	this._srcDb = new JsonDB.JsonDB(this._snapshotDir, this.prefix + '-src-snapshot');
 
         if (args.manifest != null) {
-            this._snapshot = JsonUtil.loadJson(Gio.File.new_for_path(args.manifest), cancellable);
+            let snapshotData = JsonUtil.loadJson(Gio.File.new_for_path(args.manifest), cancellable);
 	    let resolvedComponents = [];
-	    let components = this._snapshot['components'];
+	    let components = snapshotData['components'];
 	    for (let i = 0; i < components.length; i++) {
-		resolvedComponents.push(BuildUtil.resolveComponent(this._snapshot, components[i]));
+		resolvedComponents.push(BuildUtil.resolveComponent(snapshotData, components[i]));
 	    }
-            this._snapshot['components'] = resolvedComponents;
-            this._snapshot['patches'] = BuildUtil.resolveComponent(this._snapshot, this._snapshot['patches']);
-            this._snapshot['base'] = BuildUtil.resolveComponent(this._snapshot, this._snapshot['base']);
+            snapshotData['components'] = resolvedComponents;
+            snapshotData['patches'] = BuildUtil.resolveComponent(snapshotData, snapshotData['patches']);
+            snapshotData['base'] = BuildUtil.resolveComponent(snapshotData, snapshotData['base']);
+	    this._snapshot = new Snapshot.Snapshot(snapshotData, null);
         } else {
-	    [this._snapshot, this._snapshotPath] = Snapshot.load(this._srcDb, this.prefix, args.snapshot, cancellable);
+	    this._snapshot = Snapshot.Snapshot.prototype.loadFromDb(this._srcDb, this.prefix, args.snapshot, cancellable);
 	}
 
 	let componentNames;
         if (args.components.length == 0) {
-            componentNames = [];
-            componentNames.push(this._snapshot['patches']['name']);
-            componentNames.push(this._snapshot['base']['name']);
-	    this._snapshot['components'].forEach(function (component) {
-		componentNames.push(component['name']);
-	    });
+	    componentNames = this._snapshot.getAllComponentNames();
         } else {
             componentNames = args.components;
 	}
 
 	componentNames.forEach(Lang.bind(this, function (name) {
-            let component = Snapshot.getComponent(this._snapshot, name);
+            let component = this._snapshot.getComponent(name);
             let src = component['src']
             let [keytype, uri] = Vcs.parseSrcKey(src);
             let branch = component['branch'];
diff --git a/src/ostbuild/js/snapshot.js b/src/ostbuild/js/snapshot.js
index bdf9daa..2ee2b5e 100644
--- a/src/ostbuild/js/snapshot.js
+++ b/src/ostbuild/js/snapshot.js
@@ -17,10 +17,11 @@
 // Boston, MA 02111-1307, USA.
 
 const Gio = imports.gi.Gio;
+const Lang = imports.lang;
 
 const JsonDB = imports.jsondb;
 const JsonUtil = imports.jsonutil;
-const Lang = imports.lang;
+const Params = imports.params;
 
 function _componentDict(snapshot) {
     let r = {};
@@ -62,47 +63,64 @@ function snapshotDiff(a, b) {
     return [added, modified, removed];
 }
 
-function load(db, prefix, pathName, cancellable) {
-    if (pathName) {
-	let path = Gio.File.new_for_path(pathName);
-	return [JsonUtil.loadJson(path, cancellable), path];
-    } else if (prefix) {
-	let path = db.getLatestPath();
-	return [db.loadFromPath(path, cancellable), path];
-    } else {
-	throw new Error("No prefix or snapshot specified");
-    }
-}
+const Snapshot = new Lang.Class({
+    Name: 'Snapshot',
+    
+    _init: function(data, path) {
+	this.data = data;
+	this.path = path;
+	this._componentDict = _componentDict(data);
+	this._componentNames = [];
+	for (let k in this._componentDict)
+	    this._componentNames.push(k);
+    },
 
-function getComponent(snapshot, name, allowNone) {
-    let d = _componentDict(snapshot);
-    let r = d[name] || null;
-    if (!r && !allowNone)
-	throw new Error("No component " + name + " in snapshot");
-    return r;
-}
+    _expandComponent: function(component) {
+	let r = {};
+	Lang.copyProperties(component, r);
+	let patchMeta = this.data['patches'];
+	if (patchMeta) {
+	    let componentPatchFiles = component['patches'] || [];
+	    if (componentPatchFiles.length > 0) {
+		let patches = {};
+		Lang.copyProperties(patchMeta, patches);
+		patches['files'] = componentPatchFiles;
+		r['patches'] = patches;
+	    }
+	}
+	let configOpts = (this.data['config-opts'] || []).concat();
+	configOpts.push.apply(configOpts, component['config-opts'] || []);
+	r['config-opts'] = configOpts;
+	return r;
+    },
 
-function expandComponent(snapshot, component) {
-    let r = {};
-    Lang.copyProperties(component, r);
-    let patchMeta = snapshot['patches'];
-    if (patchMeta) {
-	let componentPatchFiles = component['patches'] || [];
-	if (componentPatchFiles.length > 0) {
-	    let patches = {};
-	    Lang.copyProperties(patchMeta, patches);
-	    patches['files'] = componentPatchFiles;
-	    r['patches'] = patches;
+    loadFromDb: function(db, prefix, snapshotPath, cancellable) {
+	let data, path;
+	if (snapshotPath) {
+	    path = Gio.File.new_for_path(snapshotPath);
+	    data = JsonUtil.loadJson(path, cancellable);
+	} else if (prefix) {
+	    path = db.getLatestPath();
+	    data = db.loadFromPath(path, cancellable);
+	} else {
+	    throw new Error("No prefix or snapshot specified");
 	}
-    }
-    let configOpts = (snapshot['config-opts'] || []).concat();
-    configOpts.push.apply(configOpts, component['config-opts'] || []);
-    r['config-opts'] = configOpts;
-    return r;
-}
+	return new Snapshot(data, path);
+    },
 
-function getExpanded(snapshot, name) {
-    return expandComponent(snapshot, getComponent(snapshot, name));
-}
+    getAllComponentNames: function() {
+	return this._componentNames;
+    },
 
+    getComponent: function(name, allowNone) {
+	let d = _componentDict(this.data);
+	let r = d[name] || null;
+	if (!r && !allowNone)
+	    throw new Error("No component " + name + " in snapshot");
+	return r;
+    },
 
+    getExpanded: function(name) {
+	return this._expandComponent(this.getComponent(name));
+    }
+});



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]