-
-
Save anarcat/6502988 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type RemoteName = String | |
type FileName = String | |
type Trusted = Bool | |
type Present = Bool | |
headerWhereis :: [(RemoteName, Trusted)] -> String | |
headerWhereis remotes = (unlines $ zipWith format [0..] remotes) ++ (pipes (length remotes)) | |
where | |
format n (name, trusted) = (pipes n) ++ "," ++ name ++ (trust trusted) | |
pipes = flip replicate '|' | |
trust True = "" | |
trust False = " (untrusted)" | |
formatWhereis :: [(Trusted, Present)] -> FileName -> String | |
formatWhereis remotes file = thereMap ++ " " ++ (show $ length $ filter trustedPresent remotes) ++ status ++ " " ++ file | |
where | |
thereMap = concatMap there remotes | |
there (True, True) = "X" | |
there (False, True) = "x" | |
there (_, False) = "_" | |
untrustedPresent (t, p) = not t && p | |
trustedPresent (t, p) = t && p | |
-- we can also use import Data.Text.Lazy.justifyLeft, but then we need to pack/unpack the string | |
justifyLeft n s = s ++ replicate (n - length s) ' ' | |
status = justifyLeft 2 $ (missing $ filter trustedPresent remotes) ++ (more $ filter untrustedPresent remotes) | |
more [] = "" | |
more _ = "+" | |
-- XXX: this should be dependent on numCopies | |
missing (_:_:_) = "" | |
missing _ = "!" | |
-- this should output: | |
-- ,here | |
-- |,home | |
-- ||,cabin (untrusted) | |
-- ||| | |
-- XXx 2+ img.png | |
-- _X_ 1! bigfile | |
-- XX_ 2 zort | |
-- __x 0+! maybemissing | |
-- that would be a good unit test! | |
main = do | |
putStrLn $ headerWhereis [("here", True), ("home", True), ("cabin", False)] | |
putStrLn $ formatWhereis [(True, True), (True, True), (False, True)] "img.png" | |
putStrLn $ formatWhereis [(False, False), (True, True), (False, False)] "bigfile" | |
putStrLn $ formatWhereis [(True, True), (True, True), (False, False)] "zort" | |
putStrLn $ formatWhereis [(False, False), (False, False), (False, True)] "maybemissing" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import subprocess | |
import sys | |
# http://stackoverflow.com/questions/566746/how-to-get-console-window-width-in-python | |
rows, cols = subprocess.check_output('stty size'.split()).split() | |
rows = int(rows) | |
cols = int(cols) | |
def list_repos(): | |
"""list the repositories in advance so we can print the header""" | |
repos = {} | |
output = subprocess.Popen('git annex status --fast'.split(), stdout=subprocess.PIPE) | |
line = output.stdout.readline() | |
while line: | |
if ' -- ' in line: | |
# found a repo, set it in the array | |
repos[" ".join(line.split(" -- ")[-1].split())] = [] | |
if 'transfers in progress' in line: | |
break | |
line = output.stdout.readline() | |
output.terminate() # don't wait for a potentially slow operation, --fast seems to hang sometimes | |
return repos | |
def print_repos(repos): | |
"""print the header""" | |
for i in range(len(repos)): | |
print(i * "|" + "," + repos.keys()[i]) | |
print(len(repos) * "|") | |
def print_path(repos, path): | |
"""print a specific path's location details""" | |
line = "" | |
for repo in repos: | |
if path in repos[repo]: | |
line += "X" | |
else: | |
line += "_" | |
line += " " + path | |
print(line) | |
def parse_whereis(repos): | |
"""parse output of git annex whereis and display one line per file with its status""" | |
repos_empty = repos | |
command = 'git annex whereis'.split() + sys.argv[1:] | |
output = subprocess.Popen(command, stdout=subprocess.PIPE) | |
paths = [] | |
lines = 0 # printed lines | |
for line in output.stdout: | |
if line.startswith("ok"): | |
print_path(repos, path) | |
repos = repos_empty | |
paths = [] | |
lines += 1 | |
if lines + len(repos.keys()) + 1 >= rows: | |
print_repos(repos) | |
lines = 0 | |
continue | |
if line.startswith("whereis"): | |
path = " ".join(line.split(" ")[1:-3]) | |
paths.append(path) | |
continue | |
repo = " ".join(line.split(" -- ")[-1].split()) | |
repos[repo].append(path) | |
repos = list_repos() | |
print_repos(repos) | |
parse_whereis(repos) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment