[gnome-ostree] versioneddir.js: New file, use it in task.js and task-builddisks.js



commit ba0f7c4d0601b870a22c634639d264b34ce2ed18
Author: Colin Walters <walters verbum org>
Date:   Mon Jun 17 10:16:45 2013 -0400

    versioneddir.js: New file, use it in task.js and task-builddisks.js
    
    Formerly task-builddisks.js was reusing internal task.js API, which
    broke with the refactoring.  Clean this up.

 Makefile-ostbuild.am            |    1 +
 src/js/task.js                  |   48 +++++++++-----------------------
 src/js/tasks/task-builddisks.js |    4 ++-
 src/js/versioneddir.js          |   58 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 75 insertions(+), 36 deletions(-)
---
diff --git a/Makefile-ostbuild.am b/Makefile-ostbuild.am
index f0a5ba3..ea03f2b 100644
--- a/Makefile-ostbuild.am
+++ b/Makefile-ostbuild.am
@@ -56,6 +56,7 @@ jsostbuild_DATA= \
        src/js/snapshot.js \
        src/js/streamutil.js \
        src/js/vcs.js \
+       src/js/versioneddir.js \
        $(NULL)
 
 jsostbuiltinsdir=$(jsostbuilddir)/builtins
diff --git a/src/js/task.js b/src/js/task.js
index d64d3c6..5969cae 100644
--- a/src/js/task.js
+++ b/src/js/task.js
@@ -27,6 +27,7 @@ const JsonUtil = imports.jsonutil;
 const JsonDB = imports.jsondb;
 const ProcUtil = imports.procutil;
 const BuildUtil = imports.buildutil;
+const VersionedDir = imports.versioneddir;
 
 const DefaultTaskDef = {
     TaskName: '',
@@ -357,39 +358,15 @@ const TaskRunner = new Lang.Class({
        BuildUtil.checkIsWorkDirectory(this.workdir);
     },
 
-    _loadVersionsFrom: function(dir, cancellable) {
-       let e = dir.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, cancellable);
-       let info;
-       let results = [];
-       while ((info = e.next_file(cancellable)) != null) {
-           let name = info.get_name();
-           let match = this._VERSION_RE.exec(name);
-           if (!match)
-               continue;
-           results.push(name);
-       }
-       e.close(null);
-       results.sort(BuildUtil.compareVersions);
-       return results;
-    },
-
-    _cleanOldVersions: function(dir, retain, cancellable) {
-       let versions = this._loadVersionsFrom(dir, cancellable);
-       while (versions.length > retain) {
-           let child = dir.get_child(versions.shift());
-           GSystem.shutil_rm_rf(child, cancellable);
-       }
-    },
-
     _loadAllVersions: function(cancellable) {
        let allVersions = [];
 
-       let successVersions = this._loadVersionsFrom(this._successDir, cancellable);
+       let successVersions = this._successDir.loadVersions(cancellable);
        for (let i = 0; i < successVersions.length; i++) {
            allVersions.push([true, successVersions[i]]);
        }
 
-       let failedVersions = this._loadVersionsFrom(this._failedDir, cancellable);
+       let failedVersions = this._failedDir.loadVersions(cancellable);
        for (let i = 0; i < failedVersions.length; i++) {
            allVersions.push([false, failedVersions[i]]);
        }
@@ -411,10 +388,11 @@ const TaskRunner = new Lang.Class({
        this.dir = this.taskmaster.path.resolve_relative_path(this.name);
        GSystem.file_ensure_directory(this.dir, true, cancellable);
        
-       this._successDir = this.dir.get_child('successful');
-       GSystem.file_ensure_directory(this._successDir, true, cancellable);
-       this._failedDir = this.dir.get_child('failed');
-       GSystem.file_ensure_directory(this._failedDir, true, cancellable);
+       this._topDir = new VersionedDir.VersionedDir(this.dir, this._VERSION_RE);
+       this._successDir = new VersionedDir.VersionedDir(this.dir.get_child('successful'),
+                                                        this._VERSION_RE);
+       this._failedDir = new VersionedDir.VersionedDir(this.dir.get_child('failed'),
+                                                       this._VERSION_RE);
 
        let allVersions = this._loadAllVersions(cancellable);
 
@@ -491,16 +469,16 @@ const TaskRunner = new Lang.Class({
         }
 
        if (!success) {
-           target = this._failedDir.get_child(this._version);
+           target = this._failedDir.path.get_child(this._version);
            GSystem.file_rename(this._taskCwd, target, null);
            this._taskCwd = target;
-           this._cleanOldVersions(this._failedDir, this.taskData.taskDef.RetainFailed, null);
+           this._failedDir.cleanOldVersions(this.taskData.taskDef.RetainFailed, null);
            this.onComplete(success, errmsg);
        } else {
-           target = this._successDir.get_child(this._version);
+           target = this._successDir.path.get_child(this._version);
            GSystem.file_rename(this._taskCwd, target, null);
            this._taskCwd = target;
-           this._cleanOldVersions(this._successDir, this.taskData.taskDef.RetainSuccess, null);
+           this._successDir.cleanOldVersions(this.taskData.taskDef.RetainSuccess, null);
            this.onComplete(success, null);
        }
 
@@ -521,7 +499,7 @@ const TaskRunner = new Lang.Class({
        JsonUtil.writeJsonFileAtomic(this._taskCwd.get_child('meta.json'), meta, cancellable);
 
        // Also remove any old interrupted versions
-       this._cleanOldVersions(this.dir, 0, null);
+       this._topDir.cleanOldVersions(0, null);
 
        this._updateIndex(cancellable);
 
diff --git a/src/js/tasks/task-builddisks.js b/src/js/tasks/task-builddisks.js
index 6a0ba40..d3115f3 100644
--- a/src/js/tasks/task-builddisks.js
+++ b/src/js/tasks/task-builddisks.js
@@ -30,6 +30,7 @@ const ProcUtil = imports.procutil;
 const BuildUtil = imports.buildutil;
 const LibQA = imports.libqa;
 const JsonDB = imports.jsondb;
+const VersionedDir = imports.versioneddir;
 const JsonUtil = imports.jsonutil;
 const JSUtil = imports.jsutil;
 const GuestFish = imports.guestfish;
@@ -56,6 +57,7 @@ const TaskBuildDisks = new Lang.Class({
         let subworkdir = Gio.File.new_for_path('.');
 
              let baseImageDir = this.workdir.resolve_relative_path(this._imageSubdir);
+        let baseImageVersionedDir = new VersionedDir.VersionedDir(baseImageDir, this._VERSION_RE);
         GSystem.file_ensure_directory(baseImageDir, true, cancellable);
              let currentImageLink = baseImageDir.get_child('current');
              let previousImageLink = baseImageDir.get_child('previous');
@@ -146,7 +148,7 @@ const TaskBuildDisks = new Lang.Class({
         }
         BuildUtil.atomicSymlinkSwap(baseImageDir.get_child('current'), targetImageDir, cancellable);
 
-        this._cleanOldVersions(baseImageDir, IMAGE_RETAIN_COUNT, cancellable);
+        baseImageVersionedDir.cleanOldVersions(IMAGE_RETAIN_COUNT, cancellable);
     },
 
     _postDiskCreation: function(diskPath, cancellable) {
diff --git a/src/js/versioneddir.js b/src/js/versioneddir.js
new file mode 100644
index 0000000..f3f61db
--- /dev/null
+++ b/src/js/versioneddir.js
@@ -0,0 +1,58 @@
+// 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 Lang = imports.lang;
+const Format = imports.format;
+
+const GSystem = imports.gi.GSystem;
+const BuildUtil = imports.buildutil;
+
+const VersionedDir = new Lang.Class({
+    Name: 'VersionedDir',
+
+    _init: function(path, regexp) {
+       this.path = path;
+       this._regexp = regexp;
+       GSystem.file_ensure_directory(this.path, true, null);
+    },
+
+    loadVersions: function(cancellable) {
+       let e = this.path.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, 
cancellable);
+       let info;
+       let results = [];
+       while ((info = e.next_file(cancellable)) != null) {
+           let name = info.get_name();
+           let match = this._regexp.exec(name);
+           if (!match)
+               continue;
+           results.push(name);
+       }
+       e.close(null);
+       results.sort(BuildUtil.compareVersions);
+       return results;
+    },
+
+    cleanOldVersions: function(retain, cancellable) {
+       let versions = this.loadVersions(cancellable);
+       while (versions.length > retain) {
+           let child = this.path.get_child(versions.shift());
+           GSystem.shutil_rm_rf(child, cancellable);
+       }
+    },
+});


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