forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_pkg_manifest.py
executable file
·122 lines (100 loc) · 3.55 KB
/
create_pkg_manifest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env python3
# Copyright 2016 The Dart project authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Usage: create_pkg_manifest.py --deps <DEPS file> --output <jiri manifest>
#
# This script parses the DEPS file, extracts dependencies that live under
# third_party/pkg, and writes them to a file suitable for consumption as a
# jiri manifest for Fuchsia. It is assumed that the Dart tree is under
# //dart in the Fuchsia world, and so the dependencies extracted by this script
# will go under //dart/third_party/pkg.
import argparse
import os
import sys
import utils
SCRIPT_DIR = os.path.dirname(sys.argv[0])
DART_ROOT = os.path.realpath(os.path.join(SCRIPT_DIR, '..'))
# Used in parsing the DEPS file.
class VarImpl(object):
_env_vars = {
"host_cpu": "x64",
"host_os": "linux",
}
def __init__(self, local_scope):
self._local_scope = local_scope
def Lookup(self, var_name):
"""Implements the Var syntax."""
if var_name in self._local_scope.get("vars", {}):
return self._local_scope["vars"][var_name]
# Inject default values for env variables
if var_name in self._env_vars:
return self._env_vars[var_name]
raise Exception("Var is not defined: %s" % var_name)
def ParseDepsFile(deps_file):
local_scope = {}
var = VarImpl(local_scope)
global_scope = {
'Var': var.Lookup,
'deps_os': {},
}
# Read the content.
with open(deps_file, 'r') as fp:
deps_content = fp.read()
# Eval the content.
exec (deps_content, global_scope, local_scope)
# Extract the deps and filter.
deps = local_scope.get('deps', {})
filtered_deps = {}
for k, v in deps.items():
if 'sdk/third_party/pkg' in k:
new_key = k.replace('sdk', 'third_party/dart', 1)
filtered_deps[new_key] = v
return filtered_deps
def WriteManifest(deps, manifest_file):
project_template = """
<project name="%s"
path="%s"
remote="%s"
revision="%s"/>
"""
warning = (
'<!-- This file is generated by '
'//third_party/dart/tools/create_pkg_manifest.py. DO NOT EDIT -->\n')
with open(manifest_file, 'w') as manifest:
manifest.write('<?xml version="1.0" encoding="UTF-8"?>\n')
manifest.write(warning)
manifest.write('<manifest>\n')
manifest.write(' <projects>\n')
for path, remote in sorted(deps.items()):
remote_components = remote.split('@')
remote_url = remote_components[0]
remote_version = remote_components[1]
manifest.write(
project_template % (path, path, remote_url, remote_version))
manifest.write(' </projects>\n')
manifest.write('</manifest>\n')
def ParseArgs(args):
args = args[1:]
parser = argparse.ArgumentParser(
description='A script to generate a jiri manifest for third_party/pkg.')
parser.add_argument(
'--deps',
'-d',
type=str,
help='Input DEPS file.',
default=os.path.join(DART_ROOT, 'DEPS'))
parser.add_argument(
'--output',
'-o',
type=str,
help='Output jiri manifest.',
default=os.path.join(DART_ROOT, 'dart_third_party_pkg.manifest'))
return parser.parse_args(args)
def Main(argv):
args = ParseArgs(argv)
deps = ParseDepsFile(args.deps)
WriteManifest(deps, args.output)
return 0
if __name__ == '__main__':
sys.exit(Main(sys.argv))