Revision edcd876b lib/utils.py
b/lib/utils.py | ||
---|---|---|
1437 | 1437 |
@param aliases: the list of aliases to add for the hostname |
1438 | 1438 |
|
1439 | 1439 |
""" |
1440 |
# FIXME: use WriteFile + fn rather than duplicating its efforts |
|
1441 | 1440 |
# Ensure aliases are unique |
1442 | 1441 |
aliases = UniqueSequence([hostname] + aliases)[1:] |
1443 | 1442 |
|
1444 |
fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(file_name)) |
|
1445 |
try: |
|
1446 |
out = os.fdopen(fd, 'w') |
|
1443 |
def _WriteEtcHosts(fd): |
|
1444 |
# Duplicating file descriptor because os.fdopen's result will automatically |
|
1445 |
# close the descriptor, but we would still like to have its functionality. |
|
1446 |
out = os.fdopen(os.dup(fd), "w") |
|
1447 | 1447 |
try: |
1448 |
f = open(file_name, 'r') |
|
1449 |
try: |
|
1450 |
for line in f: |
|
1451 |
fields = line.split() |
|
1452 |
if fields and not fields[0].startswith('#') and ip == fields[0]: |
|
1453 |
continue |
|
1454 |
out.write(line) |
|
1455 |
|
|
1456 |
out.write("%s\t%s" % (ip, hostname)) |
|
1457 |
if aliases: |
|
1458 |
out.write(" %s" % ' '.join(aliases)) |
|
1459 |
out.write('\n') |
|
1460 |
|
|
1461 |
out.flush() |
|
1462 |
os.fsync(out) |
|
1463 |
os.chmod(tmpname, 0644) |
|
1464 |
os.rename(tmpname, file_name) |
|
1465 |
finally: |
|
1466 |
f.close() |
|
1448 |
for line in ReadFile(file_name).splitlines(True): |
|
1449 |
fields = line.split() |
|
1450 |
if fields and not fields[0].startswith("#") and ip == fields[0]: |
|
1451 |
continue |
|
1452 |
out.write(line) |
|
1453 |
|
|
1454 |
out.write("%s\t%s" % (ip, hostname)) |
|
1455 |
if aliases: |
|
1456 |
out.write(" %s" % " ".join(aliases)) |
|
1457 |
out.write("\n") |
|
1458 |
out.flush() |
|
1467 | 1459 |
finally: |
1468 | 1460 |
out.close() |
1469 |
except: |
|
1470 |
RemoveFile(tmpname) |
|
1471 |
raise |
|
1461 |
|
|
1462 |
WriteFile(file_name, fn=_WriteEtcHosts, mode=0644) |
|
1472 | 1463 |
|
1473 | 1464 |
|
1474 | 1465 |
def AddHostToEtcHosts(hostname): |
... | ... | |
1494 | 1485 |
@param hostname: the hostname to be removed |
1495 | 1486 |
|
1496 | 1487 |
""" |
1497 |
# FIXME: use WriteFile + fn rather than duplicating its efforts
|
|
1498 |
fd, tmpname = tempfile.mkstemp(dir=os.path.dirname(file_name))
|
|
1499 |
try:
|
|
1500 |
out = os.fdopen(fd, 'w')
|
|
1488 |
def _WriteEtcHosts(fd):
|
|
1489 |
# Duplicating file descriptor because os.fdopen's result will automatically
|
|
1490 |
# close the descriptor, but we would still like to have its functionality.
|
|
1491 |
out = os.fdopen(os.dup(fd), "w")
|
|
1501 | 1492 |
try: |
1502 |
f = open(file_name, 'r') |
|
1503 |
try: |
|
1504 |
for line in f: |
|
1505 |
fields = line.split() |
|
1506 |
if len(fields) > 1 and not fields[0].startswith('#'): |
|
1507 |
names = fields[1:] |
|
1508 |
if hostname in names: |
|
1509 |
while hostname in names: |
|
1510 |
names.remove(hostname) |
|
1511 |
if names: |
|
1512 |
out.write("%s %s\n" % (fields[0], ' '.join(names))) |
|
1513 |
continue |
|
1514 |
|
|
1515 |
out.write(line) |
|
1493 |
for line in ReadFile(file_name).splitlines(True): |
|
1494 |
fields = line.split() |
|
1495 |
if len(fields) > 1 and not fields[0].startswith("#"): |
|
1496 |
names = fields[1:] |
|
1497 |
if hostname in names: |
|
1498 |
while hostname in names: |
|
1499 |
names.remove(hostname) |
|
1500 |
if names: |
|
1501 |
out.write("%s %s\n" % (fields[0], " ".join(names))) |
|
1502 |
continue |
|
1516 | 1503 |
|
1517 |
out.flush() |
|
1518 |
os.fsync(out) |
|
1519 |
os.chmod(tmpname, 0644) |
|
1520 |
os.rename(tmpname, file_name) |
|
1521 |
finally: |
|
1522 |
f.close() |
|
1504 |
out.write(line) |
|
1505 |
|
|
1506 |
out.flush() |
|
1523 | 1507 |
finally: |
1524 | 1508 |
out.close() |
1525 |
except: |
|
1526 |
RemoveFile(tmpname) |
|
1527 |
raise |
|
1509 |
|
|
1510 |
WriteFile(file_name, fn=_WriteEtcHosts, mode=0644) |
|
1528 | 1511 |
|
1529 | 1512 |
|
1530 | 1513 |
def RemoveHostFromEtcHosts(hostname): |
Also available in: Unified diff