summaryrefslogtreecommitdiffstats
path: root/third_party/liblouis/copy_tables.py
blob: 8cab92e3b65b518844070c4affd32adef9b4c6b0 (plain)
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
#!/usr/bin/env python
# Copyright 2015 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

'''Copies the liblouis braille translation tables to a destination.'''

import liblouis_list_tables
import optparse
import os
import shutil


def LinkOrCopyFiles(sources, dest_dir):
  def LinkOrCopyOneFile(src, dst):
    if os.path.exists(dst):
      os.unlink(dst)
    try:
      os.link(src, dst)
    except:
      shutil.copy(src, dst)

  if not os.path.exists(dest_dir):
    os.makedirs(dest_dir)
  for source in sources:
    LinkOrCopyOneFile(source, os.path.join(dest_dir, os.path.basename(source)))


def WriteDepfile(depfile, infiles):
  stampfile = depfile + '.stamp'
  with open(stampfile, 'w'):
    os.utime(stampfile, None)
  content = '%s: %s' % (stampfile, ' '.join(infiles))
  open(depfile, 'w').write(content)



def main():
  parser = optparse.OptionParser(description=__doc__)
  parser.add_option('-D', '--directory', dest='directories',
                     action='append', help='Where to search for table files')
  parser.add_option('-e', '--extra_file', dest='extra_files', action='append',
                    default=[], help='Extra liblouis table file to process')
  parser.add_option('-d', '--dest_dir', action='store', metavar='DIR',
                    help=('Destination directory.  Used when translating ' +
                          'input paths to output paths and when copying '
                          'files.'))
  parser.add_option('--depfile', metavar='FILENAME',
                    help=('Store .d style dependencies in FILENAME and touch '
                          'FILENAME.stamp after copying the files'))
  options, args = parser.parse_args()

  if len(args) != 1:
    parser.error('Expecting exactly one argument')
  if not options.directories:
    parser.error('At least one --directory option must be specified')
  if not options.dest_dir:
    parser.error('At least one --dest_dir option must be specified')
  files = liblouis_list_tables.GetTableFiles(args[0], options.directories,
                                             options.extra_files)
  LinkOrCopyFiles(files, options.dest_dir)
  if options.depfile:
    WriteDepfile(options.depfile, files)


if __name__ == '__main__':
  main()