viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # This is mlmanager, a python script thought to handle
  5. # downloaded file from mldonkey.
  6. #
  7. # It is released under the GNU Public License 3
  8. #
  9. # Leonardo Robol <leo@robol.it>
  10.  
  11. __author__ = "Leonardo Robol <leo@robol.it>"
  12.  
  13. import os, sys, socket, shutil
  14.  
  15. # Set file extensions to match.
  16. video_extensions = ['avi', 'mpeg', 'mpg', 'mkv', 'm2v', 'divx', 'xvid']
  17. audio_extensions = ['mp3,' 'ogg', 'wav', 'flac', 'aac' ]
  18. text_extensions = ['pdf', 'doc', 'odt', 'ods', 'odp', 'ppt', 'rtf',
  19. 'pps', 'xls' ]
  20. cdimage_extensions = [ 'iso', 'nrg' ]
  21.  
  22. class FileType():
  23. """
  24. This class represent the type of a file, i.e you
  25. can check if it is a video, a text, an image...
  26. """
  27.  
  28. def __init__(self, filename):
  29. self._filename = filename
  30. self._detect_type ()
  31.  
  32.  
  33. def _test_extension(self, extension):
  34. return self._filename.lower().endswith(extension)
  35.  
  36. def _detect_type(self):
  37. """Detect the type of the file and save it in the internal
  38. varaible _type"""
  39. if len(filter(self._test_extension, video_extensions)) > 0:
  40. self._type = "video"
  41. elif len(filter(self._test_extension, audio_extensions)) > 0:
  42. self._type = "audio"
  43. elif len(filter(self._test_extension, text_extensions)) > 0:
  44. self._type = "text"
  45. elif len(filter(self._test_extension, cdimage_extensions)) > 0:
  46. self._type = "cdimage"
  47. else:
  48. self._type = "other"
  49.  
  50. def is_video(self):
  51. return (self._type == "video")
  52.  
  53. def is_image(self):
  54. return (self._type == "audio")
  55.  
  56. def is_text(self):
  57. return (self._type == "text")
  58.  
  59. def is_cdimage(self):
  60. return (self._type == "cdimage")
  61.  
  62. def __str__(self):
  63. return self._type
  64.  
  65. def __repr__(self):
  66. return "<FileType '%s'>" % self._type
  67.  
  68.  
  69. class Download():
  70. """
  71. This class represent a file or a folder downloaded via mldonkey.
  72. You should create an instance of this calling
  73.  
  74. d = Download("path/to/file")
  75.  
  76. and you should be able to perform your processing with some useful
  77. methods
  78. """
  79.  
  80. def __init__(self, username, password, filename = None, group = None):
  81. """Perform some heuristic to determine the filetype,
  82. filename, groups and similar"""
  83.  
  84. # Set username and password
  85. self._username = username
  86. self._password = password
  87.  
  88. self._filename = filename
  89. self._group = group
  90.  
  91. # If filename is not set then we can recover it
  92. # from the environment variables.
  93. if self._filename is None:
  94. self._filename = os.getenv("FILENAME")
  95.  
  96.  
  97. # Recover other data from environment
  98. if not self._group:
  99. self._group = os.getenv("FILE_GROUP")
  100.  
  101. self._owner = os.getenv("FILE_OWNER")
  102. self._incoming = os.getenv("INCOMING")
  103.  
  104. # The file is not yet committed. You will need to commit it
  105. # before trying to move it.
  106. self._committed = False
  107.  
  108. # Construct the path of the file; this will be the real
  109. # path after it will be committed!
  110. self._dest_path = self._incoming
  111. if not self._dest_path.endswith(os.path.sep):
  112. self._dest_path += os.path.sep
  113. self._dest_path += self._filename
  114.  
  115. self._type = FileType(self._filename)
  116.  
  117. def __repr__(self):
  118. return "<Download '%s'>" % self._filename
  119.  
  120. def _authentication_command (self):
  121. return "auth %s %s" % (self._username, self._password)
  122.  
  123. def commit(self):
  124. """Commit the file, i.e. save it to the hard disk
  125. in its final position. This should be the first
  126. thing you do"""
  127.  
  128. commands = [ self._authentication_command (),
  129. "commit" ]
  130. self.send_command (commands)
  131. self._committed = True
  132.  
  133.  
  134. def send_command(self, command_list):
  135. """You can send a command, or a list of command
  136. to the daemon. Note that the every call to this
  137. function will open a connection to the daemon, so
  138. you will need to authenticate every time.
  139. """
  140. if isinstance(command_list, str):
  141. command_list = [ command_list ]
  142.  
  143. # Open the connection
  144. try:
  145. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  146. s.connect(("localhost", 4001))
  147. except Exception, e:
  148. raise RuntimeError("Unable to connect to mldonkey daemon: %s" % e)
  149.  
  150. # Costruct the command line
  151. command_line = "\n".join(command_list)
  152. # and execute it
  153. s.send(command_line + "\n")
  154.  
  155. # Cleanup
  156. s.send("quit\n")
  157. s.close ()
  158.  
  159. def move(self, destination_folder, filename = None):
  160. """Move the file to destination. destination_folder MUST be
  161. a folder. You could change the filename with the optional
  162. filename parameter"""
  163.  
  164. if not filename:
  165. filename = self._filename
  166.  
  167. # Assicuriamoci che il file sia stato creato
  168. if not self._committed:
  169. self.commit ()
  170.  
  171. # Be sure that this is a directory
  172. if not destination_folder.endswith(os.path.sep):
  173. destination_folder += os.path.sep
  174.  
  175. shutil.move (self._dest_path, destination_folder + filename)
  176.  
  177. # Update _dest_path
  178. self._dest_path = destination_folder + filename
  179.  
  180. def rsync_to(self, remote_destination):
  181. """Rsync the file to the remote destination. There must be an ssh key
  182. in the remote server otherwise nothing will happen. The script will
  183. automatically try a bunch of time to retransfer the file if
  184. the connection fail."""
  185. if not self._committed ():
  186. self.commit ()
  187.  
  188. # Initialize internal counter of the times we have tried to move the file
  189. self._rsync_counter = 0
  190. s = subprocess.Popen("rsync --partial -az --compress-level=9 %s %s" % (self._dest_path,
  191. remote_destination),
  192. shell = True, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
  193. ret_code = s.wait ()
  194. if ret_code != 0:
  195. self._rsync_counter += 1
  196. if self._rsync_counter < 5:
  197. self.rsync_to(remote_destination)
  198. else:
  199. self.notify_error("Rsync transfer of file %s failed more than 5 times, aborting" % self._filename)
  200.  
  201. def notify_error(self, message):
  202. """Notify error via email"""
  203. pass
  204.  
  205. def get_type(self):
  206. """
  207. Return the type of the selected file, it could be
  208. Video, Audio, Image or Other, if none matches.
  209. """
  210. return str(self._type)
  211.  
  212.