Last active
August 29, 2015 13:57
-
-
Save ThomasChiroux/9677057 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
#!/usr/bin/env python | |
"""walk into the current directory and subdirs, looks for hg repos, checks | |
local and remote status and report into | |
""" | |
import os | |
import subprocess | |
max_depth = 2 | |
class HgError(Exception): | |
pass | |
def check_hg_repo(): | |
searched = ['commit:', 'update:', 'remote:'] | |
process = subprocess.Popen(['hg', 'summary', '--remote'], | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
error = [] | |
for line in process.stderr: | |
error.append(line) | |
if error: | |
raise HgError | |
result = [] | |
for line in process.stdout: | |
if line.decode('utf-8')[:7] in searched: | |
result.append(line.decode('utf-8').strip()) | |
ok = False | |
for line in result: | |
if line.startswith('commit:'): | |
if 'clean' not in line: | |
break | |
elif line.startswith('update:'): | |
if 'current' not in line: | |
break | |
elif line.startswith('remotr:'): | |
if 'synced' not in line: | |
break | |
else: | |
ok = True | |
if ok: | |
return ['OK'] | |
else: | |
result.insert(0, 'NOK') | |
return result | |
def walk_dir(path, depth): | |
if depth > max_depth: | |
return | |
for dirname in os.listdir(path): | |
current_path = os.path.join(path, dirname) | |
try: | |
os.chdir(current_path) | |
except NotADirectoryError: | |
pass | |
else: | |
try: | |
result = check_hg_repo() | |
if result[0] == 'OK': | |
print("%s: OK" % current_path) | |
else: | |
print("%s: NOK: %s, %s, %s" % (current_path, | |
result[1], | |
result[2], | |
result[3])) | |
# do not walk in subdirs in hg found in current dir | |
except HgError: | |
walk_dir(current_path, depth+1) | |
def main(): | |
startup_dir = os.getcwd() | |
walk_dir(startup_dir, 0) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment