summaryrefslogtreecommitdiff
path: root/gcimagebundle/gcimagebundlelib/exclude_spec.py
blob: b5bc237fec57c97438c2c14cb1995e4d98bf4263 (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
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Exclude file specification."""



import logging
import os


class ExcludeSpec(object):
  """Specifies how exclusion of a path should be handled."""

  def __init__(self, path, preserve_file=False, preserve_dir=False,
               preserve_subdir=False):
    self.path = path
    self.preserve_dir = preserve_dir
    self.preserve_file = False
    self.preserve_subdir = False
    # Preserve files and subdirs only if dir is preserved.
    if preserve_file and preserve_dir:
      self.preserve_file = True
    if preserve_subdir and preserve_dir:
      self.preserve_subdir = True

  def ShouldExclude(self, filename):
    prefix = os.path.commonprefix([filename, self.path])
    if prefix == self.path:
      if ((self.preserve_dir and filename == self.path) or
          (self.preserve_subdir and os.path.isdir(filename)) or
          (self.preserve_file and os.path.isfile(filename))):
        logging.warning('preserving %s', filename)
        return False
      return True
    return False

  def GetSpec(self):
    return '(%s, %d:%d:%d)' % (self.path, self.preserve_file, self.preserve_dir,
                               self.preserve_subdir)

  def GetRsyncSpec(self, src):
    """Returns exclude spec in a format required by rsync.

    Args:
      src: source directory path passed to rsync. rsync expects exclude-spec to
        be relative to src directory.

    Returns:
      A string of exclude filters in rsync exclude-from file format.
    """
    spec = ''
    prefix = os.path.commonprefix([src, self.path])
    if prefix == src:
      relative_path = os.path.join('/', self.path[len(prefix):])
      if self.preserve_dir:
        spec += '+ %s\n' % relative_path
        if self.preserve_file or self.preserve_subdir:
          if os.path.isdir(self.path):
            for f in os.listdir(self.path):
              file_path = os.path.join(self.path, f)
              relative_file_path = os.path.join(relative_path, f)
              if self.preserve_file and os.path.isfile(file_path):
                spec += '+ %s\n' % relative_file_path
              if self.preserve_subdir and os.path.isdir(file_path):
                spec += '+ %s\n' % relative_file_path
      else:
        spec += '- %s\n' % relative_path
      spec += '- %s\n' % os.path.join(relative_path, '**')
    return spec