1#!/usr/bin/env python2
2
3import optparse
4import os
5import shutil
6import subprocess
7import sys
8
9upstream_git = 'https://github.com/catapult-project/catapult.git'
10PACKAGE_DIRS = [
11    'common',
12    'dependency_manager',
13    'devil',
14    'systrace',
15    'third_party/pyserial',
16    'third_party/polymer',
17    'tracing/tracing/trace_data',
18]
19PACKAGE_FILES = [
20    'tracing/tracing/__init__.py',
21    'tracing/tracing_project.py',
22]
23IGNORE_PATTERNS = ['OWNERS'] # doesn't make sense to sync owners files
24
25script_dir = os.path.dirname(os.path.abspath(sys.argv[0]))
26catapult_src_dir = os.path.join(script_dir, 'catapult-upstream')
27catapult_dst_dir = os.path.join(script_dir, 'catapult')
28
29parser = optparse.OptionParser()
30parser.add_option('--local', dest='local_dir', metavar='DIR',
31                  help='use a local catapult')
32parser.add_option('--no-min', dest='no_min', default=False, action='store_true',
33                  help='skip minification')
34options, args = parser.parse_args()
35
36## Update the source if needed.
37if options.local_dir is None:
38  # Remove the old source tree.
39  shutil.rmtree(catapult_src_dir, True)
40
41  # Pull the latest source from the upstream git.
42  git_args = ['git', 'clone', upstream_git, catapult_src_dir]
43  p = subprocess.Popen(git_args, stdout=subprocess.PIPE, cwd=script_dir)
44  p.communicate()
45  if p.wait() != 0:
46    print 'Failed to checkout source from upstream git.'
47    sys.exit(1)
48
49  catapult_git_dir = os.path.join(catapult_src_dir, '.git')
50  # Update the UPSTREAM_REVISION file
51  git_args = ['git', 'rev-parse', 'HEAD']
52  p = subprocess.Popen(git_args,
53                       stdout=subprocess.PIPE,
54                       cwd=catapult_src_dir,
55                       env={"GIT_DIR":catapult_git_dir})
56  out, err = p.communicate()
57  if p.wait() != 0:
58    print 'Failed to get revision.'
59    sys.exit(1)
60
61  shutil.rmtree(catapult_git_dir, True)
62
63  rev = out.strip()
64  with open('UPSTREAM_REVISION', 'wt') as f:
65    f.write(rev + '\n')
66else:
67  catapult_src_dir = options.local_dir
68
69
70## Update systrace_trace_viewer.html
71systrace_dir = os.path.join(catapult_src_dir, 'systrace', 'systrace')
72sys.path.append(systrace_dir)
73import update_systrace_trace_viewer
74update_systrace_trace_viewer.update(no_auto_update=True, no_min=options.no_min)
75
76## Package the result
77shutil.rmtree(catapult_dst_dir)
78
79for d in PACKAGE_DIRS:
80  src = os.path.join(catapult_src_dir, d)
81  dst = os.path.join(catapult_dst_dir, d)
82
83  # make parent dir by creating dst + ancestors, and deleting dst
84  if not os.path.isdir(dst):
85    os.makedirs(dst)
86  shutil.rmtree(dst)
87
88  # copy tree
89  shutil.copytree(src, dst, ignore=shutil.ignore_patterns(*IGNORE_PATTERNS))
90
91for f in PACKAGE_FILES:
92  src = os.path.join(catapult_src_dir, f)
93  dst = os.path.join(catapult_dst_dir, f)
94  shutil.copy(src, dst)
95