| 1 |
# debpartial-mirror - partial debian mirror package tool
|
| 2 |
# (c) 2004 Otavio Salvador <otavio@debian.org>, Marco Presi <zufus@debian.org>
|
| 3 |
#
|
| 4 |
# This program is free software; you can redistribute it and/or modify
|
| 5 |
# it under the terms of the GNU General Public License as published by
|
| 6 |
# the Free Software Foundation; either version 2 of the License, or
|
| 7 |
# (at your option) any later version.
|
| 8 |
#
|
| 9 |
# This program is distributed in the hope that it will be useful,
|
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
# GNU General Public License for more details.
|
| 13 |
#
|
| 14 |
# You should have received a copy of the GNU General Public License
|
| 15 |
# along with this program; if not, write to the Free Software
|
| 16 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
| 17 |
# $Id$
|
| 18 |
|
| 19 |
import os
|
| 20 |
import md5
|
| 21 |
import gzip
|
| 22 |
|
| 23 |
class FileSystem:
|
| 24 |
"""
|
| 25 |
This class provides methods to create backends dirs into the
|
| 26 |
partial-mirror
|
| 27 |
"""
|
| 28 |
def __init__(self, parent, directory):
|
| 29 |
self._errors = [13]
|
| 30 |
self._parent = os.path.normpath(parent)
|
| 31 |
self._dir = os.path.join(parent, directory)
|
| 32 |
if not self._checkParent():
|
| 33 |
print "Cannot create %s" % self._dir
|
| 34 |
return
|
| 35 |
self.create(self._dir)
|
| 36 |
|
| 37 |
def _checkParent (self):
|
| 38 |
""" Check for parent dir """
|
| 39 |
if not self._check (self._parent):
|
| 40 |
return (self.create (self._parent) )
|
| 41 |
return True
|
| 42 |
|
| 43 |
def _check (self, path, debug=True):
|
| 44 |
""" Check for path exsistance """
|
| 45 |
if not os.path.exists (path):
|
| 46 |
return False
|
| 47 |
return True
|
| 48 |
|
| 49 |
def check(self):
|
| 50 |
""" Check for FileSystem existance """
|
| 51 |
return (self._check(self._dir))
|
| 52 |
|
| 53 |
def create(self, path, debug=True):
|
| 54 |
""" Create dir """
|
| 55 |
try:
|
| 56 |
os.makedirs (path)
|
| 57 |
return True
|
| 58 |
except OSError, (errno,strerror):
|
| 59 |
if errno in self._errors and debug:
|
| 60 |
print strerror
|
| 61 |
return False
|
| 62 |
|
| 63 |
def remove(self):
|
| 64 |
""" Remove backend """
|
| 65 |
if self.check():
|
| 66 |
try:
|
| 67 |
print "Removing %s" % self._dir
|
| 68 |
os.rmdir(self._dir)
|
| 69 |
except OSError, (errno, strerror):
|
| 70 |
print strerror
|
| 71 |
|
| 72 |
def path(self):
|
| 73 |
""" Return FileSystem path """
|
| 74 |
return (self._dir)
|
| 75 |
|
| 76 |
def md5_on (self, f):
|
| 77 |
"""Sum files"""
|
| 78 |
try:
|
| 79 |
file_to_hash = open(os.path.join(self._dir, f), 'r')
|
| 80 |
except IOError, msg:
|
| 81 |
print msg
|
| 82 |
return -1
|
| 83 |
|
| 84 |
md5hash = md5.new()
|
| 85 |
md5hash.update(file_to_hash.read())
|
| 86 |
file_to_hash.close()
|
| 87 |
return md5hash.hexdigest()
|
| 88 |
|
| 89 |
def uncompress (self, f):
|
| 90 |
"""Uncompress a file"""
|
| 91 |
print 'Uncompressing', f
|
| 92 |
# uncompress the file
|
| 93 |
compressedFile = gzip.GzipFile(f, 'rb')
|
| 94 |
try:
|
| 95 |
outputFile = open(f.split('.gz')[0], "wb")
|
| 96 |
except IOError, msg:
|
| 97 |
print msg
|
| 98 |
sys.exit(1)
|
| 99 |
|
| 100 |
numBytes = 0
|
| 101 |
while True:
|
| 102 |
data = compressedFile.read(64 * 1024)
|
| 103 |
if not data:
|
| 104 |
break
|
| 105 |
outputFile.write(data)
|
| 106 |
numBytes = numBytes + len(data)
|
| 107 |
|
| 108 |
compressedFile.close()
|
| 109 |
outputFile.close()
|
| 110 |
return True
|
| 111 |
|