コード例 #1
0
ファイル: rmlink.py プロジェクト: synergistics/dotlink
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")
コード例 #2
0
ファイル: migrate.py プロジェクト: synergistics/dotlink
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()
コード例 #3
0
ファイル: link.py プロジェクト: synergistics/dotlink
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)
コード例 #4
0
ファイル: init.py プロジェクト: synergistics/dotlink
def init(command_relevants):
    """Initialize a dotlink directory"""
    
    # Location of dotlink directory
    path = to_specific_path(command_relevants["<path>"] or ".")
    
    # Location of dotlinks.json
    json_path = os.path.join(path, "dotlinks.json")
    
    # Location of .dotlinkrc
    dotlinkrc = os.path.join(os.environ["HOME"], ".dotlinkrc")

    # If directory exists, nothing happens to it
    os.makedirs(path, exist_ok=True)

    # Don't want to overwrite file if it already has links
    if not os.path.exists(json_path):
        with open(json_path, "w") as f:
            json.dump({}, f)
    
    # Identify location of dotlink dir
    # Will have to change once more can be added to dotlinkrc
    with open(dotlinkrc, "w") as f:
        f.write("dotlink_dir = " + to_generic_home_path(path))