def testOpenLocked(self): locked_f = helper.openLocked(config.data_dir + "/locked.file") assert locked_f with pytest.raises(BlockingIOError): locked_f_again = helper.openLocked(config.data_dir + "/locked.file") locked_f_different = helper.openLocked(config.data_dir + "/locked_different.file") locked_f.close() locked_f_different.close() os.unlink(locked_f.name) os.unlink(locked_f_different.name)
def sign(address, content, privatekey=None): if privatekey is None: with open(data_directory + "/users.json") as f: users = json.loads(f.read()) try: user = users[users.keys()[0]] privatekey = user["certs"]["zeroid.bit"]["auth_privatekey"] except KeyError: raise TypeError( "Private key for zeroid.bit not found in users.json") # Check for lock from util import helper try: with helper.openLocked("%s/lock.pid" % data_directory, "w") as f: pass # Could get lock; let's run normal sitePublish then from Site import Site site = Site(address, allow_create=False) site.content_manager.sign(inner_path=content, privatekey=privatekey, update_changed_files=True, remove_missing_optional=False) except IOError: # Could not get lock sign_socket(address, content, privatekey)
def publish(address, content): # Check for lock from util import helper try: with helper.openLocked("%s/lock.pid" % data_directory, "w") as f: pass # Could get lock; let's run normal sitePublish then from src import main as zeronet_lib zeronet_lib.actions.sitePublish(address, inner_path=content) except IOError: # Could not get lock publish_socket(address, content)
try: os.chmod(config.data_dir, stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR) except Exception, err: print "Can't change permission of %s: %s" % (config.data_dir, err) if not os.path.isfile("%s/sites.json" % config.data_dir): open("%s/sites.json" % config.data_dir, "w").write("{}") if not os.path.isfile("%s/users.json" % config.data_dir): open("%s/users.json" % config.data_dir, "w").write("{}") # Setup logging if config.action == "main": from util import helper log_file_path = "%s/debug.log" % config.log_dir try: lock = helper.openLocked("%s/lock.pid" % config.data_dir, "w") lock.write("%s" % os.getpid()) except IOError as err: print "Can't open lock file, your ZeroNet client is probably already running, exiting... (%s)" % err if config.open_browser: print "Opening browser: %s...", config.open_browser import webbrowser if config.open_browser == "default_browser": browser = webbrowser.get() else: browser = webbrowser.get(config.open_browser) browser.open("http://%s:%s/%s" % (config.ui_ip if config.ui_ip != "*" else "127.0.0.1", config.ui_port, config.homepage), new=2) sys.exit() if os.path.isfile("%s/debug.log" % config.log_dir): # Simple logrotate if os.path.isfile("%s/debug-last.log" % config.log_dir):
os.mkdir(config.log_dir) if not os.path.isdir(config.data_dir): os.mkdir(config.data_dir) if not os.path.isfile("%s/sites.json" % config.data_dir): open("%s/sites.json" % config.data_dir, "w").write("{}") os.chmod("%s/sites.json" % config.data_dir, stat.S_IRUSR | stat.S_IWUSR) if not os.path.isfile("%s/users.json" % config.data_dir): open("%s/users.json" % config.data_dir, "w").write("{}") os.chmod("%s/users.json" % config.data_dir, stat.S_IRUSR | stat.S_IWUSR) # Setup logging if config.action == "main": from util import helper log_file_path = "%s/debug.log" % config.log_dir try: helper.openLocked(log_file_path, "a") except IOError as err: print "Can't lock %s file, your ZeroNet client is probably already running, exiting... (%s)" % ( log_file_path, err) sys.exit() if os.path.isfile("%s/debug.log" % config.log_dir): # Simple logrotate if os.path.isfile("%s/debug-last.log" % config.log_dir): os.unlink("%s/debug-last.log" % config.log_dir) os.rename("%s/debug.log" % config.log_dir, "%s/debug-last.log" % config.log_dir) logging.basicConfig( format='[%(asctime)s] %(levelname)-8s %(name)s %(message)s', level=logging.DEBUG, stream=helper.openLocked(log_file_path, "a")) else:
# Create necessary files and dirs if not os.path.isdir(config.log_dir): os.mkdir(config.log_dir) if not os.path.isdir(config.data_dir): os.mkdir(config.data_dir) if not os.path.isfile("%s/sites.json" % config.data_dir): open("%s/sites.json" % config.data_dir, "w").write("{}") if not os.path.isfile("%s/users.json" % config.data_dir): open("%s/users.json" % config.data_dir, "w").write("{}") # Setup logging if config.action == "main": from util import helper log_file_path = "%s/debug.log" % config.log_dir try: helper.openLocked(log_file_path, "a") except IOError as err: print "Can't lock %s file, your ZeroNet client is probably already running, exiting... (%s)" % (log_file_path, err) sys.exit() if os.path.isfile("%s/debug.log" % config.log_dir): # Simple logrotate if os.path.isfile("%s/debug-last.log" % config.log_dir): os.unlink("%s/debug-last.log" % config.log_dir) os.rename("%s/debug.log" % config.log_dir, "%s/debug-last.log" % config.log_dir) logging.basicConfig( format='[%(asctime)s] %(levelname)-8s %(name)s %(message)s', level=logging.DEBUG, stream=helper.openLocked(log_file_path, "a") ) else: logging.basicConfig(level=logging.DEBUG, stream=open(os.devnull, "w")) # No file logging if action is not main
# Create necessary files and dirs if not os.path.isdir(config.log_dir): os.mkdir(config.log_dir) if not os.path.isdir(config.data_dir): os.mkdir(config.data_dir) if not os.path.isfile("%s/sites.json" % config.data_dir): open("%s/sites.json" % config.data_dir, "w").write("{}") if not os.path.isfile("%s/users.json" % config.data_dir): open("%s/users.json" % config.data_dir, "w").write("{}") # Setup logging if config.action == "main": from util import helper log_file_path = "%s/debug.log" % config.log_dir try: helper.openLocked(log_file_path, "a") except IOError as err: print "Can't lock %s file, your ZeroNet client is probably already running, exiting... (%s)" % (log_file_path, err) sys.exit() if os.path.isfile("%s/debug.log" % config.log_dir): # Simple logrotate if os.path.isfile("%s/debug-last.log" % config.log_dir): os.unlink("%s/debug-last.log" % config.log_dir) os.rename("%s/debug.log" % config.log_dir, "%s/debug-last.log" % config.log_dir) logging.basicConfig( format='[%(asctime)s] %(levelname)-8s %(name)s %(message)s', level=logging.DEBUG, stream=helper.openLocked(log_file_path, "a") ) else: log_file_path = "%s/cmd.log" % config.log_dir logging.basicConfig(