本文整理汇总了Python中fileasobj.FileAsObj.grep方法的典型用法代码示例。如果您正苦于以下问题:Python FileAsObj.grep方法的具体用法?Python FileAsObj.grep怎么用?Python FileAsObj.grep使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类fileasobj.FileAsObj
的用法示例。
在下文中一共展示了FileAsObj.grep方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: client_delete
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def client_delete(client):
"""
Files to update: (these files must exist)
pxeconf
hostsfile
allowfile
Files to remove: (ok if already do not exist)
tftp_pxe
hostname_ks
client_sh
"""
# client = obj.old
hostname = client.name
mac_addr = client.mac
client_ip = client.ip
dashmac = '-'.join(mac_addr.split(':'))
dashmac = '01-{0}'.format(dashmac)
#
file = FileAsObj(etc_pxe_clients_conf, verbose=True) # /opt/kickstart/etc/pxe_clients.conf
file.rm(file.grep(mac_addr))
pattern = ' {0}.{1} '.format(hostname, ks_domainname)
file.rm(file.grep(pattern))
if not file.virgin:
file.write()
#
file = FileAsObj(etc_hosts, verbose=True) # /etc/hosts
pattern = '{0} # Kickstart Client '.format(hostname)
file.rm(file.grep(pattern))
pattern = '^{0} '.format(client_ip)
file.rm(file.egrep(pattern))
if not file.virgin:
file.write()
#
file = FileAsObj(etc_hosts_allow, verbose=True) # /etc/hosts.allow
pattern = 'ALL: [email protected]{0} : ALLOW'.format(client_ip)
file.rm(pattern)
if not file.virgin:
file.write()
#
for this in [os.path.join(TFTP, dashmac),
os.path.join(KS_CONF_DIR, '{0}.ks'.format(hostname)),
os.path.join(CLIENT_DIR, '{0}.sh'.format(hostname)),
]:
if os.path.isfile(this):
newname = '{0}_{1}'.format(os.path.basename(this), int(time.time()))
target = os.path.join(BK_DIR, newname)
shutil.move(this, target)
#
return
示例2: example_all
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def example_all():
"""
Use a bunch of methods on a file.
"""
my_file = FileAsObj()
my_file.filename = '/tmp/example_file.txt'
my_file.add('# First change!')
my_file.save()
my_file = FileAsObj('/tmp/example_file.txt')
my_file.unique = True
my_file.sorted = True
my_file.add('1')
my_file.add('1')
my_file.add('2')
my_file.add('20 foo')
my_file.add('200 bar')
my_file.add('# Comment')
my_file.unique = False
my_file.add('# Comment')
my_file.add('# Comment')
my_file.unique = True
my_file.rm(my_file.egrep('^#.*'))
my_file.rm(my_file.grep('foo'))
my_file.replace(my_file.egrep('^2'), 'This line was replaced.')
print(my_file)
print(my_file.log)
示例3: test_count_comment_empty
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def test_count_comment_empty(self):
""" Test __len__ method. """
test_file = FileAsObj()
test_file.add(TESTCONTENTS)
subject = '#'
result = test_file.grep(subject)
self.assertTrue(result)
self.assertEqual(len(result), 18)
示例4: vlan_delete
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def vlan_delete(obj):
"""
Remove vlan from dhcpd.conf and delete vlan_XX.conf file
OK if vlan not found.
The only real errors are if we are unable to edit dhcpd.conf or if there a clients in the VLAN.
No return values; just raise an exception if there's a problem.
"""
if obj.client.count() is not 0:
raise ValueError('Unable to delete, there are clients in this vlan!')
file_name = 'vlan_{0}.conf'.format(obj.name)
#
dhcpd_conf = FileAsObj(os.path.join(KSROOT, 'dhcpd.conf')) # /opt/kickstart/etc/dhcpd.conf
#
dhcpd_conf.rm(dhcpd_conf.grep(file_name))
if not dhcpd_conf.virgin:
dhcpd_conf.write()
#
filename = os.path.join(KSROOT, file_name) # /opt/kickstart/etc/vlan_{name}.conf
try:
os.remove(filename)
except:
pass
return
示例5: test_count_comment_empty
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def test_count_comment_empty(self):
test_file = FileAsObj(TestFile, verbose=True)
foo = test_file.grep('#')
self.assertEqual(len(foo), 18)
示例6: test_remove_multi
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def test_remove_multi(self):
test_file = FileAsObj(TestFile, verbose=True)
self.assertTrue(test_file.rm(test_file.grep('#')))
示例7: print
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
x = '^10.*'
print('Remove {0} from {1}, RESULT={2}'.format(x, test_file.egrep(x), test_file.rm(test_file.egrep(x))))
print('---')
x = ' h0st.*'
print('Remove {0} from {1}, RESULT={2}'.format(
x,
test_file.egrep(x),
test_file.rm(test_file.egrep(x)))
)
print('---')
x = '#'
print('Remove whole line "{0}" from {1}, RESULT={2}'.format(
x,
test_file.grep(x),
test_file.rm(x))
)
print(test_file.grep(x))
print('---')
old = '172.19.18.17 freebird.example.com'
new = '172.19.18.17 freebird.example.com # Added 1976.10.29 -jh'
print('Replace {0} with {1}'.format(old, new))
print(test_file.replace(old, new))
print('---')
x = '^[ ]+#.*'
print('Remove {0} from {1}, RESULT={2}'.format(
x,
test_file.egrep(x),
示例8: file
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
#
# Print number of lines in the file (as it exists in memory)
print(len(my_file))
#
# Remove all lines that are "foo bar"
my_file.rm('foo bar')
# or
print(my_file - 'foo bar')
#
# Get all lines that contain a #
result = my_file.grep('#')
#
# Remove all lines that contain the word "foo"
# NOTE: this is dangerous and rarely needed.
my_file.rm(my_file.grep('foo'))
#
# Check for line in file, if exists remove it
if 'foo bar' in my_file:
my_file.rm('foo bar')
# or
my_file.rm(my_file.check('foo bar'))
# or
示例9: test_remove_multi
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def test_remove_multi(self):
""" Test deleting multiple lines. """
test_file = FileAsObj()
test_file.contents = TESTCONTENTS.split('\n')
self.assertTrue(test_file.rm(test_file.grep('#')))
示例10: client_create
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def client_create(form, old=False):
"""
1. Get IP if Null
2. Figure out VLAN from IP
2a. make sure IP is valid for VLAN
3. unquie add to etc/hosts
4. unquie add to etc/hosts.allow
5. unquie add client in pxe_clients.conf
6. create ks.d/hostname.ks file (kickstart)
7. create clients.d/hostname.sh (shell variables)
8. create tftpboot/01-mac-address
on first failure generate message and return false.
"""
hostname = form.cleaned_data['name'].lower()
mac_addr = form.cleaned_data['mac'].lower()
build_type = form.instance.get_build_type_display().lower()
os_release = form.cleaned_data['os_release']
#
# We use ext4 by default, if it's a EL5.x build we use ext3.
fstype = 'ext4'
if form.cleaned_data['os_release'] == 'el5':
fstype = 'ext3'
#
#
if not form.cleaned_data['ip']:
#
# No client_IP given, get from DNS (or fail)
form.instance.ip = gethostbyname(hostname)
if Client.objects.filter(ip=form.instance.ip).count() is not 0:
raise ValueError('DNS returned {0} but that IP already in use.'.format(form.instance.ip))
#
if not form.cleaned_data['vlan']:
for thisv in VLAN.objects.all():
network_cidr = '{0}/{1}'.format(thisv.network, thisv.get_cidr_display())
getvlan = ipcalc.Network(network_cidr)
if form.instance.ip in getvlan:
form.instance.vlan = thisv
if not form.instance.vlan:
raise ValueError('IP {0} not valid for any known vlans!'.format(form.instance.ip))
#
testing_net = ipcalc.Network('{0}/{1}'.format(form.instance.vlan.network, form.instance.vlan.get_cidr_display()))
if form.instance.ip not in testing_net:
raise ValueError('IP {0} not valid for VLAN {1}.'.format(form.instance.ip, form.instance.vlan))
#
# Validate that client IP is not already a server IP, network or gateway.
if VLAN.objects.filter(server_ip=form.instance.ip).count() is not 0 or \
VLAN.objects.filter(gateway=form.instance.ip).count() is not 0 or \
VLAN.objects.filter(network=form.instance.ip).count() is not 0:
raise ValueError('Client IP "{0}" is in use by a VLAN object.'.format(form.instance.ip))
#
# etc/hosts
hostsfile = FileAsObj(etc_hosts, verbose=True)
if hostsfile.egrep('^[0-9].*[ \\t]{0}'.format(hostname)):
raise ValueError('Failed to update {0}, client "{1}" already exists!'.format(etc_hosts, hostname))
if hostsfile.egrep('^{0}[ \\t]'.format(form.instance.ip)):
raise ValueError('Failed to update {0}, IP "{1}" already exists!'.format(etc_hosts, form.instance.ip))
toadd = '{CLIENT_IP} {HOSTNAME}.{DOMAINNAME} {HOSTNAME} # Kickstart Client added {NOW}'.format(
CLIENT_IP=form.instance.ip,
HOSTNAME=hostname,
DOMAINNAME=ks_domainname,
NOW=time.strftime('%Y.%m.%d', time.localtime()),
)
hostsfile.add(toadd)
#
allowfile = FileAsObj(etc_hosts_allow, verbose=True) # /etc/hosts.allow
pattern = 'ALL: [email protected]{0} : ALLOW'.format(form.instance.ip)
if pattern not in allowfile.contents:
allowfile.add(pattern)
#
pxeconf = FileAsObj(etc_pxe_clients_conf) # /opt/kickstart/etc/pxe_clients.conf
if pxeconf.grep(' {0}.{1} '.format(hostname, ks_domainname)):
raise ValueError('{0} already present in {1}'.format(hostname, etc_pxe_clients_conf))
if pxeconf.grep(mac_addr):
raise ValueError('Failed to update {0}, MAC "{1}" already present!'.format(etc_pxe_clients_conf, mac_addr))
toadd = 'host {HOSTNAME}.{DOMAINNAME} {{ hardware ethernet {MAC} ; fixed-address {CLIENT_IP} ;}}'.format(
HOSTNAME=hostname,
DOMAINNAME=ks_domainname,
MAC=mac_addr,
CLIENT_IP=form.instance.ip,
)
pxeconf.add(toadd)
pxeconf.add('}')
#
# tftpboot/pxe.default/01-mac-address.lower().replace(":","-")
dashmac = '-'.join(mac_addr.split(':'))
dashmac = '01-{0}'.format(dashmac)
filename = os.path.join(TFTP, dashmac)
if os.path.isfile(filename):
raise ValueError('Failed to add client. The file "{0}" already exists!'.format(filename))
tftp_pxe = FileAsObj()
tftp_pxe.filename = filename
tftp_pxe.contents = base_tftp.format(
KS_CONF_DIR=KS_CONF_DIR,
OS_RELEASE=os_release,
HOSTNAME=hostname,
SERVER_IP=form.instance.vlan.server_ip,
).split('\n')
#
# {ksroot}/etc/ks.d/{hostname}.ks - kickstart config file
#.........这里部分代码省略.........
示例11: example_remove_lines_matching_substring
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def example_remove_lines_matching_substring():
""" Remove all lines that CONTAIN 'bad string' """
my_file = FileAsObj('/tmp/example_file.txt')
my_file.rm(my_file.grep('bad string'))
my_file.save()
示例12: example_search_for_word_using_grep
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def example_search_for_word_using_grep():
""" Find a complete line in file. """
my_file = FileAsObj('/etc/hosts')
if my_file.grep('localhost'):
return True
示例13: example_print_lines_matching_substring
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def example_print_lines_matching_substring():
""" Print all lines that contain a # anywhere in the line. """
my_file = FileAsObj('/tmp/example_file.txt')
print(my_file.grep('#'))
示例14: example_get_lines_matching_substring
# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import grep [as 别名]
def example_get_lines_matching_substring():
""" Get all lines that contain a # anywhere in the line. """
my_file = FileAsObj('/tmp/example_file.txt')
result = my_file.grep('#')
return result