/usr/share/pyshared/gquilt_pkg/utils.py is in gquilt 0.25-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | ### Copyright (C) 2010 Peter Williams <peter_ono@users.sourceforge.net>
### This program 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; version 2 of the License only.
### This program is distributed in the hope that it will be useful,
### but WITHOUT ANY WARRANTY; without even the implied warranty of
### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
### GNU General Public License for more details.
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os
import os.path
import gobject
import subprocess
import signal
from gquilt_pkg import urlops
from gquilt_pkg import cmd_result
def path_relative_to_dir(fdir, path):
if not os.path.isdir(fdir):
return None
if fdir == path:
return os.curdir
lcwd = len(fdir)
if len(path) <= lcwd + 1 or fdir != path[0:lcwd] or path[lcwd] != os.sep:
return None
return path[lcwd + 1:]
def path_relative_to_playground(path):
return path_relative_to_dir(os.getcwd(), path)
HOME = os.path.expanduser("~")
def path_rel_home(path):
"""Return the given path as a path relative to user's home directory."""
if urlops.parse_url(path).scheme:
return path
path = os.path.abspath(path)
len_home = len(HOME)
if len(path) >= len_home and HOME == path[:len_home]:
path = "~" + path[len_home:]
return path
def cwd_rel_home():
"""Return path of current working directory relative to user's home
directory.
"""
return path_rel_home(os.getcwd())
def file_list_to_string(file_list):
"""Return the given list of file names as a single string:
- using a single space as a separator, and
- placing double quotes around file names that contain spaces.
"""
mod_file_list = []
for fname in file_list:
if fname.count(' ') == 0:
mod_file_list.append(fname)
else:
mod_file_list.append('"%s"' % fname)
return ' '.join(mod_file_list)
def string_to_file_list(string):
"""Return a list of the file names in the given string:
- assuming names are separated by spaces, and
- file names that contain spaces are inside double quotes.
"""
if string.count('"') == 0:
return string.split()
file_list = []
index = 0
lqi = string.rfind('"')
while index < lqi:
qib = string.find('"', index)
file_list += string[index:qib].split()
index = string.find('"', qib + 1) + 1
file_list.append(string[qib+1:index-1])
if index < len(string):
file_list += string[index:].split()
return file_list
# handle the fact os.path.samefile is not available on all operating systems
def samefile(filename1, filename2):
"""Return whether the given paths refer to the same file or not."""
try:
return os.path.samefile(filename1, filename2)
except AttributeError:
return os.path.abspath(filename1) == os.path.abspath(filename2)
def run_cmd(cmd, input_text=None):
"""Run the given command and report the outcome as a cmd_result tuple.
If input_text is not None pas it to the command as standard input.
"""
try:
oldterm = os.environ['TERM']
os.environ['TERM'] = "dumb"
except LookupError:
oldterm = None
is_posix = os.name == 'posix'
if is_posix:
savedsh = signal.getsignal(signal.SIGPIPE)
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
sub = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, shell=True, close_fds=is_posix, bufsize=-1)
outd, errd = sub.communicate(input_text)
if is_posix:
signal.signal(signal.SIGPIPE, savedsh)
if oldterm:
os.environ['TERM'] = oldterm
return cmd_result.Result(sub.returncode, outd, errd)
def _wait_for_bgnd_cmd_timeout(pid):
"""Callback to clean up after background tasks complete"""
try:
if os.name == 'nt' or os.name == 'dos':
rpid, _ = os.waitpid(pid, 0)
else:
rpid, _ = os.waitpid(pid, os.WNOHANG)
return rpid != pid
except OSError:
return False
def run_cmd_in_bgnd(cmd):
"""Run the given command in the background and poll for its exit using
_wait_for_bgnd_timeout() as a callback.
"""
if not cmd:
return False
pid = subprocess.Popen(string_to_file_list(cmd)).pid
if not pid:
return False
gobject.timeout_add(2000, _wait_for_bgnd_cmd_timeout, pid)
return True
if os.name == 'nt' or os.name == 'dos':
def _which(cmd):
"""Return the path of the executable for the given command"""
for dirpath in os.environ['PATH'].split(os.pathsep):
potential_path = os.path.join(dirpath, cmd)
if os.path.isfile(potential_path) and \
os.access(potential_path, os.X_OK):
return potential_path
return None
NT_EXTS = ['.bat', '.bin', '.exe']
def which(cmd):
"""Return the path of the executable for the given command"""
path = _which(cmd)
if path:
return path
_, ext = os.path.splitext(cmd)
if ext in NT_EXTS:
return None
for ext in NT_EXTS:
path = _which(cmd + ext)
if path is not None:
return path
return None
else:
def which(cmd):
"""Return the path of the executable for the given command"""
for dirpath in os.environ['PATH'].split(os.pathsep):
potential_path = os.path.join(dirpath, cmd)
if os.path.isfile(potential_path) and \
os.access(potential_path, os.X_OK):
return potential_path
return None
|