| OLD | NEW |
| 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): | 22 def ReplaceInFiles(paths, subs): |
| 23 '''Reads a series of files, applies a series of substitutions to each, and | 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.''' | 24 saves them back out. subs should be a list of (pattern, replace) tuples.''' |
| 25 for path in paths: | 25 for path in paths: |
| 26 contents = open(path).read() | 26 contents = open(path).read() |
| 27 for pattern, replace in subs: | 27 for pattern, replace in subs: |
| 28 contents = re.sub(pattern, replace, contents) | 28 contents = re.sub(pattern, replace, contents) |
| 29 | 29 |
| 30 dest = open(path, 'w') | 30 dest = open(path, 'w') |
| 31 dest.write(contents) | 31 dest.write(contents) |
| 32 dest.close() | 32 dest.close() |
| 33 | 33 |
| 34 | |
| 35 def ReadVersion(file, field): | 34 def ReadVersion(file, field): |
| 36 for line in open(file).read().split('\n'): | 35 for line in open(file).read().split('\n'): |
| 37 [k, v] = re.split('\s+', line) | 36 [k, v] = re.split('\s+', line) |
| 38 if field == k: | 37 if field == k: |
| 39 return int(v) | 38 return int(v) |
| 40 | 39 |
| 41 def Main(argv): | 40 def Main(argv): |
| 42 HOME = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) | 41 HOME = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) |
| 43 | 42 |
| 44 versionFile = os.path.join(HOME, 'tools', 'VERSION') | 43 versionFile = os.path.join(HOME, 'tools', 'VERSION') |
| 45 major = ReadVersion(versionFile, 'MAJOR') | 44 major = ReadVersion(versionFile, 'MAJOR') |
| 46 minor = ReadVersion(versionFile, 'MINOR') | 45 minor = ReadVersion(versionFile, 'MINOR') |
| 47 build = ReadVersion(versionFile, 'BUILD') | 46 build = ReadVersion(versionFile, 'BUILD') |
| 48 patch = ReadVersion(versionFile, 'PATCH') | 47 patch = ReadVersion(versionFile, 'PATCH') |
| 49 | 48 |
| 50 # bleeding_edge has a fixed version number of 0.1.x.y . Don't allow users | 49 # bleeding_edge has a fixed version number of 0.1.x.y . Don't allow users |
| 51 # to publish packages from bleeding_edge. | 50 # to publish packages from bleeding_edge. |
| 52 if major == 0 and minor <= 1: | 51 if major == 0 and minor <= 1: |
| 53 print 'Error: Do not run this script from a bleeding_edge checkout.' | 52 print 'Error: Do not run this script from a bleeding_edge checkout.' |
| 54 return -1 | 53 return -1 |
| 55 | 54 |
| 56 if patch != 0: | 55 if patch != 0: |
| 57 version = '%d.%d.%d+%d' % (major, minor, build, patch) | 56 version = '%d.%d.%d+%d' % (major, minor, build, patch) |
| 58 else: | 57 else: |
| 59 version = '%d.%d.%d' % (major, minor, build) | 58 version = '%d.%d.%d' % (major, minor, build) |
| 60 | 59 |
| 61 tmpDir = tempfile.mkdtemp() | 60 tmpDir = tempfile.mkdtemp() |
| 62 pkgName = argv[1].split('/').pop() | 61 pkgName = argv[1].split('/').pop() |
| 63 | 62 |
| 64 pubspec = os.path.join(tmpDir, pkgName, 'pubspec.yaml') | 63 pubspec = os.path.join(tmpDir, pkgName, 'pubspec.yaml') |
| 65 | 64 |
| 65 replaceInFiles = [] |
| 66 |
| 66 if os.path.exists(os.path.join(HOME, argv[1], 'pubspec.yaml')): | 67 if os.path.exists(os.path.join(HOME, argv[1], 'pubspec.yaml')): |
| 67 # | 68 # |
| 68 # If pubspec.yaml exists, add the SDK's version number if | 69 # If pubspec.yaml exists, add the SDK's version number if |
| 69 # no version number is present. | 70 # no version number is present. |
| 70 # | 71 # |
| 71 shutil.copytree(os.path.join(HOME, argv[1]), | 72 shutil.copytree(os.path.join(HOME, argv[1]), |
| 72 os.path.join(tmpDir, pkgName)) | 73 os.path.join(tmpDir, pkgName)) |
| 73 with open(pubspec) as pubspecFile: | 74 with open(pubspec) as pubspecFile: |
| 74 lines = pubspecFile.readlines() | 75 lines = pubspecFile.readlines() |
| 75 with open(pubspec, 'w') as pubspecFile: | 76 with open(pubspec, 'w') as pubspecFile: |
| 76 foundVersion = False | 77 foundVersion = False |
| 77 inDependencies = False | 78 inDependencies = False |
| 78 for line in lines: | 79 for line in lines: |
| 79 if line.startswith('dependencies:'): | 80 if line.startswith('dependencies:'): |
| 80 inDependencies = True | 81 inDependencies = True |
| 81 elif line[0].isalpha(): | 82 elif line[0].isalpha(): |
| (...skipping 11 matching lines...) Expand all Loading... |
| 93 else: | 94 else: |
| 94 pubspecFile.write(line) | 95 pubspecFile.write(line) |
| 95 if not foundVersion: | 96 if not foundVersion: |
| 96 pubspecFile.write('\nversion: ' + version + '\n') | 97 pubspecFile.write('\nversion: ' + version + '\n') |
| 97 else: | 98 else: |
| 98 # | 99 # |
| 99 # If there's a lib/ directory in the package, copy the package. | 100 # If there's a lib/ directory in the package, copy the package. |
| 100 # Otherwise, move the package's contents to lib/. | 101 # Otherwise, move the package's contents to lib/. |
| 101 # | 102 # |
| 102 if os.path.exists(os.path.join(HOME, argv[1], 'lib')): | 103 if os.path.exists(os.path.join(HOME, argv[1], 'lib')): |
| 103 shutil.copytree(os.path.join(HOME, argv[1]), | 104 shutil.copytree(os.path.join(HOME, argv[1]), |
| 104 os.path.join(tmpDir, pkgName)) | 105 os.path.join(tmpDir, pkgName)) |
| 105 else: | 106 else: |
| 106 os.makedirs(os.path.join(tmpDir, pkgName)) | 107 os.makedirs(os.path.join(tmpDir, pkgName)) |
| 107 shutil.copytree(os.path.join(HOME, argv[1]), | 108 shutil.copytree(os.path.join(HOME, argv[1]), |
| 108 os.path.join(tmpDir, pkgName, 'lib')) | 109 os.path.join(tmpDir, pkgName, 'lib')) |
| 109 | 110 |
| 110 # Create pubspec.yaml . | 111 # Create pubspec.yaml . |
| 111 with open(pubspec, 'w') as pubspecFile: | 112 with open(pubspec, 'w') as pubspecFile: |
| 112 pubspecFile.write('name: ' + pkgName + '_unsupported\n') | 113 pubspecFile.write('name: ' + pkgName + '_unsupported\n') |
| 113 pubspecFile.write('version: ' + version + '\n') | 114 pubspecFile.write('version: ' + version + '\n') |
| 114 pubspecFile.write("description: >\n") | 115 pubspecFile.write("description: >\n") |
| 115 pubspecFile.write(' A completely unsupported clone of Dart SDK library\n'
) | 116 pubspecFile.write(' A completely unsupported clone of Dart SDK library\n'
) |
| 116 pubspecFile.write(' ' + argv[1] + ' . This package will change in\n') | 117 pubspecFile.write(' ' + argv[1] + ' . This package will change in\n') |
| 117 pubspecFile.write(' unpredictable/incompatible ways without warning.\n') | 118 pubspecFile.write(' unpredictable/incompatible ways without warning.\n') |
| 118 pubspecFile.write('dependencies:\n') | 119 pubspecFile.write('dependencies:\n') |
| 119 | 120 |
| 121 libpath = os.path.join(HOME, argv[1], '../libraries.dart') |
| 122 if os.path.exists(libpath): |
| 123 # Copy libraries.dart into the package source code |
| 124 shutil.copy(libpath, os.path.join(tmpDir, pkgName, 'lib/libraries.dart')) |
| 125 |
| 126 # Replace '../../libraries.dart' with '../libraries.dart' |
| 127 replaceInFiles.append( |
| 128 (r'(import|part)(\s+)(\'|")\.\./(\.\./)*libraries.dart', |
| 129 r'\1\2\3\4libraries.dart')) |
| 130 |
| 131 replaceInFiles.append( |
| 132 (r'(import|part)(\s+)(\'|")(\.\./)+pkg/', r'\1\2\3package:')) |
| 133 |
| 120 # Replace '../*/pkg' imports and parts. | 134 # Replace '../*/pkg' imports and parts. |
| 121 for root, dirs, files in os.walk(os.path.join(tmpDir, pkgName)): | 135 for root, dirs, files in os.walk(os.path.join(tmpDir, pkgName)): |
| 122 for name in files: | 136 for name in files: |
| 123 if name.endswith('.dart'): | 137 if name.endswith('.dart'): |
| 124 ReplaceInFiles([os.path.join(root, name)], | 138 ReplaceInFiles([os.path.join(root, name)], replaceInFiles) |
| 125 [(r'(import|part)(\s+)(\'|")(\.\./)+pkg/', r'\1\2\3package:')]) | 139 |
| 126 | |
| 127 print 'publishing version ' + version + ' of ' + argv[1] + ' to pub.\n' | 140 print 'publishing version ' + version + ' of ' + argv[1] + ' to pub.\n' |
| 128 subprocess.call(['pub', 'publish'], cwd=os.path.join(tmpDir, pkgName)) | 141 subprocess.call(['pub', 'publish'], cwd=os.path.join(tmpDir, pkgName)) |
| 129 shutil.rmtree(tmpDir) | 142 shutil.rmtree(tmpDir) |
| 130 | 143 |
| 131 if __name__ == '__main__': | 144 if __name__ == '__main__': |
| 132 sys.exit(Main(sys.argv)) | 145 sys.exit(Main(sys.argv)) |
| OLD | NEW |