Beispiel #1
0
def stagedir(pathname):
    if isdir(pathname):
        data = None
        path.walk(pathname, stageinvisitor, data)
    else:
        stcmd = "stagein -A deferred --nowait --rdonly "
        stcmd += " -p %s " % stagepool
        stcmd += " -M " + path.abspath(pathname)
        print stcmd
        os.system(stcmd)
Beispiel #2
0
def all_matching_files(d, pattern):
    def addfiles(fls, dir, nfiles):
        nfiles = fnmatch.filter(nfiles, pattern)
        nfiles = [path.join(dir, f) for f in nfiles]
        fls.extend(nfiles)

    files = []
    for dirpath, dirnames, filenames in walk(d):
        addfiles(files, dirpath, filenames)
    return files
Beispiel #3
0
def all_matching_files(d, pattern):
    def addfiles(fls, dir, nfiles):
        nfiles = fnmatch.filter(nfiles, pattern)
        nfiles = [path.join(dir, f) for f in nfiles]
        fls.extend(nfiles)

    files = []
    for dirpath, dirnames, filenames in walk(d):
        addfiles(files, dirpath, filenames)
    return files
Beispiel #4
0
def epub_zip_up_book_contents(ebook_path, epub_filepath):
        outzip = zipfile.ZipFile(pathof(epub_filepath), 'w')
        files = path.walk(ebook_path)
        if 'mimetype' in files:
            outzip.write(pathof(os.path.join(ebook_path, 'mimetype')), pathof('mimetype'), zipfile.ZIP_STORED)
        else:
            raise Exception('mimetype file is missing')
        files.remove('mimetype')
        for file in files:
            filepath = os.path.join(ebook_path, file)
            outzip.write(pathof(filepath),pathof(file),zipfile.ZIP_DEFLATED)
        outzip.close()
Beispiel #5
0
def epub_zip_up_book_contents(ebook_path, epub_filepath):
    outzip = zipfile.ZipFile(pathof(epub_filepath), 'w')
    files = path.walk(ebook_path)
    if 'mimetype' in files:
        outzip.write(pathof(os.path.join(ebook_path, 'mimetype')),
                     pathof('mimetype'), zipfile.ZIP_STORED)
    else:
        raise Exception('mimetype file is missing')
    files.remove('mimetype')
    for file in files:
        filepath = os.path.join(ebook_path, file)
        outzip.write(pathof(filepath), pathof(file), zipfile.ZIP_DEFLATED)
    outzip.close()
Beispiel #6
0
    def __init__(self, ebook_root, outdir, op, debug=False):
        self._debug = debug
        self.ebook_root = ebook_root
        self.outdir = outdir
        # dictionaries used to map opf manifest information
        self.id_to_href = {}
        self.id_to_mime = {}
        self.href_to_id = {}
        self.spine_ppd = None
        self.spine = []
        self.guide = []
        self.package_tag = ''
        self.metadataxml = ''
        self.op = op
        if self.op is not None:
            # copy in data from parsing of initial opf
            self.opfname = op.opfname
            self.id_to_href = op.get_manifest_id_to_href_dict().copy()
            self.id_to_mime = op.get_manifest_id_to_mime_dict().copy()
            self.href_to_id = op.get_href_to_manifest_id_dict().copy()
            self.spine_ppd = op.get_spine_ppd()
            self.spine = op.get_spine()
            self.guide = op.get_guide()
            self.package_tag = op.get_package_tag()
            self.metadataxml = op.get_metadataxml()
        self.other = []  # non-manifest file information
        self.id_to_filepath = {}
        self.modified = {}
        self.added = []
        self.deleted = []

        # walk the ebook directory tree building up initial list of
        # all unmanifested (other) files
        for filepath in path.walk(ebook_root):
            book_href = filepath.replace(os.sep, "/")
            # OS X file names and paths use NFD form. The EPUB
            # spec requires all text including filenames to be in NFC form.
            ubook_href = book_href.decode('utf-8')
            ubook_href = unicodedata.normalize('NFC', ubook_href)
            book_href = ubook_href.encode('utf-8')
            # if book_href file in manifest convert to manifest id
            id = None
            if book_href.startswith('OEBPS/'):
                href = book_href[6:]
                id = self.href_to_id.get(href, None)
            if id is None:
                self.other.append(book_href)
                self.id_to_filepath[book_href] = filepath
            else:
                self.id_to_filepath[id] = filepath
Beispiel #7
0
    def __init__(self, ebook_root, outdir, op, debug = False):
        self._debug = debug
        self.ebook_root = ebook_root
        self.outdir = outdir
        # dictionaries used to map opf manifest information
        self.id_to_href = {}
        self.id_to_mime = {}
        self.href_to_id = {}
        self.spine_ppd = None
        self.spine = []
        self.guide = []
        self.package_tag = ''
        self.metadataxml = ''
        self.op = op
        if self.op is not None:
            # copy in data from parsing of initial opf
            self.opfname = op.opfname
            self.id_to_href = op.get_manifest_id_to_href_dict().copy()
            self.id_to_mime = op.get_manifest_id_to_mime_dict().copy()
            self.href_to_id = op.get_href_to_manifest_id_dict().copy()
            self.spine_ppd = op.get_spine_ppd()
            self.spine = op.get_spine()
            self.guide = op.get_guide()
            self.package_tag = op.get_package_tag()
            self.metadataxml = op.get_metadataxml()
        self.other = []  # non-manifest file information
        self.id_to_filepath = {}
        self.modified = {}
        self.added = []
        self.deleted = []

        # walk the ebook directory tree building up initial list of
        # all unmanifested (other) files
        for filepath in path.walk(ebook_root):
            book_href = filepath.replace(os.sep, "/")
            # OS X file names and paths use NFD form. The EPUB
            # spec requires all text including filenames to be in NFC form.
            ubook_href = book_href.decode('utf-8')
            ubook_href = unicodedata.normalize('NFC', ubook_href)
            book_href = ubook_href.encode('utf-8')
            # if book_href file in manifest convert to manifest id
            id = None
            if book_href.startswith('OEBPS/'):
                href = book_href[6:]
                id = self.href_to_id.get(href,None)
            if id is None:
                self.other.append(book_href)
                self.id_to_filepath[book_href] = filepath
            else:
                self.id_to_filepath[id] = filepath
Beispiel #8
0
def test_forwarding():
    state = dict(
        id='1',
        snakes={
            '1': [(0, 2), (0, 1)],
            '2': [(1, 3), (0, 3)],
        },
        food=[(0, 9)],
        height=10,
        width=10,
    )
    matrix = _weights(**state)
    head = state['snakes']['1'][0]
    target = (0, 9)
    route = path.walk(matrix, head, target)
    assert state == {
        'id': '1',
        'snakes': {
            '1': [(0, 2), (0, 1)],
            '2': [(1, 3), (0, 3)]
        },
        'food': [(0, 9)],
        'height': 10,
        'width': 10
    }
    state = _play_forward(route, **state)
    assert state == {
        'id': '1',
        'snakes': {
            '1': [(0, 9), (0, 2)],
            '2': [(1, 3), (0, 3)]
        },
        'food': [],
        'height': 10,
        'width': 10
    }
    head = state['snakes']['1'][0]
    tail = state['snakes']['1'][-1]
    assert _safe_path(head, tail, **state)
Beispiel #9
0
def main(pattern, args):
	for dirname in args:
		path.walk(dirname, visit, pattern)
Beispiel #10
0
def _ideal_path(id=None,
                snakes=None,
                food=None,
                height=None,
                width=None,
                health=None,
                friendlies=None):
    head = snakes[id][0]
    tail = snakes[id][-1]
    size = len(snakes[id])
    matrix = _weights(id, snakes, food, height, width)

    target_tiers = []

    # Retrieves smallest non friendly snake.
    smallest, smallest_size = _smallest_snake(id, snakes, friendlies)

    # Eat if hungry.
    if health < min((width, height)) * 2:
        target_tiers.append(('food', food))

    # Found a snake we could eat that is smaller than ourselves.
    if smallest is not None and smallest_size < size:
        s = snakes[smallest]
        next_head = path.moved_position(s[0], path.direction(s[1], s[0]))
        if next_head == head:
            next_head = s[0]
        target_tiers.append(('attack', [next_head]))

    # Always add food at this level.
    target_tiers.append(('food', food))

    # Add the tail to the tier always.
    target_tiers.append(('tail', [tail]))

    # Add all corners on the board.
    target_tiers.append(('corners', path.corners(height, width)))

    target = None
    target_cost = math.inf
    for name, tier in target_tiers:
        for t in tier:
            cost = path.cost(matrix, head, t)
            if cost < target_cost:
                route = path.walk(matrix, head, t)
                if route:
                    forward_state = _play_forward(
                        route,
                        id=id,
                        snakes=copy.deepcopy(snakes),
                        food=food[:],
                        height=height,
                        width=width,
                    )
                    forward_head = forward_state['snakes'][id][0]
                    forward_tail = forward_state['snakes'][id][-1]
                    if _safe_path(forward_head, forward_tail, **forward_state):
                        target_cost = cost
                        target = t
                    else:
                        print('tier no safe path to tail', name, t)
        if target_cost != math.inf:
            print('tier success', name, target_cost)
            break
        print('tier failed', name, target_cost, tier)

    # Go for the lowest local cost.
    if target_cost == math.inf:
        print('local lowest')
        for t in path.neighbours(head, width, height):
            cost = path.cost(matrix, head, t)
            if cost < target_cost:
                target_cost = cost
                target = t

    # Go for a safe local square.
    if target_cost == math.inf:
        print('safe local')
        return _safe_local(
            id=id,
            snakes=snakes,
            food=food,
            height=height,
            width=width,
        )

    route = path.walk(matrix, head, target)
    return route