[arch-dev-public] [PATCH 3/4] Move script detection to Namcap.util

Dan McGee dan at archlinux.org
Tue Jun 15 01:29:25 EDT 2010


And also add the same chmod() business we do for the is_elf() checks. Of
course, a refactor is coming that will make for less code duplication.

Signed-off-by: Dan McGee <dan at archlinux.org>
---

After this patch, we can successfully parse both the sudo and udev
packages which were blowing up namcap before.

-Dan

 Namcap/depends.py |   20 +++++---------------
 Namcap/util.py    |   36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/Namcap/depends.py b/Namcap/depends.py
index 39cd508..ea03129 100644
--- a/Namcap/depends.py
+++ b/Namcap/depends.py
@@ -18,10 +18,7 @@
 # 
 
 import re, os, os.path, pacman, subprocess
-from Namcap.util import is_elf
-
-supported_scripts = ['python', 'perl', 'ruby', 'wish', 'expect',
-	'tk', 'bash', 'sh', 'dash', 'tcsh', 'pdksh' ]
+from Namcap.util import is_elf, script_type
 
 pkgcache = {}
 
@@ -67,9 +64,7 @@ def scanlibs(data, dirname, names):
 	If they depend on a library, store that library's path in sharedlibs
 	"""
 	sharedlibs, scripts = data
-	global supported_scripts
 	shared = re.compile('Shared library: \[(.*)\]')
-	script = re.compile('#!.*/(.*)')
 	for i in names:
 		path = dirname + '/' + i
 		if is_elf(path):
@@ -90,15 +85,10 @@ def scanlibs(data, dirname, names):
 						# We didn't know about the library, so add it for fail later
 						sharedlibs.setdefault(n.group(1), {})[path] = 1
 		# Maybe it is a script file
-		elif os.path.isfile(path):
-			fd = open(path)
-			firstline = fd.readline()
-			fd.close()
-			firstlinere = script.match(firstline)
-			if firstlinere != None:
-				cmdname = firstlinere.group(1).split()[0]
-				if cmdname in supported_scripts:
-					scripts.setdefault(cmdname, {})[path] = 1
+		else:
+			cmd = script_type(path)
+			if cmd != None:
+				scripts.setdefault(cmd, {})[path] = 1
 
 def finddepends(liblist):
 	dependlist = {}
diff --git a/Namcap/util.py b/Namcap/util.py
index 3d09212..4568891 100644
--- a/Namcap/util.py
+++ b/Namcap/util.py
@@ -54,6 +54,42 @@ def is_elf(path):
 	else:
 		return False
 
+supported_scripts = ['python', 'perl', 'ruby', 'wish', 'expect',
+	'tk', 'bash', 'sh', 'dash', 'tcsh', 'pdksh' ]
+
+def script_type(path):
+	global supported_scripts
+	if not os.path.isfile(path):
+		return None
+	reset_perms = False
+	if not os.access(path, os.R_OK):
+		# don't mess with links we can't read
+		if os.path.islink(path):
+			return None
+		reset_perms = True
+		# attempt to make it readable if possible
+		statinfo = os.stat(path)
+		newmode = statinfo.st_mode | stat.S_IRUSR
+		try:
+			os.chmod(path, newmode)
+		except IOError:
+			return None
+
+	fd = open(path)
+	firstline = fd.readline()
+	fd.close()
+	# reset permissions if necessary
+	if reset_perms:
+		# set file back to original permissions
+		os.chmod(path, statinfo.st_mode)
+	script = re.compile('#!.*/(.*)')
+	firstlinere = script.match(firstline)
+	if firstlinere != None:
+		cmdname = firstlinere.group(1).split()[0]
+		if cmdname in supported_scripts:
+			return cmdname
+	return None
+
 clean_filename = lambda s: re.search(r"/tmp/namcap\.[0-9]*/(.*)", s).group(1)
 
 # vim: set ts=4 sw=4 noet:
-- 
1.7.1



More information about the arch-dev-public mailing list