Skip to content

Commit c5b8e35

Browse files
committed
support patches
1 parent c6dbac8 commit c5b8e35

File tree

3 files changed

+829
-0
lines changed

3 files changed

+829
-0
lines changed

tools/file_util.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
# Copyright (c) 2011 The Chromium Embedded Framework Authors. All rights
2+
# reserved. Use of this source code is governed by a BSD-style license that
3+
# can be found in the LICENSE file.
4+
5+
from glob import iglob
6+
import os
7+
import shutil
8+
import sys
9+
import time
10+
11+
def read_file(name, normalize = True):
12+
""" Read a file. """
13+
try:
14+
f = open(name, 'r')
15+
# read the data
16+
data = f.read()
17+
if normalize:
18+
# normalize line endings
19+
data = data.replace("\r\n", "\n")
20+
return data
21+
except IOError, (errno, strerror):
22+
sys.stderr.write('Failed to read file '+name+': '+strerror)
23+
raise
24+
else:
25+
f.close()
26+
27+
def write_file(name, data):
28+
""" Write a file. """
29+
try:
30+
f = open(name, 'w')
31+
# write the data
32+
f.write(data)
33+
except IOError, (errno, strerror):
34+
sys.stderr.write('Failed to write file '+name+': '+strerror)
35+
raise
36+
else:
37+
f.close()
38+
39+
def path_exists(name):
40+
""" Returns true if the path currently exists. """
41+
return os.path.exists(name)
42+
43+
def backup_file(name):
44+
""" Rename the file to a name that includes the current time stamp. """
45+
move_file(name, name+'.'+time.strftime('%Y-%m-%d-%H-%M-%S'))
46+
47+
def copy_file(src, dst, quiet = True):
48+
""" Copy a file. """
49+
try:
50+
shutil.copy(src, dst)
51+
if not quiet:
52+
sys.stdout.write('Transferring '+src+' file.\n')
53+
except IOError, (errno, strerror):
54+
sys.stderr.write('Failed to copy file from '+src+' to '+dst+': '+strerror)
55+
raise
56+
57+
def move_file(src, dst, quiet = True):
58+
""" Move a file. """
59+
try:
60+
shutil.move(src, dst)
61+
if not quiet:
62+
sys.stdout.write('Moving '+src+' file.\n')
63+
except IOError, (errno, strerror):
64+
sys.stderr.write('Failed to move file from '+src+' to '+dst+': '+strerror)
65+
raise
66+
67+
def copy_files(src_glob, dst_folder, quiet = True):
68+
""" Copy multiple files. """
69+
for fname in iglob(src_glob):
70+
dst = os.path.join(dst_folder, os.path.basename(fname))
71+
if os.path.isdir(fname):
72+
copy_dir(fname, dst, quiet)
73+
else:
74+
copy_file(fname, dst, quiet)
75+
76+
def remove_file(name, quiet = True):
77+
""" Remove the specified file. """
78+
try:
79+
if path_exists(name):
80+
os.remove(name)
81+
if not quiet:
82+
sys.stdout.write('Removing '+name+' file.\n')
83+
except IOError, (errno, strerror):
84+
sys.stderr.write('Failed to remove file '+name+': '+strerror)
85+
raise
86+
87+
def copy_dir(src, dst, quiet = True):
88+
""" Copy a directory tree. """
89+
try:
90+
remove_dir(dst, quiet)
91+
shutil.copytree(src, dst)
92+
if not quiet:
93+
sys.stdout.write('Transferring '+src+' directory.\n')
94+
except IOError, (errno, strerror):
95+
sys.stderr.write('Failed to copy directory from '+src+' to '+dst+': '+strerror)
96+
raise
97+
98+
def remove_dir(name, quiet = True):
99+
""" Remove the specified directory. """
100+
try:
101+
if path_exists(name):
102+
shutil.rmtree(name)
103+
if not quiet:
104+
sys.stdout.write('Removing '+name+' directory.\n')
105+
except IOError, (errno, strerror):
106+
sys.stderr.write('Failed to remove directory '+name+': '+strerror)
107+
raise
108+
109+
def make_dir(name, quiet = True):
110+
""" Create the specified directory. """
111+
try:
112+
if not path_exists(name):
113+
if not quiet:
114+
sys.stdout.write('Creating '+name+' directory.\n')
115+
os.makedirs(name)
116+
except IOError, (errno, strerror):
117+
sys.stderr.write('Failed to create directory '+name+': '+strerror)
118+
raise
119+
120+
def get_files(search_glob):
121+
""" Returns all files matching the search glob. """
122+
# Sort the result for consistency across platforms.
123+
return sorted(iglob(search_glob))
124+
125+
def read_version_file(file, args):
126+
""" Read and parse a version file (key=value pairs, one per line). """
127+
lines = read_file(file).split("\n")
128+
for line in lines:
129+
parts = line.split('=', 1)
130+
if len(parts) == 2:
131+
args[parts[0]] = parts[1]
132+
133+
def eval_file(src):
134+
""" Loads and evaluates the contents of the specified file. """
135+
return eval(read_file(src), {'__builtins__': None}, None)
136+
137+
def normalize_path(path):
138+
""" Normalizes the path separator to match the Unix standard. """
139+
if sys.platform == 'win32':
140+
return path.replace('\\', '/')
141+
return path

0 commit comments

Comments
 (0)