def test__writefile(self): """ common.writefile: writes contents to file. """ # text file filename = self.make_file() data = 'this is a test' + os.linesep self.assertTrue(common.writefile(filename, data, binary=False)) self.assertEqual(open(filename, 'r').read(), data) # binary file filename = self.make_file() data = '\x00\x12\x34\x56\x78' self.assertTrue(common.writefile(filename, data, binary=True)) self.assertEqual(open(filename, 'rb').read(), data)
def _register_pidfile(filename): """ Registers a pid file for the current process which will cleaned up when the process terminates. `filename` Filename to save pid to. """ if common.writefile(filename, str(os.getpid()) + os.linesep): os.chmod(filename, 0644) # rw-r--r-- def _cleanup_pid(): """ Removes pidfile. """ common.safe_remove_file(filename) atexit.register(_cleanup_pid)
def _handle_block(self, task, disable=False): """ Handles blocking domains using hosts file. `task` ``Task`` instance. `disable` Set to ``True``, to turn off blocking and restore hosts file; otherwise, ``False`` will enable blocking by updating hosts file. Returns boolean. """ backup_file = os.path.join(task.task_dir, '.hosts.bak') self.orig_data = self.orig_data or common.readfile(backup_file) self.last_updated = self.last_updated or -1 if not self.orig_data: # should't attempt restore without good original data, bail if disable: return False # attempt to fetch data from the source self.orig_data = common.readfile(self.hosts_file) if not self.orig_data: return False # restore backup if not os.path.exists(backup_file): common.writefile(backup_file, self.orig_data) # bail early if hosts file modification time hasn't changed try: should_write = (disable or self.last_updated != os.path.getmtime(self.hosts_file)) except OSError: should_write = True # file was removed, let's write! if not should_write: return True # make copy of original data, in case we need to modify data = self.orig_data # if not restoring, tack on domains mapped # to localhost to end of file data if not disable: # convert the set to a list and sort domains = list(self.domains) domains.sort() data += ('\n'.join('127.0.0.1\t{0}\t# FOCUS' .format(d) for d in domains) + '\n') # make temp file with new host file data with tempfile.NamedTemporaryFile(prefix='focus_') as tempf: tempf.write(data) tempf.flush() # overwrite hosts file with our modified copy. if not self.run_root('cp "{0}" "{1}"'.format(tempf.name, self.hosts_file)): return False # MacOS X generally requires flushing the system dns cache to pick # up changes to the hosts file: # dscacheutil -flushcache or lookupd -flushcache if common.IS_MACOSX: dscacheutil, lookupd = [common.which(x) for x in ('dscacheutil', 'lookupd')] self.run_root(' '.join([dscacheutil or lookupd, '-flushcache'])) if disable: common.safe_remove_file(backup_file) # cleanup the backup # store last modification time try: self.last_updated = os.path.getmtime(self.hosts_file) except OSError: # file was removed, let's update next time around self.last_updated = -1 return True
def _handle_block(self, task, disable=False): """ Handles blocking domains using hosts file. `task` ``Task`` instance. `disable` Set to ``True``, to turn off blocking and restore hosts file; otherwise, ``False`` will enable blocking by updating hosts file. Returns boolean. """ backup_file = os.path.join(task.task_dir, '.hosts.bak') self.orig_data = self.orig_data or common.readfile(backup_file) self.last_updated = self.last_updated or -1 if not self.orig_data: # should't attempt restore without good original data, bail if disable: return False # attempt to fetch data from the source self.orig_data = common.readfile(self.hosts_file) if not self.orig_data: return False # restore backup if not os.path.exists(backup_file): common.writefile(backup_file, self.orig_data) # bail early if hosts file modification time hasn't changed try: should_write = ( disable or self.last_updated != os.path.getmtime(self.hosts_file)) except OSError: should_write = True # file was removed, let's write! if not should_write: return True # make copy of original data, in case we need to modify data = self.orig_data # if not restoring, tack on domains mapped # to localhost to end of file data if not disable: # convert the set to a list and sort domains = list(self.domains) domains.sort() data += ('\n'.join('127.0.0.1\t{0}\t# FOCUS'.format(d) for d in domains) + '\n') # make temp file with new host file data with tempfile.NamedTemporaryFile(prefix='focus_') as tempf: tempf.write(data) tempf.flush() # overwrite hosts file with our modified copy. if not self.run_root('cp "{0}" "{1}"'.format( tempf.name, self.hosts_file)): return False # MacOS X generally requires flushing the system dns cache to pick # up changes to the hosts file: # dscacheutil -flushcache or lookupd -flushcache if common.IS_MACOSX: dscacheutil, lookupd = [ common.which(x) for x in ('dscacheutil', 'lookupd') ] self.run_root(' '.join([dscacheutil or lookupd, '-flushcache'])) if disable: common.safe_remove_file(backup_file) # cleanup the backup # store last modification time try: self.last_updated = os.path.getmtime(self.hosts_file) except OSError: # file was removed, let's update next time around self.last_updated = -1 return True