Henrik Tunedal avatar Henrik Tunedal committed b6ee195

Added simple interactive mode.

This mode lets you easily step through all the packages and view their
files by license using less(1).

For best results, add this to your ~/.lesskey:
#command
\n next-file

Comments (0)

Files changed (1)

             results.setdefault(license_type, []).append(filename)
     return results
 
+def print_check_summary(results, indent=0):
+    res = results.items()
+    for test,files in sorted(res,key=lambda i: len(i[1]),reverse=True):
+        if files:
+            print indent*" " + "%-14s %i files" % (test + ":", len(files))
+
 # Packages can be specified in three equivalent forms:
 # source dir, package id or metadata file.
 def interpret_package_name(name):
             pass
     raise ValueError, "invalid package name: '%s'" % name
 
+def interactive_mode(packages):
+    def print_info(meta, res):
+        print "%i/%i: %s " % (i+1, len(packages), meta["package id"])
+        print "Summary: %s" % meta["summary"]
+        print "License: %s" % meta["license"]
+        print "Website: %s" % meta["web site"]
+        print "File analysis:",
+        if sourcedir_for_id(meta["package id"]):
+            print
+            print_check_summary(res, 2)
+        else:
+            print "no source available"
+    packages = list(packages)
+    i = 0
+    while i < len(packages):
+        meta = metadata_for_id(packages[i])
+        res = check_package(meta["package id"])
+        print_info(meta, res)
+        while True:
+            print
+            try:
+                input = raw_input("Action [mnpqrvw]> ")
+            except EOFError:
+                input = "q"
+            if not input:
+                input = "n"
+            args = input.split()
+            action = args[0]
+            if action == "m":
+                print_info(meta, res)
+            elif action == "n":
+                i += 1; break
+            elif action == "p":
+                if i == 0:
+                    print "Already at first package."
+                else:
+                    i -= 1; break
+            elif action == "q":
+                print "Quit."
+                return
+            elif action == "r":
+                print "Re-licensing not implemented yet."
+            elif action == "v":
+                key = len(args) > 1 and args[1].strip().lower() or "unknown"
+                matches = [v for k, v in res.items() if k.lower() == key]
+                if matches:
+                    subprocess.call(["less"] + matches[0])
+                else:
+                    print "No files under that license."
+            elif action == "w":
+                print "Opening '%s'..." % meta["web site"]
+                import webbrowser
+                webbrowser.open(meta["web site"])
+            else:
+                print "Invalid command."
+
 def main():
     packages = [interpret_package_name(p) for p in args.packages]
     if args.download or args.update:
         clone_everything(packages, update=args.update)
+    elif args.interactive:
+        interactive_mode(
+            packages or
+            [p[:-4] for p in os.listdir(metadata_root) if p.endswith(".txt")])
     elif args.list:
         license_type = args.list.lower()
         for package in (packages or downloaded_packages()):
         for package in (packages or downloaded_packages()):
             meta = metadata_for_id(package)
             print "%s (%s)" % (meta["package id"], meta["license"])
-            res = check_package(meta["package id"], args.apps).items()
-            for test,files in sorted(res,key=lambda i: len(i[1]),reverse=True):
-                if files:
-                    print "  %-14s %i files" % (test + ":", len(files))
+            results = check_package(meta["package id"], args.apps)
+            print_check_summary(results, 2)
             print
 
 if __name__ == "__main__":
                         help='update application sources')
     parser.add_argument('-l', "--list", action="store", metavar="LICENSE",
                         help='list files with the specified license')
+    parser.add_argument('-i', "--interactive", action="store_true",
+                        help='interactive mode')
     parser.add_argument("packages", nargs="*", metavar="PACKAGE",
                       help='package to process (all packages if not specified)')
     args = parser.parse_args()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.