Commit 0a79b418bfa1057094f5f0270e459503d4bd5d63

Thomas de Grivel 2023-02-20T12:43:43

rbpkg clone, rbpkg clean-sources

diff --git a/.gitignore b/.gitignore
index 4f00cd9..2c0cdd0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,6 @@
+/bin/
+/lib/
 /src/
+/var/lock/
+/var/log/
+
diff --git a/bin/rbpkg b/bin/rbpkg
index 7cceb0a..b69f8bc 100755
--- a/bin/rbpkg
+++ b/bin/rbpkg
@@ -1,6 +1,13 @@
 #!/usr/bin/env ruby
 require __FILE__ + '/../../lib/rbpkg'
 
+Rbpkg.init()
+Rbpkg.lock("rbpkg")
+
+log_path = Rbpkg.log_path("rbpkg")
+$log = File.open(log_path, "w")
+#verbose 2, "Logging to #{log_path}"
+
 def usage()
   STDERR.puts """Usage: #{File.basename($0)} COMMAND PKG ...
 
@@ -8,12 +15,7 @@ Source directory commands :
  clone            shortcut for git clone
  clean-sources    remove source directory
 """
-  1
-end
-
-def cmd(string)
-  verbose(1, string)
-  `#{string}` || exit(1)
+  exit 1
 end
 
 def rbpkg(args)
@@ -33,21 +35,4 @@ def rbpkg(args)
   end
 end
 
-def sh_quote (str)
-  if str.scan("\n") == []
-    if str.match?(/^[-+\/=.,:^_0-9A-Za-z]*$/)
-      str
-    else
-      "\"" + str.gsub(/([$`\\\"])/, "\\\1") + "\""
-    end
-  end
-end
-      
-def verbose(level, msg)
-  case level
-  when 1
-    puts "\33[0;34m#{msg}\33[0m\n"
-  end
-end
-
 rbpkg ARGV
diff --git a/etc/profile b/etc/profile
new file mode 100644
index 0000000..f577a58
--- /dev/null
+++ b/etc/profile
@@ -0,0 +1,4 @@
+: ${RBPKG_DIR:=~/rbpkg}
+export LD_LIBRARY_PATH="${RBPKG_DIR}/lib:${LD_LIBRARY_PATH}"
+export PATH="${RBPKG_DIR}/bin:$PATH"
+export RBPKG_DIR
diff --git a/lib/rbpkg.rb b/lib/rbpkg.rb
index 8307584..57d8419 100644
--- a/lib/rbpkg.rb
+++ b/lib/rbpkg.rb
@@ -1,3 +1,4 @@
+require 'time'
 require __FILE__ + '/../rbpkg/repos'
 
 module Rbpkg
@@ -8,17 +9,92 @@ module Rbpkg
     end
   end
 
+  @@date = nil
+  def self.date
+    @@date || @@date = Time.now.utc.iso8601
+  end
+
   def self.dir
     ENV["RBPKG_DIR"] || "#{ENV["HOME"]}/rbpkg"
   end
 
+  def self.ensure_dir(path)
+    if ! File.directory?(path)
+      cmd "mkdir -p #{sh_quote(path)}"
+    end
+  end
+
   def self.git_clone(repos)
     repos.each do |name|
       Repos.repo(name).git_clone()
     end
   end
 
+  def self.init
+    [dir,
+     lock_dir,
+     log_dir
+    ].each do |d|
+      ensure_dir(d)
+    end
+  end
+
+  def self.lock(name)
+    path = lock_path(name)
+    #verbose(2, "Locking #{path}")
+    while File.file?(path)
+      sleep 1000.ms
+    end
+    File.write(path, "")
+    at_exit { FileUtils.rm(path) }
+  end
+
+  def self.lock_dir
+    "#{dir}/var/lock"
+  end
+
+  def self.lock_path(name)
+    "#{lock_dir}/#{name}.lock"
+  end
+
+  def self.log_dir
+    "#{dir}/var/log/shpkg"
+  end
+
+  def self.log_path(name)
+    "#{log_dir}/#{name}_#{$$}_#{date}.log"
+  end
+
   def self.src_dir
     "#{dir}/src"
   end
 end
+
+def cmd(string)
+  verbose(1, string)
+  IO.popen(string, "r") do |pipe|
+    while line = pipe.gets()
+      $log.puts line
+      puts line
+    end
+  end
+end
+
+def sh_quote (str)
+  if str.scan("\n") == []
+    if str.match?(/^[-+\/=.,:^_0-9A-Za-z]*$/)
+      str
+    else
+      "\"" + str.gsub(/([$`\\\"])/, "\\\1") + "\""
+    end
+  end
+end
+      
+def verbose(level, msg)
+  case level
+  when 1
+    puts "\33[0;34m#{msg}\33[0m\n"
+  when 2
+    puts "\33[0;35m#{msg}\33[0m\n"
+  end
+end
diff --git a/lib/rbpkg/repo.rb b/lib/rbpkg/repo.rb
index 6edd265..5484800 100644
--- a/lib/rbpkg/repo.rb
+++ b/lib/rbpkg/repo.rb
@@ -2,16 +2,20 @@ require 'fileutils'
 
 class Rbpkg::Repo
 
+  def clean_sources
+    if File.directory?(src_dir)
+      cmd "rm -rf #{sh_quote(src_dir)}"
+    end
+    if File.directory?(src_parent_dir) && Dir.new(src_parent_dir).children == []
+      cmd "rmdir #{sh_quote(src_parent_dir)}"
+    end
+  end
+
   def dir
     "#{File.basename(File.dirname(git_url))}/#{name}"
   end
 
   def git_clone
-    puts "name: #{name.inspect}"
-    puts "dir: #{dir.inspect}"
-    puts "git_url: #{git_url.inspect}"
-    puts "src_dir: #{src_dir.inspect}"
-    puts "src_parent_dir: #{src_parent_dir.inspect}"
     if File.directory?(src_dir)
       false
     else
@@ -26,6 +30,16 @@ class Rbpkg::Repo
     raise "no git url for #{self}"
   end
 
+  def inspect
+    """%Repo{
+ name: #{name.inspect}
+ dir: #{dir.inspect}
+ git_url: #{git_url.inspect}
+ src_dir: #{src_dir.inspect}
+ src_parent_dir: #{src_parent_dir.inspect}
+}"""
+  end
+
   def name
     self.class.name.to_s.downcase.scan(/::([^:]*)$/)[0][0]
   end