]> git.proxmox.com Git - mirror_frr.git/blame - python/clippy/__init__.py
Merge pull request #3543 from donaldsharp/eigrp_router_id_is_the_bee
[mirror_frr.git] / python / clippy / __init__.py
CommitLineData
5578a14d
DL
1# FRR CLI preprocessor
2#
3# Copyright (C) 2017 David Lamparter for NetDEF, Inc.
4#
5# This program is free software; you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by the Free
7# Software Foundation; either version 2 of the License, or (at your option)
8# any later version.
9#
10# This program is distributed in the hope that it will be useful, but WITHOUT
11# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13# more details.
14#
15# You should have received a copy of the GNU General Public License along
16# with this program; see the file COPYING; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
67eb7a6c 19import os, stat
5578a14d
DL
20import _clippy
21from _clippy import parse, Graph, GraphNode
22
23def graph_iterate(graph):
24 '''iterator yielding all nodes of a graph
25
26 nodes arrive in input/definition order, graph circles are avoided.
27 '''
28
29 queue = [(graph.first(), frozenset(), 0)]
30 while len(queue) > 0:
31 node, stop, depth = queue.pop(0)
32 yield node, depth
33
34 join = node.join()
35 if join is not None:
36 queue.insert(0, (join, stop.union(frozenset([node])), depth))
37 join = frozenset([join])
38
39 stop = join or stop
40 nnext = node.next()
41 for n in reversed(nnext):
42 if n not in stop and n is not node:
43 queue.insert(0, (n, stop, depth + 1))
44
45def dump(graph):
46 '''print out clippy.Graph'''
47
48 for i, depth in graph_iterate(graph):
49 print('\t%s%s %r' % (' ' * (depth * 2), i.type, i.text))
50
67eb7a6c 51def wrdiff(filename, buf, reffiles = []):
5578a14d
DL
52 '''write buffer to file if contents changed'''
53
54 expl = ''
55 if hasattr(buf, 'getvalue'):
56 buf = buf.getvalue()
57 old = None
58 try: old = open(filename, 'r').read()
59 except: pass
60 if old == buf:
67eb7a6c
DL
61 for reffile in reffiles:
62 # ensure output timestamp is newer than inputs, for make
63 reftime = os.stat(reffile)[stat.ST_MTIME]
64 outtime = os.stat(filename)[stat.ST_MTIME]
65 if outtime <= reftime:
66 os.utime(filename, (reftime + 1, reftime + 1))
5578a14d
DL
67 # sys.stderr.write('%s unchanged, not written\n' % (filename))
68 return
67eb7a6c
DL
69
70 newname = '%s.new-%d' % (filename, os.getpid())
71 with open(newname, 'w') as out:
5578a14d 72 out.write(buf)
67eb7a6c 73 os.rename(newname, filename)