当前位置: 首页>>代码示例>>Python>>正文


Python FileAsObj.rm方法代码示例

本文整理汇总了Python中fileasobj.FileAsObj.rm方法的典型用法代码示例。如果您正苦于以下问题:Python FileAsObj.rm方法的具体用法?Python FileAsObj.rm怎么用?Python FileAsObj.rm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fileasobj.FileAsObj的用法示例。


在下文中一共展示了FileAsObj.rm方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: example_all

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [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)
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:28,代码来源:examples.py

示例2: test_rm_failure

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
 def test_rm_failure(self):
     """ Test wrong param type in rm() """
     test_file = FileAsObj()
     with self.assertRaises(TypeError):
         test_file.rm(1)
     with self.assertRaises(TypeError):
         test_file.rm(True)
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:9,代码来源:tests_fileasobj.py

示例3: client_delete

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [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
开发者ID:jhazelwo,项目名称:ksdj,代码行数:52,代码来源:kickstart.py

示例4: vlan_delete

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [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
开发者ID:jhazelwo,项目名称:ksdj,代码行数:26,代码来源:kickstart.py

示例5: test_remove_multi

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
 def test_remove_multi(self):
     test_file = FileAsObj(TestFile, verbose=True)
     self.assertTrue(test_file.rm(test_file.grep('#')))
开发者ID:nullpass,项目名称:fileasobj,代码行数:5,代码来源:test_fileasobj.py

示例6: print

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

x = '.*mail[0-9]'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

x = 'tld$'
print('Find {0}'.format(x))
print(test_file.egrep(x))
print('---')

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))
开发者ID:jkudria,项目名称:fileasobj,代码行数:33,代码来源:tests.py

示例7: print

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
print(my_file + 'foo')


#
# Add line even if it exists in the file already.
my_file.add('foo', unique=False)


#
# 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'))

开发者ID:jkudria,项目名称:fileasobj,代码行数:31,代码来源:examples.py

示例8: test_remove_multi

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [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('#')))
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:7,代码来源:tests_fileasobj.py

示例9: client_create

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [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
#.........这里部分代码省略.........
开发者ID:jhazelwo,项目名称:ksdj,代码行数:103,代码来源:kickstart.py

示例10: example_remove_lines_matching_string

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
def example_remove_lines_matching_string():
    """ Remove all lines that ARE '# T0DO: remove this line.' (This matches an entire line.) """
    my_file = FileAsObj('/tmp/example_file.txt')
    my_file.rm('# T0DO: remove this line.')
    my_file.save()
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:7,代码来源:examples.py

示例11: example_remove_lines_matching_substring

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [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()
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:7,代码来源:examples.py

示例12: example_write_file_to_disk_if_changed

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
def example_write_file_to_disk_if_changed():
    """ Try to remove all comments from a file, and save it if changes were made. """
    my_file = FileAsObj('/tmp/example_file.txt')
    my_file.rm(my_file.egrep('^#'))
    if my_file.changed:
        my_file.save()
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:8,代码来源:examples.py

示例13: example_remove_lines_matching_string_with_print

# 需要导入模块: from fileasobj import FileAsObj [as 别名]
# 或者: from fileasobj.FileAsObj import rm [as 别名]
def example_remove_lines_matching_string_with_print():
    """ Remove all lines that ARE "# T0DO: remove this line." and print(True|False) if my_file was changed. """
    my_file = FileAsObj('/tmp/example_file.txt')
    print(my_file.rm('# T0DO: remove this line.'))
    my_file.save()
开发者ID:jhazelwo,项目名称:python-fileasobj,代码行数:7,代码来源:examples.py


注:本文中的fileasobj.FileAsObj.rm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。