[Top][All Lists]

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


From: D Goel
Subject: shs-utils.el
Date: Fri, 29 Jul 2005 12:52:53 -0400
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

 shs-utils.el --- Commonly used commandline utils. 

This is the current development version of shs-utils.el.  shs-utils.el
is needed if you use beep.el's shell-interface, just posted.

The latest version can be had from .
;;;---------------- CUT HERE -------------------------------

;;; shs-utils.el --- Commonly used commandline utils. 
;; Time-stamp: <2005-07-11 16:53:51 deego>
;; Copyright (C) 2005 D. Goel
;; Emacs Lisp Archive entry
;; Filename: shs.el
;; Package: shs
;; Author: D. Goel <address@hidden>
;; Keywords:
;; Version:  0.0
;; URL:
;; For latest version:

(defconst shs-utils-home-page

;; Copyright (C) 2005 D. Goel

;; This file is NOT (yet) part of GNU Emacs.
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;; These are commonly used bash utils, to be used when shs has been started..

(require 'cl)
(require 'shs)

(defun shsu-username ()
  (shsh "/usr/bin/whoami"))

(defalias 'shsu-user 'shsu-username)
(defalias 'shsu-whoami 'shsu-username)

(defun shsu-start-stop (startfcn stopfcn &optional ssarg scriptname
                                 &rest fcnargs)
  "/etc/init.d type functionality. 
When callername is supplied, we use that in the error message. 

ssarg is the start-stop argument. 

fcnargs are any arguments to be passed to the function. 
   ((member ssarg '("start"))
    (apply startfcn fcnargs ))

   ((member ssarg '("stop"))
    (apply stopfcn fcnargs))
    (unless scriptname (setq scriptname "[scriptname]"))
     "Syntax: %s %s" scriptname "start/stop"))))

(defun shsu-mounted-p (string)
  "Return nil if string does not appear in mount output, else return
the matched lines. "
    (shsh (format "%s %s" "mount | grep" 
                  (shell-quote-argument (format "%s" string))))))

(defun shsu-as-root (string)
  "shsh on string.  If not root, append a sudo before the string.

Run a shell command indicated by string, but with sudo appended if
   ((string= (shsu-whoami) "root")
    (shsh string))
    (shsm "Your sudo should [1] either be password-expiry free")
    (shsm "[2] Else, make sure to reauthenticate before running this script,")
    (shsm ".. for example, by typing sudo echo hi")
    (shsm ".... else this script will fail")
    (shsh (format "%s %s" "sudo" string)))))

(defun shsu-directory-files (dir)
  "Removes . and .."
  (let ((f
          (remove ".." (directory-files dir)))))
     (lambda (f) (expand-file-name f dir)) f)))

(defun shsu-directory-files-dirs-files (dir)
  "Removes . and ..  List dirs first"
   (shsu-directory-files dir)

(defun shsu-sort-predicate-dirs-files (a b)
  "Dirs first, then symlinks to dirs, then files, then symlinks to files"
  (let ((ascore (shsu-sort-dirs-files-score a))
        (bscore (shsu-sort-dirs-files-score b)))
     ((< ascore bscore)
     ((= ascore bscore) (string-lessp a b))
     (t nil))))

(defun shsu-sort-dirs-files-score (f)
   ((file-directory-p f) 
    (if (file-symlink-p f) 1 0))
   (t (if (file-symlink-p f) 3 2))))

(defun shsu-funcall-recursive (f dir &optional predf0 predr0 predf2 predr2)
  "Apply function f to each non-directory in the root of the supplied 

More specifically, do so only when predf is satisfied.  By default,
  predf is true only when the file is a regular file, which is not a

Predrecurse (predr0 and predr2) tells when to descend.  By default,
  this is true only if the subdir is a regular directory, which is not
  a symlink.  Both predr0 and predr2 must be satisfied. 

predf0 and predr0 are meant to be specified by you for
situational-specific stuff in general.  Their default values is
basically absent.  Left blank for you.  If you want to just deal with
every file, just supply us nil for these.... For example, they could
be functions that check to see if the file already has a gz suffix.

If you are in the mood to overwrite our descending criteria, then use
predf2 and predr2, Predf2 and predr2 are ADDITIONAL predicates to be
satisfied.  These DO have default values which check for symlinks,
etc. . Use them if you DO want alter the default predicates, which are
predf0 and predr0.

  (require 'cl)
  (shsu-funcall-recursive1 f (file-truename dir) predf0 predr0 predf2 predr2))

(defun shsu-funcall-recursive1 (f dir &optional predf0 predr0 predf2 predr2)
  (unless predf0
    (setq predf0
          (lambda (arg)

  (unless predr0
    (setq predr0
          (lambda (arg)

  (unless predf2
    (setq predf2 
          (lambda (arg)
             (file-exists-p arg)
             (not (file-symlink-p arg))
             (not (file-directory-p arg))))))
  (unless predr2
    (setq predr2 
          (lambda (a)
             (file-exists-p a)
             (not (file-symlink-p a))
             (file-directory-p a)))))
      ((allf (shsu-directory-files dir))
       (dof (remove-if-not predf0 (remove-if-not predf2 (copy-tree allf))))
        (remove-if-not predr0 (remove-if-not predr2 (copy-tree allf)))))
     (mapcar f dof)
     (mapcar (lambda (dir)
               (shsu-funcall-recursive1 f dir predf0 predr0 predf2 predr2))

(defun shsu-gz-recursive (dir)
  "There will be ignored errors, for example, when there are hard
   (lambda (arg)
     (shsm "Gzipping %s..." arg)
     (shs-ignore-errors-flag (shsh (concat "/bin/gzip " arg)))
     (shsm "Gzipping %s...done\n" arg))
   (lambda (a)
     (let ((e (file-name-extension a)))
       (not (member e '("gz" "GZ" "txt")))))))

(defun shsu-sanitize-filename (file)
  "Remove all stupid MSDOS spaces from filenames in this file. 
You typically receive such files from windoze users.
Replace spaces by dashes"
  (let* ((fname (file-name-nondirectory file))
         (replacep (shs-ignore-errors-flag (string-match " " fname)))
         (dir (shs-ignore-errors-flag
               (and replacep (shs-ignore-errors-flag (file-name-directory 
           (and replacep (replace-regexp-in-string " " "-" fname))))
          (if dir 
              (and replacep (shs-expand-file-name fname2 
          (and replacep (concat "Move " (progn file) 
                                " to " (progn file2)))))
        (and replacep
      (rename-file file file2))))

(defun shsu-sanitize-filename-recursive (file)
  "See shsu-sanitize-filename.  Do this recursively. "
  (shsu-funcall-recursive #'shsu-sanitize-filename file))

(defun shsu-hostname ()
  (shsh "hostname"))

(provide 'shs-utils)
(provide 'shsu)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]