/usr/share/pyshared/freevo/util/fileops.py is in python-freevo 1.9.2b2-4.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 | # -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# Some File Operation Utilities
# -----------------------------------------------------------------------
# $Id: fileops.py 11905 2011-11-14 21:54:46Z adam $
# -----------------------------------------------------------------------
# Freevo - A Home Theater PC framework
# Copyright (C) 2002 Krister Lagerstrom, et al.
# Please see the file freevo/Docs/CREDITS for a complete list of authors.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MER-
# CHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# -----------------------------------------------------------------------
"""
Some File Operation Utilities
"""
import logging
logger = logging.getLogger("freevo.util.fileops")
import os
import sys
from stat import *
from statvfs import *
import string
import copy
import subprocess
from subprocess import PIPE
try:
import cPickle as pickle
except ImportError:
import pickle
import fnmatch
import traceback
# image stuff
import kaa.imlib2
from kaa.metadata.image import EXIF as exif
if float(sys.version[0:3]) < 2.3:
PICKLE_PROTOCOL = 1
else:
PICKLE_PROTOCOL = pickle.HIGHEST_PROTOCOL
if traceback.extract_stack()[0][0].find('install.py') == -1:
# Configuration file. Determines where to look for AVI/MP3 files, etc
import config
# import stuff from util.misc
import misc
#
#
# misc file ops
#
#
def getdirnames(dirname, softlinks=True, sort=True):
"""
Get all subdirectories in the given directory.
Returns a list that is case insensitive sorted.
"""
if not dirname.endswith('/'):
dirname += '/'
try:
dirnames = [ dirname + dname for dname in os.listdir(dirname) if \
os.path.isdir(dirname + dname) and \
(softlinks or not os.path.islink(dirname + dname))]
except OSError:
return []
dirnames.sort(lambda l, o: cmp(l.upper(), o.upper()))
return dirnames
def gzopen(file):
"""
open a gzip file and return the fd. If it's not a gzip file, try
to open it as plain text file.
"""
import gzip
m = open(file)
magic = m.read(2)
m.close
if magic == '\037\213':
f = gzip.open(file)
else:
f = open(file)
return f
def readfile(filename):
"""
return the complete file as list
"""
fd = open(str(filename), 'r')
ret = fd.readlines()
fd.close()
return ret
def human_size(space):
"""
Convert a number of bytes into a human readable/understandable size.
"""
ret = ''
metrics = ['bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
space = float(space)
for p, suffix in enumerate(metrics):
div = pow(1000, p)
s = space / float(div)
if s < 1000.0:
if p == 0 and s == 1:
return '1 byte'
break
result = '%.1f' % s
if result.endswith('.0'):
result = result[:-2]
return result + suffix
def freespace(path):
"""
freespace(path) -> integer
Return the number of bytes available to the user on the file system
pointed to by path.
"""
try:
s = os.statvfs(path)
return s[F_BAVAIL] * long(s[F_BSIZE])
except OSError, why:
_debug_('"%r: %s' % (path, why), DWARNING)
return 0
def totalspace(path):
"""
totalspace(path) -> integer
Return the number of total bytes available on the file system
pointed to by path.
"""
try:
s = os.statvfs(path)
return s[F_BLOCKS] * long(s[F_BSIZE])
except OSError, why:
_debug_('"%r: %s' % (path, why), DWARNING)
return 0
def touch(file):
"""
touch a file (maybe create it)
"""
try:
fd = open(file,'w+')
fd.close()
except IOError:
pass
return 0
def rmrf_helper(result, dirname, names):
"""
help function for rm -rf
"""
for name in names:
fullpath = os.path.join(dirname, name)
if os.path.isfile(fullpath):
result[0].append(fullpath)
result[1] = [dirname] + result[1]
return result
def rmrf(top=None):
"""
Pure python version of 'rm -rf'
"""
if not top == '/' and not top == '' and not top == ' ' and top:
files = [[],[]]
path_walk = os.path.walk(top, rmrf_helper, files)
for f in files[0]:
try:
os.remove(f)
except IOError:
pass
for d in files[1]:
try:
os.rmdir(d)
except (IOError, OSError), why:
_debug_('rmrf: %s' % why, DWARNING)
#
#
# find files by pattern or suffix
#
#
def match_suffix(filename, suffixlist):
"""
Check if a filename ends in a given suffix, case is ignored.
"""
fsuffix = vfs.splitext(filename)[1].lower()[1:]
for suffix in suffixlist:
if fsuffix == suffix:
return 1
return 0
def match_files(dirname, suffix_list):
"""
Find all files in a directory that has matches a list of suffixes.
Returns a list that is case insensitive sorted.
"""
try:
files = [ vfs.join(dirname, fname) for fname in os.listdir(dirname) if vfs.isfile(vfs.join(dirname, fname)) ]
except OSError, why:
_debug_('Cannot match files in "%s": %s' % (dirname, why), DWARNING)
return []
matches = [ fname for fname in files if match_suffix(fname, suffix_list) ]
matches.sort(lambda l, o: cmp(l.upper(), o.upper()))
return matches
def find_matches(files, suffix_list):
"""
return all files in 'files' that match one of the suffixes in 'suffix_list'
The correct implementation is
filter(lambda x: os.path.splitext(x)[1].lower()[1:] in suffix_list, files)
but this one should also work and is _much_ faster. On a Duron 800, Python 2.2
and 700 photos 0.01 secs instead of 0.2.
"""
return filter(lambda x: x[x.rfind('.')+1:].lower() in suffix_list, files)
def match_files_recursively_helper(result, dirname, names):
"""
help function for match_files_recursively
"""
if dirname:
(files, dirs, follow_symlinks) = result
dirs.append(os.path.abspath(dirname))
lastdir = dirname[dirname.rfind('/'):]
if len(lastdir) > 1 and lastdir[1] == '.':
# ignore directories starting with a dot
# Note: subdirectories of that dir will still be searched
return result
for name in names:
fullpath = vfs.join(dirname, name)
if os.path.islink(fullpath) and os.path.isdir(fullpath):
if follow_symlinks and not os.path.realpath(fullpath) in dirs:
os.path.walk(fullpath, match_files_recursively_helper, result)
else:
files.append(fullpath)
return result
def match_files_recursively_skip_protected(result, dirname, names):
"""
help function for match_files_recursively_skip_protected, skipping directories with a .password file
"""
if dirname:
(files, dirs, follow_symlinks) = result
dirs.append(os.path.abspath(dirname))
lastdir = dirname[dirname.rfind('/'):]
if os.path.exists(os.path.join(dirname, '.password')):
_debug_('.password found in "%s"' % dirname)
return result
if len(lastdir) > 1 and lastdir[1] == '.':
return result
for name in names:
fullpath = vfs.join(dirname, name)
if os.path.islink(fullpath) and os.path.isdir(fullpath):
if follow_symlinks and not os.path.realpath(fullpath) in dirs:
os.path.walk(fullpath, match_files_recursively_skip_protected, result)
else:
files.append(fullpath)
return result
def match_files_recursively(dir, suffix_list, skip_password=False, follow_symlinks=False):
"""
get all files matching suffix_list in the dir and in it's subdirectories
"""
all_files = []
if skip_password:
os.path.walk(dir, match_files_recursively_skip_protected, (all_files, [], follow_symlinks))
else:
os.path.walk(dir, match_files_recursively_helper, (all_files, [], follow_symlinks))
matches = misc.unique([f for f in all_files if match_suffix(f, suffix_list) ])
matches.sort(lambda l, o: cmp(l.upper(), o.upper()))
return matches
def get_subdirs_recursively(dir, follow_symlinks=False):
"""
get all subdirectories recursively in the given directory
"""
all_files = []
os.path.walk(dir, match_files_recursively_helper, (all_files, [], follow_symlinks))
matches = misc.unique([f for f in all_files if os.path.isdir(f) ])
matches.sort(lambda l, o: cmp(l.upper(), o.upper()))
return matches
def recursefolders(root, recurse=0, pattern='*', return_folders=0):
"""
Before anyone asks why I didn't use os.path.walk; it's simple,
os.path.walk is difficult, clunky and doesn't work right in my
mind.
Here's how you use this function::
songs = recursefolders('/media/Music/Guttermouth', 1, '*.mp3', 1)
for song in songs:
print song
Should be easy to add to the mp3.py app.
"""
# initialize
result = []
# must have at least root folder
try:
names = os.listdir(root)
except os.error:
return result
# expand pattern
pattern = pattern or '*'
pat_list = string.splitfields(pattern, ';')
# check each file
for name in names:
fullname = os.path.normpath(vfs.join(root, name))
# grab if it matches our pattern and entry type
for pat in pat_list:
if fnmatch.fnmatch(name, pat):
if vfs.isfile(fullname) or (return_folders and vfs.isdir(fullname)):
result.append(fullname)
continue
# recursively scan other folders, appending results
if recurse:
if vfs.isdir(fullname) and not vfs.islink(fullname):
result += recursefolders(fullname, recurse, pattern, return_folders)
return result
#
#
# Media stuff
#
#
mounted_dirs = []
def is_mounted(dir):
"""
return if the dir is mounted (according to the internal list "mounted_dirs"
that this module keeps)
(according to the internal list "mounted_dirs" that this module keeps)
"""
_debug_('is_mounted(dir=%r)' % (dir,), 2)
global mounted_dirs
return dir in mounted_dirs
def mount(dir, force=False):
"""
mount a directory
"""
if config.ROM_DRIVES_AUTOFS:
return
_debug_('mount(dir=%r, force=%r)' % (dir, force), 2)
if not dir:
return
global mounted_dirs
if not os.path.ismount(dir):
p = subprocess.Popen(['mount', dir], stdout=PIPE, stderr=PIPE)
rc = p.wait()
so, se = p.communicate()
if rc in (1,32,):
_debug_('mounting %r: %s' % (dir, se), DWARNING)
traceback.print_stack()
if os.path.ismount(dir) and not dir in mounted_dirs:
mounted_dirs.append(dir)
if force and not dir in mounted_dirs:
mounted_dirs.append(dir)
def umount(dir):
"""
umount a directory
"""
if config.ROM_DRIVES_AUTOFS:
return
_debug_('umount(dir=%r)' % (dir,), 2)
if not dir:
return
global mounted_dirs
if os.path.ismount(dir):
p = subprocess.Popen(['umount', dir], stdout=PIPE, stderr=PIPE)
rc = p.wait()
so, se = p.communicate()
if rc in (1,):
_debug_('unmounting %r: %s' % (dir, se), DWARNING)
if not os.path.ismount(dir) and dir in mounted_dirs:
mounted_dirs.remove(dir)
def umount_all():
"""
umount all mounted directories
note that this function should be used only in emergency situations,
e.g. when freevo crashes and exits; since it does not take care
of mount reference counting (see rom_drives.py)
"""
_debug_('umount_all()', 2)
global mounted_dirs
for d in copy.copy(mounted_dirs):
umount(d)
mounted_dirs = []
def resolve_media_mountdir(*arg):
"""
get the mount point of the media with media_id
"""
_debug_('resolve_media_mountdir(arg=%r)' % (arg,), 2)
if len(arg) == 1 and isinstance(arg[0], dict):
media_id = arg[0]['media_id']
file = arg[0]['file']
elif len(arg) == 2:
media_id = arg[0]
file = arg[1]
else:
raise KeyError
mountdir = ''
# Find on what media it is located
for media in config.REMOVABLE_MEDIA:
if media_id == media.id:
# Then set the filename
mountdir = media.mountdir
file = vfs.join(media.mountdir, file)
break
return mountdir, file
def check_media(media_id):
"""
check if media_id is a valid media in one of the drives
"""
_debug_('check_media(media_id=%r)' % (media_id,), 2)
for media in config.REMOVABLE_MEDIA:
if media_id == media.id:
return media
return None
def is_net_mount(path):
"""
Return true if it thinks the path specified is a on network mount point.
"""
info = get_mount_point(path)
return info[2] in ('nfs', 'smbfs', 'cifs')
def get_mount_point(path):
"""
Find the mount point for the specified path and return information on the
mount point.
A list of device, path, options, dump, pass.
"""
mounts = {}
f = open('/proc/mounts')
for l in f:
mount = l.split(' ')
mounts[mount[1]] = mount
f.close()
path = os.path.abspath(path)
mnt_point = ''
while path:
if os.path.ismount(path):
mnt_point = path
break
if os.path.islink(path):
path = os.readlink(path)
continue
parent = os.path.dirname(path)
if parent == path:
path = ''
mnt_point = parent
else:
path = parent
return mounts[mnt_point]
#
#
# pickle helper
#
#
def read_pickle(file):
"""
read a file with pickle
"""
try:
f = vfs.open(file, 'r')
data = pickle.load(f)
f.close()
return data
except:
return None
def save_pickle(data, file):
"""
save the data with pickle to the given file
"""
try:
if vfs.isfile(file):
vfs.unlink(file)
f = vfs.open(file, 'w')
pickle.dump(data, f, PICKLE_PROTOCOL)
f.close()
except IOError:
_debug_('unable to save to cachefile %r' % (file,), DWARNING)
#
# cache utils
#
def read_thumbnail(filename):
"""
return image cached inside filename
"""
f = open(filename)
header = f.read(10)
if not header[:3] == 'FRI':
return read_pickle(filename)
data = f.read(), (ord(header[3]), ord(header[4])), header[5:].strip(' ')
f.close()
return data
def create_thumbnail(filename, thumbnail=None):
"""
cache image for faster access
"""
_debug_('create_thumbnail(filename=%r, thumbnail=%r)' % (filename, thumbnail), 2)
thumb = vfs.getoverlay(filename + '.raw')
image = None
if thumbnail:
try:
image = kaa.imlib2.open_from_memory(thumbnail)
except Exception, why:
_debug_('invalid thumbnail %r: %s' % (filename, why), DINFO)
if not image:
if __freevo_app__ == 'main':
try:
tags = exif.process_file(open(filename, 'rb'))
if tags.has_key('JPEGThumbnail'):
image = kaa.imlib2.open_from_memory(tags['JPEGThumbnail'])
except Exception, why:
_debug_('Error loading thumbnail %r: %s' % (filename, why), DWARNING)
if not image or image.width < 100 or image.height < 100:
try:
image = kaa.imlib2.open(filename)
except Exception, why:
_debug_('Cannot cache image %r: %s' % (filename, why), DWARNING)
return None
try:
if image.width > 255 or image.height > 255:
image.thumbnail((255,255))
mode = image.mode
if mode == 'P':
mode = 'RGB'
if mode == 'BGRA':
mode = 'RGBA'
data = (str(image.get_raw_data(format=mode)), image.size, mode)
f = vfs.open(thumb, 'w')
f.write('FRI%s%s%5s' % (chr(image.width), chr(image.height), mode))
f.write(data[0])
f.close()
del image
return data
except Exception, why:
_debug_('Cannot cache image %r: %s' % (filename, why), DWARNING)
return None
def cache_image(filename, thumbnail=None, use_exif=False):
"""
cache image for faster access, return cached image
"""
thumb = vfs.getoverlay(filename + '.raw')
try:
if os.stat(thumb)[ST_MTIME] > os.stat(filename)[ST_MTIME]:
data = read_thumbnail(thumb)
if data:
return data
except OSError:
pass
return create_thumbnail(filename, thumbnail)
def scale_rectangle_to_max(size, max_size):
"""
returns a scaled rectangle size fitting the size to max_size
this can be used to scale images for viewing in a browser and
retaining the aspect ratio
"""
try:
scaled_width = max_size[0]
scaled_height = max_size[1]
# if actual image aspect ratio > scaled image aspect ratio then scale height
if float(size[0]) / size[1] > float(scaled_width) / scaled_height:
scaled_height = scaled_width * size[1] / size[0]
else: # else scale width
scaled_width = scaled_height * size[0] / size[1]
except ZeroDivisionError:
pass
return (scaled_width, scaled_height)
def www_image_path(filename, subdir='.thumbs'):
"""
returns the path to the thumbnail image for a given filename
"""
dirname = os.path.dirname(filename)
file_base, file_extn = os.path.splitext(filename)
file_extn = file_extn.lower()
if file_extn == ".gif":
file_extn = ".jpg"
# the filename extension needs to be lowercase for imlib2 but we need to
# keep the original filename for cache to be able to clean the files
imagename = os.path.basename(file_base).lower() + file_extn
#imagepath = vfs.getwwwoverlay(imagename)
imagepath = os.path.join(dirname, subdir, imagename)
return imagepath
def www_image(filename, subdir, force=False, getsize=False, size=128, verbose=0):
"""
creates a webserver image image and returns its size.
"""
orientation_map = {
# vflip hflip orientate
1: (False, False, 0, 'Horizontal (normal)'),
2: (False, True, 0, 'Mirrored horizontal'),
3: (False, False, 2, 'Rotated 180'),
4: (True, False, 0, 'Mirrored vertical'),
5: (False, True, 3, 'Mirrored horizontal then rotated 90 CCW'),
6: (False, False, 1, 'Rotated 90 CW'),
7: (False, True, 1, 'Mirrored horizontal then rotated 90 CW'),
8: (False, False, 3, 'Rotated 90 CCW'),
}
imagename = www_image_path(filename, subdir)
if verbose >= 2:
print 'Checking image %r, force=%r getsize=%r' % (imagename, force, getsize)
if os.path.exists(imagename):
if not force:
if getsize:
image = kaa.imlib2.open(imagename)
return image.size
return (0, 0)
else:
force = True
try:
try:
if not os.path.isdir(os.path.dirname(imagename)):
if verbose >= 1:
print 'Make directory %r' % os.path.dirname(imagename)
os.makedirs(os.path.dirname(imagename), mode=04775)
except (OSError, IOError), why:
_debug_('error creating dir %s: %s' % (os.path.dirname(imagename), why), DWARNING)
raise
if force or os.stat(filename)[ST_MTIME] > os.stat(imagename)[ST_MTIME]:
orientation_str = ''
image = kaa.imlib2.open(filename)
try:
tags = exif.process_file(open(filename, 'rb'))
orientation = tags.get('Image Orientation').values[0]
flip_vert, flip_hori, orientate, orientate_str = orientation_map[orientation]
if orientation != 1:
if flip_vert:
image.flip_vertical()
if verbose >= 2:
print 'flipped vertically'
if flip_hori:
image.flip_horizontal()
if verbose >= 2:
print 'flipped horizontally'
if orientate:
if verbose >= 2:
print orientate_str
image.orientate(orientate)
except AttributeError, why:
pass
except Exception, why:
print why
if isinstance(size, tuple):
image = image.scale_preserve_aspect(size)
else:
image = image.scale_preserve_aspect((size, size))
image.save(imagename)
print 'Made image %r @ %s -> %r%s' % (imagename, size, image.size, orientation_str)
except IOError, why:
_debug_(why, DWARNING)
return (0, 0)
return image.size
def www_thumb_create(filename, force=False, getsize=False, size=config.WWW_IMAGE_THUMBNAIL_SIZE, verbose=0):
"""
creates a webserver image image and returns its size.
"""
try:
return www_image(filename, '.thumbs', force, getsize, size, verbose)
except OSError:
return (0, 0)
def www_image_create(filename, force=False, getsize=False, size=config.WWW_IMAGE_SIZE, verbose=0):
"""
creates a webserver image image and returns its size.
"""
try:
return www_image(filename, '.images', force, getsize, size, verbose)
except OSError:
return (0, 0)
|