Package pybaz :: Module util
[frames] | no frames]

Source Code for Module pybaz.util

  1  # arch-tag: david@allouche.net - 2003-11-17 15:23:30 335369000 
  2  # Copyright (C) 2003 John Goerzen, David Allouche 
  3  # <jgoerzen@complete.org> <david@allouche.net> 
  4  # 
  5  #    This program is free software; you can redistribute it and/or modify 
  6  #    it under the terms of the GNU General Public License as published by 
  7  #    the Free Software Foundation; either version 2 of the License, or 
  8  #    (at your option) any later version. 
  9  # 
 10  #    This program is distributed in the hope that it will be useful, 
 11  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  #    GNU General Public License for more details. 
 14  # 
 15  #    You should have received a copy of the GNU General Public License 
 16  #    along with this program; if not, write to the Free Software 
 17  #    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 18   
 19  """Obsolete utility module 
 20  """ 
 21   
 22  import os 
 23  import re 
 24   
 25   
 26  # For compatibility 
 27  from errors import ExecProblem 
 28  from pathname import PathName, DirName, FileName 
 29  from backends.logger import Logger 
 30  from backends.forkexec import exec_safe 
 31  from backends.forkexec import exec_safe_status_stdout 
 32  from backends.forkexec import exec_safe_iter_stdout 
 33  from backends.forkexec import ChildProcess 
 34  from backends.forkexec import getnull 
 35  from backends.forkexec import StringOutput 
 36   
 37   
38 -def exec_safe_stdout(program, args=[], expected=0, chdir=None, logger=None):
39 status, output = exec_safe_status_stdout(program, args, expected, 40 chdir, logger) 41 return output
42 43
44 -def exec_safe_silent(program, args = [], expected = 0, chdir = None):
45 """Silently runs the specified program.""" 46 null = getnull() 47 proc = ChildProcess(program, args, expected, chdir) 48 proc.spawn(null, null, null) 49 proc.wait() 50 return proc.status
51 52
53 -def new_numbered_name(dir, prefix):
54 def int_tail(name): 55 if not name.startswith(prefix): 56 return 0 57 else: 58 tail = name[len(prefix):] 59 try: return int(tail) 60 except ValueError: return 0
61 62 N = max(map(None, map(int_tail, os.listdir(dir)))) 63 return dir/(prefix + str(N+1)) 64 65
66 -def maketree(path, addpath = DirName(), ignore = [], res = None):
67 """Sorted recursive listing of the directory at @path. 68 69 @ignore is a list of regular expression strings. Files and 70 directories which match any of these are skipped. 71 72 The listing is actually a list of DirName and FileName objects. 73 They are storted directories first, then files. 74 """ 75 retval = [] 76 others = [] 77 if res == None: 78 res = [re.compile(x) for x in ignore] 79 for item in os.listdir(path): 80 skip = 0 81 for retest in res: 82 if retest.search(item): 83 skip = 1 84 break 85 if skip: 86 continue 87 88 dirname = os.path.join(path, item) 89 if os.path.isdir(dirname): 90 item, dirname = DirName(item), DirName(dirname) 91 retval.append(addpath/item) 92 others.extend(maketree(dirname, addpath/item, res = res)) 93 else: 94 item = FileName(item) 95 retval.append(addpath/item) 96 return sorttree(retval + others)
97 98
99 -def sorttree(srctree):
100 dirs = [x for x in srctree if isinstance(x, DirName)] 101 files = [x for x in srctree if isinstance(x, FileName)] 102 dirs.sort() 103 files.sort() 104 return dirs + files
105 106
107 -def copyfrom(srcdir, destdir, verbose=False):
108 """Copy the contents of @srcdir into @destdir using a tar pipe. 109 110 All files in @srcdir will be copied into @destdir and files which 111 are unique to @destdir will be preserved. Sparse files and 112 permissions from @srcdir will be preserved. 113 114 The sparse file preservation feature causes a dependence on GNU tar. 115 """ 116 read_end, write_end = os.pipe() 117 verbargs = [] 118 if verbose: 119 verbargs.append('-v') 120 reader = ChildProcess("tar", ["-cSpf", "-", "."], chdir=srcdir) 121 reader.spawn(stdout = write_end, closefds = [read_end]) 122 writer = ChildProcess("tar", ["-xSpf", "-"] + verbargs, chdir=destdir) 123 writer.spawn(stdin = read_end, closefds = [write_end]) 124 os.close(read_end) 125 os.close(write_end) 126 reader.wait() 127 writer.wait()
128