8000 osx_linux.py 改进 每次只需执行该脚本即可 · Issue #17 · smarthosts/smarthosts · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

osx_linux.py 改进 每次只需执行该脚本即可 #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
larryhu opened this issue Jun 7, 2014 · 0 comments
Open

osx_linux.py 改进 每次只需执行该脚本即可 #17

larryhu opened this issue Jun 7, 2014 · 0 comments

Comments

@larryhu
Copy link
larryhu commented Jun 7, 2014

osx_linux.py脚本文件改进,每次只需要执行该脚本即可,无需对原hosts文件手动修改,解决问题如下:
场景需求: 假如我的hosts文件里配置了几个家庭主机的host信息(或者公司的host信息)例:

192.168.1.9  hosts1
192.168.1.6  hosts2
192.168.1.8  hosts3

原来脚本执行之后,需要手动从hosts.bak文件恢复,新脚本将无需改动自动在后面追加云hosts配置信息,当再次更新时会自动查找并删除原云hosts配置信息,家庭或企业hosts信息将保留,无需手动修改

osx_linux.py脚本文件以及patch见最后面.

tag: googlecode地址能访问的几率远远低于github,故将云hosts的URL文件修改为github地址

非常感谢你们的项目,愿尽绵薄之力,谢谢!
另敬请对外提供donate页面

osx_linux.py脚本文件

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#author:rikugun

import sys
import urllib
import os
from shutil import copyfile


HOSTS_URL='https://raw.githubusercontent.com/smarthosts/smarthosts/master/trunk/hosts'
# HOSTS_URL='https://smarthosts.googlecode.com/svn/trunk/hosts'

LOCAL_HOSTS='/etc/hosts'
TAG_START = '###start smarthosts###'
TAG_END = '###end smarthosts###'

def main():
    """主函数"""
    #备份文件
    copyfile(LOCAL_HOSTS,'hosts.bak')

    lines = list()
    start = -1
    end = -1
    with open(LOCAL_HOSTS, 'r') as f:
        for i, line in enumerate(f):
            if line.startswith(TAG_START):
                start = i
            elif line.startswith(TAG_END):
                end = i
                line=''
            if end == -1 and start == -1:
                lines.append(line)
            elif end > -1:
                lines.append(line)

    with open(LOCAL_HOSTS,'w') as f:
        for line in lines:
            f.write(line)
        f.write(TAG_START+os.linesep)
        for line in urllib.urlopen(HOSTS_URL):
            f.write(line.strip()+os.linesep)
        f.write(TAG_END)

    print "success!"

if __name__ == '__main__':
    if len(sys.argv)>1:
        HOSTS_URL = sys.argv[1]
    main()

patch文件内容

diff --git a/trunk/osx_linux.py b/trunk/osx_linux.py
index 35be490..2827978 100644
--- a/trunk/osx_linux.py
+++ b/trunk/osx_linux.py
@@ -8,20 +8,41 @@ import os
 from shutil import copyfile


-HOSTS_URL='https://smarthosts.googlecode.com/svn/trunk/hosts'
+HOSTS_URL='https://raw.githubusercontent.com/smarthosts/smarthosts/master/trunk/hosts'
+# HOSTS_URL='https://smarthosts.googlecode.com/svn/trunk/hosts'

 LOCAL_HOSTS='/etc/hosts'
+TAG_START = '###start smarthosts###'
+TAG_END = '###end smarthosts###'

 def main():
     """主函数"""
     #备份文件
     copyfile(LOCAL_HOSTS,'hosts.bak')
-    with open(LOCAL_HOSTS,'aw') as hosts:
-        hosts.write(os.linesep)
-        #转义windows和unix的换行
-        for line in urllib.urlopen(HOSTS_URL):
-            hosts.write(line.strip()+os.linesep)
+    
+    lines = list()
+    start = -1
+    end = -1
+    with open(LOCAL_HOSTS, 'r') as f:
+        for i, line in enumerate(f):
+            if line.startswith(TAG_START):
+                start = i
+            elif line.startswith(TAG_END):
+                end = i
+                line=''
+            if end == -1 and start == -1:
+                lines.append(line)
+            elif end > -1:
+                lines.append(line)

+    with open(LOCAL_HOSTS,'w') as f:
+        for line in lines:
+            f.write(line)
+        f.write(TAG_START+os.linesep)
+        for line in urllib.urlopen(HOSTS_URL):
+            f.write(line.strip()+os.linesep)
+        f.write(TAG_END)
+            
     print "success!"

 if __name__ == '__main__':

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0