3
"""Usage: symlink-relative [-h] <base-directory> [<action> ...]
5
Creates relative symbolic links in <base-directory> that target the source specified by each <action>.
11
Creates a relative symbolic link that targets <file>.
13
--files-in-directory <directory>
14
Creates a relative symbolic link that targets each file in <directory>.
16
--file-with-name <file> <name>
17
Creates a relative symbolic link called <name> that targets <file>.
25
def symlink_file_with_name(base_directory, path, name):
26
base_directory = os.path.abspath(base_directory)
27
path = os.path.abspath(path)
29
os.makedirs(base_directory)
35
os.chdir(base_directory)
36
if os.path.exists(name):
38
os.symlink(os.path.relpath(path, base_directory), name)
42
def symlink_file(base_directory, path):
43
symlink_file_with_name(base_directory, path, os.path.basename(path))
45
def symlink_files_in_directory(base_directory, path):
46
for f in os.listdir(path):
47
symlink_file(base_directory, os.path.join(path, f))
49
def help(option, opt, value, parser):
54
parser = optparse.OptionParser(add_help_option=False)
56
# The auto-generated help string does not correctly handle nargs > 1, so we
57
# implement the help flag ourselves.
58
parser.add_option("-h", "--help", action="callback", callback=help)
59
parser.add_option("--file", action="append")
60
parser.add_option("--files-in-directory", action="append")
61
parser.add_option("--file-with-name", action="append", nargs=2)
63
(options, args) = parser.parse_args()
65
print("Error: no base directory specified. See --help for usage.")
68
base_directory = args[0]
70
for path in (options.file or []):
71
symlink_file(base_directory, path)
73
for path in (options.files_in_directory or []):
74
symlink_files_in_directory(base_directory, path)
76
for path, name in (options.file_with_name or []):
77
symlink_file_with_name(base_directory, path, name)
79
if __name__ == "__main__":