Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: tools/publish_pkg.py

Issue 62473002: Re-land r29957 (add versions and constraints for packages and samples), with (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tools/publish_all_pkgs.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # 2 #
3 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 3 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
4 # for details. All rights reserved. Use of this source code is governed by a 4 # for details. All rights reserved. Use of this source code is governed by a
5 # BSD-style license that can be found in the LICENSE file. 5 # BSD-style license that can be found in the LICENSE file.
6 # 6 #
7 # Script to push a package to pub. 7 # Script to push a package to pub.
8 # 8 #
9 # Usage: publish_pkg.py pkg_dir 9 # Usage: publish_pkg.py pkg_dir
10 # 10 #
11 # "pub" must be in PATH. 11 # "pub" must be in PATH.
12 12
13 13
14 import os 14 import os
15 import os.path 15 import os.path
16 import re 16 import re
17 import shutil 17 import shutil
18 import sys 18 import sys
19 import subprocess 19 import subprocess
20 import tempfile 20 import tempfile
21 21
22 def ReplaceInFiles(paths, subs):
23 '''Reads a series of files, applies a series of substitutions to each, and
24 saves them back out. subs should be a list of (pattern, replace) tuples.'''
25 for path in paths:
26 contents = open(path).read()
27 for pattern, replace in subs:
28 contents = re.sub(pattern, replace, contents)
29
30 dest = open(path, 'w')
31 dest.write(contents)
32 dest.close()
33
34 def ReadVersion(file, field):
35 for line in open(file).read().split('\n'):
36 [k, v] = re.split('\s+', line)
37 if field == k:
38 return int(v)
39
40 def Main(argv): 22 def Main(argv):
41 HOME = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) 23 HOME = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
42 24
43 versionFile = os.path.join(HOME, 'tools', 'VERSION') 25 pkgName = os.path.basename(os.path.normpath(argv[1]))
44 major = ReadVersion(versionFile, 'MAJOR')
45 minor = ReadVersion(versionFile, 'MINOR')
46 build = ReadVersion(versionFile, 'BUILD')
47 patch = ReadVersion(versionFile, 'PATCH')
48 26
49 # bleeding_edge has a fixed version number of 0.1.x.y . Don't allow users 27 pubspec = os.path.join(HOME, argv[1], 'pubspec.yaml')
50 # to publish packages from bleeding_edge. 28 if not os.path.exists(pubspec):
51 if major == 0 and minor <= 1: 29 print 'Error: did not find pubspec.yaml at ' + pubspec
52 print 'Error: Do not run this script from a bleeding_edge checkout.'
53 return -1 30 return -1
54 31
55 if patch != 0: 32 with open(pubspec) as pubspecFile:
56 version = '%d.%d.%d+%d' % (major, minor, build, patch) 33 lines = pubspecFile.readlines()
57 else: 34
58 version = '%d.%d.%d' % (major, minor, build) 35 version = None
36 foundSdkContraint = False
37 inDependencies = False
38 for line in lines:
39 if line.startswith('dependencies:'):
40 inDependencies = True
41 elif line.startswith('environment:'):
42 foundSdkContraint = True
43 elif line[0].isalpha():
44 inDependencies = False
45 if line.startswith('version:'):
46 version = line[len('version:'):].strip()
47 if inDependencies:
48 if line.endswith(': any'):
49 print 'Error in %s: should not use "any" version constraint: %s' % (
50 pubspec, line)
51 return -1
52
53 if not version:
54 print 'Error in %s: did not find package version.' % pubspec
55 return -1
56
57 if not foundSdkContraint:
58 print 'Error in %s: did not find SDK version constraint.' % pubspec
59 return -1
59 60
60 tmpDir = tempfile.mkdtemp() 61 tmpDir = tempfile.mkdtemp()
61 pkgName = os.path.basename(os.path.normpath(argv[1]))
62 62
63 pubspec = os.path.join(tmpDir, pkgName, 'pubspec.yaml') 63 #
64 64 # If pubspec.yaml exists, check that the SDK's version constraint is valid
65 replaceInDart = [] 65 #
66 replaceInPubspec = [] 66 shutil.copytree(os.path.join(HOME, argv[1]),
67 67 os.path.join(tmpDir, pkgName))
68 if os.path.exists(os.path.join(HOME, argv[1], 'pubspec.yaml')):
69 #
70 # If pubspec.yaml exists, add the SDK's version number if
71 # no version number is present.
72 #
73 shutil.copytree(os.path.join(HOME, argv[1]),
74 os.path.join(tmpDir, pkgName))
75 with open(pubspec) as pubspecFile:
76 lines = pubspecFile.readlines()
77 with open(pubspec, 'w') as pubspecFile:
78 foundVersion = False
79 inDependencies = False
80 for line in lines:
81 if line.startswith('dependencies:'):
82 inDependencies = True
83 elif line[0].isalpha():
84 inDependencies = False
85 if line.startswith('version:'):
86 foundVersion = True
87 if inDependencies:
88 #
89 # Within dependencies, don't print line that start with " sdk:"
90 # and strip out "{ sdk: package_name }".
91 #
92 if not line.startswith(' sdk:'):
93 line = re.sub(r'{(\s*)sdk:(\s+)([a-z0-9_]+)(\s*)}', '', line)
94 pubspecFile.write(line)
95 else:
96 pubspecFile.write(line)
97 if not foundVersion:
98 pubspecFile.write('\nversion: ' + version + '\n')
99 pubspecFile.write('environment:\n')
100 pubspecFile.write(' sdk: ">=' + version + '"\n')
101
102 else:
103 #
104 # If there's a lib/ directory in the package, copy the package.
105 # Otherwise, move the package's contents to lib/.
106 #
107 if os.path.exists(os.path.join(HOME, argv[1], 'lib')):
108 shutil.copytree(os.path.join(HOME, argv[1]),
109 os.path.join(tmpDir, pkgName))
110 else:
111 os.makedirs(os.path.join(tmpDir, pkgName))
112 shutil.copytree(os.path.join(HOME, argv[1]),
113 os.path.join(tmpDir, pkgName, 'lib'))
114
115 # Create pubspec.yaml .
116 with open(pubspec, 'w') as pubspecFile:
117 pubspecFile.write('name: ' + pkgName + '_unsupported\n')
118 pubspecFile.write('author: None\n')
119 pubspecFile.write('homepage: http://None\n')
120 pubspecFile.write('version: ' + version + '\n')
121 pubspecFile.write("description: >\n")
122 pubspecFile.write(' A completely unsupported clone of Dart SDK library\n' )
123 pubspecFile.write(' ' + argv[1] + ' . This package will change in\n')
124 pubspecFile.write(' unpredictable/incompatible ways without warning.\n')
125 pubspecFile.write('dependencies:\n')
126 pubspecFile.write('environment:\n')
127 pubspecFile.write(' sdk: ">=' + version + '"\n')
128
129 libpath = os.path.join(HOME, argv[1], '../libraries.dart')
130 if os.path.exists(libpath):
131 # Copy libraries.dart into the package source code
132 shutil.copy(libpath, os.path.join(tmpDir, pkgName, 'lib/libraries.dart'))
133
134 # Replace '../../libraries.dart' with '../libraries.dart'
135 replaceInDart.append(
136 (r'(import|part)(\s+)(\'|")\.\./(\.\./)*libraries.dart',
137 r'\1\2\3\4libraries.dart'))
138 68
139 if not os.path.exists(os.path.join(tmpDir, pkgName, 'LICENSE')): 69 if not os.path.exists(os.path.join(tmpDir, pkgName, 'LICENSE')):
140 with open(os.path.join(tmpDir, pkgName, 'LICENSE'), 'w') as licenseFile: 70 with open(os.path.join(tmpDir, pkgName, 'LICENSE'), 'w') as licenseFile:
141 licenseFile.write( 71 licenseFile.write(
142 '''Copyright 2013, the Dart project authors. All rights reserved. 72 '''Copyright 2013, the Dart project authors. All rights reserved.
143 Redistribution and use in source and binary forms, with or without 73 Redistribution and use in source and binary forms, with or without
144 modification, are permitted provided that the following conditions are 74 modification, are permitted provided that the following conditions are
145 met: 75 met:
146 76
147 * Redistributions of source code must retain the above copyright 77 * Redistributions of source code must retain the above copyright
(...skipping 12 matching lines...) Expand all
160 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 90 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
161 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 91 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
162 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 92 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
163 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 93 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
164 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 94 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
165 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 95 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
166 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 96 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
167 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 97 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
168 '''); 98 ''');
169 99
170 replaceInDart.append( 100 print 'publishing version ' + version + ' of ' + argv[1] + ' to pub.\n'
171 (r'(import|part)(\s+)(\'|")(\.\./)+pkg/([^/]+/)lib/', r'\1\2\3package:\5'))
172 101
173 # Replace '../*/pkg' imports and parts. 102 # TODO(jmesserly): this code puts things in the pub cache. Useful for testing
174 for root, dirs, files in os.walk(os.path.join(tmpDir, pkgName)): 103 # without actually uploading.
175 # TODO(dgrove): Remove this when dartbug.com/7487 is fixed. 104 #cacheDir = os.path.join(
176 if '.svn' in dirs: 105 # os.path.expanduser('~/.pub-cache/hosted/pub.dartlang.org'),
177 shutil.rmtree(os.path.join(root, '.svn')) 106 # pkgName + '-' + version)
178 for name in files: 107 #print 'Moving to ' + cacheDir
179 if name.endswith('.dart'): 108 #shutil.move(os.path.join(tmpDir, pkgName), cacheDir)
180 ReplaceInFiles([os.path.join(root, name)], replaceInDart)
181 elif name == 'pubspec.yaml':
182 ReplaceInFiles([os.path.join(root, name)], replaceInPubspec)
183 109
184 print 'publishing version ' + version + ' of ' + argv[1] + ' to pub.\n'
185 subprocess.call(['pub', 'publish'], cwd=os.path.join(tmpDir, pkgName)) 110 subprocess.call(['pub', 'publish'], cwd=os.path.join(tmpDir, pkgName))
186 shutil.rmtree(tmpDir) 111 shutil.rmtree(tmpDir)
187 112
188 if __name__ == '__main__': 113 if __name__ == '__main__':
189 sys.exit(Main(sys.argv)) 114 sys.exit(Main(sys.argv))
OLDNEW
« no previous file with comments | « tools/publish_all_pkgs.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698