~armagetronad-ct/armagetronad/0.4-advrim

« back to all changes in this revision

Viewing changes to batch/make/symlink-relative

  • Committer: Voodoo
  • Date: 2013-04-11 10:46:24 UTC
  • Revision ID: voodoo-20130411104624-zse1f33o7ten188z
merge -r 1454..1476 lp:armagetronad/0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
"""Usage: symlink-relative [-h] <base-directory> [<action> ...]
 
4
 
 
5
Creates relative symbolic links in <base-directory> that target the source specified by each <action>.
 
6
 
 
7
Actions
 
8
=======
 
9
 
 
10
--file <file>
 
11
    Creates a relative symbolic link that targets <file>.
 
12
 
 
13
--files-in-directory <directory>
 
14
    Creates a relative symbolic link that targets each file in <directory>.
 
15
    
 
16
--file-with-name <file> <name>
 
17
    Creates a relative symbolic link called <name> that targets <file>.
 
18
"""
 
19
 
 
20
import optparse
 
21
import os
 
22
import os.path
 
23
import sys
 
24
 
 
25
def symlink_file_with_name(base_directory, path, name):
 
26
    base_directory = os.path.abspath(base_directory)
 
27
    path = os.path.abspath(path)
 
28
    try:
 
29
        os.makedirs(base_directory)
 
30
    except os.error, e:
 
31
        pass
 
32
    
 
33
    old_dir = os.getcwd()
 
34
    try:
 
35
        os.chdir(base_directory)
 
36
        if os.path.exists(name):
 
37
            os.unlink(name)
 
38
        os.symlink(os.path.relpath(path, base_directory), name)
 
39
    finally:
 
40
        os.chdir(old_dir)
 
41
 
 
42
def symlink_file(base_directory, path):
 
43
    symlink_file_with_name(base_directory, path, os.path.basename(path))
 
44
 
 
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))
 
48
 
 
49
def help(option, opt, value, parser):
 
50
    print(__doc__)
 
51
    sys.exit(0)
 
52
 
 
53
def main():
 
54
    parser = optparse.OptionParser(add_help_option=False)
 
55
    
 
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)
 
62
 
 
63
    (options, args) = parser.parse_args()
 
64
    if len(args) != 1:
 
65
        print("Error: no base directory specified. See --help for usage.")
 
66
        sys.exit(1)
 
67
    
 
68
    base_directory = args[0]
 
69
    
 
70
    for path in (options.file or []):
 
71
        symlink_file(base_directory, path)
 
72
    
 
73
    for path in (options.files_in_directory or []):
 
74
        symlink_files_in_directory(base_directory, path)
 
75
    
 
76
    for path, name in (options.file_with_name or []):
 
77
        symlink_file_with_name(base_directory, path, name)
 
78
 
 
79
if __name__ == "__main__":
 
80
    main()