[Sugar-devel] [PATCH v2 Browse] Remove temporary downloaded (cancelled) files SL #3973

Martin Langhoff martin.langhoff at gmail.com
Tue Oct 2 12:43:36 EDT 2012


On Tue, Oct 2, 2012 at 9:59 AM, Manuel Kaufmann <humitos at gmail.com> wrote:
> +        try:
> +            uptime_proc = open('/proc/uptime', 'r').read()
> +            uptime_seconds = float(uptime_proc.split()[0])
> +            uptime = datetime.timedelta(seconds=uptime_seconds)
> +        except:
> +            logging.warning('/proc/uptime could not be read')
> +            uptime = None

I like.

> +
> +        temp_path = os.path.join(self.get_activity_root(), 'instance')
> +        now = datetime.datetime.now()

Tiny optimization and elegance improvement: just calculate your cutoff
here. The cutoff is the largest of
 - "yesterday" (now - 1 day)
 - boottime  (now - uptime) -- if you managed to read uptime

> +        for f in os.listdir(temp_path):
> +            if f.startswith('.goutputstream-'):
> +                fpath = os.path.join(temp_path, f)
> +                mtime = os.path.getmtime(fpath)
> +                mdate = datetime.datetime.fromtimestamp(mtime)
> +                delta = now - mdate
> +                if delta.days > 0 or \
> +                        (uptime is not None and (now - uptime) > mdate):

replace this confitional with

 if mdate < cutoff:

> +                    logging.warning('Removing old temporary file: %s', fpath)
> +                    try:
> +                        os.remove(fpath)
> +                    except OSError:
> +                        logging.error('Temporary file could not be '
> +                                      'removed: %s', fpath)

Should catch both OSError _and_ IOError. Or perhaps EnvironmentError
which, strange as the name might sound, is the granddaddy of both.




m
-- 
 martin.langhoff at gmail.com
 martin at laptop.org -- Software Architect - OLPC
 - ask interesting questions
 - don't get distracted with shiny stuff  - working code first
 - http://wiki.laptop.org/go/User:Martinlanghoff


More information about the Sugar-devel mailing list