Example #1
0
 def __init__(self, filename):
     UserDict.UserDict.__init__(self)
     try:
         filehandle = open(filename);
         try:
             self.control = apt_inst.debExtractControl(filehandle)
         finally:
             # Make sure that file is always closed.
             filehandle.close()
     except SystemError:
         log.debug("Had problems reading: %s"%(filename), 'AptDpkgInfo')
         raise
     for line in self.control.split('\n'):
         if line.find(': ') != -1:
             key, value = line.split(': ', 1)
             self.data[key] = value
Example #2
0
def import_directory(factory, dir, recursive=0):
    """
    Import all files in a given directory into the cache
    This is used by apt-proxy-import to import new files
    into the cache
    """
    if not os.path.exists(dir):
        log.err('Directory ' + dir + ' does not exist', 'import')
        return

    if recursive:    
        log.debug("Importing packages from directory tree: " + dir, 'import')
        for root, dirs, files in os.walk(dir):
            for file in files:
                import_file(factory, root, file)
    else:
        log.debug("Importing packages from directory: " + dir, 'import')
        for file in os.listdir(dir):
            mode = os.stat(dir + '/' + file)[stat.ST_MODE]
            if not stat.S_ISDIR(mode):
                import_file(factory, dir, file)

    for backend in factory.backends:
        backend.get_packages_db().unload()
Example #3
0
def import_file(factory, dir, file):
    """
    Import a .deb or .udeb into cache from given filename
    """
    if file[-4:]!='.deb' and file[-5:]!='.udeb':
        log.msg("Ignoring (unknown file type):"+ file, 'import')
        return
    
    log.debug("considering: " + dir + '/' + file, 'import')
    try:
        paths = get_mirror_path(factory, dir+'/'+file)
    except SystemError:
        log.msg(file + ' skipped - wrong format or corrupted', 'import')
        return
    if paths:
        if len(paths) != 1:
            log.debug("WARNING: multiple ocurrences", 'import')
            log.debug(str(paths), 'import')
        cache_path = paths[0]
    else:
        log.debug("Not found, trying to guess", 'import')
        info = AptDpkgInfo(dir+'/'+file)
        cache_path = closest_match(info,
                                get_mirror_versions(factory, info['Package']))
    if cache_path:
        log.debug("MIRROR_PATH:"+ cache_path, 'import')
        src_path = dir+'/'+file
        dest_path = factory.cache_dir+cache_path
        
        if not os.path.exists(dest_path):
            log.debug("IMPORTING:" + src_path, 'import')
            dest_path = re.sub(r'/\./', '/', dest_path)
            if not os.path.exists(dirname(dest_path)):
                os.makedirs(dirname(dest_path))
            f = open(dest_path, 'w')
            fcntl.lockf(f.fileno(), fcntl.LOCK_EX)
            f.truncate(0)
            shutil.copy2(src_path, dest_path)
            f.close()
            if hasattr(factory, 'access_times'):
                atime = os.stat(src_path)[stat.ST_ATIME]
                factory.access_times[cache_path] = atime
            log.msg(file + ' imported', 'import')
        else:
            log.msg(file + ' skipped - already in cache', 'import')

    else:
        log.msg(file + ' skipped - no suitable backend found', 'import')