Esempio n. 1
0
def migrate(command_relevants):
    """Move a file not managed with dotlink to the dotlink directory"""

    dotlink_dir = get_dotlink_dir()

    # Path of file to be moved to dotlink directory
    src = to_specific_path(command_relevants["<src>"])

    # Will create error if dotlink_dir/dest does not exist by default
    dest = os.path.join(dotlink_dir, command_relevants["<dest>"] or os.path.basename(src))

    # Docopt like options passed to link command
    options = {
            "<target>": dest,
            "<link_name>": src,
            "-s": command_relevants["-s"]
        }

    with open(os.path.join(dotlink_dir, "dotlinks.json"), "r") as f:
        dotlinks = json.load(f)

    # If file is already a link under a target
    if to_generic_home_path(src) in map(lambda x: x["link_name"], dotlinks.values()):
        print("dotlink: File", src, "already linked with dotlink, cannot migrate")
        sys.exit()
    elif os.path.exists(src):
        shutil.move(src, dest)
        link(options)
    else:
        print("dotlink:" + src + ": No such file or directory")
        sys.exit()
Esempio n. 2
0
def rmlink(command_relevants):
    """Remove the link of a file in the dotlink directory"""

    link_name = to_specific_path(command_relevants["<link_name>"])
    dotlink_dir = get_dotlink_dir()

    with open(os.path.join(dotlink_dir, "dotlinks.json"), "r") as f:
        dotlinks = json.load(f)
        new_dotlinks = dotlinks.copy()

    # Only remove the dotlink in dotlinks.json and the link file if possible, otherwise just remove the record
    if to_generic_home_path(link_name) in map(lambda x: x["link_name"], dotlinks.values()):
        if os.path.exists(link_name):
            try:
                os.remove(link_name)
            except OSError as e:
                print("dotlink:", e)
                print("dotlink: Proceeding to remove link in dotlinks.json")
        else:
            print("dotlink:", link_name, ": No such file or directory" 
                  "\nOnly removing link records in", os.path.join(dotlink_dir, "dotlinks.json"))

        for x in dotlinks.items():
            if x[1]["link_name"] == to_generic_home_path(link_name):
                new_dotlinks.pop(x[0], None)

        with open(os.path.join(dotlink_dir, "dotlinks.json"), "w") as f:
            json.dump(new_dotlinks, f)
    else:
        print("dotlink: Unable to remove link: Link not found in", 
              os.path.join(dotlink_dir, "dotlinks.json"), "under any targets")
Esempio n. 3
0
def show(command_relevants):
    """List the current dotlinks on the filesystem recorded in dotlinks.json"""

    dotlink_dir = get_dotlink_dir()
    
    with open(os.path.join(dotlink_dir, "dotlinks.json"), "r") as f:
        dotlinks = json.load(f) 

    for x in list(dotlinks.items()):
        print("target:", x[0])
        print("link_name:", x[1]["link_name"])
        print("symbolic:", x[1]["symbolic"], "\n")
Esempio n. 4
0
def link(command_relevants):
    """Link a file in the dotlink directory to a location on the filesystem""" 

    # File in dotlink dir
    target = command_relevants["<target>"]

    # File to link to
    link_name = to_specific_path(command_relevants["<link_name>"])

    symbolic = command_relevants["-s"] 

    dotlink_dir = get_dotlink_dir()
    target_path = os.path.join(dotlink_dir, target)

    with open(os.path.join(dotlink_dir, "dotlinks.json"), "r") as f:
        dotlinks = json.load(f) 

    # Set target path relative to dotlink dir (probably the basename) in dotlink dir
    dotlinks[os.path.relpath(target_path, start=dotlink_dir)] = {
            "link_name": to_generic_home_path(link_name), 
            "symbolic": symbolic
        }

    # Will not symlink if path does not exist (does not conform to ln commmand)
    if symbolic:
        if not os.path.exists(target_path):
            print("dotlink:", target_path, ": No such file or directory")
            print("dotlink: Cannot symlink to a file that does not exist")
            sys.exit()

        try:
            os.symlink(os.path.expandvars(target_path), link_name)
        except OSError as e:
            print("dotlink:", e)
            print("dotlink: Symlink not created")
        else:
            with open(os.path.join(dotlink_dir, "dotlinks.json"), "w") as f:
                json.dump(dotlinks, f)
    else:
        try:
            os.link(os.path.expandvars(target_path), link_name)
        except OSError as e:
            print("dotlink:", e)
            print("dotlink: Link not created")
        else:
            with open(os.path.join(dotlink_dir, "dotlinks.json"), "w") as f:
                json.dump(dotlinks, f)