OLD | NEW |
| (Empty) |
1 #!/usr/bin/env python | |
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 | |
4 # BSD-style license that can be found in the LICENSE file. | |
5 | |
6 | |
7 """Tool for creating symlinks from SOURCES to TARGET. | |
8 | |
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 | |
11 the name of the target link. | |
12 | |
13 If a SOURCE contains ":", the left side is the path and the right side is the | |
14 name of the package symlink. | |
15 | |
16 Before creating any links, the old entries of the TARGET directory will be | |
17 removed. | |
18 | |
19 Usage: | |
20 python tools/make_links.py OPTIONS TARGET SOURCES... | |
21 | |
22 """ | |
23 | |
24 import optparse | |
25 import os | |
26 import shutil | |
27 import subprocess | |
28 import sys | |
29 import urllib | |
30 import urlparse | |
31 import utils | |
32 | |
33 # Useful messages when we find orphaned checkouts. | |
34 old_directories = { | |
35 'package_config': 'Please remove third_party/pkg/package_config.', | |
36 'analyzer_cli': 'Please remove third_party/pkg/analyzer_cli.'} | |
37 | |
38 def get_options(): | |
39 result = optparse.OptionParser() | |
40 result.add_option("--timestamp_file", "", | |
41 help='Create a timestamp file when done creating the links.', | |
42 default='') | |
43 result.add_option("-q", "--quiet", | |
44 help="Don't print any messages", | |
45 action="store_true", | |
46 dest="quiet", | |
47 default=False) | |
48 result.add_option("--create-links", | |
49 help='Create links to the package lib directories in "packages/".', | |
50 action='store_false', | |
51 default=True) | |
52 result.add_option("--create-package-file", | |
53 help='Create a ".packages" file pointing to the packages.', | |
54 action='store_false', | |
55 default=True) | |
56 | |
57 return result.parse_args() | |
58 | |
59 def make_link(quiet, source, target, orig_source): | |
60 if os.path.islink(target): | |
61 if not quiet: | |
62 print 'Removing %s' % target | |
63 sys.stdout.flush() | |
64 os.unlink(target) | |
65 | |
66 if os.path.isdir(target): | |
67 if not quiet: | |
68 print 'Removing %s' % target | |
69 sys.stdout.flush() | |
70 os.rmdir(target) | |
71 | |
72 if os.path.isfile(orig_source): | |
73 if not quiet: | |
74 print 'Copying file from %s to %s' % (orig_source, target) | |
75 sys.stdout.flush() | |
76 shutil.copyfile(orig_source, target) | |
77 return 0 | |
78 else: | |
79 if not quiet: | |
80 print 'Creating link from %s to %s' % (source, target) | |
81 sys.stdout.flush() | |
82 | |
83 if utils.GuessOS() == 'win32': | |
84 return subprocess.call(['mklink', '/j', target, source], shell=True) | |
85 else: | |
86 return subprocess.call(['ln', '-s', source, target]) | |
87 | |
88 def create_timestamp_file(options): | |
89 if options.timestamp_file != '': | |
90 dir_name = os.path.dirname(options.timestamp_file) | |
91 if not os.path.exists(dir_name): | |
92 os.mkdir(dir_name) | |
93 open(options.timestamp_file, 'w').close() | |
94 | |
95 def main(argv): | |
96 (options, args) = get_options() | |
97 target_dir = os.path.relpath(args[0]) | |
98 target = os.path.join(target_dir, 'packages') | |
99 if os.path.exists(target): | |
100 # If the packages directory already exists, delete the current links in | |
101 # it. This is necessary, otherwise we can end up having links in there | |
102 # pointing to directories which no longer exist (on incremental builds). | |
103 for link in os.listdir(target): | |
104 full_link = os.path.join(target, link) | |
105 if os.path.isdir(full_link) and utils.IsWindows(): | |
106 # It seems like python on Windows is treating pseudo symlinks to | |
107 # directories as directories. | |
108 os.rmdir(full_link) | |
109 else: | |
110 os.remove(full_link) | |
111 else: | |
112 os.makedirs(target) | |
113 target = os.path.join(target_dir, '.packages') | |
114 if os.path.exists(target): | |
115 os.remove(target) | |
116 | |
117 linked_names = {}; | |
118 package_file_contents = '# .package file created by tools/make_links.py\n' | |
119 for source in args[1:]: | |
120 # Assume the source directory is named ".../NAME/lib". | |
121 split = source.split(':') | |
122 name = None | |
123 if len(split) == 2: (source, name) = split | |
124 | |
125 (path, lib) = os.path.split(source) | |
126 if lib != 'lib': | |
127 path = source | |
128 # Remove any additional path components preceding NAME, if one wasn't | |
129 # specified explicitly. | |
130 if not name: (_, name) = os.path.split(path) | |
131 # We have an issue with left-behind checkouts in third_party/pkg and | |
132 # third_party/pkg_tested when we move entries in DEPS. This reports them. | |
133 if name in linked_names: | |
134 print 'Duplicate directory %s is linked to both %s and %s.' % ( | |
135 name, linked_names[name], path) | |
136 if name in old_directories: | |
137 print old_directories[name] | |
138 return 1 | |
139 linked_names[name] = path | |
140 orig_source = source | |
141 if options.create_links: | |
142 if utils.GuessOS() == 'win32': | |
143 source = os.path.relpath(source) | |
144 else: | |
145 source = os.path.relpath(source, start=target) | |
146 exit_code = make_link(options.quiet, | |
147 source, os.path.join(target, name), orig_source) | |
148 if exit_code != 0: | |
149 return exit_code | |
150 abs_source = os.path.abspath(orig_source) | |
151 source_url = urlparse.urljoin('file:', urllib.pathname2url(abs_source)) | |
152 package_file_contents += '%s:%s\n' % (name, source_url) | |
153 if options.create_package_file: | |
154 with open(os.path.join(target_dir, '.packages'), 'w') as package_file: | |
155 package_file.write(package_file_contents) | |
156 create_timestamp_file(options) | |
157 return 0 | |
158 | |
159 | |
160 if __name__ == '__main__': | |
161 sys.exit(main(sys.argv)) | |
OLD | NEW |