summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Gnoutcheff <gnoutchd@softwarefreedom.org>2016-06-29 16:36:14 -0400
committerDaniel Gnoutcheff <gnoutchd@softwarefreedom.org>2016-06-29 16:36:14 -0400
commit4c86254bd0af752727e56ff984d189064ccf2a88 (patch)
tree539073e2d4703afb5d7ad0bb9202e8ae35260eeb
parentd013f8534767cf906d55076797afd50df14733af (diff)
Split AVFS stuff into its own module.
-rw-r--r--.gitignore1
-rw-r--r--avfs.py110
-rwxr-xr-xsuspicious101
3 files changed, 114 insertions, 98 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..bee8a64
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+__pycache__
diff --git a/avfs.py b/avfs.py
new file mode 100644
index 0000000..fda6c72
--- /dev/null
+++ b/avfs.py
@@ -0,0 +1,110 @@
+#!/usr/bin/env python3
+
+"""
+Traversing directories and archives with AVFS/fuse
+"""
+
+import os
+import os.path
+import subprocess
+
+avfs_sys_mount = os.environ['HOME'] + '/.avfs'
+avfs_started = False
+
+def sys_name(fpath):
+ global avfs_started
+ if not avfs_started:
+ subprocess.check_call(['mountavfs'])
+ avfs_started = True
+ return avfs_sys_mount + os.path.abspath(fpath)
+
+def exists(fpath):
+ return os.path.exists(sys_name(fpath))
+
+def isdir(fpath):
+ return os.path.isdir(sys_name(fpath))
+
+orig_open = open
+def open(fpath, *pargs, **kwargs):
+ return orig_open(sys_name(fpath), *pargs, **kwargs)
+
+# AVFS has its own automatic view selection using file extensions, but it
+# includes plugins (like #patch) that will lead us into an infinite loop
+# if we try to do a directory traversal. Also, there are a few
+# extensions we want to add.
+
+avfscmds = {
+ ('.gz', '#ugz'),
+ ('.tgz', '#ugz#utar'),
+ ('.tar.bz2', '#ubz2#utar'),
+ ('.bz2', '#ubz2'),
+ ('.bz', '#ubz2'),
+ ('.tbz2', '#ubz2#utar'),
+ ('.tbz', '#ubz2#utar'),
+ ('.Z', '#uz'),
+ ('.tpz', '#uz#utar'),
+ ('.tz', '#uz#utar'),
+ ('.taz', '#uz#utar'),
+ ('.a', '#uar'),
+ ('.deb', '#uar'),
+ ('.tar', '#utar'),
+ ('.gem', '#utar'), # Add upstream
+ ('.rar', '#urar'),
+ ('.sfx', '#urar'),
+ ('.zip', '#uzip'),
+ ('.jar', '#uzip'),
+ ('.ear', '#uzip'),
+ ('.war', '#uzip'),
+ ('.nupkg', '#uzip'), # Add upstream
+ ('.whl', '#uzip'), # Add upstream
+ ('.7z', '#u7z'),
+ ('.zoo', '#uzoo'),
+ ('.lha', '#ulha'),
+ ('.lhz', '#ulha'),
+ ('.arj', '#uarj'),
+ ('.cpio', '#ucpio'),
+ ('.rpm', '#rpm'),
+ ('.tar.xz', '#uxze#utar'),
+ ('.txz', '#uxze#utar'),
+ ('.xz', '#uxze'),
+ ('.lzma', '#uxze'),
+}
+
+def guesscmd(filename):
+ for ext, cmd in avfscmds:
+ if filename.endswith(ext):
+ return cmd + guesscmd(filename[:-len(ext)])
+ return ''
+
+def find(rootdir, prunedirs):
+ """
+ Recursively list all files under rootdir, including files in archives
+ supported by AVFS.
+ """
+
+ sys_rootdir = sys_name(rootdir)
+
+ for name in os.listdir(sys_rootdir):
+ path = rootdir + '/' + name
+ sys_path = sys_rootdir + '/' + name
+
+ if os.path.isdir(sys_path):
+ if name not in prunedirs:
+ yield from find(path, prunedirs)
+ else:
+ cmd = guesscmd(name)
+ filtered_path = path + cmd
+ sys_filtered_path = sys_path + cmd
+
+ if cmd and os.path.exists(sys_filtered_path):
+ if os.path.isdir(sys_filtered_path):
+ yield from find(filtered_path, prunedirs)
+ else:
+ yield filtered_path
+ else:
+ yield path
+
+if __name__ == "__main__":
+ import sys
+ for f in find(sys.argv[1], {'.git'}):
+ print(f)
diff --git a/suspicious b/suspicious
index 4d62ad9..9f6e6ea 100755
--- a/suspicious
+++ b/suspicious
@@ -32,102 +32,7 @@ import sys
import datetime
import string
import subprocess
-
-# AVFS stuff ------------------------------------------------------------------
-
-avfs_sys_mount = os.environ['HOME'] + '/.avfs'
-avfs_started = False
-def avfs_sys_name(fpath):
- if not avfs_started:
- subprocess.check_call(['mountavfs'])
- return avfs_sys_mount + os.path.abspath(fpath)
-
-def avfs_exists(fpath):
- return os.path.exists(avfs_sys_name(fpath))
-
-def avfs_isdir(fpath):
- return os.path.isdir(avfs_sys_name(fpath))
-
-def avfs_open(fpath, *pargs, **kwargs):
- return open(avfs_sys_name(fpath), *pargs, **kwargs)
-
-# AVFS has its own automatic view selection using file extensions, but it
-# includes plugins (like #patch) that will lead us into an infinite loop
-# if we try to do a directory traversal. Also, there are a few
-# extensions we want to add.
-
-avfscmds = {
- ('.gz', '#ugz'),
- ('.tgz', '#ugz#utar'),
- ('.tar.bz2', '#ubz2#utar'),
- ('.bz2', '#ubz2'),
- ('.bz', '#ubz2'),
- ('.tbz2', '#ubz2#utar'),
- ('.tbz', '#ubz2#utar'),
- ('.Z', '#uz'),
- ('.tpz', '#uz#utar'),
- ('.tz', '#uz#utar'),
- ('.taz', '#uz#utar'),
- ('.a', '#uar'),
- ('.deb', '#uar'),
- ('.tar', '#utar'),
- ('.gem', '#utar'), # Add upstream
- ('.rar', '#urar'),
- ('.sfx', '#urar'),
- ('.zip', '#uzip'),
- ('.jar', '#uzip'),
- ('.ear', '#uzip'),
- ('.war', '#uzip'),
- ('.nupkg', '#uzip'), # Add upstream
- ('.whl', '#uzip'), # Add upstream
- ('.7z', '#u7z'),
- ('.zoo', '#uzoo'),
- ('.lha', '#ulha'),
- ('.lhz', '#ulha'),
- ('.arj', '#uarj'),
- ('.cpio', '#ucpio'),
- ('.rpm', '#rpm'),
- ('.tar.xz', '#uxze#utar'),
- ('.txz', '#uxze#utar'),
- ('.xz', '#uxze'),
- ('.lzma', '#uxze'),
-}
-
-def avfs_guesscmd(filename):
- for ext, cmd in avfscmds:
- if filename.endswith(ext):
- return cmd + avfs_guesscmd(filename[:-len(ext)])
- return ''
-
-def avfs_find(rootdir, prunedirs):
- """
- Recursively list all files under rootdir, including files in archives
- supported by AVFS.
- """
-
- sys_rootdir = avfs_sys_name(rootdir)
-
- for name in os.listdir(sys_rootdir):
- path = rootdir + '/' + name
- sys_path = sys_rootdir + '/' + name
-
- if os.path.isdir(sys_path):
- if name not in prunedirs:
- yield from avfs_find(path, prunedirs)
- else:
- cmd = avfs_guesscmd(name)
- filtered_path = path + cmd
- sys_filtered_path = sys_path + cmd
-
- if cmd and os.path.exists(sys_filtered_path):
- if os.path.isdir(sys_filtered_path):
- yield from avfs_find(filtered_path, prunedirs)
- else:
- yield filtered_path
- else:
- yield path
-
-# -----------------------------------------------------------------------------
+import avfs
report = {}
wordscore = {}
@@ -383,7 +288,7 @@ if options.displaysummary and options.summaryfile:
prunedirs = {'CVS', '.git', '.bzr', '.hg', '.svn'}
if len(args) > 0:
for a in args:
- filelist.extend(avfs_find(a, prunedirs))
+ filelist.extend(avfs.find(a, prunedirs))
start = datetime.datetime.now()
for file in filelist:
@@ -391,7 +296,7 @@ for file in filelist:
skipped += 1
continue
try:
- f = avfs_open(file)
+ f = avfs.open(file)
except:
print("failed to open: " + file)
continue