From 9a61f6f84ea28591e6d0cde0033f4f4b6661bc97 Mon Sep 17 00:00:00 2001 From: Aleksey Lim Date: Fri, 05 Oct 2012 19:44:51 +0000 Subject: Keep all tmp files in one place --- diff --git a/sugar-network-server b/sugar-network-server index b5feb0e..3a1639b 100755 --- a/sugar-network-server +++ b/sugar-network-server @@ -24,7 +24,7 @@ import active_document as ad import sugar_network_webui as webui from active_toolkit import coroutine, application from active_toolkit.options import Option -from sugar_network import node, local +from sugar_network import node, local, toolkit from sugar_network.local.mounts import LocalMount from sugar_network.local.mountset import Mountset from sugar_network.local.mounts import LocalMount @@ -40,9 +40,8 @@ class Application(application.Daemon): jobs = coroutine.Pool() def run(self): - if node.tmpdir.value and not exists(node.tmpdir.value): - os.makedirs(node.tmpdir.value) - sneakernet.TMPDIR = node.tmpdir.value + if toolkit.tmpdir.value and not exists(toolkit.tmpdir.value): + os.makedirs(toolkit.tmpdir.value) ssl_args = {} if node.keyfile.value: @@ -90,6 +89,7 @@ locale.setlocale(locale.LC_ALL, '') Option.seek('main', application) Option.seek('webui', webui) Option.seek('node', node) +Option.seek('node', [toolkit.tmpdir]) Option.seek('stats', stats) Option.seek('obs', obs) Option.seek('active-document', ad) diff --git a/sugar-network-service b/sugar-network-service index 3553c60..5ce403a 100755 --- a/sugar-network-service +++ b/sugar-network-service @@ -81,9 +81,8 @@ class Application(application.Application): application.logdir.value = sugar.profile_path('logs') application.rundir.value = join(local.local_root.value, 'run') - if not exists(local.tmpdir.value): - os.makedirs(local.tmpdir.value) - sneakernet.TMPDIR = local.tmpdir.value + if not exists(toolkit.tmpdir.value): + os.makedirs(toolkit.tmpdir.value) coroutine.signal(signal.SIGCHLD, self.__SIGCHLD_cb) @@ -203,12 +202,12 @@ application.debug.value = sugar.logger_level() node.trust_users.value = True # If tmpfs is mounted to /tmp, `os.fstat()` will return 0 free space # and will brake offline synchronization logic -local.tmpdir.value = sugar.profile_path('tmp') +toolkit.tmpdir.value = sugar.profile_path('tmp') Option.seek('main', [application.debug]) Option.seek('webui', webui) Option.seek('local', local) -Option.seek('local', [sugar.keyfile]) +Option.seek('local', [sugar.keyfile, toolkit.tmpdir]) Option.seek('node', [node.port, node.sync_dirs]) Option.seek('stats', stats) Option.seek('active-document', ad) diff --git a/sugar_network/local/__init__.py b/sugar_network/local/__init__.py index 6428025..9a3c8f8 100644 --- a/sugar_network/local/__init__.py +++ b/sugar_network/local/__init__.py @@ -65,10 +65,6 @@ lazy_open = Option( 'do not open all indexes at once on startup', default=True, type_cast=Option.bool_cast, action='store_true') -tmpdir = Option( - 'if specified, use this directory for temporary files, such files ' - 'might take hunder of megabytes while node synchronizing') - ipc_port = Option( 'port number to listen for incomming connections from IPC clients', default=5001, type_cast=int, name='ipc_port') diff --git a/sugar_network/local/activities.py b/sugar_network/local/activities.py index 8b1e052..d7d89a3 100644 --- a/sugar_network/local/activities.py +++ b/sugar_network/local/activities.py @@ -16,7 +16,6 @@ import os import hashlib import logging -import tempfile from os.path import join, exists, lexists, relpath, dirname, basename, isdir from os.path import abspath, islink @@ -164,7 +163,7 @@ class _Inotify(Inotify): icon_path = join(spec.root, spec['icon']) if exists(icon_path): self._contexts.set_blob(context, 'artifact_icon', icon_path) - with tempfile.NamedTemporaryFile() as f: + with toolkit.NamedTemporaryFile() as f: toolkit.svg_to_png(icon_path, f.name, 32, 32) self._contexts.set_blob(context, 'icon', f.name) diff --git a/sugar_network/node/__init__.py b/sugar_network/node/__init__.py index f88e510..26ad337 100644 --- a/sugar_network/node/__init__.py +++ b/sugar_network/node/__init__.py @@ -47,10 +47,6 @@ find_limit = Option( 'limit the resulting list for search requests', default=32, type_cast=int) -tmpdir = Option( - 'if specified, use this directory for temporary files, such files ' - 'might take hunder of megabytes while node synchronizing') - sync_dirs = Option( 'colon separated list of paths to directories to synchronize with ' 'master server', diff --git a/sugar_network/node/commands.py b/sugar_network/node/commands.py index ae63305..f5babcd 100644 --- a/sugar_network/node/commands.py +++ b/sugar_network/node/commands.py @@ -15,11 +15,10 @@ import logging import hashlib -import tempfile from os.path import exists, join import active_document as ad -from sugar_network import node +from sugar_network import node, toolkit from sugar_network.node.sync_master import SyncCommands from sugar_network.node import auth from sugar_network.resources.volume import Commands, VolumeCommands @@ -224,7 +223,7 @@ class MasterCommands(NodeCommands, SyncCommands): def _load_pubkey(pubkey): pubkey = pubkey.strip() try: - with tempfile.NamedTemporaryFile() as key_file: + with toolkit.NamedTemporaryFile() as key_file: key_file.file.write(pubkey) key_file.file.flush() # SSH key needs to be converted to PKCS8 to ket M2Crypto read it diff --git a/sugar_network/node/sync_master.py b/sugar_network/node/sync_master.py index cfa2568..bb2f70e 100644 --- a/sugar_network/node/sync_master.py +++ b/sugar_network/node/sync_master.py @@ -24,7 +24,7 @@ from os.path import exists, join from pylru import lrucache import active_document as ad -from sugar_network import node +from sugar_network import node, toolkit from sugar_network.toolkit.sneakernet import InPacket, OutBufferPacket, \ OutPacket, DiskFull from sugar_network.toolkit.collection import Sequence @@ -172,7 +172,7 @@ class _Pull(object): self.exception = None self.seconds_remained = 0 self.content_type = None - self._path = join(node.tmpdir.value, pull_key + '.pull') + self._path = join(toolkit.tmpdir.value, pull_key + '.pull') self._job = None if exists(self._path): diff --git a/sugar_network/resources/volume.py b/sugar_network/resources/volume.py index 98fb2a0..a8f732b 100644 --- a/sugar_network/resources/volume.py +++ b/sugar_network/resources/volume.py @@ -15,12 +15,11 @@ import json import logging -import tempfile from os.path import join import active_document as ad from active_document import directory as ad_directory -from sugar_network import local, node +from sugar_network import local, node, toolkit from sugar_network.toolkit.sneakernet import DiskFull from sugar_network.toolkit.collection import Sequence from sugar_network.toolkit import http @@ -161,7 +160,7 @@ class Volume(ad.SingleVolume): content_length = response.headers.get('Content-Length') content_length = int(content_length) if content_length else 0 - ostream = tempfile.NamedTemporaryFile() + ostream = toolkit.NamedTemporaryFile() try: chunk_size = min(content_length, BUFFER_SIZE) # pylint: disable-msg=E1103 diff --git a/sugar_network/toolkit/__init__.py b/sugar_network/toolkit/__init__.py index e1f7949..4dc64b3 100644 --- a/sugar_network/toolkit/__init__.py +++ b/sugar_network/toolkit/__init__.py @@ -16,11 +16,19 @@ import os import logging import hashlib +import tempfile from os.path import isfile, lexists, exists, dirname +from active_toolkit.options import Option from active_toolkit import util +tmpdir = Option( + 'if specified, use this directory for temporary files; such files ' + 'might take considerable number of bytes while downloading of ' + 'synchronizing Sugar Network content', + name='tmpdir') + _logger = logging.getLogger('toolkit') @@ -79,3 +87,9 @@ def svg_to_png(src_path, dst_path, width, height): svg.render_cairo(context) surface.write_to_png(dst_path) + + +def NamedTemporaryFile(*args, **kwargs): + if tmpdir.value: + kwargs['dir'] = tmpdir.value + return tempfile.NamedTemporaryFile(*args, **kwargs) diff --git a/sugar_network/toolkit/http.py b/sugar_network/toolkit/http.py index f820d08..eed6ebd 100644 --- a/sugar_network/toolkit/http.py +++ b/sugar_network/toolkit/http.py @@ -22,7 +22,6 @@ import time import shutil import logging import hashlib -import tempfile from os.path import isdir, exists, dirname, join import requests @@ -33,7 +32,7 @@ import active_document as ad from sugar_network.zerosugar import Bundle from active_toolkit.sockets import decode_multipart, BUFFER_SIZE from sugar_network.toolkit import sugar -from sugar_network import local +from sugar_network import local, toolkit from active_toolkit import coroutine, enforce # Let toolkit.http work in concurrence @@ -233,7 +232,7 @@ class Client(object): shutil.rmtree(out_path, ignore_errors=True) raise elif extract: - tmp_file = tempfile.NamedTemporaryFile(delete=False) + tmp_file = toolkit.NamedTemporaryFile(delete=False) try: if fetch(tmp_file): tmp_file.close() diff --git a/sugar_network/toolkit/sneakernet.py b/sugar_network/toolkit/sneakernet.py index 0a2c3df..69a0d55 100644 --- a/sugar_network/toolkit/sneakernet.py +++ b/sugar_network/toolkit/sneakernet.py @@ -19,18 +19,16 @@ import time import gzip import tarfile import logging -import tempfile from cStringIO import StringIO from contextlib import contextmanager from os.path import join, exists import active_document as ad +from sugar_network import toolkit from active_toolkit.sockets import BUFFER_SIZE from active_toolkit import util, enforce -TMPDIR = None - _RESERVED_SIZE = 1024 * 1024 _MAX_PACKET_SIZE = 1024 * 1024 * 100 _PACKET_COMPRESS_MODE = 'gz' @@ -66,7 +64,7 @@ class InPacket(object): self._file = stream = file(path, 'rb') elif not hasattr(stream, 'seek'): # tarfile/gzip/zip might require seeking - self._file = tempfile.TemporaryFile(dir=TMPDIR) + self._file = toolkit.NamedTemporaryFile() if hasattr(stream, 'read'): while True: @@ -325,7 +323,7 @@ class OutPacket(object): self._flush(0, True) limit = self._enforce_limit() - with tempfile.TemporaryFile(dir=TMPDIR) as arcfile: + with toolkit.NamedTemporaryFile() as arcfile: while True: limit -= len(chunk) if limit <= 0: @@ -418,7 +416,7 @@ class OutFilePacket(OutPacket): def __init__(self, root=None, **kwargs): stream = None if root is None: - stream = tempfile.NamedTemporaryFile(dir=TMPDIR) + stream = toolkit.NamedTemporaryFile() OutPacket.__init__(self, root=root, stream=stream, **kwargs) diff --git a/tests/__init__.py b/tests/__init__.py index 725508a..09e3fa0 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -22,7 +22,7 @@ from sugar_network.toolkit.router import Router from sugar_network.local.ipc_client import Router as IPCRouter from sugar_network.local.mounts import HomeMount, RemoteMount from sugar_network.local.mountset import Mountset -from sugar_network import local, node +from sugar_network import local, node, toolkit from sugar_network.resources.user import User from sugar_network.resources.context import Context from sugar_network.resources.implementation import Implementation @@ -73,7 +73,6 @@ class Test(unittest.TestCase): ad.index_flush_timeout.value = 0 ad.index_flush_threshold.value = 1 node.find_limit.value = 1024 - node.tmpdir.value = tmpdir + '/tmp' node.data_root.value = tmpdir node.sync_dirs.value = [] node.static_url.value = None @@ -94,6 +93,7 @@ class Test(unittest.TestCase): obs._client = None http._RECONNECTION_NUMBER = 0 auth._config = None + toolkit.tmpdir.value = tmpdir + '/tmp' Volume.RESOURCES = [ 'sugar_network.resources.user', @@ -107,7 +107,6 @@ class Test(unittest.TestCase): sneakernet._RESERVED_SIZE = 0 sneakernet._PACKET_COMPRESS_MODE = '' - sneakernet.TMPDIR = tmpdir + '/tmp' os.makedirs('tmp') self._logfile = file(self.logfile + '.out', 'a') -- cgit v0.9.1