[gnome-ostree] dyntask: Split tasks into separate files



commit e5cf25c95e5572e2096b28cd4354882a76e4b7f9
Author: Colin Walters <walters verbum org>
Date:   Wed Jan 23 19:25:23 2013 -0500

    dyntask: Split tasks into separate files
    
    More modular.

 Makefile-ostbuild.am                   |    5 +
 src/ostbuild/js/dyntask.js             |  154 +++++++++-----------------------
 src/ostbuild/js/tasks/task-checksum.js |  123 +++++++++++++++++++++++++
 3 files changed, 170 insertions(+), 112 deletions(-)
---
diff --git a/Makefile-ostbuild.am b/Makefile-ostbuild.am
index dcf16e1..c36bfcb 100644
--- a/Makefile-ostbuild.am
+++ b/Makefile-ostbuild.am
@@ -72,4 +72,9 @@ jsostbuiltins_DATA= \
 	src/ostbuild/js/builtins/shell.js \
 	$(NULL)
 
+jsosttasksdir=$(jsostbuilddir)/tasks
+jsosttasks_DATA= \
+	src/ostbuild/js/tasks/task-checksum.js \
+	$(NULL)
+
 endif
diff --git a/src/ostbuild/js/dyntask.js b/src/ostbuild/js/dyntask.js
index e8e8369..a99cab3 100644
--- a/src/ostbuild/js/dyntask.js
+++ b/src/ostbuild/js/dyntask.js
@@ -1,3 +1,20 @@
+// Copyright (C) 2012,2013 Colin Walters <walters verbum org>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the
+// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
 const GLib = imports.gi.GLib;
 const Gio = imports.gi.Gio;
 const format = imports.format;
@@ -26,14 +43,29 @@ const TaskMaster = new Lang.Class({
 	this._seenTasks = {};
 	this._completeTasks = {};
 	this._taskErrors = {};
+
+	let taskdir = Gio.File.new_for_path(GLib.getenv('OSTBUILD_DATADIR')).resolve_relative_path('js/tasks');
+	let denum = taskdir.enumerate_children('standard::*', 0, null);
+	let finfo;
+	
+	for (let taskname in imports.tasks) {
+	    let taskMod = imports.tasks[taskname];
+	    for (let defname in taskMod) {
+		if (defname.indexOf('Task') !== 0)
+		    continue;
+		let cls = taskMod[defname];
+		let instance = new cls;
+		this.register(instance);
+	    }
+	}
     },
 
     register: function(taskdef) {
 	this._tasks.push(taskdef);
     },
-    
-    push: function(taskName) {
-	if (this._seenTasks[taskName])
+
+    _pushRecurse: function(taskName, seen) {
+	if (seen[taskName])
 	    return null;
 	let result = null;
 	for (let i = 0; i < this._tasks.length; i++) {
@@ -55,7 +87,7 @@ const TaskMaster = new Lang.Class({
 	    for (let j = 0; j < specifiedDependencies.length; j++) {
 		let depName = specifiedDependencies[j];
 		if (!this._completeTasks[depName]) {
-		    let depTask = this.push(depName);
+		    let depTask = this._pushRecurse(depName, seen);
 		    waitingDependencies[depName] = depTask;
 		}
 	    }
@@ -67,7 +99,7 @@ const TaskMaster = new Lang.Class({
 		      serial: serial,
 		      result: null };
 	    this._pendingTasksList.push(result);
-	    this._seenTasks[taskName] = true;
+	    seen[taskName] = true;
 	    break;
 	}
 	if (!result)
@@ -76,6 +108,10 @@ const TaskMaster = new Lang.Class({
 	return result;
     },
 
+    push: function(taskName) {
+	return this._pushRecurse(taskName, {});
+    },
+
     _queueRecalculate: function() {
 	if (this._idleRecalculateId > 0)
 	    return;
@@ -191,119 +227,13 @@ const TaskDef = new Lang.Class({
     }
 });
 
-const TaskChecksumSha256 = new Lang.Class({
-    Name: 'TaskChecksumSha256',
-    Extends: TaskDef,
-
-    _init: function() {
-    },
-
-    getPattern: function() {
-	return [/\/ChecksumSha256\/(.*)$/, 'PATH'];
-    },
-
-    _onAsyncOpComplete: function(error) {
-	let state = this;
-	state.asyncOutstanding--;
-	if (state.asyncOutstanding != 0)
-	    return;
-	if (error) {
-	    state.onComplete(null, error);
-	} else {
-	    let csumStr = state.buf.steal_as_bytes().toArray().toString();
-	    state.onComplete(csumStr.substr(0, csumStr.indexOf(' ')), null);
-	}
-    },
-
-    _onSpliceComplete: function(stream, result) {
-	let state = this;
-
-	let error = null;
-	try {
-	    stream.splice_finish(result);
-	} catch (e) {
-	    if (e.domain != undefined)
-		error = e;
-	    else
-		throw e;
-	}
-	Lang.bind(state, state.me._onAsyncOpComplete)(error);
-    },
-
-    _onProcWait: function(proc, result) {
-	let state = this;
-
-	let error = null;
-	try {
-	    let [success,ecode] = proc.wait_finish(result);
-	    GLib.spawn_check_exit_status(ecode);
-	} catch (e) {
-	    if (e.domain != undefined)
-		error = e;
-	    else
-		throw e;
-	}
-	Lang.bind(state, state.me._onAsyncOpComplete)(error);
-    },
-
-    execute: function(inputs, dependResults, cancellable, onComplete) {
-	let state = {me: this,
-		     onComplete: onComplete,
-		     buf: null,
-		     asyncOutstanding: 2};
-	let path = inputs.PATH;
-	let context = new GSystem.SubprocessContext({argv: ['sha256sum', path]});
-	context.set_stdout_disposition(GSystem.SubprocessStreamDisposition.PIPE);
-	let proc = new GSystem.Subprocess({context: context});
-	proc.init(cancellable);
-	let stdout = proc.get_stdout_pipe();
-	state.buf = Gio.MemoryOutputStream.new_resizable();
-	state.buf.splice_async(stdout, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE |
-			       Gio.OutputStreamSpliceFlags.CLOSE_TARGET, GLib.PRIORITY_DEFAULT,
-			       cancellable, Lang.bind(state, this._onSpliceComplete));
-	proc.wait(cancellable, Lang.bind(state, this._onProcWait));
-    }
-});
-
-const TaskChecksumMany = new Lang.Class({
-    Name: 'TaskChecksumMany',
-    Extends: TaskDef,
-
-    _init: function() {
-    },
-
-    getPattern: function() {
-	return [/\/ChecksumMany\/(.*)$/, 'FILENAMES'];
-    },
-
-    getDepends: function(inputs) {
-	let filenamesStr = inputs.FILENAMES;
-	let filenames = filenamesStr.split(',');
-	let r = [];
-	for (let i = 0; i < filenames.length; i++)
-	    r.push('/ChecksumSha256/' + filenames[i]);
-	return r;
-    },
-
-    execute: function(inputs, dependResults, cancellable, onComplete) {
-	let r = '';
-	for (let i = 0; i < dependResults.length; i++)
-	    r += dependResults[i] + '\n';
-	GLib.idle_add(GLib.PRIORITY_DEFAULT, function() {
-	    onComplete(r, null);
-	});
-    }
-});
-
 function demo(argv) {
     var loop = GLib.MainLoop.new(null, true);
     let ecode = 1;
     var app = new TaskMaster('taskmaster/', {onEmpty: function() {
-	print("TaskMaster: Complete!");
+	print("TaskMaster: idle");
 	loop.quit();
     }});
-    app.register(new TaskChecksumSha256());
-    app.register(new TaskChecksumMany());
     for (let i = 0; i < argv.length; i++) {
 	let taskName = argv[i];
 	app.push(taskName);
diff --git a/src/ostbuild/js/tasks/task-checksum.js b/src/ostbuild/js/tasks/task-checksum.js
new file mode 100644
index 0000000..a91dabf
--- /dev/null
+++ b/src/ostbuild/js/tasks/task-checksum.js
@@ -0,0 +1,123 @@
+// Copyright (C) 2012,2013 Colin Walters <walters verbum org>
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the
+// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+// Boston, MA 02111-1307, USA.
+
+const GLib = imports.gi.GLib;
+const Gio = imports.gi.Gio;
+const format = imports.format;
+const Lang = imports.lang;
+
+const GSystem = imports.gi.GSystem;
+const Params = imports.params;
+const DynTask = imports.dyntask;
+
+const TaskChecksumSha256 = new Lang.Class({
+    Name: 'TaskChecksumSha256',
+    Extends: DynTask.TaskDef,
+
+    getPattern: function() {
+	return [/\/ChecksumSha256\/(.*)$/, 'PATH'];
+    },
+
+    _onAsyncOpComplete: function(error) {
+	let state = this;
+	state.asyncOutstanding--;
+	if (state.asyncOutstanding != 0)
+	    return;
+	if (error) {
+	    state.onComplete(null, error);
+	} else {
+	    let csumStr = state.buf.steal_as_bytes().toArray().toString();
+	    state.onComplete(csumStr.substr(0, csumStr.indexOf(' ')), null);
+	}
+    },
+
+    _onSpliceComplete: function(stream, result) {
+	let state = this;
+
+	let error = null;
+	try {
+	    stream.splice_finish(result);
+	} catch (e) {
+	    if (e.domain != undefined)
+		error = e;
+	    else
+		throw e;
+	}
+	Lang.bind(state, state.me._onAsyncOpComplete)(error);
+    },
+
+    _onProcWait: function(proc, result) {
+	let state = this;
+
+	let error = null;
+	try {
+	    let [success,ecode] = proc.wait_finish(result);
+	    GLib.spawn_check_exit_status(ecode);
+	} catch (e) {
+	    if (e.domain != undefined)
+		error = e;
+	    else
+		throw e;
+	}
+	Lang.bind(state, state.me._onAsyncOpComplete)(error);
+    },
+
+    execute: function(inputs, dependResults, cancellable, onComplete) {
+	let state = {me: this,
+		     onComplete: onComplete,
+		     buf: null,
+		     asyncOutstanding: 2};
+	let path = inputs.PATH;
+	let context = new GSystem.SubprocessContext({argv: ['sha256sum', path]});
+	context.set_stdout_disposition(GSystem.SubprocessStreamDisposition.PIPE);
+	let proc = new GSystem.Subprocess({context: context});
+	proc.init(cancellable);
+	let stdout = proc.get_stdout_pipe();
+	state.buf = Gio.MemoryOutputStream.new_resizable();
+	state.buf.splice_async(stdout, Gio.OutputStreamSpliceFlags.CLOSE_SOURCE |
+			       Gio.OutputStreamSpliceFlags.CLOSE_TARGET, GLib.PRIORITY_DEFAULT,
+			       cancellable, Lang.bind(state, this._onSpliceComplete));
+	proc.wait(cancellable, Lang.bind(state, this._onProcWait));
+    }
+});
+
+const TaskChecksumMany = new Lang.Class({
+    Name: 'TaskChecksumMany',
+    Extends: DynTask.TaskDef,
+
+    getPattern: function() {
+	return [/\/ChecksumMany\/(.*)$/, 'FILENAMES'];
+    },
+
+    getDepends: function(inputs) {
+	let filenamesStr = inputs.FILENAMES;
+	let filenames = filenamesStr.split(',');
+	let r = [];
+	for (let i = 0; i < filenames.length; i++)
+	    r.push('/ChecksumSha256/' + filenames[i]);
+	return r;
+    },
+
+    execute: function(inputs, dependResults, cancellable, onComplete) {
+	let r = '';
+	for (let i = 0; i < dependResults.length; i++)
+	    r += dependResults[i] + '\n';
+	GLib.idle_add(GLib.PRIORITY_DEFAULT, function() {
+	    onComplete(r, null);
+	});
+    }
+});



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