Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hostinfo
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Anders Blomdell
hostinfo
Commits
a7d80063
Commit
a7d80063
authored
14 years ago
by
Anders Blomdell
Browse files
Options
Downloads
Patches
Plain Diff
Version 2011-05-09 14:42
M src/hostinfo.py M src/hostinfo/ifconfig.py M src/hostinfo/parser.py
parent
0eefb091
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/hostinfo.py
+42
-12
42 additions, 12 deletions
src/hostinfo.py
src/hostinfo/ifconfig.py
+3
-0
3 additions, 0 deletions
src/hostinfo/ifconfig.py
src/hostinfo/parser.py
+26
-5
26 additions, 5 deletions
src/hostinfo/parser.py
with
71 additions
and
17 deletions
src/hostinfo.py
+
42
−
12
View file @
a7d80063
...
...
@@ -88,13 +88,46 @@ attr_weight = {
(
'
subnet
'
,
'
name_servers
'
)
:
6
,
}
def
cmp
(
a
,
b
):
if
a
<
b
:
#def cmp(a,b):
# if a < b:
# return -1
# elif a == b:
# return 0
# else:
# return 1
def
host_order
(
a
,
b
):
def
at_top
(
n
):
# Place hosts without ethernet or ip address at top
for
i
in
n
.
_interface_
:
if
i
.
ip
[
0
]:
return
False
if
i
.
ethernet
[
0
]:
return
False
pass
return
True
def
at_bottom
(
n
):
# Place hosts with ethernet but without ip address at bottom
result
=
False
for
i
in
n
.
_interface_
:
if
i
.
ip
[
0
]:
return
False
if
i
.
ethernet
[
0
]:
result
=
True
pass
return
result
if
(
at_top
(
a
)
==
at_top
(
b
)
and
at_bottom
(
a
)
==
at_bottom
(
b
)):
return
cmp
(
a
.
name
[
0
],
b
.
name
[
0
])
elif
at_top
(
a
):
return
-
1
elif
a
==
b
:
return
0
else
:
elif
at_top
(
b
):
return
1
elif
at_bottom
(
a
):
return
1
elif
at_bottom
(
b
):
return
-
1
else
:
raise
Exception
(
'
Should never happen
'
)
tag_weight
=
{
(
'
hostinfo
'
,
'
soa
'
)
:
(
1
,
...
...
@@ -104,12 +137,10 @@ tag_weight = {
(
'
hostinfo
'
,
'
netgroup
'
)
:
(
3
,
lambda
a
,
b
:
cmp
(
a
.
name
[
0
],
b
.
name
[
0
])),
(
'
hostinfo
'
,
'
host
'
)
:
(
4
,
lambda
a
,
b
:
cmp
(
a
.
name
[
0
],
b
.
name
[
0
])),
(
'
host
'
,
'
netgroup
'
)
:
(
1
,
lambda
a
,
b
:
cmp
(
a
.
name
[
0
],
b
.
name
[
0
])),
(
'
host
'
,
'
automount
'
)
:
(
2
,
host_order
),
(
'
host
'
,
'
automount
'
)
:
(
1
,
lambda
a
,
b
:
cmp
(
a
.
host
[
0
],
b
.
host
[
0
])),
(
'
host
'
,
'
mio
'
)
:
(
3
,
(
'
host
'
,
'
mio
'
)
:
(
2
,
None
),
(
'
interface
'
,
'
kickstart
'
)
:
(
1
,
None
),
...
...
@@ -267,7 +298,6 @@ if __name__ == '__main__':
result
+=
tree
.
_xml
(
attr_sort
=
attr_sort
,
tag_sort
=
tag_sort
)
print
result
.
encode
(
"
iso8859-1
"
)
if
options
.
yp
:
for
(
f
,
c
)
in
hostinfo
.
yp
.
generate
(
tree
,
options
.
yp_auto_domain
):
file
[
"
%s/%s
"
%
(
options
.
yp
,
f
)]
=
c
...
...
This diff is collapsed.
Click to expand it.
src/hostinfo/ifconfig.py
+
3
−
0
View file @
a7d80063
...
...
@@ -98,8 +98,11 @@ def generate(tree, host):
def
subnet
(
tree
,
ip
):
for
s
in
tree
.
_subnet_
:
if
not
s
.
netmask
[
0
]
or
not
s
.
network
[
0
]:
continue
if
aton
(
s
.
network
[
0
])
==
aton
(
ip
)
&
aton
(
s
.
netmask
[
0
]):
return
s
pass
return
None
def
device
(
ethernet
):
...
...
This diff is collapsed.
Click to expand it.
src/hostinfo/parser.py
+
26
−
5
View file @
a7d80063
...
...
@@ -360,6 +360,12 @@ class Node:
def
_xml
(
self
,
indent
=
0
,
attr_sort
=
None
,
tag_sort
=
None
,
width
=
80
):
"""
Generate a prettyprinted xml of the tree rooted in this node
"""
if
self
.
_tag
==
''
:
# Comment node
result
=
"
%s<!--
"
%
(
"
"
*
indent
)
result
+=
self
.
_attribute
[
''
]
result
+=
"
-->
\n
"
return
result
result
=
""
line
=
"
%s<%s
"
%
(
"
"
*
indent
,
self
.
_tag
)
blanks
=
"
"
*
len
(
line
)
...
...
@@ -436,17 +442,27 @@ class Parser(xml.sax.ContentHandler):
self
.
tree
=
None
# Create a xml parser
parser
=
xml
.
sax
.
make_parser
()
import
xml.parsers.expat
self
.
parser
=
xml
.
parsers
.
expat
.
ParserCreate
()
self
.
parser
.
StartElementHandler
=
self
.
startElement
self
.
parser
.
EndElementHandler
=
self
.
endElement
self
.
parser
.
CharacterDataHandler
=
self
.
characters
self
.
parser
.
CommentHandler
=
self
.
comment
"""
parser = xml.sax.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, 0)
parser.setContentHandler(self)
try:
parser.setFeature(xml.sax.handler.feature_external_ges, 0)
except:
pass
"""
# Parse the url
self
.
current
=
[
self
]
parser
.
parse
(
url
)
self
.
parser
.
Parse
(
open
(
url
).
read
())
#import urllib2
#parser.Parse(urllib2.urlopen(url).read())
# Set the mtime
mtime
=
0
...
...
@@ -469,7 +485,7 @@ class Parser(xml.sax.ContentHandler):
# SAX parser callbacks
#
def
startElement
(
self
,
tag
,
attrs
):
child
=
Node
(
tag
,
self
.
_locator
.
ge
tLineNumber
()
)
child
=
Node
(
tag
,
self
.
parser
.
Curren
tLineNumber
)
for
(
name
,
value
)
in
attrs
.
items
():
child
.
_set
(
name
,
value
)
self
.
current
.
append
(
child
)
...
...
@@ -483,6 +499,11 @@ class Parser(xml.sax.ContentHandler):
if
not
char
.
isspace
():
self
.
current
[
-
1
].
_append
(
char
)
def
comment
(
self
,
data
):
# Ugly hack, ...
parent
=
self
.
current
[
-
1
]
parent
.
_add
(
Node
(
''
,
self
.
parser
.
CurrentLineNumber
,
{
''
:
data
}))
def
parse
(
url
):
"""
Parse the given
'
url
'
returning an easily traversable tree
"""
return
Parser
(
url
).
tree
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment