Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 33b61405b1
Fetching contributors…

Cannot retrieve contributors at this time

file 265 lines (227 sloc) 9.865 kb
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
# This file is part of pdar.
#
# Copyright 2011 Jason Penney
#
# 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.

from bz2 import BZ2File
from datetime import datetime
from gzip import GzipFile
from pdar import PDAR_VERSION, DEFAULT_HASH_TYPE
from pdar.entry import *
from pdar.errors import *
from pdar.patcher import DEFAULT_PATCHER_TYPE
from pkg_resources import parse_version
from shutil import rmtree
from tempfile import SpooledTemporaryFile, mkstemp
import filecmp
import fnmatch
import logging
import os
import re
import tarfile

__all__ = ['PDArchive', 'PDAR_MAGIC', 'PDAR_ID']

PDAR_MAGIC = 'PDAR'
PDAR_ID = '%s%03d%c' % (
    PDAR_MAGIC, int(parse_version(PDAR_VERSION)[0]), 0)

ARCHIVE_HEADER_VERSION = 'pdar_version'
ARCHIVE_HEADER_CREATED = 'pdar_created_datetime'
ARCHIVE_HEADER_HASH_TYPE = 'pdar_hash_type'


class PDArchive(object):

    def __init__(self, orig_path, dest_path, patterns=['*'], payload=None,
                 hash_type=DEFAULT_HASH_TYPE):
        self._hash_type = hash_type
        if orig_path and dest_path and patterns and not payload:
            logging.debug("""\
creating new pdar:
orig_path: %s
dest_path: %s
patterns: %s""" % (orig_path, dest_path, str(patterns)))
            self._patches = []
            pattern_re = r'|'.join([
                    fnmatch.translate(pat) for pat in patterns])
            pattern_re = re.compile(pattern_re)

            def target_gen(path):
                for root, dummy, files in os.walk(path):
                    for dest in (
                        os.path.normcase(
                            os.path.join(root, f)) for f in files \
                            if pattern_re.match(f)):
                        yield os.path.relpath(dest, path)

            orig_targets = set(target_gen(orig_path))
            dest_targets = set(target_gen(dest_path))

            common_targets = [
                (target, target, target) for target in (
                    orig_targets & dest_targets)]
            moved_targets = []
            deleted_targets = []
            new_targets = []
            copied_targets = []

            orig_only = orig_targets - dest_targets
            dest_only = dest_targets - orig_targets

            source_match = {}
            for target in dest_only:
                matched = False
                dest_target_path = os.path.join(dest_path, target)
                for potential_match in orig_targets:
                    if filecmp.cmp(dest_target_path,
                                   os.path.join(orig_path, potential_match)):
                        source_match.setdefault(potential_match, [])
                        source_match[potential_match].append(target)
                        matched = True
                        break
                if not matched:
                    new_targets.append((target, None, target))

            matched = source_match.keys()
            for target in orig_only:
                if target not in matched:
                    deleted_targets.append((target, target, None))

            for source, matches in source_match.iteritems():
                move_match = None

                # does this path still exist in dest
                if source not in dest_targets:
                    move_match = matches[-1]
                    matches = matches[:-1]

                for target in matches:
                    copied_targets.append((target, source, target))

                if move_match:
                    target = move_match
                    moved_targets.append((target, source, target))

            def add_entry(targets, cls):
                args = list(targets)
                args += [orig_path, dest_path, self.hash_type]
                entry = cls.create(*args) # pylint: disable=W0142
                if entry:
                    logging.info("adding '%s' entry for: %s"
                                 % (entry.type_code, entry.target))
                    self._patches.append(entry)
                else:
                    logging.debug("unchanged file: %s" % target[0])

            for target in copied_targets:
                add_entry(target, PDARCopyEntry)

            for target in moved_targets:
                add_entry(target, PDARMoveEntry)

            for target in common_targets:
                add_entry(target, PDARDiffEntry)

            for target in deleted_targets:
                add_entry(target, PDARDeleteEntry)

            for target in new_targets:
                add_entry(target, PDARNewEntry)

            self._pdar_version = PDAR_VERSION
            self._created_datetime = datetime.utcnow()
        elif payload and not orig_path and not dest_path:
            self._patches = payload['patches']
            self._pdar_version = payload[ARCHIVE_HEADER_VERSION]
            self._created_datetime = payload[ARCHIVE_HEADER_CREATED]
            self._hash_type = payload[ARCHIVE_HEADER_HASH_TYPE]

        else:
            raise InvalidParameterError(
                "You must pass either 'orig_path', 'dest_path', and "
                "'patterns' OR 'payload'")

    @property
    def hash_type(self):
        return self._hash_type

    @property
    def pdar_version(self):
        return self._pdar_version

    @property
    def created_datetime(self):
        return self._created_datetime

    @property
    def patches(self):
        return self._patches

    def save(self, path, force=False):
        if os.path.exists(path) and not force:
            raise RuntimeError('File already exists: %s' % path)
        with SpooledTemporaryFile() as tmpfile:
            tfile = tarfile.open(
                mode='w', fileobj=tmpfile,
                format=tarfile.PAX_FORMAT,
                pax_headers={
                    ARCHIVE_HEADER_VERSION: unicode(self.pdar_version),
                    ARCHIVE_HEADER_CREATED: unicode(
                        self.created_datetime.isoformat()),
                    ARCHIVE_HEADER_HASH_TYPE: unicode(self.hash_type)})

            try:
                for patch in self.patches:
                    patch.pax_dump(tfile)
            finally:
                tfile.close()
            tmpfile.flush()

            # find best compression
            archive_path = None
            for comp in [GzipFile, BZ2File]:
                dummy, test_path = mkstemp()
                os.close(dummy)
                compfile = comp(test_path, mode='wb',
                                compresslevel=9)
                tmpfile.seek(0)
                compfile.writelines(tmpfile)
                compfile.close()
                if not archive_path or os.path.getsize(
                    archive_path) > os.path.getsize(test_path):
                    if archive_path and os.path.exists(archive_path):
                        os.unlink(archive_path)
                    archive_path = test_path

            with open(path, 'wb') as patchfile:
                patchfile.write(PDAR_ID)
                with open(archive_path, 'rb') as archive:
                    patchfile.writelines(archive)
                patchfile.flush()

    def patch(self, path=None, patcher=None):
        if patcher is None:
            patcher = DEFAULT_PATCHER_TYPE(self, path)

        patcher.apply_archive()

    @classmethod
    def load(cls, path):
        with SpooledTemporaryFile() as archive:
            with open(path, 'rb') as patchfile:
                file_id = patchfile.read(len(PDAR_ID))
                if not file_id.startswith(PDAR_MAGIC):
                    raise PDArchiveFormatError("Not a pdar file: %s" % (path))
                if file_id != PDAR_ID:
                    raise PDArchiveFormatError(
                        "Unsupported pdar version ID '%s': %s"
                        % (file_id[len(PDAR_MAGIC):-1], path))
                archive.writelines(patchfile)
            archive.seek(0)
            patches = []
            payload = {}
            tfile = tarfile.open(mode='r:*', fileobj=archive)
            try:
                payload.update(tfile.pax_headers)
                if 'created_datetime' in payload:
                    cdt = payload['created_datetime']
                    if isinstance(cdt, basestring):
                        iso, iso_ms = cdt.split('.', 1)
                        cdt = datetime.strptime(
                            iso.replace("-", ""), "%Y%m%dT%H:%M:%S")
                        if iso_ms:
                            cdt = cdt.replace(microsecond=int(iso_ms))
                    payload['created_datetime'] = cdt

                data = tfile.next()
                while data:
                    patch = PDAREntry.pax_load(tfile, data)
                    patches.append(patch)
                    data = tfile.next()
            finally:
                tfile.close()
            payload['patches'] = patches[:]

            # if 0 > cmp(parse_version(PDAR_VERSION),
            # parse_version(patch.pdar_version)):
            # raise RuntimeError(
            # "File '%s' created with pdar protocal %s. "
            # "This verion of pdar only supports up to %s."
            # % (patch.pdar_version, PDAR_VERSION))
            # return patch
            return cls(orig_path=None, dest_path=None, patterns=None,
                       payload=payload)
Something went wrong with that request. Please try again.