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