mkmfs: fix zone bitmap initialization.
[helenos.git] / tools / imgutil.py
blob41ab4d82b55c93cf2326652e75a8075f23c7a60e
1 #!/usr/bin/env python
3 # Copyright (c) 2008 Martin Decky
4 # Copyright (c) 2011 Martin Sucha
5 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
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.
31 """
32 Utilities for filesystem image creators
33 """
35 import os
36 import stat
38 exclude_names = set(['.svn', '.bzr'])
40 def align_up(size, alignment):
41 "Return size aligned up to alignment"
43 if (size % alignment == 0):
44 return size
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"
59 i = 0
60 if (num == 0): raise ValueError()
61 while num & 1 == 0:
62 i += 1
63 num = num >> 1
64 return i
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)
76 class ItemToPack:
77 "Stores information about one directory item to be added to the image"
79 def __init__(self, parent, name):
80 self.parent = parent
81 self.name = 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:
93 continue
95 item = ItemToPack(path, name)
97 if not (item.is_dir or item.is_file):
98 continue
100 yield item
102 def chunks(item, chunk_size):
103 "Iterate contents of a file in chunks of a given size"
105 inf = open(item.path, 'rb')
106 rd = 0
108 while (rd < item.size):
109 data = bytes(inf.read(chunk_size))
110 yield data
111 rd += len(data)
113 inf.close()