summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/tools/oshelpers.py
blob: 9e878d30dbaa4e37706d5a60e7fb6ba859a1e4cb (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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#!/usr/bin/env python
# Copyright (c) 2012 The Native Client Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import fnmatch
import glob
import optparse
import os
import shutil
import sys
import time


def IncludeFiles(filters, files):
  """Filter files based on inclusion lists
  
  Return a list of files which match and of the Unix shell-style wildcards
  provided, or return all the files if no filter is provided."""
  if not filters: 
    return files
  match = set()
  for filter in filters:
    match |= set(fnmatch.filter(files, filter))
  return [name for name in files if name in match]


def ExcludeFiles(filters, files):
  """Filter files based on exclusions lists
  
  Return a list of files which do not match any of the Unix shell-style
  wildcards provided, or return all the files if no filter is provided."""
  if not filters: 
    return files
  match = set()
  for filter in filters:
    excludes = set(fnmatch.filter(files, filter))
    match |= excludes
  return [name for name in files if name not in match]


def CopyPath(options, src, dst):
  """CopyPath from src to dst
  
  Copy a fully specified src to a fully specified dst.  If src and dst are
  both files, the dst file is removed first to prevent error.  If and include
  or exclude list are provided, the destination is first matched against that
  filter."""
  if options.includes:
    if not IncludeFiles(options.includes, [src]):
      return

  if options.excludes:
    if not ExcludeFiles(options.excludes, [src]):
      return

  if options.verbose:
    print 'cp %s %s' % (src, dst)

  # If the source is a single file, copy it individually
  if os.path.isfile(src):
    # We can not copy over a directory with a file.
    if os.path.exists(dst):
      if not os.path.isfile(dst):
        msg = "cp: cannot overwrite non-file '%s' with file." % dst
        raise OSError(msg)
      # If the destination exists as a file, remove it before copying to avoid
      # 'readonly' issues.
      os.remove(dst)

    # Now copy to the non-existent fully qualified target
    shutil.copy(src, dst)
    return

  # Otherwise it's a directory, ignore it unless allowed
  if os.path.isdir(src):
    if not options.recursive:
      print "cp: omitting directory '%s'" % src
      return

    # We can not copy over a file with a directory.
    if os.path.exists(dst):
      if not os.path.isdir(dst):
        msg = "cp: cannot overwrite non-directory '%s' with directory." % dst
        raise OSError(msg)
    else:
      # if it didn't exist, create the directory
      os.makedirs(dst)

    # Now copy all members
    for filename in os.listdir(src):
      srcfile = os.path.join(src, filename)
      dstfile = os.path.join(dst, filename)
      CopyPath(options, srcfile, dstfile)
  return


def Copy(args):
  """A Unix cp style copy.
  
  Copies multiple sources to a single destination using the normal cp 
  semantics.  In addition, it support inclusion and exclusion filters which
  allows the copy to skip certain types of files."""
  parser = optparse.OptionParser(usage='usage: cp [Options] souces... dest')
  parser.add_option(
      '-R', '-r', '--recursive', dest='recursive', action='store_true',
      default=False,
      help='copy directories recursively.')
  parser.add_option(
      '-v', '--verbose', dest='verbose', action='store_true',
      default=False,
      help='verbose output.')
  parser.add_option(
      '--include', dest='includes', action='append', default=[],
      help='include files matching this expression.')
  parser.add_option(
      '--exclude', dest='excludes', action='append', default=[],
      help='exclude files matching this expression.')
  options, files = parser.parse_args(args)
  if len(files) < 2:
    parser.error('ERROR: expecting SOURCE(s) and DEST.')

  srcs = files[:-1]
  dst = files[-1]

  src_list = []
  for src in srcs:
    files = glob.glob(src)
    if len(files) == 0:
      raise OSError('cp: no such file or directory: ' + src)
    if files:
      src_list.extend(files)

  for src in src_list:
    # If the destination is a directory, then append the basename of the src
    # to the destination.
    if os.path.isdir(dst):
      CopyPath(options, src, os.path.join(dst, os.path.basename(src)))
    else:
      CopyPath(options, src, dst)


def Mkdir(args):
  """A Unix style mkdir"""
  parser = optparse.OptionParser(usage='usage: mkdir [Options] DIRECTORY...')
  parser.add_option(
      '-p', '--parents', dest='parents', action='store_true',
      default=False,
      help='ignore existing parents, create parents as needed.')
  parser.add_option(
      '-v', '--verbose', dest='verbose', action='store_true',
      default=False,
      help='verbose output.')

  options, dsts = parser.parse_args(args)
  if len(dsts) < 1:
    parser.error('ERROR: expecting DIRECTORY...')

  for dst in dsts:
    if options.verbose:
      print 'mkdir ' + dst
    try:
      os.makedirs(dst)
    except OSError as error:
      if os.path.isdir(dst):
        if options.parents:
          continue
        raise OSError('mkdir: Already exsists: ' + dst)
      else:
        raise OSError('mkdir: Failed to create: ' + dst)
  return 0


def MovePath(options, src, dst):
  """MovePath from src to dst
  
  Moves the src to the dst much like the Unix style mv command, except it
  only handles one source at a time.  Because of possible temporary failures
  do to locks (such as anti-virus software on Windows), the function will retry
  up to five times.""" 
  # if the destination is not an existing directory, then overwrite it
  if os.path.isdir(dst):
    dst = os.path.join(dst, os.path.basename(src))

  # If the destination exists, the remove it
  if os.path.exists(dst):
    if options.force:
      Remove(['-vfr', dst])
      if os.path.exists(dst):
        raise OSError('mv: FAILED TO REMOVE ' + dst)
    else:
      raise OSError('mv: already exists ' + dst)
  for i in range(5):
    try:
      os.rename(src, dst)
      return
    except OSError as error:
      print 'Failed on %s with %s, retrying' % (src, error)
      time.sleep(5)
  print 'Gave up.'
  raise OSError('mv: ' + error)


def Move(args):
  parser = optparse.OptionParser(usage='usage: mv [Options] souces... dest')
  parser.add_option(
      '-v', '--verbose', dest='verbose', action='store_true',
      default=False,
      help='verbose output.')
  parser.add_option(
      '-f', '--force', dest='force', action='store_true',
      default=False,
      help='force, do not error it files already exist.')
  options, files = parser.parse_args(args)
  if len(files) < 2:
    parser.error('ERROR: expecting SOURCE... and DEST.')
  if options.verbose:
    print 'mv %s %s' % (src, dst)

  srcs = files[:-1]
  dst = files[-1]

  for src in srcs:
    MovePath(options, src, dst)
  return 0


def Remove(args):
  """A Unix style rm.
  
  Removes the list of paths.  Because of possible temporary failures do to locks
  (such as anti-virus software on Windows), the function will retry up to five
  times.""" 
  parser = optparse.OptionParser(usage='usage: rm [Options] PATHS...')
  parser.add_option(
      '-R', '-r', '--recursive', dest='recursive', action='store_true',
      default=False,
      help='remove directories recursively.')
  parser.add_option(
      '-v', '--verbose', dest='verbose', action='store_true',
      default=False,
      help='verbose output.')
  parser.add_option(
      '-f', '--force', dest='force', action='store_true',
      default=False,
      help='force, do not error it files does not exist.')
  options, files = parser.parse_args(args)
  if len(files) < 1:
    parser.error('ERROR: expecting FILE...')

  try:
    for pattern in files:
      dst_files = glob.glob(pattern)
      # Ignore non existing files when using force
      if len(files) == 0 and options.force:
        print "rm: Skipping " + pattern
        continue
      elif len(files) == 0:
        raise OSError('rm: no such file or directory: ' + pattern)

      for dst in dst_files:
        if options.verbose:
          print 'rm ' + dst

        if os.path.isfile(dst) or os.path.islink(dst):
          for i in range(5):
            try:
              # Check every time, since it may have been deleted after the
              # previous failed attempt.
              if os.path.isfile(dst) or os.path.islink(dst):
                os.remove(dst)
              break
            except OSError as error:
              if i == 5:
                print 'Gave up.'
                raise OSError('rm: ' + str(error))
              print 'Failed remove with %s, retrying' % error
              time.sleep(5)

        if options.recursive:
          for i in range(5):
            try:
              if os.path.isdir(dst):
                shutil.rmtree(dst)
              break
            except OSError as error:
              if i == 5:
                print 'Gave up.'
              raise OSError('rm: ' + str(error))
              print 'Failed rmtree with %s, retrying' % error
              time.sleep(5)

  except OSError as error:
    print error
  return 0


FuncMap = {
  'cp': Copy,
  'mkdir': Mkdir,
  'mv': Move,
  'rm': Remove,
}


if __name__ == '__main__':
  func = FuncMap.get(sys.argv[1])
  if not func:
    print 'Do not recognize: ' + sys.argv[1]
    sys.exit(1)
  sys.exit(func(sys.argv[2:]))