#!/usr/bin/python3 import lliurex.moving import sys import os import shutil import logging logging.basicConfig(format='%(levelname)s %(funcName)s %(message)s', level=logging.INFO) def print_usage(): print("") print("Usage: llx-moving-cmd ") print("") print("Available commands:") print("load loads profiles from server") print("save save profiles to server") print("show show rules") print("match list matched files") print( "test (load|save) performs a test without copying or deleting anything" ) print("purge (local|remote) purges either local or remote sync files") print("backup NAME creates a profile backup") print("help print this help") def main(): try: if (len(sys.argv) < 2): print_usage() else: if (sys.argv[1] == "load"): mp = lliurex.moving.Client() mp.load_session() elif (sys.argv[1] == "save"): mp = lliurex.moving.Client() mp.save_session() elif (sys.argv[1] == "show"): mp = lliurex.moving.Client() print("Include:") for name in mp.cfg["include"]: value = mp.cfg["include"][name] tab = " " * (32 - len(name)) print("* " + name + tab + "[" + value + "]") print("") print("Exclude:") for name in mp.cfg["exclude"]: value = mp.cfg["exclude"][name] tab = " " * (32 - len(name)) print("* " + name + tab + "[" + value + "]") elif (sys.argv[1] == "match"): mp = lliurex.moving.Client() home = os.path.expanduser("~") print("Matched files:") for fname in os.listdir(home): if (mp.match(fname)): print("->" + fname) elif (sys.argv[1] == "test" and len(sys.argv) > 2): if (sys.argv[2] == "load"): mp = lliurex.moving.Client() home = os.path.expanduser("~") moving_path = mp.get_profile_path() profile_files = os.listdir(moving_path) update_files = [] delete_files = [] append_files = [] for fname in os.listdir(home): if mp.match(fname): if fname not in profile_files: delete_files.append(fname) else: update_files.append(fname) home_files = os.listdir(home) for fname in profile_files: if (mp.match(fname)): if (not fname in home_files): append_files.append(fname) print("Local files updated:") for fname in update_files: print("->" + fname) print("Local files deleted:") for fname in delete_files: print("->" + fname) print("Local files appended:") for fname in append_files: print("->" + fname) elif (sys.argv[2] == "save"): mp = lliurex.moving.Client() home = os.path.expanduser("~") moving_path = mp.get_profile_path() home_files = os.listdir(home) update_files = [] delete_files = [] append_files = [] for fname in os.listdir(moving_path): if not fname in home_files: delete_files.append(fname) else: if (mp.match(fname)): update_files.append(fname) else: delete_files.append(fname) profile_files = os.listdir(moving_path) for fname in home_files: if (mp.match(fname)): if not fname in profile_files: append_files.append(fname) print("Remote files updated:") for fname in update_files: print("->" + fname) print("Remote files deleted:") for fname in delete_files: print("->" + fname) print("Remote files appended:") for fname in append_files: print("->" + fname) else: print_usage() elif (sys.argv[1] == "purge" and len(sys.argv) > 2): if (sys.argv[2] == "local"): mp = lliurex.moving.Client() home = os.path.expanduser("~") logging.info("Removing local files:") for fname in os.listdir(home): if (mp.match(fname)): fpath = home + os.path.sep + fname logging.info("-> " + fpath) if (os.path.isdir(fpath)): shutil.rmtree(fpath) if (os.path.isfile(fpath)): os.remove(fpath) logging.info("done") elif (sys.argv[2] == "remote"): mp = lliurex.moving.Client() moving_path = mp.get_profile_path() profile_files = os.listdir(moving_path) logging.info("Removing remote files:") for fname in profile_files: fpath = moving_path + os.path.sep + fname logging.info("-> " + fpath) if (os.path.isdir(fpath)): shutil.rmtree(fpath) if (os.path.isfile(fpath)): os.remove(fpath) logging.info("done") else: print_usage() elif (sys.argv[1] == "backup" and len(sys.argv) > 2): mp = lliurex.moving.Client() mp.backup(sys.argv[2]) elif (sys.argv[1] == "help"): print_usage() else: logging.warning("Unknown command :" + sys.argv[1]) except Exception as ex: logging.exception(ex) #exit gracefully even when something goes bad sys.exit(0) sys.exit(0) if __name__ == "__main__": main()