[gnome-continuous] Move versioned directory logic more into versioneddir



commit 7dfc0fcbba7fafe3fd6abe4661a421b6e29460a6
Author: Colin Walters <walters verbum org>
Date:   Sun Nov 17 17:41:13 2013 -0500

    Move versioned directory logic more into versioneddir
    
    The goal of this is twofold:
    
    1) Migrate to year/month/day/serial subdirectories, since
       this is a lot more efficient to load and view
    2) Say we wanted to generate a dirlist.json; we could do that
       centrally here.
    
    that could be loaded from the

 src/js/builtins/autobuilder.js |   63 ++++------------------
 src/js/task.js                 |    6 ++-
 src/js/versioneddir.js         |  116 +++++++++++++++++++++++++++++++++++----
 3 files changed, 119 insertions(+), 66 deletions(-)
---
diff --git a/src/js/builtins/autobuilder.js b/src/js/builtins/autobuilder.js
index a811888..4808b0e 100644
--- a/src/js/builtins/autobuilder.js
+++ b/src/js/builtins/autobuilder.js
@@ -43,8 +43,6 @@ const Autobuilder = new Lang.Class({
 
     DESCRIPTION: "Automatically fetch git repositories and build",
 
-    _VERSION_RE: /^(\d+\d\d\d\d)\.(\d+)$/,
-
     _init: function() {
        this.parent();
 
@@ -170,55 +168,6 @@ const Autobuilder = new Lang.Class({
        return true;
     },
 
-    _getLastVersion: function(cancellable) {
-        let allVersions = this._buildsDir.loadVersions(cancellable);
-        if (allVersions.length > 0)
-            return allVersions[allVersions.length-1];
-        else
-            return null;
-    },
-
-    _fillBuildDirectory: function(buildPath, cancellable) {
-        let version = buildPath.get_basename();
-
-        let meta = { "version": version };
-        JsonUtil.writeJsonFileAtomic(buildPath.get_child('meta.json'), meta, cancellable);
-    },
-
-    _getNextBuildDirectory: function(cancellable) {
-        let currentTime = GLib.DateTime.new_now_utc();
-        let currentYmd = Format.vprintf('%d%02d%02d', [currentTime.get_year(),
-                                                       currentTime.get_month(),
-                                                       currentTime.get_day_of_month()]);
-
-        let version = null;
-        let lastVersion = this._getLastVersion(cancellable);
-        if (lastVersion) {
-            let match = this._VERSION_RE.exec(lastVersion);
-            if (!match) throw new Error();
-            let lastYmd = match[1];
-            let lastSerial = match[2];
-            if (lastYmd == currentYmd) {
-                version = currentYmd + '.' + (parseInt(lastSerial) + 1);
-            }
-        }
-        if (version === null) {
-            version = currentYmd + '.0';
-        }
-
-        let buildPath = this._buildsDir.path.get_child(version);
-        GSystem.file_ensure_directory(buildPath, true, cancellable);
-
-        if (lastVersion) {
-            let lastBuildPath = this._buildsDir.path.get_child(lastVersion);
-            BuildUtil.atomicSymlinkSwap(buildPath.get_child('last-build'), lastBuildPath, null);
-        }
-
-        this._fillBuildDirectory(buildPath, cancellable);
-
-        return buildPath;
-    },
-
     _runResolve: function() {
        let cancellable = null;
        
@@ -234,7 +183,17 @@ const Autobuilder = new Lang.Class({
            ProcUtil.runSync(['git', 'pull', '-r'], cancellable,
                             { cwd: this._autoupdate_self })
 
-        let buildPath = this._getNextBuildDirectory(cancellable);
+       let previousVersion = this._buildsDir.currentVersion(cancellable);
+        let buildPath = this._buildsDir.allocateNewVersion(cancellable);
+        let version = this._buildsDir.pathToVersion(buildPath);
+        if (previousVersion) {
+            let lastBuildPath = this._buildsDir.createPathForVersion(previousVersion);
+            BuildUtil.atomicSymlinkSwap(buildPath.get_child('last-build'), lastBuildPath, null);
+        }
+
+        let meta = { "version": version };
+        JsonUtil.writeJsonFileAtomic(buildPath.get_child('meta.json'), meta, cancellable);
+
        if (this._initialResolveNeeded) {
            this._initialResolveNeeded = false;
            this._taskmaster.pushTask(buildPath, 'resolve', { });
diff --git a/src/js/task.js b/src/js/task.js
index 706550f..d5f0930 100644
--- a/src/js/task.js
+++ b/src/js/task.js
@@ -24,6 +24,7 @@ const Signals = imports.signals;
 const GSystem = imports.gi.GSystem;
 const OSTree = imports.gi.OSTree;
 const Params = imports.params;
+const VersionedDir = imports.versioneddir;
 const JsonUtil = imports.jsonutil;
 const ProcUtil = imports.procutil;
 const BuildUtil = imports.buildutil;
@@ -343,7 +344,8 @@ const Task = new Lang.Class({
        BuildUtil.checkIsWorkDirectory(this.workdir);
         this.builddir = Gio.File.new_for_path(GLib.getenv('_OSTBUILD_BUILDDIR'));
 
-        this._buildName = this.builddir.get_basename();
+       let relpath = this.workdir.get_relative_path(this.builddir);
+       this._buildName = VersionedDir.VersionedDir.prototype.relpathToVersion(relpath);
 
        this.mirrordir = this.workdir.get_child('src');
        GSystem.file_ensure_directory(this.mirrordir, true, null);
@@ -387,7 +389,7 @@ const TaskRunner = new Lang.Class({
         let buildPath = this.taskmaster.tasksPath.resolve_relative_path(this.name);
         this.buildPath = GSystem.file_realpath(buildPath);
 
-        this.buildName = this.buildPath.get_basename();
+        this.buildName = this.workdir.get_relative_path(this.buildPath);
         this.taskCwd = this.buildPath.get_child(this.name);
         GSystem.file_ensure_directory(this.taskCwd, false, cancellable);
 
diff --git a/src/js/versioneddir.js b/src/js/versioneddir.js
index f3f61db..6705180 100644
--- a/src/js/versioneddir.js
+++ b/src/js/versioneddir.js
@@ -26,33 +26,125 @@ const BuildUtil = imports.buildutil;
 const VersionedDir = new Lang.Class({
     Name: 'VersionedDir',
 
-    _init: function(path, regexp) {
+    _YMD_SERIAL_VERSION_RE: /^(\d+)(\d\d)(\d\d)\.(\d+)$/,
+    _YEAR_OR_SERIAL_VERSION_RE: /^(\d+)$/,
+    _MONTH_OR_DAY_VERSION_RE: /^\d\d$/,
+
+    _init: function(path) {
        this.path = path;
-       this._regexp = regexp;
+       this._cachedResults = null;
        GSystem.file_ensure_directory(this.path, true, null);
     },
 
-    loadVersions: function(cancellable) {
-       let e = this.path.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, 
cancellable);
+    _createPathForParsedVersion: function(year, month, day, serial) {
+       return this.path.resolve_relative_path(Format.vprintf('%s/%s/%s/%s',
+                                                             [year, month, day, serial]));
+    },
+
+    createPathForVersion: function(ymdSerial) {
+       let match = this._YMD_SERIAL_VERSION_RE.exec(ymdSerial);
+       if (!match) throw new Error();
+       return this.path.resolve_relative_path(match[1] + '/' + match[2] + '/' +
+                                              match[3] + '/' + match[4]);
+    },
+
+    _iterateChildrenMatching: function(dir, pattern, callback, cancellable) {
+       let e = dir.enumerate_children('standard::*', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS, cancellable);
        let info;
-       let results = [];
        while ((info = e.next_file(cancellable)) != null) {
+           let srcpath = e.get_child(info);
            let name = info.get_name();
-           let match = this._regexp.exec(name);
+           let match = pattern.exec(name);
            if (!match)
                continue;
-           results.push(name);
+           callback(srcpath, match, cancellable);
        }
        e.close(null);
+    },
+
+    _loadYear: function(yeardir, results, cancellable) {
+       this._iterateChildrenMatching(yeardir, this._MONTH_OR_DAY_VERSION_RE,
+                                     Lang.bind(this, function(srcpath, match, cancellable) {
+                                         this._loadMonth(srcpath, results, cancellable);
+                                     }), cancellable);
+    },
+
+    _loadMonth: function(monthdir, results, cancellable) {
+       this._iterateChildrenMatching(monthdir, this._MONTH_OR_DAY_VERSION_RE,
+                                     Lang.bind(this, function(srcpath, match, cancellable) {
+                                         this._loadDay(srcpath, results, cancellable);
+                                     }), cancellable);
+    },
+
+    _loadDay: function(daydir, results, cancellable) {
+       this._iterateChildrenMatching(daydir, this._YEAR_OR_SERIAL_VERSION_RE,
+                                     Lang.bind(this, function(srcpath, match, cancellable) {
+                                         results.push(this.pathToVersion(srcpath));
+                                     }), cancellable);
+    },
+
+    _convertLegacyLayout: function(cancellable) {
+       this._iterateChildrenMatching(this.path, this._YMD_SERIAL_VERSION_RE,
+                                     Lang.bind(this, function(srcpath, match, cancellable) {
+                                         let path = this._createPathForParsedVersion(match[1], match[2], 
match[3], match[4]);
+                                         print("convert " + srcpath.get_path() + " -> " + path.get_path());
+                                         GSystem.file_ensure_directory(path.get_parent(), true, cancellable);
+                                         GSystem.file_rename(srcpath, path, cancellable);
+                                     }), cancellable);
+    },
+
+    relpathToVersion: function(relpath) {
+       let parts = relpath.split('/');
+       return parts[0] + parts[1] + parts[2] + '.' + parts[3];
+    },
+
+    pathToVersion: function(path) {
+       return this.relpathToVersion(this.path.get_relative_path(path));
+    },
+
+    loadVersions: function(cancellable) {
+       if (this._cachedResults !== null)
+           return this._cachedResults;
+       let results = [];
+       this._convertLegacyLayout(cancellable);
+       this._iterateChildrenMatching(this.path, this._YEAR_OR_SERIAL_VERSION_RE,
+                                     Lang.bind(this, function(srcpath, match, cancellable) {
+                                         this._loadYear(srcpath, results, cancellable);
+                                     }), cancellable);
        results.sort(BuildUtil.compareVersions);
+       this._cachedResults = results;
        return results;
     },
 
-    cleanOldVersions: function(retain, cancellable) {
+    currentVersion: function(cancellable) {
        let versions = this.loadVersions(cancellable);
-       while (versions.length > retain) {
-           let child = this.path.get_child(versions.shift());
-           GSystem.shutil_rm_rf(child, cancellable);
-       }
+       if (versions.length > 0)
+           return versions[versions.length - 1];
+       return null;
     },
+
+    allocateNewVersion: function(cancellable) {
+        let currentTime = GLib.DateTime.new_now_utc();
+        let currentYmd = Format.vprintf('%d%02d%02d', [currentTime.get_year(),
+                                                       currentTime.get_month(),
+                                                       currentTime.get_day_of_month()]);
+       let versions = this.loadVersions(cancellable);
+       let newVersion = null;
+       if (versions.length > 0) {
+           let last = versions[versions.length-1];
+           let match = this._YMD_SERIAL_VERSION_RE.exec(last);
+           if (!match) throw new Error();
+            let lastYmd = match[1] + match[2] + match[3];
+            let lastSerial = match[4];
+            if (lastYmd == currentYmd) {
+                newVersion = currentYmd + '.' + (parseInt(lastSerial) + 1);
+            }
+       }
+       if (newVersion === null)
+           newVersion = currentYmd + '.0';
+       let path = this.createPathForVersion(newVersion);
+        GSystem.file_ensure_directory(path, true, cancellable);
+       this._cachedResults.push(newVersion);
+       return path;
+    }
 });


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