OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
5 | 5 |
6 | 6 |
7 '''Tool for creating symlinks from SOURCES to TARGET. | 7 '''Tool for creating symlinks from SOURCES to TARGET. |
8 | 8 |
9 For each SOURCE in SOURCES create a link from SOURCE to TARGET. If a | 9 For each SOURCE in SOURCES create a link from SOURCE to TARGET. If a |
10 SOURCE ends with .../lib, the lib suffix is ignored when determining | 10 SOURCE ends with .../lib, the lib suffix is ignored when determining |
(...skipping 14 matching lines...) Expand all Loading... |
25 import utils | 25 import utils |
26 | 26 |
27 | 27 |
28 def get_options(): | 28 def get_options(): |
29 result = optparse.OptionParser() | 29 result = optparse.OptionParser() |
30 result.add_option("--timestamp_file", "", | 30 result.add_option("--timestamp_file", "", |
31 help='Create a timestamp file when done creating the links.', | 31 help='Create a timestamp file when done creating the links.', |
32 default='') | 32 default='') |
33 return result.parse_args() | 33 return result.parse_args() |
34 | 34 |
35 def make_link(source, target): | 35 def make_link(source, target, orig_source): |
36 if os.path.islink(target): | 36 if os.path.islink(target): |
37 print 'Removing %s' % target | 37 print 'Removing %s' % target |
38 sys.stdout.flush() | 38 sys.stdout.flush() |
39 os.unlink(target) | 39 os.unlink(target) |
40 | 40 |
41 if os.path.isdir(target): | 41 if os.path.isdir(target): |
42 print 'Removing %s' % target | 42 print 'Removing %s' % target |
43 sys.stdout.flush() | 43 sys.stdout.flush() |
44 os.rmdir(target) | 44 os.rmdir(target) |
45 | 45 |
46 print 'Creating link from %s to %s' % (source, target) | 46 if os.path.isfile(orig_source): |
47 sys.stdout.flush() | 47 print 'Copying file from %s to %s' % (orig_source, target) |
| 48 sys.stdout.flush() |
| 49 shutil.copyfile(orig_source, target) |
| 50 return 0 |
| 51 else: |
| 52 print 'Creating link from %s to %s' % (source, target) |
| 53 sys.stdout.flush() |
48 | 54 |
49 if utils.GuessOS() == 'win32': | 55 if utils.GuessOS() == 'win32': |
50 return subprocess.call(['mklink', '/j', target, source], shell=True) | 56 return subprocess.call(['mklink', '/j', target, source], shell=True) |
51 else: | 57 else: |
52 return subprocess.call(['ln', '-s', source, target]) | 58 return subprocess.call(['ln', '-s', source, target]) |
53 | 59 |
54 def create_timestamp_file(options): | 60 def create_timestamp_file(options): |
55 if options.timestamp_file != '': | 61 if options.timestamp_file != '': |
56 dir_name = os.path.dirname(options.timestamp_file) | 62 dir_name = os.path.dirname(options.timestamp_file) |
57 if not os.path.exists(dir_name): | 63 if not os.path.exists(dir_name): |
58 os.mkdir(dir_name) | 64 os.mkdir(dir_name) |
59 open(options.timestamp_file, 'w').close() | 65 open(options.timestamp_file, 'w').close() |
60 | 66 |
61 | 67 |
62 def main(argv): | 68 def main(argv): |
(...skipping 12 matching lines...) Expand all Loading... |
75 os.remove(os.path.join(target, link)) | 81 os.remove(os.path.join(target, link)) |
76 else: | 82 else: |
77 os.makedirs(target) | 83 os.makedirs(target) |
78 for source in args[1:]: | 84 for source in args[1:]: |
79 # Assume the source directory is named ".../NAME/lib". | 85 # Assume the source directory is named ".../NAME/lib". |
80 (name, lib) = os.path.split(source) | 86 (name, lib) = os.path.split(source) |
81 if lib != 'lib': | 87 if lib != 'lib': |
82 name = source | 88 name = source |
83 # Remove any addtional path components preceding NAME. | 89 # Remove any addtional path components preceding NAME. |
84 (path, name) = os.path.split(name) | 90 (path, name) = os.path.split(name) |
| 91 orig_source = source |
85 if utils.GuessOS() == 'win32': | 92 if utils.GuessOS() == 'win32': |
86 source = os.path.relpath(source) | 93 source = os.path.relpath(source) |
87 else: | 94 else: |
88 source = os.path.relpath(source, start=target) | 95 source = os.path.relpath(source, start=target) |
89 exit_code = make_link(source, os.path.join(target, name)) | 96 exit_code = make_link(source, os.path.join(target, name), orig_source) |
90 if exit_code != 0: | 97 if exit_code != 0: |
91 return exit_code | 98 return exit_code |
92 create_timestamp_file(options) | 99 create_timestamp_file(options) |
93 return 0 | 100 return 0 |
94 | 101 |
95 | 102 |
96 if __name__ == '__main__': | 103 if __name__ == '__main__': |
97 sys.exit(main(sys.argv)) | 104 sys.exit(main(sys.argv)) |
OLD | NEW |