Beispiel #1
0
    def save(self, path=None, common=None, sudo_password=None):

        from stat import ST_MODE
        fn_stat = 744

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            if time_delta < 0.001:
                pass

        path = path or self.path
        path = path.encode(co.getLocalEncoding())

        if sudo_password:
            # 先修改系统hosts文件的权限
            fn_stat = oct(os.stat(path)[ST_MODE])[-3:]
            cmd = "echo '%s' | sudo -S chmod %s %s" % (sudo_password, 766,
                                                       path)
            os.popen(cmd)

        try:
            # 写系统hosts
            open(path, "w").write(self.contentWithCommon(common))

        finally:
            if sudo_password:
                # 再将系统hosts文件的权限改回来
                cmd = "echo '%s' | sudo -S chmod %s %s" % (sudo_password,
                                                           fn_stat, path)
                os.popen(cmd)

        self.last_save_time = time.time()

        return True
Beispiel #2
0
    def save(self, path=None, common=None, sudo_password=None):

        from stat import ST_MODE
        fn_stat = 744

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            if time_delta < 0.001:
                pass

        path = path or self.path
        path = path.encode(co.getLocalEncoding())

        if sudo_password:
            # 先修改系统hosts文件的权限
            fn_stat = oct(os.stat(path)[ST_MODE])[-3:]
            cmd = "echo '%s' | sudo -S chmod %s %s" % (sudo_password, 766, path)
            os.popen(cmd)

        # 写系统hosts
        open(path, "w").write(self.contentWithCommon(common))

        if sudo_password:
            # 再将系统hosts文件的权限改回来
            cmd = "echo '%s' | sudo -S chmod %s %s" % (sudo_password, fn_stat, path)
            os.popen(cmd)

        self.last_save_time = time.time()

        return True
Beispiel #3
0
    def __init__(
        self,
        mainjob,
        instance_name,
        parent=None,
        id=wx.ID_ANY,
        title=None,
        pos=wx.DefaultPosition,
        size=wx.DefaultSize,
        style=wx.DEFAULT_FRAME_STYLE,
        version=None,
        working_path=None,
        taskbar_icon=None,
    ):
        u""""""

        self.mainjob = mainjob
        self.instance_name = instance_name
        self.version = version
        self.default_title = "SwitchHosts! %s" % self.version
        self.sudo_password = ""

        ui.Frame.__init__(self, parent, id, title or self.default_title, pos,
                          size, style)

        self.taskbar_icon = taskbar_icon or TaskBarIcon(self)
        if taskbar_icon:
            self.taskbar_icon.setMainFrame(self)

        self.latest_stable_version = "0"
        self.__sys_hosts_path = None
        self.local_encoding = co.getLocalEncoding()
        self.sys_type = co.getSystemType()

        if working_path:
            working_path = working_path.decode(self.local_encoding)
            self.working_path = working_path
            self.configs_path = os.path.join(self.working_path, "configs.json")
            self.hosts_path = os.path.join(self.working_path, "hosts")
            if not os.path.isdir(self.hosts_path):
                os.makedirs(self.hosts_path)

        self.active_fn = os.path.join(self.working_path, ".active")
        self.task_qu = Queue.Queue(4096)
        self.startBackThreads(2)
        self.makeHostsContextMenu()

        self.init2()
        self.initBind()

        self.task_qu.put(self.chkActive)
Beispiel #4
0
    def save(self, path=None, common=None):

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            if time_delta < 0.001:
                pass

        path = path or self.path
        path = path.encode(co.getLocalEncoding())
        open(path, "w").write(self.contentWithCommon(common))

        self.last_save_time = time.time()

        return True
Beispiel #5
0
    def __init__(
        self,
        mainjob,
        instance_name,
        parent=None,
        id=wx.ID_ANY,
        title=None,
        pos=wx.DefaultPosition,
        size=wx.DefaultSize,
        style=wx.DEFAULT_FRAME_STYLE,
        version=None,
        working_path=None,
        taskbar_icon=None,
    ):
        u""""""

        self.mainjob = mainjob
        self.instance_name = instance_name
        self.version = version
        self.default_title = "SwitchHosts! %s" % self.version
        self.sudo_password = ""

        ui.Frame.__init__(self, parent, id, title or self.default_title, pos, size, style)

        self.taskbar_icon = taskbar_icon or TaskBarIcon(self)
        if taskbar_icon:
            self.taskbar_icon.setMainFrame(self)

        self.latest_stable_version = "0"
        self.__sys_hosts_path = None
        self.local_encoding = co.getLocalEncoding()
        self.sys_type = co.getSystemType()

        if working_path:
            working_path = working_path.decode(self.local_encoding)
            self.working_path = working_path
            self.configs_path = os.path.join(self.working_path, "configs.json")
            self.hosts_path = os.path.join(self.working_path, "hosts")
            if not os.path.isdir(self.hosts_path):
                os.makedirs(self.hosts_path)

        self.active_fn = os.path.join(self.working_path, ".active")
        self.task_qu = Queue.Queue(4096)
        self.startBackThreads(2)
        self.makeHostsContextMenu()

        self.init2()
        self.initBind()

        self.task_qu.put(self.chkActive)
Beispiel #6
0
    def save(self, path=None, common=None):

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            if time_delta < 0.001:
                pass

        path = path or self.path
        path = path.encode(co.getLocalEncoding())
        open(path, "w").write(self.contentWithCommon(common))

        self.last_save_time = time.time()

        return True
Beispiel #7
0
    def save(self, path=None, extraContent=""):

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
#            co.log(time_delta)
            if time_delta < 0.001:
                pass
#                return False

        path = path or self.path
        path = path.encode(co.getLocalEncoding())

        open(path, "w").write( self.full_content + self.encodeText(extraContent) )
        self.last_save_time = time.time()

        return True
Beispiel #8
0
    def save(self, path=None, extraContent=""):

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            #            co.log(time_delta)
            if time_delta < 0.001:
                pass
#                return False

        path = path or self.path
        path = path.encode(co.getLocalEncoding())

        open(path,
             "w").write(self.full_content + self.encodeText(extraContent))
        self.last_save_time = time.time()

        return True
Beispiel #9
0
    def save(self, path=None):

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
#            co.log(time_delta)
            if time_delta < 0.01:
                return False

        path = path or self.path
        try:
            path = path.encode(co.getLocalEncoding())
        except Exception:
            pass

        open(path, "w").write(self.full_content)
        self.last_save_time = time.time()

        return True
Beispiel #10
0
    def save(self, path=None, common=None, sudo_password=None):

        from stat import ST_MODE
        # fn_stat = 744

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            if time_delta < 0.001:
                pass

        path = path or self.path
        path = path.encode(co.getLocalEncoding())
        fn_stat = None
        backup_name = None

        if os.path.isfile(path):
            fn_stat = oct(os.stat(path)[ST_MODE])[-3:]
            backup_name = self.makeBackupHostsName(path)
            self.tryToBackupOriginalHosts(path, backup_name, sudo_password)

        if sudo_password:
            # 先修改系统hosts文件的权限
            cmd = [
                "echo '%s' | sudo -S chmod %s %s" % (sudo_password, 766, path),
                ]
            os.popen(";".join(cmd))

        try:
            # 写系统hosts
            open(path, "w").write(self.contentWithCommon(common))

        finally:
            if sudo_password and fn_stat and backup_name:
                # 再将系统hosts文件的权限改回来
                cmd = [
                    "echo '%s' | sudo -S chmod %s %s" % (sudo_password, fn_stat, path),
                    "echo '%s' | sudo -S chmod %s %s" % (sudo_password, fn_stat, backup_name),
                ]
                os.popen(";".join(cmd))

        self.last_save_time = time.time()

        return True
Beispiel #11
0
    def save(self, path=None, common=None, sudo_password=None):

        from stat import ST_MODE
        # fn_stat = 744

        if self.last_save_time:
            time_delta = time.time() - self.last_save_time
            if time_delta < 0.001:
                pass

        path = path or self.path
        path = path.encode(co.getLocalEncoding())
        fn_stat = oct(os.stat(path)[ST_MODE])[-3:]

        backup_name = self.makeBackupHostsName(path)
        self.tryToBackupOriginalHosts(path, backup_name, sudo_password)

        if sudo_password:
            # 先修改系统hosts文件的权限
            cmd = [
                "echo '%s' | sudo -S chmod %s %s" % (sudo_password, 766, path),
            ]
            os.popen(";".join(cmd))

        try:
            # 写系统hosts
            open(path, "w").write(self.contentWithCommon(common))

        finally:
            if sudo_password:
                # 再将系统hosts文件的权限改回来
                cmd = [
                    "echo '%s' | sudo -S chmod %s %s" %
                    (sudo_password, fn_stat, path),
                    "echo '%s' | sudo -S chmod %s %s" %
                    (sudo_password, fn_stat, backup_name),
                ]
                os.popen(";".join(cmd))

        self.last_save_time = time.time()

        return True