2011-09-27 17:02:08 +02:00
|
|
|
# Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
|
2010-10-03 23:04:50 +02:00
|
|
|
# reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
# can be found in the LICENSE file.
|
|
|
|
|
2011-09-27 17:02:08 +02:00
|
|
|
from glob import iglob
|
2010-10-03 23:04:50 +02:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import time
|
|
|
|
|
|
|
|
def read_file(name, normalize = True):
|
2011-09-27 17:02:08 +02:00
|
|
|
""" Read a file. """
|
2010-10-03 23:04:50 +02:00
|
|
|
try:
|
|
|
|
f = open(name, 'r')
|
|
|
|
# read the data
|
|
|
|
data = f.read()
|
|
|
|
if normalize:
|
|
|
|
# normalize line endings
|
|
|
|
data = data.replace("\r\n", "\n")
|
|
|
|
return data
|
|
|
|
except IOError, (errno, strerror):
|
2011-09-27 17:02:08 +02:00
|
|
|
sys.stderr.write('Failed to read file '+name+': '+strerror)
|
2010-10-03 23:04:50 +02:00
|
|
|
raise
|
|
|
|
else:
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
def write_file(name, data):
|
2011-09-27 17:02:08 +02:00
|
|
|
""" Write a file. """
|
2010-10-03 23:04:50 +02:00
|
|
|
try:
|
|
|
|
f = open(name, 'w')
|
|
|
|
# write the data
|
|
|
|
f.write(data)
|
|
|
|
except IOError, (errno, strerror):
|
|
|
|
sys.stderr.write('Failed to write file '+name+': '+strerror)
|
|
|
|
raise
|
|
|
|
else:
|
|
|
|
f.close()
|
|
|
|
|
2011-09-27 17:02:08 +02:00
|
|
|
def path_exists(name):
|
|
|
|
""" Returns true if the path currently exists. """
|
2010-10-03 23:04:50 +02:00
|
|
|
return os.path.exists(name)
|
|
|
|
|
|
|
|
def backup_file(name):
|
2011-09-27 17:02:08 +02:00
|
|
|
""" Rename the file to a name that includes the current time stamp. """
|
|
|
|
move_file(name, name+'.'+time.strftime('%Y-%m-%d-%H-%M-%S'))
|
|
|
|
|
|
|
|
def copy_file(src, dst, quiet = True):
|
|
|
|
""" Copy a file. """
|
|
|
|
try:
|
|
|
|
shutil.copy(src, dst)
|
|
|
|
if not quiet:
|
|
|
|
sys.stdout.write('Transferring '+src+' file.\n')
|
|
|
|
except IOError, (errno, strerror):
|
|
|
|
sys.stderr.write('Failed to copy file from '+src+' to '+dst+': '+strerror)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def move_file(src, dst, quiet = True):
|
|
|
|
""" Move a file. """
|
|
|
|
try:
|
|
|
|
shutil.move(src, dst)
|
|
|
|
if not quiet:
|
|
|
|
sys.stdout.write('Moving '+src+' file.\n')
|
|
|
|
except IOError, (errno, strerror):
|
|
|
|
sys.stderr.write('Failed to move file from '+src+' to '+dst+': '+strerror)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def copy_files(src_glob, dst_folder, quiet = True):
|
|
|
|
""" Copy multiple files. """
|
|
|
|
for fname in iglob(src_glob):
|
2011-11-08 18:35:59 +01:00
|
|
|
dst = os.path.join(dst_folder, os.path.basename(fname))
|
|
|
|
if os.path.isdir(fname):
|
|
|
|
copy_dir(fname, dst, quiet)
|
|
|
|
else:
|
|
|
|
copy_file(fname, dst, quiet)
|
2011-09-27 17:02:08 +02:00
|
|
|
|
|
|
|
def copy_dir(src, dst, quiet = True):
|
|
|
|
""" Copy a directory tree. """
|
|
|
|
try:
|
|
|
|
remove_dir(dst, quiet)
|
|
|
|
shutil.copytree(src, dst)
|
|
|
|
if not quiet:
|
|
|
|
sys.stdout.write('Transferring '+src+' directory.\n')
|
|
|
|
except IOError, (errno, strerror):
|
|
|
|
sys.stderr.write('Failed to copy directory from '+src+' to '+dst+': '+strerror)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def remove_dir(name, quiet = True):
|
|
|
|
""" Remove the specified directory. """
|
|
|
|
try:
|
|
|
|
if path_exists(name):
|
|
|
|
shutil.rmtree(name)
|
|
|
|
if not quiet:
|
|
|
|
sys.stdout.write('Removing '+name+' directory.\n')
|
|
|
|
except IOError, (errno, strerror):
|
|
|
|
sys.stderr.write('Failed to remove directory '+name+': '+strerror)
|
|
|
|
raise
|
|
|
|
|
|
|
|
def make_dir(name, quiet = True):
|
|
|
|
""" Create the specified directory. """
|
|
|
|
try:
|
|
|
|
if not path_exists(name):
|
|
|
|
if not quiet:
|
|
|
|
sys.stdout.write('Creating '+name+' directory.\n')
|
|
|
|
os.makedirs(name)
|
|
|
|
except IOError, (errno, strerror):
|
|
|
|
sys.stderr.write('Failed to create directory '+name+': '+strerror)
|
2011-11-08 18:35:59 +01:00
|
|
|
raise
|
|
|
|
|
|
|
|
def get_files(search_glob):
|
|
|
|
""" Returns all files matching the search glob. """
|
|
|
|
return iglob(search_glob)
|