2011-07-08 23:14:01 +02:00
import sys
import os
import logging
from configobj import ConfigObj
2011-07-14 04:17:38 +02:00
from optparse import OptionParser , OptionValueError
2011-08-05 18:35:50 +02:00
from api_clients import api_client as apc
2011-07-08 23:14:01 +02:00
import json
import shutil
2011-09-13 20:31:14 +02:00
import commands
2011-07-08 23:14:01 +02:00
2012-11-20 23:21:13 +01:00
sys . path . append ( ' /usr/lib/airtime/media-monitor/mm2/ ' )
from media . monitor . pure import is_file_supported
2011-07-08 23:14:01 +02:00
# create logger
logger = logging . getLogger ( )
2011-07-12 23:43:24 +02:00
# no logging
2011-07-14 04:17:38 +02:00
ch = logging . StreamHandler ( )
logging . disable ( 50 )
2011-07-08 23:14:01 +02:00
# add ch to logger
logger . addHandler ( ch )
2012-04-03 19:45:13 +02:00
if ( os . geteuid ( ) != 0 ) :
2011-09-13 20:31:14 +02:00
print ' Must be a root user. '
sys . exit ( )
2012-10-29 16:40:23 +01:00
2011-07-08 23:14:01 +02:00
# loading config file
try :
config = ConfigObj ( ' /etc/airtime/media-monitor.cfg ' )
except Exception , e :
print ( ' Error loading config file: %s ' , e )
sys . exit ( )
2012-09-13 17:16:42 +02:00
api_client = apc . AirtimeApiClient ( config )
2011-07-08 23:14:01 +02:00
2011-07-12 23:43:24 +02:00
#helper functions
2011-07-08 23:14:01 +02:00
# copy or move files
2011-07-12 23:43:24 +02:00
# flag should be 'copy' or 'move'
2011-07-08 23:14:01 +02:00
def copy_or_move_files_to ( paths , dest , flag ) :
2011-09-13 16:35:25 +02:00
try :
for path in paths :
if ( path [ 0 ] == " / " or path [ 0 ] == " ~ " ) :
path = os . path . realpath ( path )
else :
path = currentDir + path
path = apc . encode_to ( path , ' utf-8 ' )
dest = apc . encode_to ( dest , ' utf-8 ' )
if ( os . path . exists ( path ) ) :
if ( os . path . isdir ( path ) ) :
path = format_dir_string ( path )
#construct full path
sub_path = [ ]
for temp in os . listdir ( path ) :
sub_path . append ( path + temp )
copy_or_move_files_to ( sub_path , dest , flag )
elif ( os . path . isfile ( path ) ) :
#copy file to dest
2012-11-20 23:21:13 +01:00
if ( is_file_supported ( path ) ) :
2011-09-13 16:35:25 +02:00
destfile = dest + os . path . basename ( path )
if ( flag == ' copy ' ) :
print " Copying %(src)s to %(dest)s ... " % { ' src ' : path , ' dest ' : destfile }
shutil . copyfile ( path , destfile )
elif ( flag == ' move ' ) :
print " Moving %(src)s to %(dest)s ... " % { ' src ' : path , ' dest ' : destfile }
shutil . move ( path , destfile )
else :
print " Cannot find file or path: %s " % path
except Exception as e :
print " Error: " , e
2012-10-29 16:40:23 +01:00
2011-07-14 16:03:04 +02:00
def format_dir_string ( path ) :
if ( path [ - 1 ] != ' / ' ) :
path = path + ' / '
return path
2012-10-29 16:40:23 +01:00
2011-07-08 23:46:07 +02:00
def helper_get_stor_dir ( ) :
2013-02-04 22:05:58 +01:00
try :
res = api_client . list_all_watched_dirs ( )
except Exception , e :
2011-07-12 23:43:24 +02:00
return res
2013-02-04 22:05:58 +01:00
if ( res [ ' dirs ' ] [ ' 1 ' ] [ - 1 ] != ' / ' ) :
out = res [ ' dirs ' ] [ ' 1 ' ] + ' / '
return out
2011-07-12 23:43:24 +02:00
else :
2013-02-04 22:05:58 +01:00
return res [ ' dirs ' ] [ ' 1 ' ]
2011-07-08 23:14:01 +02:00
2011-07-14 04:17:38 +02:00
def checkOtherOption ( args ) :
for i in args :
2011-07-22 22:24:31 +02:00
if ( i [ 0 ] == ' - ' ) :
2011-07-14 04:17:38 +02:00
return True
2012-10-29 16:40:23 +01:00
2011-07-14 19:32:45 +02:00
def errorIfMultipleOption ( args , msg = ' ' ) :
2011-07-14 04:17:38 +02:00
if ( checkOtherOption ( args ) ) :
2011-07-14 19:32:45 +02:00
if ( msg != ' ' ) :
raise OptionValueError ( msg )
else :
raise OptionValueError ( " This option cannot be combined with other options " )
2012-10-29 16:40:23 +01:00
2011-07-14 04:17:38 +02:00
def printHelp ( ) :
storage_dir = helper_get_stor_dir ( )
if ( storage_dir is None ) :
2012-10-29 16:40:23 +01:00
storage_dir = " Unknown "
2011-07-14 04:17:38 +02:00
else :
storage_dir + = " imported/ "
print """
2011-07-14 20:03:33 +02:00
== == == == == == == == == == == ==
Airtime Import Script
== == == == == == == == == == == ==
There are two ways to import audio files into Airtime :
2011-07-12 23:43:24 +02:00
2011-07-18 19:30:40 +02:00
1 ) Use airtime - import to copy or move files into the storage folder .
2011-07-12 23:43:24 +02:00
2011-07-14 20:03:33 +02:00
Copied or moved files will be placed into the folder :
% s
2012-10-29 16:40:23 +01:00
2011-07-14 20:03:33 +02:00
Files will be automatically organized into the structure
" Artist/Album/TrackNumber-TrackName-Bitrate.file_extension " .
2011-07-12 23:43:24 +02:00
2011-07-18 19:30:40 +02:00
2 ) Use airtime - import to add a folder to the Airtime library ( " watch " a folder ) .
2012-10-29 16:40:23 +01:00
2011-07-14 20:03:33 +02:00
All the files in the watched folder will be imported to Airtime and the
folder will be monitored to automatically detect any changes . Hence any
2012-10-29 16:40:23 +01:00
changes done in the folder ( add , delete , edit a file ) will trigger
2011-07-18 19:30:40 +02:00
updates in Airtime library .
2011-07-14 20:03:33 +02:00
""" % s torage_dir
2011-07-14 04:17:38 +02:00
parser . print_help ( )
print " "
def CopyAction ( option , opt , value , parser ) :
errorIfMultipleOption ( parser . rargs )
2011-09-08 15:20:16 +02:00
if ( len ( parser . rargs ) == 0 ) :
raise OptionValueError ( " No argument found. This option requires at least one argument. " )
2011-07-14 04:17:38 +02:00
stor = helper_get_stor_dir ( )
if ( stor is None ) :
2011-09-09 23:08:23 +02:00
print " Unable to connect to the Airtime server. "
return
2011-07-14 04:17:38 +02:00
dest = stor + " organize/ "
copy_or_move_files_to ( parser . rargs , dest , ' copy ' )
def MoveAction ( option , opt , value , parser ) :
errorIfMultipleOption ( parser . rargs )
2011-09-08 15:20:16 +02:00
if ( len ( parser . rargs ) == 0 ) :
raise OptionValueError ( " No argument found. This option requires at least one argument. " )
2011-07-14 04:17:38 +02:00
stor = helper_get_stor_dir ( )
if ( stor is None ) :
2011-07-18 19:30:40 +02:00
exit ( " Unable to connect to the Airtime server. " )
2011-07-14 04:17:38 +02:00
dest = stor + " organize/ "
copy_or_move_files_to ( parser . rargs , dest , ' move ' )
def WatchAddAction ( option , opt , value , parser ) :
errorIfMultipleOption ( parser . rargs )
if ( len ( parser . rargs ) > 1 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " Too many arguments. This option requires exactly one argument. " )
2011-07-14 19:32:45 +02:00
elif ( len ( parser . rargs ) == 0 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " No argument found. This option requires exactly one argument. " )
2011-07-14 04:17:38 +02:00
path = parser . rargs [ 0 ]
2011-07-28 16:47:57 +02:00
if ( path [ 0 ] == " / " or path [ 0 ] == " ~ " ) :
path = os . path . realpath ( path )
else :
path = currentDir + path
2011-08-05 20:40:03 +02:00
path = apc . encode_to ( path , ' utf-8 ' )
2011-07-14 04:17:38 +02:00
if ( os . path . isdir ( path ) ) :
2012-11-20 23:35:18 +01:00
#os.chmod(path, 0765)
2013-02-04 22:05:58 +01:00
try :
res = api_client . add_watched_dir ( path )
except Exception , e :
2011-07-14 04:17:38 +02:00
exit ( " Unable to connect to the server. " )
# sucess
if ( res [ ' msg ' ] [ ' code ' ] == 0 ) :
print " %s added to watched folder list successfully " % path
else :
2011-07-18 19:30:40 +02:00
print " Adding a watched folder failed: %s " % res [ ' msg ' ] [ ' error ' ]
2012-11-06 00:08:47 +01:00
print " This error most likely caused by wrong permissions "
print " Try fixing this error by chmodding the parent directory(ies) "
2011-07-14 04:17:38 +02:00
else :
print " Given path is not a directory: %s " % path
def WatchListAction ( option , opt , value , parser ) :
errorIfMultipleOption ( parser . rargs )
if ( len ( parser . rargs ) > 0 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " This option doesn ' t take any arguments. " )
2013-02-04 22:05:58 +01:00
try :
res = api_client . list_all_watched_dirs ( )
except Exception , e :
2011-07-18 19:30:40 +02:00
exit ( " Unable to connect to the Airtime server. " )
2011-07-14 04:17:38 +02:00
dirs = res [ " dirs " ] . items ( )
# there will be always 1 which is storage folder
if ( len ( dirs ) == 1 ) :
print " No watch folders found "
else :
for key , v in dirs :
if ( key != ' 1 ' ) :
print v
def WatchRemoveAction ( option , opt , value , parser ) :
errorIfMultipleOption ( parser . rargs )
if ( len ( parser . rargs ) > 1 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " Too many arguments. This option requires exactly one argument. " )
2011-07-14 19:32:45 +02:00
elif ( len ( parser . rargs ) == 0 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " No argument found. This option requires exactly one argument. " )
2011-07-14 04:17:38 +02:00
path = parser . rargs [ 0 ]
2011-07-28 16:47:57 +02:00
if ( path [ 0 ] == " / " or path [ 0 ] == " ~ " ) :
path = os . path . realpath ( path )
else :
path = currentDir + path
2011-08-05 20:40:03 +02:00
path = apc . encode_to ( path , ' utf-8 ' )
2011-07-14 04:17:38 +02:00
if ( os . path . isdir ( path ) ) :
2013-02-04 22:05:58 +01:00
try :
res = api_client . remove_watched_dir ( path )
except Exception , e :
2011-07-18 19:30:40 +02:00
exit ( " Unable to connect to the Airtime server. " )
2011-07-14 04:17:38 +02:00
# sucess
if ( res [ ' msg ' ] [ ' code ' ] == 0 ) :
2011-07-18 19:30:40 +02:00
print " %s removed from watch folder list successfully. " % path
2011-07-14 04:17:38 +02:00
else :
2011-07-18 19:30:40 +02:00
print " Removing the watch folder failed: %s " % res [ ' msg ' ] [ ' error ' ]
2011-07-14 04:17:38 +02:00
else :
2011-07-18 19:30:40 +02:00
print " The given path is not a directory: %s " % path
2012-10-29 16:40:23 +01:00
2011-07-14 04:17:38 +02:00
def StorageSetAction ( option , opt , value , parser ) :
2011-07-14 19:32:45 +02:00
bypass = False
isF = ' -f ' in parser . rargs
isForce = ' --force ' in parser . rargs
if ( isF or isForce ) :
bypass = True
if ( isF ) :
parser . rargs . remove ( ' -f ' )
if ( isForce ) :
parser . rargs . remove ( ' --force ' )
if ( not bypass ) :
errorIfMultipleOption ( parser . rargs , " Only [-f] and [--force] option is allowed with this option. " )
2011-07-18 21:09:27 +02:00
possibleInput = [ ' y ' , ' Y ' , ' n ' , ' N ' ]
2011-07-18 17:56:41 +02:00
confirm = raw_input ( " Are you sure you want to change the storage direcory? (y/N) " )
confirm = confirm or ' N '
while ( confirm not in possibleInput ) :
2011-07-18 19:30:40 +02:00
print " Not an acceptable input: %s \n " % confirm
confirm = raw_input ( " Are you sure you want to change the storage direcory? (y/N) " )
2011-07-18 17:56:41 +02:00
confirm = confirm or ' N '
if ( confirm == ' n ' or confirm == ' N ' ) :
2011-07-14 19:32:45 +02:00
sys . exit ( 1 )
2012-10-29 16:40:23 +01:00
2011-07-14 04:17:38 +02:00
if ( len ( parser . rargs ) > 1 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " Too many arguments. This option requires exactly one argument. " )
2011-07-14 19:32:45 +02:00
elif ( len ( parser . rargs ) == 0 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " No argument found. This option requires exactly one argument. " )
2012-10-29 16:40:23 +01:00
2011-07-14 19:32:45 +02:00
path = parser . rargs [ 0 ]
2011-07-28 16:47:57 +02:00
if ( path [ 0 ] == " / " or path [ 0 ] == " ~ " ) :
path = os . path . realpath ( path )
else :
path = currentDir + path
2011-08-05 20:40:03 +02:00
path = apc . encode_to ( path , ' utf-8 ' )
2011-07-14 19:32:45 +02:00
if ( os . path . isdir ( path ) ) :
2013-02-04 22:05:58 +01:00
try :
res = api_client . set_storage_dir ( path )
except Exception , e :
2011-07-18 19:30:40 +02:00
exit ( " Unable to connect to the Airtime server. " )
2013-02-04 22:05:58 +01:00
# success
2011-07-14 04:17:38 +02:00
if ( res [ ' msg ' ] [ ' code ' ] == 0 ) :
2011-07-14 19:32:45 +02:00
print " Successfully set storage folder to %s " % path
2011-07-14 04:17:38 +02:00
else :
2011-07-18 19:30:40 +02:00
print " Setting storage folder failed: %s " % res [ ' msg ' ] [ ' error ' ]
2011-07-14 04:17:38 +02:00
else :
2011-07-18 19:30:40 +02:00
print " The given path is not a directory: %s " % path
2012-10-29 16:40:23 +01:00
2011-07-14 04:17:38 +02:00
def StorageGetAction ( option , opt , value , parser ) :
errorIfMultipleOption ( parser . rargs )
if ( len ( parser . rargs ) > 0 ) :
2011-07-18 19:30:40 +02:00
raise OptionValueError ( " This option does not take any arguments. " )
2011-07-14 04:17:38 +02:00
print helper_get_stor_dir ( )
2012-10-29 16:40:23 +01:00
2011-09-09 22:12:21 +02:00
class OptionValueError ( RuntimeError ) :
def __init__ ( self , msg ) :
self . msg = msg
2012-10-29 16:40:23 +01:00
2011-07-14 20:03:33 +02:00
usage = """ [-c|--copy FILE/DIR [FILE/DIR...]] [-m|--move FILE/DIR [FILE/DIR...]]
2011-07-18 19:30:40 +02:00
[ - - watch - add DIR ] [ - - watch - list ] [ - - watch - remove DIR ]
2011-07-14 20:03:33 +02:00
[ - - storage - dir - set DIR ] [ - - storage - dir - get ] """
2011-07-14 04:17:38 +02:00
2011-07-14 20:03:33 +02:00
parser = OptionParser ( usage = usage , add_help_option = False )
2011-07-14 04:17:38 +02:00
parser . add_option ( ' -c ' , ' --copy ' , action = ' callback ' , callback = CopyAction , metavar = ' FILE ' , help = ' Copy FILE(s) into the storage directory. \n You can specify multiple files or directories. ' )
parser . add_option ( ' -m ' , ' --move ' , action = ' callback ' , callback = MoveAction , metavar = ' FILE ' , help = ' Move FILE(s) into the storage directory. \n You can specify multiple files or directories. ' )
parser . add_option ( ' --watch-add ' , action = ' callback ' , callback = WatchAddAction , help = ' Add DIR to the watched folders list. ' )
parser . add_option ( ' --watch-list ' , action = ' callback ' , callback = WatchListAction , help = ' Show the list of folders that are watched. ' )
parser . add_option ( ' --watch-remove ' , action = ' callback ' , callback = WatchRemoveAction , help = ' Remove DIR from the watched folders list. ' )
parser . add_option ( ' --storage-dir-set ' , action = ' callback ' , callback = StorageSetAction , help = ' Set storage dir to DIR. ' )
parser . add_option ( ' --storage-dir-get ' , action = ' callback ' , callback = StorageGetAction , help = ' Show the current storage dir. ' )
parser . add_option ( ' -h ' , ' --help ' , dest = ' help ' , action = ' store_true ' , help = ' show this help message and exit ' )
2011-07-12 23:43:24 +02:00
2011-07-22 22:24:31 +02:00
# pop "--dir"
sys . argv . pop ( 1 )
# pop "invoked pwd"
currentDir = sys . argv . pop ( 1 ) + ' / '
2011-07-12 23:43:24 +02:00
if ( ' -l ' in sys . argv or ' --link ' in sys . argv ) :
print " \n The [-l][--link] option is deprecated. Please use the --watch-add option. \n Try ' airtime-import -h ' for more detail. \n "
2011-07-14 04:17:38 +02:00
sys . exit ( )
if ( ' -h ' in sys . argv ) :
printHelp ( )
sys . exit ( )
2011-09-08 15:20:16 +02:00
if ( len ( sys . argv ) == 1 or ' - ' not in sys . argv [ 1 ] ) :
2011-07-14 04:17:38 +02:00
printHelp ( )
sys . exit ( )
2012-10-29 16:40:23 +01:00
2011-09-09 22:12:21 +02:00
try :
( option , args ) = parser . parse_args ( )
except Exception , e :
printHelp ( )
2011-09-12 19:08:02 +02:00
if hasattr ( e , ' msg ' ) :
print " Error: " + e . msg
else :
2011-09-13 16:35:25 +02:00
print " Error: " , e
2011-09-09 22:12:21 +02:00
sys . exit ( )
except SystemExit :
printHelp ( )
sys . exit ( )
2012-10-29 16:40:23 +01:00
2011-07-14 04:17:38 +02:00
if option . help :
printHelp ( )
sys . exit ( )
2011-07-12 23:43:24 +02:00
2011-07-08 23:14:01 +02:00