Advertisement
tabebqena2

Untitled

Oct 1st, 2014
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.30 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3.  
  4. import sys
  5. import os
  6. import pygtk
  7. pygtk.require('2.0')
  8. import gtk
  9. import gtk.glade
  10. import time
  11. import traceback
  12.  
  13. home = os.environ['HOME']
  14. __version__ = "2.0.1.0"
  15. help_message = """
  16. Rockdome is gtk frontend for aria2c designed for linux users
  17. Version = %s
  18.  
  19. Usage:
  20.            rd <options><arguments>
  21.            where otions include:
  22.  
  23. Rockdome options:
  24.            <no options>            run Rockdome if not run.
  25.  
  26.            --alive                  Is rockdome running or not ??
  27.  
  28.            --id                    print the id of the running Rockdome instance. or None
  29.  
  30.            --disable-clipboard     disable clipboard integration
  31.  
  32.            --quit                  Quit the running Rockdome instance.
  33.  
  34.            -h
  35.            --help                  print this message "How to run rockdome??".
  36.                                    If you want document about "How to use Rockdome??":
  37.                                        1. Search in menu >> help >> content (Will be available soon).
  38.                                        2. Try to press "Help" button in each window. (Will be available soon).
  39.                                        3. Read tips.
  40.  
  41.            --url                   Url to be added to Rockdome,
  42.                                    Values : valid web link or existed torrent, meta or input file
  43.  
  44.            -v
  45.            --version               print version.
  46.  
  47.            --opt                   exclussively run Rockdome from /opt/rockdome/ path
  48.  
  49.            --bf                    run rockdome from custom installation path
  50.  
  51. """%__version__
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65. def get_args():
  66.    
  67.     try:
  68.         args = sys.argv[1:]
  69.         for i in args:
  70.             if i == "":
  71.                 continue
  72.     except:
  73.         args = []
  74.     return args
  75.  
  76. def get_dict_args(list_):
  77.     dict_ ={}
  78.     for arg in list_:
  79.             if "=" in arg:
  80.                 k,v = arg.split("=",1)
  81.                 dict_[k] = v
  82.             else:
  83.                 dict_[arg] = ""
  84.     return dict_
  85.  
  86.  
  87.  
  88. class rd():
  89.     def __init__(self, args):
  90.         self.args = get_dict_args(args)
  91.     def run(self):
  92.         run = self.getstate()
  93.         print "is rockdome running?? " , run
  94.         if  run  ==  False :
  95.            self.initiate()
  96.         print "rd finish is work"
  97.         os._exit(0)
  98.  
  99.     def get_id(self):
  100.         p = ''
  101.         try :
  102.            with open('%s/.aria2/pid'%home) as r:
  103.                p = r.readline().strip()
  104.                if p == "":
  105.                    
  106.                    return False
  107.                
  108.                else:
  109.                    return p
  110.         except :
  111.          
  112.             return False
  113.  
  114.     def getstate(self):
  115.        
  116.         p = self.get_id()
  117.         if not p :
  118.            
  119.             return False
  120.         p   = os.path.join("/" , "proc" , p )
  121.         if  os.path.exists(p):
  122.            
  123.             return True
  124.         else :
  125.            
  126.             return False
  127.  
  128.     def get_executeble_path(self):
  129.         opt_path = None
  130.         bf_path  = None
  131.         if os.path.exists("/opt/rockdome/bin/mainwin.py") :
  132.                 opt_path =  "/opt/rockdome"
  133.  
  134.         try:
  135.            r = open('%s/.aria2/.bf'%home)
  136.            bf_path =  r.read().strip()
  137.            r.close()
  138.         except Exception as e:
  139.            if e[0] == 2:
  140.                bf_path = None
  141.        
  142.         if bf_path == "/opt/rockdome":
  143.             return bf_path
  144.         if not bf_path and opt_path:
  145.             return opt_path
  146.         if not opt_path and bf_path:
  147.             return bf_path
  148.         if opt_path and bf_path and "--opt" in self.args:
  149.             return opt_path
  150.         if opt_path and bf_path and "--bf" in self.args:
  151.             if opt_path == bf_path :
  152.                 x = raw_input("Write the executeble path >> \n")
  153.                 return x
  154.             else:
  155.                 return bf_path
  156.  
  157.         if not bf_path and not opt_path :
  158.             return
  159.         return opt_path
  160.     def initiate(self):
  161.        
  162.         rock_path = self.get_executeble_path()
  163.        
  164.         if not rock_path:
  165.             print "can't found valid executeble path"
  166.             print "use the --bf option or edit the $HOME/.aria2/.bf file"
  167.             return
  168.         sys.path.append(os.path.join(rock_path,"bin"))
  169.         os.chdir(os.path.join(rock_path,"bin"))
  170.         try:
  171.             import mainwin
  172.         except ImportError :
  173.             print  traceback.format_exc()
  174.             print "ImportError: \n\
  175.            Possibilities:\n\
  176.                1.corrupted installation,\n\
  177.                2.missed dependency\n\
  178.                3.fatal bug\n\
  179.                \n\
  180.                action : exit\
  181.            "
  182.             return
  183.         m = mainwin.main_()
  184.         m.main()
  185.  
  186.     def send_cmd(self, cmd):
  187.        
  188.         wfPath = os.path.join(home , ".aria2" , ".p" )
  189.         wp = open(wfPath, 'a')
  190.         wp.write(cmd)
  191.         wp.write("\n")
  192.         wp.close()
  193.  
  194.  
  195.  
  196. if __name__ == '__main__':
  197.     args = get_args()
  198.    
  199.     rd   = rd(args=args)
  200.     if args == ["--help"] or args == ["-h"]:
  201.         print help_message
  202.         os._exit(0)
  203.     if args == ["-v"] or args == ["--version"]:
  204.         print __version__
  205.         os._exit(0)
  206.     if "--alive" in args :
  207.         print rd.getstate()
  208.         os._exit(0)
  209.     if "--id" in args :
  210.         print rd.get_id()
  211.         os._exit(0)
  212.     elif args == ["--disable-clipboard"]:
  213.         rd.send_cmd("--disable-clipboard")
  214.         os._exit(0)
  215.     elif args == ["--quit"]:
  216.         rd.send_cmd(args)
  217.         os._exit(0)
  218.     #dict_ = get_dict_args(args)
  219.     if len(args) == 0:
  220.         rd.run()
  221.     elif len(args) == 1:
  222.         rd.send_cmd(args[0])
  223.         rd.run()
  224.     else:
  225.         for i in args :
  226.             if i.startswith("--url") or i.startswith("url"):
  227.                 x = 0
  228.                 break
  229.             else :
  230.                 x =1
  231.         if x == 1 :
  232.             url = "url="
  233.             for i in args:
  234.                 url  = url+i
  235.                 rd.send_cmd(realargs)
  236.                 rd.run()
  237.         elif x == 0:
  238.             realargs = ""
  239.             j = "  "
  240.             for i in args :
  241.                 i = j+i
  242.                 realargs += i
  243.             rd.send_cmd(realargs)
  244.             rd.run()
  245.     os._exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement