Python script for cutting nginx configuration files

Source: Internet
Author: User

#!/usr/bin/env pythonimport osimport sysdef text_with_color (color,text): def base (color,text): Color_code = {"BLACK": 3                  0, "Red": +, "green": +, "yellow": +, "blue": 34, "White": \033[;%sm%s\033[0m return "% (Color_code[color],text) return base (Color,tex T) def read_nginx_file (file_name): Try:with open (file_name) as F:all_content = F.readlines () exce PT Exception,err:print "Open nginx config file error,err_msg:", Err Sys.exit (3) temp = All_conten T.pop () Count = 0 head = [] Servers = {} Server_count = 1 while count < Len (all_content): if                Not All_content[count].strip (). Replace ("", "") = = "server{": If Len (All_content[count].strip ())! = 0: Head.append (All_content[count]) Count + = 1 Else:break while count <       Len (all_content): Servers[server_count] = [] Servers[server_count].append (All_content[count]) count + = 1 while Count & Lt Len (all_content): If not All_content[count].strip (). Replace ("", "") = = "server{": If Len (All_con            Tent[count].strip ())! = 0:servers[server_count].append (All_content[count]) count + = 1 Else:server_count + = 1 break head.extend (["\ n", "include conf.d/*.conf" , "\ n", "}"]) return head,servers def write_main_conf_file (old_file_name,config): Os.rename (old_file_name,old _file_name + ". Back") Main_file = open ("nginx.conf", "WB") for I in Config:main_file.write (i) main_file.cl         OSE () def write_server_conf_file (server): If not os.path.exists ("CONF.D"): Os.mkdir ("CONF.D") for I in server: If "SERVER_NAME" in I.split (): server_file = open ("CONF.D" + "/" + I.split () [1].strip (";") + ". conf", "W B ") for I in ServEr:server_file.write (i) server_file.close () def file_format_clear (config): count = 0 for i in config: Config[count] = I.strip () + "\ n" Count + = 1 space = "" Indent_count = 0 count = 0 for i in con Fig:if "{" in Config[count]: config[count] = space * Indent_count + i indent_count +  = 1 Elif "}" in Config[count]: config[count] = space * (indent_count-1) + i indent_count-= 1 Else:config[count] = space * Indent_count + I count + = 1if __name__ = = ' __main__ ': File            _name = "nginx.conf" head,servers = Read_nginx_file (file_name) #load nginx config file File_format_clear (head) #format clear for I in Servers.key                                    S (): File_format_clear (Servers[i]) write_main_conf_file (file_name,head) #write Main config fiLe for i in Servers.keys (): #write server config file Write_se    Rver_conf_file (Servers[i]) print Text_with_color ("green", "Nginx config file cut success.") Print Text_with_color ("Yellow", "Please copy nginx.conf and CONF.D to your Nginx conf directory.")

  

Python script for cutting nginx configuration files

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.