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
|
#!/usr/bin/python
# Copyright (c) 2010 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.
"""Utilities for PyAuto."""
import logging
import os
import shutil
import sys
import tempfile
import zipfile
class ExistingPathReplacer(object):
"""Facilitates backing up a given path (file or dir)..
Often you want to manipulate a directory or file for testing but don't want to
meddle with the existing contents. This class lets you make a backup, and
reinstate the backup when done. A backup is made in an adjacent directory,
so you need to make sure you have write permissions to the parent directory.
Works seemlessly in cases where the requested path already exists, or not.
Automatically reinstates the backed up path (if any) when object is deleted.
"""
_path = ''
_backup_dir = None # dir to which existing content is backed up
_backup_basename = ''
def __init__(self, path, path_type='dir'):
"""Initialize the object, making backups if necessary.
Args:
path: the requested path to file or directory
path_type: path type. Options: 'file', 'dir'. Default: 'dir'
"""
assert path_type in ('file', 'dir'), 'Invalid path_type: %s' % path_type
self._path_type = path_type
self._path = path
if os.path.exists(self._path):
if 'dir' == self._path_type:
assert os.path.isdir(self._path), '%s is not a directory' % self._path
else:
assert os.path.isfile(self._path), '%s is not a file' % self._path
# take a backup
self._backup_basename = os.path.basename(self._path)
self._backup_dir = tempfile.mkdtemp(dir=os.path.dirname(self._path),
prefix='bkp-' + self._backup_basename)
logging.info('Backing up %s in %s' % (self._path, self._backup_dir))
shutil.move(self._path,
os.path.join(self._backup_dir, self._backup_basename))
self._CreateRequestedPath()
def __del__(self):
"""Cleanup. Reinstate backup."""
self._CleanupRequestedPath()
if self._backup_dir: # Reinstate, if backed up.
from_path = os.path.join(self._backup_dir, self._backup_basename)
logging.info('Reinstating backup from %s to %s' % (from_path, self._path))
shutil.move(from_path, self._path)
self._RemoveBackupDir()
def _CreateRequestedPath(self):
# Create intermediate dirs if needed.
if not os.path.exists(os.path.dirname(self._path)):
os.makedirs(os.path.dirname(self._path))
if 'dir' == self._path_type:
os.mkdir(self._path)
else:
open(self._path, 'w').close()
def _CleanupRequestedPath(self):
if os.path.exists(self._path):
if os.path.isdir(self._path):
shutil.rmtree(self._path, ignore_errors=True)
else:
os.remove(self._path)
def _RemoveBackupDir(self):
if self._backup_dir and os.path.isdir(self._backup_dir):
shutil.rmtree(self._backup_dir, ignore_errors=True)
def RemovePath(path):
"""Remove the given path (file or dir)."""
if os.path.isdir(path):
shutil.rmtree(path, ignore_errors=True)
return
try:
os.remove(path)
except OSError:
pass
def UnzipFilenameToDir(filename, dir):
"""Unzip |filename| to directory |dir|.
This works with as low as python2.4 (used on win).
"""
zf = zipfile.ZipFile(filename)
pushd = os.getcwd()
if not os.path.isdir(dir):
os.mkdir(dir)
os.chdir(dir)
# Extract files.
for info in zf.infolist():
name = info.filename
if name.endswith('/'): # dir
if not os.path.isdir(name):
os.makedirs(name)
else: # file
dir = os.path.dirname(name)
if not os.path.isdir(dir):
os.makedirs(dir)
out = open(name, 'wb')
out.write(zf.read(name))
out.close()
# Set permissions. Permission info in external_attr is shifted 16 bits.
os.chmod(name, info.external_attr >> 16L)
os.chdir(pushd)
def GetCurrentPlatform():
"""Get a string representation for the current platform.
Returns:
'mac', 'win' or 'linux'
"""
if sys.platform == 'darwin':
return 'mac'
if sys.platform == 'win32':
return 'win'
if sys.platform == 'linux2':
return 'linux'
raise RuntimeError('Unknown platform')
|