3
# Copyright (c) 2008 Martin Decky
4
# Copyright (c) 2011 Martin Sucha
7
# Redistribution and use in source and binary forms, with or without
8
# modification, are permitted provided that the following conditions
11
# - Redistributions of source code must retain the above copyright
12
# notice, this list of conditions and the following disclaimer.
13
# - Redistributions in binary form must reproduce the above copyright
14
# notice, this list of conditions and the following disclaimer in the
15
# documentation and/or other materials provided with the distribution.
16
# - The name of the author may not be used to endorse or promote products
17
# derived from this software without specific prior written permission.
19
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
Utilities for filesystem image creators
38
exclude_names = set(['.svn', '.bzr'])
40
def align_up(size, alignment):
41
"Return size aligned up to alignment"
43
if (size % alignment == 0):
46
return ((size // alignment) + 1) * alignment
48
def count_up(size, alignment):
49
"Return units necessary to fit the size"
51
if (size % alignment == 0):
52
return (size // alignment)
54
return ((size // alignment) + 1)
56
def num_of_trailing_bin_zeros(num):
57
"Return number of trailing zeros in binary representation"
60
if (num == 0): raise ValueError()
66
def get_bit(number, n):
67
"Return True if n-th least-significant bit is set in the given number"
69
return bool((number >> n) & 1)
71
def set_bit(number, n):
72
"Return the number with n-th least-significant bit set"
74
return number | (1 << n)
77
"Stores information about one directory item to be added to the image"
79
def __init__(self, parent, name):
82
self.path = os.path.join(parent, name)
83
self.stat = os.stat(self.path)
84
self.is_dir = stat.S_ISDIR(self.stat.st_mode)
85
self.is_file = stat.S_ISREG(self.stat.st_mode)
86
self.size = self.stat.st_size
88
def listdir_items(path):
89
"Return a list of items to be packed inside a fs image"
91
for name in os.listdir(path):
92
if name in exclude_names:
94
item = ItemToPack(path, name)
95
if not (item.is_dir or item.is_file):
99
def chunks(item, chunk_size):
100
"Iterate contents of a file in chunks of a given size"
102
inf = open(item.path, 'rb')
104
while (rd < item.size):
105
data = bytes(inf.read(chunk_size))