From 98ef81153bfccee855aafa249e87d2959274dd1f Mon Sep 17 00:00:00 2001
From: Philipp Gesang <phg@phi-gamma.net>
Date: Mon, 27 Apr 2015 08:17:35 +0200
Subject: [import] prepare our own packaging

---
 scripts/mkimport | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 62 insertions(+), 4 deletions(-)

(limited to 'scripts/mkimport')

diff --git a/scripts/mkimport b/scripts/mkimport
index 2f64d62..313185e 100644
--- a/scripts/mkimport
+++ b/scripts/mkimport
@@ -31,6 +31,7 @@ local ioloaddata   = io.loaddata
 local iowrite      = io.write
 local md5sumhexa   = md5.sumhexa
 local stringformat = string.format
+local tableconcat  = table.concat
 
 -------------------------------------------------------------------------------
 -- config
@@ -632,6 +633,61 @@ local tell = function (arg)
   return describe (target, location)
 end
 
+--[[doc--
+
+  Packaging works as follows:
+
+      * Files are looked up the usual way, allowing us to override the
+        distribution-supplied scripts with our own alternatives in the
+        local path.
+
+      * The merged package is written to the same directory as the
+        packaging script (not ``$PWD``).
+
+  There is some room for improvements: Instead of reading a file with
+  fixed content from disk, the merge script could be composed
+  on-the-fly from a list of files and then written to memory (not sure
+  though if we can access shm_open or memfd and the likes from Lua).
+
+--doc]]--
+
+local package = function (args)
+  local orig_dir    = lfs.currentdir ()
+  local base_dir    = orig_dir .. "/src/fontloader/"
+  local merge_name  = base_dir .. "luaotfload-package.lua"
+  --- output name is fixed so we have to deal with it but maybe we can
+  --- get a patch to mtx-package upstreamed in the future
+  local output_name = base_dir .. "luaotfload-package-merged.lua"
+  local target_name = stringformat ("fontloader-%s.lua",
+                                    os.date ("%F"))
+  status ("assuming fontloader source in      %s", base_dir)
+  status ("reading merge instructions from    %s", merge_name)
+  status ("writing output to                  %s", target_name)
+
+  --- check preconditions
+
+  if not lfs.isdir (base_dir)           then die ("directory %s does not exist", emphasis (base_dir   )) end
+  if not lfs.isfile (merge_name)        then die ("missing merge file at %s",    emphasis (merge_name )) end
+  if not file.is_writable (output_name) then die ("cannot write to %s",          emphasis (output_name)) end
+  if not file.is_writable (target_name) then die ("cannot write to %s",          emphasis (target_name)) end
+  if not lfs.chdir (base_dir)           then die ("failed to cd into %s",        emphasis (base_dir   )) end
+
+  --- perform merge
+
+  local cmd = { "mtxrun", "--script", "package", "--merge", merge_name }
+  status ("invoking %s as “%s”",
+          emphasis "mtx-package",
+          tableconcat (cmd, " "))
+
+  --- check postconditions
+
+  --- clean up
+
+  if not lfs.chdir (orig_dir) then
+    status ("warning: failed to cd retour into %s", emphasis (orig_dir))
+  end
+end
+
 local help = function ()
   iowrite "usage: mkimport  <command> [<args>]\n"
   iowrite "\n"
@@ -640,14 +696,16 @@ local help = function ()
   iowrite "   tell      Display information about a file’s integration\n"
   iowrite "   news      Check Context for updated files\n"
   iowrite "   import    Update with files from Context\n"
+  iowrite "   package   Invoke mtx-package on the current fontloader\n"
   iowrite "\n"
 end
 
 local job_kind = table.mirrored {
-  news   = news,
-  import = import,
-  tell   = tell,
-  help   = help,
+  help    = help,
+  import  = import,
+  news    = news,
+  package = package,
+  tell    = tell,
 }
 
 -------------------------------------------------------------------------------
-- 
cgit v1.2.3


From a5a66bb26ea893b627941d1d7bf63143b61b8d91 Mon Sep 17 00:00:00 2001
From: Philipp Gesang <phg@phi-gamma.net>
Date: Wed, 29 Apr 2015 00:04:33 +0200
Subject: [import] implement packaging step

---
 scripts/mkimport | 91 ++++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 69 insertions(+), 22 deletions(-)

(limited to 'scripts/mkimport')

diff --git a/scripts/mkimport b/scripts/mkimport
index 313185e..a430587 100644
--- a/scripts/mkimport
+++ b/scripts/mkimport
@@ -20,6 +20,8 @@
 ---    
 -------------------------------------------------------------------------------
 
+local debug = false
+
 kpse.set_program_name "luatex"
 
 local lfs = require "lfs"
@@ -27,11 +29,17 @@ local md5 = require "md5"
 
 require "lualibs"
 
-local ioloaddata   = io.loaddata
-local iowrite      = io.write
-local md5sumhexa   = md5.sumhexa
-local stringformat = string.format
-local tableconcat  = table.concat
+local fileiswritable   = file.is_writable
+local ioloaddata       = io.loaddata
+local iopopen          = io.popen
+local iowrite          = io.write
+local lfschdir         = lfs.chdir
+local lfsisdir         = lfs.isdir
+local lfsisfile        = lfs.isfile
+local md5sumhexa       = md5.sumhexa
+local osgettimeofday   = os.gettimeofday
+local stringformat     = string.format
+local tableconcat      = table.concat
 
 -------------------------------------------------------------------------------
 -- config
@@ -213,7 +221,7 @@ local imports = {
 } --[[ [imports] ]]
 
 local hash_file = function (fname)
-  if not lfs.isfile (fname) then
+  if not lfsisfile (fname) then
     die ("cannot find %s.", fname)
   end
   local raw = ioloaddata (fname)
@@ -226,7 +234,7 @@ end
 local derive_category_path = function (cat)
   local subpath  = origin_paths[cat] or die ("category " .. cat .. " unknown")
   local location = file.join (context_root, subpath)
-  if not lfs.isdir (location) then
+  if not lfsisdir (location) then
     die ("invalid base path defined for category "
          .. cat .. " at " .. location)
   end
@@ -332,7 +340,7 @@ local news = function ()
         status.missing[#status.missing + 1] = ourname
       else
         --- Source file exists and is readable.
-        if not lfs.isdir (fontloader_subdir) then
+        if not lfsisdir (fontloader_subdir) then
           die ("path for fontloader tree ("
               .. fontloader_subdir .. ") is not a directory")
         end
@@ -429,12 +437,12 @@ local import_file = function (name, kind, def, cat)
   local ourpath  = file.join (fontloader_subdir, subdir)
   local src      = file.join (srcdir, fullname)
   local dst      = file.join (ourpath, ourname)
-  local new      = not lfs.isfile (dst)
+  local new      = not lfsisfile (dst)
   if not new and hash_file (src) == hash_file (dst) then
     status ("file %s is unchanged, skipping", fullname)
     return import_skipped
   end
-  if not (lfs.isdir (ourpath) or not lfs.mkdirs (ourpath)) then
+  if not (lfsisdir (ourpath) or not lfs.mkdirs (ourpath)) then
     die ("failed to create directory %s for file %s",
          ourpath, ourname)
   end
@@ -476,7 +484,7 @@ end --[[ [local import = function (arg)] ]]
 
 local find_in_path = function (root, subdir, target)
   local file = file.join (root, subdir, target)
-  if lfs.isfile (file) then
+  if lfsisfile (file) then
     return file
   end
 end
@@ -555,7 +563,7 @@ end
 local search = function (target)
   local look_for
   --- pick a file
-  if lfs.isfile (target) then --- absolute path given
+  if lfsisfile (target) then --- absolute path given
     look_for = target
     goto found
   else
@@ -652,6 +660,7 @@ end
 --doc]]--
 
 local package = function (args)
+  local t0          = osgettimeofday ()
   local orig_dir    = lfs.currentdir ()
   local base_dir    = orig_dir .. "/src/fontloader/"
   local merge_name  = base_dir .. "luaotfload-package.lua"
@@ -666,26 +675,64 @@ local package = function (args)
 
   --- check preconditions
 
-  if not lfs.isdir (base_dir)           then die ("directory %s does not exist", emphasis (base_dir   )) end
-  if not lfs.isfile (merge_name)        then die ("missing merge file at %s",    emphasis (merge_name )) end
-  if not file.is_writable (output_name) then die ("cannot write to %s",          emphasis (output_name)) end
-  if not file.is_writable (target_name) then die ("cannot write to %s",          emphasis (target_name)) end
-  if not lfs.chdir (base_dir)           then die ("failed to cd into %s",        emphasis (base_dir   )) end
+  if not lfsisdir (base_dir)          then die ("directory %s does not exist", emphasis (base_dir   )) end
+  if not lfsisfile (merge_name)       then die ("missing merge file at %s",    emphasis (merge_name )) end
+  if not fileiswritable (output_name) then die ("cannot write to %s",          emphasis (output_name)) end
+  if not fileiswritable (target_name) then die ("cannot write to %s",          emphasis (target_name)) end
+  if not lfschdir (base_dir)          then die ("failed to cd into %s",        emphasis (base_dir   )) end
+
+  if lfsisfile (output_name) then
+    status ("output file already exists at “%s”, unlinking", output_name)
+    local ret, err = os.remove (output_name)
+    if ret == nil then
+      if not lfschdir (orig_dir) then
+        status ("warning: failed to cd retour into %s", emphasis (orig_dir))
+      end
+      die ("failed to remove existing merge package")
+    end
+  end
+    --die ("missing merge file at %s",    emphasis (merge_name )) end
 
   --- perform merge
 
   local cmd = { "mtxrun", "--script", "package", "--merge", merge_name }
-  status ("invoking %s as “%s”",
-          emphasis "mtx-package",
-          tableconcat (cmd, " "))
+  local shl = tableconcat (cmd, " ")
 
-  --- check postconditions
+  status ("invoking %s as “%s”", emphasis "mtx-package", shl)
+
+  local fh = iopopen (shl, "r")
+
+  if not fh then
+    if not lfschdir (orig_dir) then
+      status ("warning: failed to cd retour into %s", emphasis (orig_dir))
+    end
+    die ("merge failed; failed to invoke mtxrun")
+  end
+
+  local junk = fh.read (fh, "*all")
+  if not junk then
+    status ("warning: received no output from mtxrun; this is strange")
+  end
+
+  fh.close (fh)
+
+  if debug then print (junk) end
 
   --- clean up
 
-  if not lfs.chdir (orig_dir) then
+  if not lfschdir (orig_dir) then
     status ("warning: failed to cd retour into %s", emphasis (orig_dir))
   end
+
+  --- check postconditions
+
+  if not lfsisfile (output_name) then die ("merge failed; package not found at " .. output_name)   end
+
+  --- at this point we know that mtxrun was invoked correctly and the
+  --- result file has been created
+
+  status ("merge complete; operation finished in %.0f ms",
+          (osgettimeofday() - t0) * 1000)
 end
 
 local help = function ()
-- 
cgit v1.2.3