This file is indexed.

/usr/share/pyshared/freevo/config.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
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# -*- coding: iso-8859-1 -*-
# -----------------------------------------------------------------------
# Handle the configuration file init. Also start logging.
# -----------------------------------------------------------------------
# $Id: config.py 11904 2011-11-12 22:26:54Z 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
#
# -----------------------------------------------------------------------

"""
Handle the configuration file initialization
and start logging.

Try to find the freevo_config.py config file in the following places:
    1. ~/.freevo/freevo_config.py       The user's private config
    2. /etc/freevo/freevo_config.py     Systemwide config
    3. ./freevo_config.py               Defaults from the freevo dist
"""
import logging
logger = logging.getLogger("freevo.config")

import sys, os, time, re, string, pwd
from threading import RLock
import setup_freevo
import traceback
import __builtin__
import locale
import logging

try:
    import freevo.version as version
    import freevo.revision as revision
except:
    import version
    import revision

DINFO = 0
DWARNING = -1
DERROR = -2
DCRITICAL = -3

locale.setlocale(locale.LC_TIME, '')

if sys.hexversion >= 0x02030000:
    import warnings
    warnings.simplefilter("ignore", category=FutureWarning)
    warnings.simplefilter("ignore", category=DeprecationWarning)

# For Internationalization purpose
# an exception is raised with Python 2.1 if LANG is unavailable.
import gettext
try:
    gettext.install('freevo', os.environ['FREEVO_LOCALE'], 1)
except: # unavailable, define '_' for all modules
    import __builtin__
    __builtin__.__dict__['_'] = lambda m: m


# temp solution until this is fixed to True and False
# in all freevo modules
__builtin__.__dict__['TRUE']  = 1
__builtin__.__dict__['FALSE'] = 0


# String helper function. Always use this function to detect if the
# object is a string or not. It checks against str and unicode
def __isstring__(s):
    return isinstance(s, str) or isinstance(s, unicode)

__builtin__.__dict__['isstring'] = __isstring__


class Logger:
    """
    Class to create a logger object which will send messages to stdout and log them
    into a logfile
    """
    def __init__(self, logger, logtype='(unknown)'):
        self.lineno = 1
        self.logtype = logtype
        self.logger = logger
        self.buffer = ''


    def write(self, msg):
        global lock
        if lock:
            lock.acquire()
        try:
            if isinstance(msg, unicode):
                msg = msg.encode(LOCALE, 'replace')
            sys.__stdout__.write(msg)
            sys.__stdout__.flush()
            self.buffer += msg
            pos = self.buffer.find('\n')
            start_pos = 0
            while pos != -1:
                self.logger.info('%s', self.buffer[start_pos:pos])
                sys.__stdout__.flush()

                start_pos = pos + 1
                pos = self.buffer.find('\n', start_pos)
            if start_pos:
                self.buffer = self.buffer[start_pos:]
        finally:
            if lock:
                lock.release()
        return

    def log(self, msg):
        self.write(msg + '\n')


    def flush(self):
        pass

    def close(self):
        pass


class VideoGroup:
    """
    """
    def __init__(self, vdev=None, vvbi='/dev/vbi', adev=None, input_type=None,
                 input_num=0, tuner_norm=None, tuner_chanlist=None,
                 tuner_type='internal', tuner_chan=None, irsend_trans=None,
                 record_group=None, desc='Freevo Default Video Group',
                 group_type='normal', cmd=None, avol=0):
        """
        Initialise an instance of a VideoGroup

        @ivar vdev: The video recording device, such as /dev/video0.
        @ivar vvbi: The video vbi device, such as /dev/vbi0.
        @ivar adev: The audio device, such as: None, /dev/dsp.
        @ivar avol: Default sound level for this videogroup (v4l2 value, 0 if not set)
        @ivar input_type: tuner, webcam
        @ivar input_num: The number of this input according to V4L
        @ivar tuner_type: internal (on a v4l device), or external (cable or sat box)
        @ivar tuner_norm: NTSC, PAL, SECAM
        @ivar tuner_chanlist: us-cable,
        @ivar tuner_chan: If using input_type=tuner and tuner_type=external set this to
            what channel it needs to be to get the signal, usually 3 or 4.
        @ivar irsend_trans: IR transmitter to use for multiple external tuners.
        @ivar record_group: VideoGroup that records for this tuner, default is to use the
            same device for record and play
        @ivar desc: A nice description for this VideoGroup.
        @ivar group_type: Special variable to identify devices like dvb or ivtv.  This
            can be one of: 'normal', 'ivtv', 'dvb', 'tvalsa' or 'webcam'.
        @ivar cmd: Command for execute external prog after the channel switched,
            such as 'sudo /usr/local/bin/setuptuner'
        """

        (v_norm, v_input, v_clist, v_dev) = TV_SETTINGS.split()
        if vdev is None:
            vdev = v_dev
        if input_type is None:
            input_type = v_input
        if tuner_norm is None:
            tuner_norm = v_norm
        if tuner_chanlist is None:
            tuner_chanlist = v_clist

        self.vdev = vdev
        self.vvbi = vvbi
        self.adev = adev
        self.avol = avol
        self.input_type = string.lower(input_type)
        self.input_num  = int(input_num)
        self.tuner_type = tuner_type
        self.tuner_norm = string.upper(tuner_norm)
        self.tuner_chanlist = tuner_chanlist
        self.tuner_chan = tuner_chan
        self.irsend_trans = irsend_trans
        self.record_group = record_group
        self.desc = desc
        self.group_type = group_type
        self.in_use = FALSE
        self.tuner = None
        self.cmd = None
        if cmd != None and isinstance(cmd, str) and cmd.strip() != '':
            self.cmd = cmd.strip()

    def __str__(self):
        s = '<%s: %s %s:%r %r>' % (self.group_type, self.vdev, self.input_num, self.input_type, self.tuner_norm)
        return s


    def checkvdev(self, vdev):
        """
        Check if the video device is correctly configured
        """
        from tv.v4l2 import Videodev
        try:
            dev = Videodev(vdev)
            try:
                if input_type != 'webcam':
                    try:
                        if input_type:
                            input_num = dev.getinputbyname(input_type)[0]
                    except KeyError, e:
                        print 'cannot find tuner %r for %r\npossible values are: %r' % \
                            (input_type, vdev, dev.inputs.keys())
                        sys.exit(1)
                    try:
                        if tuner_norm:
                            tuner_std = dev.getstdbyname(tuner_norm)
                    except KeyError, e:
                        print 'cannot find norm %r for %r\npossible values are: %r' % \
                            (tuner_norm, vdev, dev.standards.keys())
                        sys.exit(1)
                    print '%r:%r=%r' % (vdev, input_type, input_num)
                    print '%r:%r=%r' % (vdev, tuner_norm, tuner_std)
                else:
                    print '%r:%r=%r' % (vdev, input_type, dev.inputs.keys())
                    print '%r:%r=%r' % (vdev, tuner_norm, dev.standards.keys())
            finally:
                dev.close()
        except OSError, e:
            print 'Video device %r: %s' % (vdev, e)


def print_config_changes(conf_version, file_version, changelist):
    """
    print changes made between version on the screen
    """
    ver_old = float(file_version)
    ver_new = float(conf_version)
    if ver_old == ver_new:
        return
    print
    print 'You are using version %s, changes since then:' % file_version
    changed = [(cv, cd) for (cv, cd) in changelist if cv > ver_old]
    if not changed:
        print 'The changelist has not been updated, please notify the developers!'
    else:
        for change_ver, change_desc in changed:
            print 'Version %s:' % change_ver
            for line in change_desc.split('\n'):
                print '    ', line.strip()
            print
    print


def print_help():
    """
    print some help about config files
    """
    print("""Freevo is not completely configured to start The configuration is based
    on three files. This may sound oversized, but this way it's easier to
    configure.

    First Freevo loads a file called 'freevo.conf'. This file will be generated
    by calling 'freevo setup'. Use 'freevo setup --help' to get information
    about the parameter. Based on the information in that file, Freevo will
    guess some settings for your system. This takes place in a file called
    'freevo_config.py'. Since this file may change from time to time, you
    should not edit this file. After freevo_config.py is loaded, Freevo will
    load the Debian specific '/etc/freevo/debconf.py', and then it will
    look for a file called 'local_conf.py'. You can overwrite the variables
    from 'freevo_config.py' in here. There is an example for 'local_conf.py'
    called 'local_conf.py.example' in the Freevo distribution.

    If you need more help, use the internal webserver to get more information
    how to setup Freevo. To do this, you need to set WWW_USERS = { 'username' :
    'password' } in your local_conf.py and then you can access the doc at
    http://localhost:8080/help/

    The location of freevo_config.py is %s""" % os.environ['FREEVO_CONFIG'])

    print('Freevo searches for freevo.conf and local_conf.py in the following locations:')
    for dirname in cfgfilepath:
        print '  '+dirname
    print



#
# get information about what is started here:
# helper = some script from src/helpers or is webserver or recordserver
#
HELPER          = 0
IS_RECORDSERVER = 0
IS_WEBSERVER    = 0
IS_ENCODINGSERVER = 0
IS_RSSSERVER = 0
IS_PROMPT = 0

__builtin__.__dict__['__freevo_app__'] = os.path.splitext(os.path.basename(sys.argv[0]))[0]

if sys.argv[0].find('main.py') == -1:
    HELPER = 1
    if sys.argv[0].find('recordserver.py') != -1:
        IS_RECORDSERVER = 1
    elif sys.argv[0].find('webserver.py') != -1:
        IS_WEBSERVER = 1
    elif sys.argv[0].find('encodingserver.py') != -1:
        IS_ENCODINGSERVER = 1
    elif sys.argv[0].find('rssserver.py') != -1:
        IS_RSSSERVER = 1
    elif sys.argv[0] == '':
        IS_PROMPT = 1

#
# Send debug to stdout as well as to the logfile?
#
DEBUG_STDOUT = 0

#
# debugging messages are set by the logging level
# except for higher debugging message levels
# the DEBUG setting is overridden in local_conf.py
#
DEBUG = 0

LOGGING = logging.DEBUG


def make_freevodir(envvar, linux_dir, bsd_dir, private_dir):
    """
    Make the freevo specific directory and return it's name
    """
    if os.environ.has_key('OS_' + envvar):
        os_dirname = os.environ['OS_' + envvar]
    elif os.uname()[0] == 'FreeBSD':
        os_dirname = bsd_dir
    else:
        os_dirname = linux_dir

    if os.environ.has_key('FREEVO_' + envvar):
        freevo_dirname = os.environ['FREEVO_' + envvar]
    else:
        freevo_dirname = os.path.join(os_dirname, 'freevo')

    if not os.path.isdir(freevo_dirname):
        try:
            print 'trying "%s"...' % (freevo_dirname)
            os.makedirs(freevo_dirname)
            os.chmod(freevo_dirname, 01777)
        except OSError:
            freevo_dirname = os.path.join(os.environ['HOME'], '.freevo', private_dir)
            if not os.path.isdir(freevo_dirname):
                try:
                    print 'trying "%s"...' % (freevo_dirname)
                    os.makedirs(freevo_dirname)
                    os.chmod(freevo_dirname, 0755)
                except OSError, e:
                    print 'Warning: %s does not exist and can\'t be created' % freevo_dirname
                    print 'Please create this directory as root and set permissions for the'
                    print 'Freevo user to write to it.'
                    os_dirname = '/tmp'
                    freevo_dirname = os.path.join(os_dirname, 'freevo')
                    if not os.path.isdir(freevo_dirname):
                        try:
                            print 'trying "%s"...' % (freevo_dirname)
                            os.makedirs(freevo_dirname)
                        except OSError:
                            os_dirname = '/tmp'
                            freevo_dirname = os.path.join(os_dirname, ('freevo-' + os.getuid()), private_dir)
                            if not os.path.isdir(freevo_dirname):
                                print 'trying "%s"...' % (freevo_dirname)
                                os.makedirs(freevo_dirname)
                    print 'Using %s as cache directory, but this is a bad idea' % freevo_dirname
                    print
    return (os_dirname, freevo_dirname)


#
# find the log directory
#
OS_LOGDIR, FREEVO_LOGDIR = make_freevodir('LOGDIR', '/var/log', '/var/log', 'log')

#
# Freevo static dir:
#
# Under Linux, use /var/lib. Under FreeBSD, use /var/db.
#
OS_STATICDIR, FREEVO_STATICDIR = make_freevodir('STATICDIR', '/var/lib', '/var/db', 'static')

#
# Freevo cache dir:
#
# Under Linux, use /var/cache. Under FreeBSD, use /var/db.
#
OS_CACHEDIR, FREEVO_CACHEDIR = make_freevodir('CACHEDIR', '/var/cache', '/var/db', 'cache')

#
# Redirect stdout and stderr to stdout and /tmp/freevo.log
#
lock = RLock()
#if not HELPER:
if not IS_PROMPT:
    try:
        appname = os.path.splitext(os.path.basename(sys.argv[0]))[0]
        if not appname:
            appname = 'prompt'
        logfile = os.path.join(FREEVO_LOGDIR, '%s-%s.log' % (appname, os.getuid()))
        fp = open(logfile, 'a')
        fp.close()
    except IOError, e:
        print '%s' % e
        logfile = '/dev/null'

    logging.basicConfig(level=LOGGING, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                        filename=logfile, filemode='a')

    old_stdout = sys.stdout
    old_stderr = sys.stderr
    sys.stdout = Logger(logging.getLogger('stdout'), sys.argv[0] + ':stdout')
    sys.stderr = Logger(logging.getLogger('stderr'), sys.argv[0] + ':stderr')
    ts = time.asctime(time.localtime(time.time()))
    sys.stdout.log('=' * 80)
    sys.stdout.log('Freevo %s r%s started at %s' % (version.__version__, revision.__revision__, ts))
    sys.stdout.log('-' * 80)


def shutdown():
    sys.stdout.log('-' * 80)
    sys.stdout.log('Freevo %s r%s finished at %s' % (version.__version__, revision.__revision__, ts))
    sys.stdout.log('=' * 80)
    sys.stdout.close()
    sys.stderr.close()
    sys.stdout = old_stdout
    sys.stderr = old_stderr


def _stack_function_(message='', limit=None):
    import traceback
    stack = traceback.extract_stack()
    if stack:
        if limit:
            logging.debug('%s\n*** %s' % (message, '*** '.join(traceback.format_list(stack[-limit-1:-1]))))
        else:
            logging.debug('%s\n*** %s' % (message, '*** '.join(traceback.format_list(stack)[0:-1])))


def _debug_function_(s, level=1):
    """
    The debug function that is mapped to the _debug_ builtin There are different
    levels of debugging and logging. Debug messages range from 1 (default) to 9
    (most verbose), logging messages range from NOTSET to DCRITICAL
    """
    if DEBUG < level:
        return
    if not s:
        return
    global lock
    global DEBUG_STDOUT
    if lock:
        lock.acquire()
    try:
        try:
            # add the current trace to the string
            if isinstance(s, unicode):
                s = s.encode(encoding, 'replace')
            where =  traceback.extract_stack(limit = 2)[0]
            msg = '%s (%s): %s' % (where[0][where[0].rfind('/')+1:], where[1], s)
            prefix = ''
            # log all the messages
            if level <= DCRITICAL:
                logging.critical(msg)
                prefix = _('CRITICAL')
            elif level == DERROR:
                logging.error(msg)
                prefix = _('ERROR')
            elif level == DWARNING:
                logging.warning(msg)
                prefix = _('WARNING')
            elif level == DINFO:
                logging.info(msg)
                prefix = _('INFO')
            else:
                logging.debug(msg)
                prefix = _('DEBUG')
            # print the message for info, warning, error and critical
            if level <= DWARNING or DEBUG_STDOUT:
                sys.__stdout__.write('%s: %s\n' % (prefix, s))
                sys.__stdout__.flush()
        except UnicodeEncodeError:
            print "_debug_ failed: %r" % msg
        except Exception, why:
            print "_debug_ failed: %r" % why
    finally:
        if lock:
            lock.release()


__builtin__.__dict__['_debug_'] = _debug_function_
__builtin__.__dict__['_stack_'] = _stack_function_
__builtin__.__dict__['DCRITICAL'] = DCRITICAL
__builtin__.__dict__['DERROR'] = DERROR
__builtin__.__dict__['DWARNING'] = DWARNING
__builtin__.__dict__['DINFO'] = DINFO


#
# Config file handling
#
# Debian change : '.' may lead to unpredictable behaviours,
#  '/usr/local/etc/freevo' is redundant
cfgfilepath = [ os.path.expanduser('~/.freevo'), '/etc/freevo']
 


#
# Default settings
# These will be overwritten by the contents of 'freevo.conf'
#
CONF = setup_freevo.FreevoConf()

#
# Read the environment set by the start script
#
SHARE_DIR   = os.path.abspath(os.environ['FREEVO_SHARE'])
CONTRIB_DIR = os.path.abspath(os.environ['FREEVO_CONTRIB'])

SKIN_DIR  = os.path.join(SHARE_DIR, 'skins')
ICON_DIR  = os.path.join(SHARE_DIR, 'icons')
IMAGE_DIR = os.path.join(SHARE_DIR, 'images')
FONT_DIR  = os.path.join(SHARE_DIR, 'fonts')

RUNAPP = os.environ['RUNAPP']
logger.debug('RUNAPP: %s', RUNAPP)

logger.info('LOGDIR: %s %s', OS_LOGDIR, FREEVO_LOGDIR)
logger.info('STATICDIR: %s %s', OS_STATICDIR, FREEVO_STATICDIR)
logger.info('CACHEDIR: %s %s', OS_CACHEDIR, FREEVO_CACHEDIR)

#
# Check that freevo_config.py is not found in the config file dirs
#
for dirname in cfgfilepath[1:]:
    freevoconf = os.path.join(dirname, 'freevo_config.py')
    if os.path.isfile(freevoconf):
        print(('\nERROR: freevo_config.py found in %s, please remove it and use local_conf.py instead!') % freevoconf)
        sys.exit(1)

#
# Search for freevo.conf:
#
for dirname in cfgfilepath:
    freevoconf = os.path.join(dirname, 'freevo.conf')
    logger.debug('Trying freevo configuration file "%s"...', freevoconf)
    if os.path.isfile(freevoconf):
        logger.info('Loading freevo configuration file "%s"', freevoconf)

        commentpat = re.compile('([^#]*)( *#.*)')
        c = open(freevoconf)
        for line in c.readlines():
            if commentpat.search(line):
                line = commentpat.search(line).groups()[0]
            line = line.strip()
            if len(line) == 0:
                continue
            vals = line.split()
            logger.debug('Cfg file data: "%s"', line)
            try:
                name, val = vals[0].strip(), vals[2].strip()
            except:
                print 'Error parsing config file data "%s"' % line
                continue
            CONF.__dict__[name] = val

        c.close()
        w, h = CONF.geometry.split('x')
        x, y = CONF.position.split(',')
        CONF.width, CONF.height = int(w), int(h)
        CONF.x, CONF.y = int(x), int(y)
        break
else:
    print
    print 'Error: freevo.conf not found'
    print
    print_help()
    sys.exit(1)

#Debian specific /etc/freevo/debconf.py file, to read configurations
# as entered at install time using the debconf interface
overridefile = '/etc/freevo/debconf.py'
if os.path.isfile(overridefile):
    _debug_('Loading debconf configuration file "%s"' % overridefile, DINFO)
    execfile(overridefile, globals(), locals())

#
# search missing programs at runtime
#
for program, valname, needed in setup_freevo.EXTERNAL_PROGRAMS:
    if not hasattr(CONF, valname) or not getattr(CONF, valname):
        setup_freevo.check_program(CONF, program, valname, needed)
    if not hasattr(CONF, valname) or not getattr(CONF, valname):
        setattr(CONF, valname, '')

#
# fall back to x11 if display is mga or fb and DISPLAY ist set
# or switch to fbdev if we have no DISPLAY and x11 or dga is used
#
if not HELPER:
    if os.environ.has_key('DISPLAY') and os.environ['DISPLAY']:
        if CONF.display in ('mga', 'fbdev'):
            print
            print 'Warning: display is set to %s, but the environment ' % CONF.display + \
                  'has DISPLAY=%s.' % os.environ['DISPLAY']
            print 'this could mess up your X display, setting display to x11.'
            print 'If you really want to do this, start \'DISPLAY="" freevo\''
            print
            CONF.display = 'x11'
    else:
        if CONF.display == 'x11':
            print
            print 'Warning: display is set to %s, but the environment ' % CONF.display + \
                  'has no DISPLAY set. Setting display to fbdev.'
            print
            CONF.display = 'fbdev'

elif CONF.display == 'dxr3':
    # don't use dxr3 for helpers. They don't use the osd anyway, but
    # it may mess up the dxr3 output (don't ask why).
    CONF.display = 'fbdev'

#
# Load freevo_config.py:
#
if os.path.isfile(os.environ['FREEVO_CONFIG']):
    logger.debug('Loading cfg: %s', os.environ['FREEVO_CONFIG'])
    logger.info('Loading freevo configuration file: "%s"', os.environ['FREEVO_CONFIG'])
    try:
        execfile(os.environ['FREEVO_CONFIG'], globals(), locals())
    except Exception, why:
        traceback.print_exc()
        raise SystemExit
    logger.info('Loaded freevo configuration file: "%s"', os.environ['FREEVO_CONFIG'])
else:
    print
    print "Error: %s: no such file" % os.environ['FREEVO_CONFIG']
    print
    sys.exit(1)

#Debian specific /etc/freevo/debconf.py file, to read configurations
# as entered at install time using the debconf interface
overridefile = '/etc/freevo/debconf.py'
if os.path.isfile(overridefile):
    _debug_('Loading debconf configuration file "%s"' % overridefile, DINFO)
    execfile(overridefile, globals(), locals())

#
# Search for local_conf.py:
#
for dirname in cfgfilepath:
    overridefile = os.path.join(dirname, 'local_conf.py')
    logger.debug('Trying local configuration file "%s"...', overridefile)
    if os.path.isfile(overridefile):
        logger.info('Loading local configuration file: "%s"', overridefile)
        our_locals = {}
        try:
            execfile(overridefile, globals(), our_locals)
        except Exception, why:
            traceback.print_exc()
            raise SystemExit
        locals().update(our_locals)
        logger.info('Loaded local configuration file: "%s"', overridefile)

        try:
            CONFIG_VERSION
        except NameError:
            print
            print 'Error: your local_conf.py file has no version information'
            print 'Please check freevo_config.py for changes and set CONFIG_VERSION'
            print 'in %s to %s' % (overridefile, LOCAL_CONF_VERSION)
            print
            sys.exit(1)

        if int(str(CONFIG_VERSION).split('.')[0]) != int(str(LOCAL_CONF_VERSION).split('.')[0]):
            print
            print 'Error: The version information in freevo_config.py doesn\'t'
            print 'match the version in your local_conf.py.'
            print 'Please check freevo_config.py for changes and set CONFIG_VERSION'
            print 'in %s to %s' % (overridefile, LOCAL_CONF_VERSION)
            print_config_changes(LOCAL_CONF_VERSION, CONFIG_VERSION, LOCAL_CONF_CHANGES)
            sys.exit(1)

        if int(str(CONFIG_VERSION).split('.')[1]) != int(str(LOCAL_CONF_VERSION).split('.')[1]):
            print
            print 'Warning: freevo_config.py was changed, please check local_conf.py'
            print_config_changes(LOCAL_CONF_VERSION, CONFIG_VERSION, LOCAL_CONF_CHANGES)
        break

else:
    print
    print 'Error: local_conf.py not found'
    print
    print_help()
    print
    print 'Since it\'s highly unlikly you want to start Freevo without further'
    print 'configuration, Freevo will exit now.'
    sys.exit(0)


# set the umask
os.umask(UMASK)


#if not HELPER:
logging.getLogger('').setLevel(LOGGING)
for module, level in LOGGERS.items():
    logging.getLogger(module).setLevel(level)

#
# force fullscreen when freevo is it's own windowmanager
#
if len(sys.argv) >= 2 and sys.argv[1] == '--force-fs':
    START_FULLSCREEN_X = 1


#
# set default font
#
OSD_DEFAULT_FONTNAME = os.path.join(FONT_DIR, OSD_DEFAULT_FONTNAME)

#
# set list of video files to []
# (fill be filled from the plugins)
#
VIDEO_SUFFIX = []

for p in plugin.getall():
    if p.startswith('video'):
        suffix = p[6:].upper()
        if suffix:
            try:
                for s in eval('VIDEO_%s_SUFFIX' % suffix):
                    if not s in VIDEO_SUFFIX:
                        VIDEO_SUFFIX.append(s)
            except NameError:
                pass


#
# set data dirs
# if not set, set it to root and home dir
# if set, make all path names absolute
#
for type in ('video', 'audio', 'image', 'games'):
    n = '%s_ITEMS' % type.upper()
    x = eval(n)
    if x == None:
        x = []
        if os.environ.has_key('HOME') and os.environ['HOME']:
            x.append(('Home', os.environ['HOME']))
        x.append(('Root', '/'))
        exec('%s = x' % n)
        if not HELPER and plugin.is_active('mediamenu', type):
            print
            print 'Error: %s not set, set it to Home directory' % n
            print
        if type == 'video':
            VIDEO_ONLY_SCAN_DATADIR = True

    elif type == 'games':
        abs = []
        for d in x:
            pos = d[1].find(':')
            if pos == -1:
                abs.append((d[0], os.path.abspath(d[1]), d[2]))
            else:
                if pos > d[1].find('/'):
                    abs.append((d[0], os.path.abspath(d[1]), d[2]))
                else:
                    abs.append((d[0], d[1][0:pos+1] + os.path.abspath(d[1][pos+1:]), d[2]))
        exec ('%s = abs' % n)
    else:
        # The algorithm doesn't work for GAMES_ITEMS, so we leave it out
        abs = []
        for d in x:
            if isstring(d):
                pos = d.find(':')
                if pos == -1:
                    abs.append(os.path.abspath(d))
                else:
                    if pos > d.find('/'):
                        abs.append(os.path.abspath(d))
                    else:
                        abs.append(d[0:pos+1] + os.path.abspath(d[pos+1:]))
            else:
                pos = d[1].find(':')
                if pos == -1:
                    abs.append((d[0], os.path.abspath(d[1])))
                else:
                    if pos > d[1].find('/'):
                        abs.append((d[0], os.path.abspath(d[1])))
                    else:
                        abs.append((d[0], d[1][0:pos+1] + os.path.abspath(d[1][pos+1:])))
        exec ('%s = abs' % n)



if not TV_RECORD_DIR:
    TV_RECORD_DIR = VIDEO_ITEMS[0][1]
    if not HELPER and plugin.is_active('tv'):
        print
        print 'Error: TV_RECORD_DIR not set'
        print 'Please set TV_RECORD_DIR to the directory, where recordings should be stored'
        print 'or remove the tv plugin. Autoset variable to %s.' % TV_RECORD_DIR
        print

if not VIDEO_SHOW_DATA_DIR and not HELPER:
    print 'Error: VIDEO_SHOW_DATA_DIR not found'

#
# Autodetect the CD/DVD drives in the system if not given in local_conf.py
#
# ROM_DRIVES == None means autodetect
# ROM_DRIVES == [] means ignore ROM drives
#
if ROM_DRIVES == None:
    ROM_DRIVES = []
    if os.path.isfile('/etc/fstab'):
        re_cd        = re.compile('^(/dev/cdrom[0-9]*|/dev/[am]?cd[0-9]+[a-z]?)[ \t]+([^ \t]+)[ \t]+', re.I)
        re_cdrec     = re.compile('^(/dev/cdrecorder[0-9]*)[ \t]+([^ \t]+)[ \t]+', re.I)
        re_dvd       = re.compile('^(/dev/dvd[0-9]*)[ \t]+([^ \t]+)[ \t]+', re.I)
        re_iso       = re.compile('^([^ \t#]+)[ \t]+([^ \t]+)[ \t]+(iso|cd)9660', re.I)
        re_automount = re.compile('^none[ \t]+([^ \t]+).*supermount.*dev=([^,]+).*', re.I)
        re_bymountcd = re.compile('^(/dev/[^ \t]+)[ \t]+([^ ]*cdrom[0-9]*)[ \t]+', re.I)
        re_bymountdvd= re.compile('^(/dev/[^ \t]+)[ \t]+([^ ]*dvd[0-9]*)[ \t]+', re.I)
        fd_fstab = open('/etc/fstab')
        for line in fd_fstab:
            # Match on the devices /dev/cdrom, /dev/dvd, and fstype iso9660
            match_cd        = re_cd.match(line)
            match_cdrec     = re_cdrec.match(line)
            match_dvd       = re_dvd.match(line)
            match_iso       = re_iso.match(line)
            match_automount = re_automount.match(line)
            match_bymountcd = re_bymountcd.match(line)
            match_bymountdvd= re_bymountdvd.match(line)
            mntdir = devname = dispname = ''
            if match_cd or match_bymountcd:
                m = match_cd or match_bymountcd
                logger.debug('match_cd or match_bymountcd=%r', m.groups())
                mntdir = m.group(2)
                devname = m.group(1)
                dispname = 'CD-%s' % (len(ROM_DRIVES)+1)
            elif match_cdrec:
                logger.debug('match_cdrec=%r', match_cdrec.groups())
                mntdir = match_cdrec.group(2)
                devname = match_cdrec.group(1)
                dispname = 'CDREC-%s' % (len(ROM_DRIVES)+1)
            elif match_dvd or match_bymountdvd:
                m = match_dvd or match_bymountdvd
                logger.debug('match_dvd or match_bymountdvd=%r', m.groups())
                mntdir = m.group(2)
                devname = m.group(1)
                dispname = 'DVD-%s' % (len(ROM_DRIVES)+1)
            elif match_iso:
                logger.debug('match_iso=%r', match_iso.groups())
                mntdir = match_iso.group(2)
                devname = match_iso.group(1)
                dispname = 'CD-%s' % (len(ROM_DRIVES)+1)
            elif match_automount:
                logger.debug('match_automount=%r', match_automount.groups())
                mntdir = match_automount.group(1)
                devname = match_automount.group(2)
                # Must check that the supermount device is cd or dvd
                if devname.lower().find('cd') != -1:
                    dispname = 'CD-%s' % (len(ROM_DRIVES)+1)
                elif devname.lower().find('dvd') != -1:
                    dispname = 'DVD-%s' % (len(ROM_DRIVES)+1)
                elif devname.lower().find('hd') != -1:
                    logger.info('Trying to autodetect type of %r', devname)
                    if os.path.exists('/proc/ide/' + re.sub(r'^(/dev/)', '', devname) + '/media'):
                        if open('/proc/ide/' + re.sub(r'^(/dev/)', '', devname) + \
                            '/media', 'r').read().lower().find('cdrom') != 1:
                            dispname = 'CD-%s' % (len(ROM_DRIVES)+1)
                            logger.info('%r is a cdrom drive', devname)
                    else:
                        logger.info("%r doesn't seems to be a cdrom drive", devname)
                        mntdir = devname = dispname = ''
                else:
                    mntdir = devname = dispname = ''
            if mntdir:
                logger.info('line=%r, mntdir=%r, devname=%r, dispname=%r', line, mntdir, devname, dispname)

            if os.uname()[0] == 'FreeBSD':
                # FreeBSD-STABLE mount point is often device name + "c",
                # strip that off
                if devname and devname[-1] == 'c':
                    devname = devname[:-1]
                # Use native FreeBSD device names
                dispname = devname[5:]

            # Weed out duplicates
            for rd_mntdir, rd_devname, rd_dispname in ROM_DRIVES:
                if os.path.realpath(rd_devname) == os.path.realpath(devname):
                    if not HELPER:
                        print (('ROM_DRIVES: Auto-detected that %s is the same ' +
                                'device as %s, skipping') % (devname, rd_devname))
                    break
            else:
                # This was not a duplicate of another device
                if mntdir and devname and dispname:
                    ROM_DRIVES += [(mntdir, devname, dispname)]
                    if not HELPER:
                        print 'ROM_DRIVES: Auto-detected and added "%s"' % (ROM_DRIVES[-1], )
        fd_fstab.close()



#
# List of objects representing removable media, e.g. CD-ROMs,
# DVDs, etc.
#
REMOVABLE_MEDIA = []

#
# Auto detect xmltv channel list
#

def sortchannels(list, key):
    # This should be more generic, but I couldn't get it
    # to sort properly without specifying the nested array
    # index for the tunerid and forcing 'int'
    for l in list:
        if len(l[key]) == 1:
            l[key].append(('0', ))
    nlist = map(lambda x, key = key: (string.split(x[key][1][0])[0], x), list)
    nlist.sort()
    return map(lambda (key, x): x, nlist)


def detect_channels():
    """
    Auto detect a list of possible channels in the xmltv file
    """
    import codecs
    try:
        import cPickle as pickle
    except ImportError:
        import pickle

    file = XMLTV_FILE

    pname = os.path.join(FREEVO_CACHEDIR, 'xmltv_channels.pickle')

    if not os.path.isfile(file):
        if not HELPER:
            print
            print 'Error: can\'t find %s' % file
            print 'Use xmltv to create this file or when you don\'t want to use the tv'
            print 'module at all, add TV_CHANNELS = [] and plugin.remove(\'tv\') to your'
            print 'local_conf.py. TVguide is deactivated now.'
            print
        return []

    elif os.path.isfile(pname) and (os.path.getmtime(pname) > os.path.getmtime(file)):
        try:
            f = open(pname, 'r')
            data = pickle.load(f)
            f.close()
            return data
        except:
            if not HELPER:
                print 'Error: unable to read cachefile %s' % pname
            return []

    else:
        from tv import xmltv
        input = open(file, 'r')
        tmp   = open('/tmp/xmltv_parser', 'w')
        while(1):
            line = input.readline()
            if not line:
                break
            if line.find('<programme') > 0:
                tmp.write('</tv>\n')
                break
            tmp.write(line)

        input.close()
        tmp.close()

        tmp   = open('/tmp/xmltv_parser', 'r')
        xmltv_channels = xmltv.read_channels(tmp)
        tmp.close()

        xmltv_channels = sortchannels(xmltv_channels, 'display-name')
        chanlist = []

        for a in xmltv_channels:
            if (a['display-name'][1][0][0].isdigit()):
                display_name = a['display-name'][0][0].encode(LOCALE, 'ignore')
                tunerid = string.split(a['display-name'][1][0].encode(LOCALE, 'ignore'))[0]
            else:
                display_name = a['display-name'][1][0].encode(LOCALE, 'ignore')
                tunerid = string.split(a['display-name'][0][0].encode(LOCALE, 'ignore'))[0]
            id = a['id'].encode(LOCALE, 'ignore')

            chanlist += [(id, display_name, tunerid)]

        f = TV_CHANNELS_COMPARE
        chanlist.sort(f)

        try:
            if os.path.isfile(pname):
                os.unlink(pname)
            f = open(pname, 'w')
            pickle.dump(chanlist, f, 1)
            f.close()
        except IOError:
            if not HELPER:
                print 'Error: unable to save to cachefile %s' % pname

        for c in chanlist:
            if c[2] == 0:
                print_list = 1
                if not HELPER:
                    print
                    print 'Error: XMLTV auto detection failed'
                    print 'Some channels in the channel list have no station id. Please add'
                    print 'it by putting the list in your local_conf.py. Start '
                    print '\'freevo tv_grab --help\' for more information'
                    print
                break
        else:
            if not HELPER:
                print 'XMTV: Auto-detected channel list'

        return chanlist


if TV_CHANNELS == None and plugin.is_active('tv'):
    # auto detect them
    try:
        TV_CHANNELS = detect_channels()
    except Exception, e:
        print
        print 'Error in TV.xml file, unable to set TV_CHANNELS'
        print e
        print
        TV_CHANNELS = []

#
# compile the regexp
#
VIDEO_SHOW_REGEXP_MATCH = re.compile("^.*" + VIDEO_SHOW_REGEXP).match
VIDEO_SHOW_REGEXP_SPLIT = re.compile("[\.\- ]*" + VIDEO_SHOW_REGEXP + "[\.\- ]*").split


#
# create cache subdirs
#
if not OVERLAY_DIR or OVERLAY_DIR == '/':
    print
    print 'ERROR: bad OVERLAY_DIR.'
    print 'Set OVERLAY_DIR it to a directory on the local filesystem where Freevo'
    print 'can store the metadata. Make sure this filesystem has about 100 MB free space'
    sys.exit(0)

if not os.path.isdir(OVERLAY_DIR):
    os.makedirs(OVERLAY_DIR)

# Make sure OVERLAY_DIR doesn't ends with a slash
# With that, we don't need to use os.path.join, normal string
# concat is much faster
if OVERLAY_DIR and OVERLAY_DIR.endswith('/'):
    OVERLAY_DIR = OVERLAY_DIR[:-1]
logger.info('overlaydir: %s', OVERLAY_DIR)

if not os.path.isdir(OVERLAY_DIR + '/disc'):
    os.makedirs(OVERLAY_DIR + '/disc')

if not os.path.isdir(OVERLAY_DIR + '/disc/metadata'):
    os.makedirs(OVERLAY_DIR + '/disc/metadata')

if not os.path.isdir(OVERLAY_DIR + '/disc-set'):
    os.makedirs(OVERLAY_DIR + '/disc-set')


#
# delete LD_PRELOAD for all helpers, main.py does it after
# starting the display
#
if HELPER:
    os.environ['LD_PRELOAD'] = ''

encoding = None
try:
    encoding = os.environ['LANG'].split('.')[1]
    ''.encode(encoding)
except:
    try:
        encoding = os.environ['LC_ALL'].split('.')[1]
        ''.encode(encoding)
    except:
        encoding = LOCALE

if not encoding:
    encoding = LOCALE

if not HELPER:
    logger.debug("Using '%s' encoding", encoding)

for k, v in CONF.__dict__.items():
    logger.debug('%r: %r', k, v)

# make sure USER and HOME are set
os.environ['USER'] = pwd.getpwuid(os.getuid())[0]
os.environ['HOME'] = pwd.getpwuid(os.getuid())[5]