1#!/usr/bin/env python
2# Copyright (c) 2012 The Chromium Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6# This script is used to copy all dependencies into the local directory.
7# The package of files can then be uploaded to App Engine.
8import os
9import shutil
10import stat
11import sys
12
13SRC_DIR = os.path.join(sys.path[0], os.pardir, os.pardir, os.pardir, os.pardir,
14    os.pardir)
15THIRD_PARTY_DIR = os.path.join(SRC_DIR, 'third_party')
16LOCAL_THIRD_PARTY_DIR = os.path.join(sys.path[0], 'third_party')
17TOOLS_DIR = os.path.join(SRC_DIR, 'tools')
18SCHEMA_COMPILER_FILES = ['memoize.py',
19                         'model.py',
20                         'idl_schema.py',
21                         'schema_util.py',
22                         'json_parse.py']
23
24def MakeInit(path):
25  path = os.path.join(path, '__init__.py')
26  with open(os.path.join(path), 'w') as f:
27    os.utime(os.path.join(path), None)
28
29def OnError(function, path, excinfo):
30  os.chmod(path, stat.S_IWUSR)
31  function(path)
32
33def CopyThirdParty(src, dest, files=None, make_init=True):
34  dest_path = os.path.join(LOCAL_THIRD_PARTY_DIR, dest)
35  if not files:
36    shutil.copytree(src, dest_path)
37    if make_init:
38      MakeInit(dest_path)
39    return
40  try:
41    os.makedirs(dest_path)
42  except Exception:
43    pass
44  if make_init:
45    MakeInit(dest_path)
46  for filename in files:
47    shutil.copy(os.path.join(src, filename), os.path.join(dest_path, filename))
48
49def main():
50  if os.path.isdir(LOCAL_THIRD_PARTY_DIR):
51    try:
52      shutil.rmtree(LOCAL_THIRD_PARTY_DIR, False, OnError)
53    except OSError:
54      print('*-------------------------------------------------------------*\n'
55            '| If you are receiving an upload error, try removing          |\n'
56            '| chrome/common/extensions/docs/server2/third_party manually. |\n'
57            '*-------------------------------------------------------------*\n')
58
59
60  CopyThirdParty(os.path.join(THIRD_PARTY_DIR, 'handlebar'), 'handlebar')
61  CopyThirdParty(os.path.join(SRC_DIR, 'ppapi', 'generators'),
62                 'json_schema_compiler')
63  CopyThirdParty(os.path.join(THIRD_PARTY_DIR, 'ply'),
64                 os.path.join('json_schema_compiler', 'ply'))
65  CopyThirdParty(os.path.join(TOOLS_DIR, 'json_schema_compiler'),
66                 'json_schema_compiler',
67                 SCHEMA_COMPILER_FILES)
68  CopyThirdParty(os.path.join(TOOLS_DIR, 'json_comment_eater'),
69                 'json_schema_compiler',
70                 ['json_comment_eater.py'])
71  CopyThirdParty(os.path.join(THIRD_PARTY_DIR, 'simplejson'),
72                 os.path.join('json_schema_compiler', 'simplejson'),
73                 make_init=False)
74  MakeInit(LOCAL_THIRD_PARTY_DIR)
75
76  # To be able to use the Handlebar class we need this import in __init__.py.
77  with open(os.path.join(LOCAL_THIRD_PARTY_DIR,
78                         'handlebar',
79                         '__init__.py'), 'a') as f:
80    f.write('from handlebar import Handlebar\n')
81
82if __name__ == '__main__':
83  main()
84