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. #
  12. # START OF CONFIGURATION SECTION
  13. #
  14.  
  15. # The fully qualified (or not fully qualified - it really doesn't matter)
  16. # domain that the server is part of.
  17. local_domain = "robol.it"
  18.  
  19. # This is the mail address that will be set as sender for all
  20. # the emails generated by the script.
  21. from_addr = "mldonkey <mldonkey@%s>" % local_domain
  22.  
  23. # Mail server that the script will use to deliver emails. It must be properly
  24. # configured to relay mail from the domain selected.
  25. mail_server = "localhost"
  26.  
  27. # Users that should be notified when an error occurs in the script. You
  28. # can use the wildcard "owner" to match the owner of the file downladed.
  29. # This is generally true for every email function in mlmanager
  30. error_recipients = [ "owner" ]
  31.  
  32. # Number of times that rsync should try to transfer the file before
  33. # giving up.
  34. rsync_tries = 5
  35.  
  36. # Set file extensions to match. You can add extensions in every category
  37. video_extensions = ['avi', 'mpeg', 'mpg', 'mkv', 'm2v', 'divx', 'xvid']
  38. audio_extensions = ['mp3,' 'ogg', 'wav', 'flac', 'aac' ]
  39. text_extensions = ['pdf', 'doc', 'odt', 'ods', 'odp', 'ppt', 'rtf',
  40. 'pps', 'xls' , 'txt' ]
  41. cdimage_extensions = [ 'iso', 'nrg' ]
  42. archive_extensions = [ 'rar', 'zip', '7z', 'tar.gz', 'tar.bz2', 'lzo' ]
  43.  
  44.  
  45. #
  46. # END OF CONFIGURATION
  47. #
  48. #
  49. # START OF CODE
  50. #
  51.  
  52. __author__ = "Leonardo Robol <leo@robol.it>"
  53.  
  54. import os, sys, socket, shutil, subprocess, time, smtplib
  55. from email.mime.text import MIMEText
  56.  
  57. class FileType():
  58. """
  59. This class represent the type of a file, i.e you
  60. can check if it is a video, a text, an image...
  61. It can be:
  62. - video
  63. - audio
  64. - text
  65. - archive
  66. - other
  67. """
  68.  
  69. def __init__(self, filename):
  70. self._filename = filename
  71. self._detect_type ()
  72.  
  73.  
  74. def _test_extension(self, extension):
  75. return self._filename.lower().endswith(extension)
  76.  
  77. def _detect_type(self):
  78. """Detect the type of the file and save it in the internal
  79. varaible _type"""
  80. if len(filter(self._test_extension, video_extensions)) > 0:
  81. self._type = "video"
  82. elif len(filter(self._test_extension, audio_extensions)) > 0:
  83. self._type = "audio"
  84. elif len(filter(self._test_extension, text_extensions)) > 0:
  85. self._type = "text"
  86. elif len(filter(self._test_extension, cdimage_extensions)) > 0:
  87. self._type = "cdimage"
  88. elif len(filter(self._test_extension, archive_extensions)) > 0:
  89. self._type = "archive"
  90. else:
  91. self._type = "other"
  92.  
  93. def is_video(self):
  94. return (self._type == "video")
  95.  
  96. def is_image(self):
  97. return (self._type == "audio")
  98.  
  99. def is_text(self):
  100. return (self._type == "text")
  101.  
  102. def is_cdimage(self):
  103. return (self._type == "cdimage")
  104.  
  105. def is_archive(self):
  106. return (self._type == "archive")
  107.  
  108. def __str__(self):
  109. return self._type
  110.  
  111. def __repr__(self):
  112. return "<FileType '%s'>" % self._type
  113.  
  114.  
  115. class Download():
  116. """
  117. This class represent a file or a folder downloaded via mldonkey.
  118. You should create an instance of this calling
  119.  
  120. d = Download("path/to/file")
  121.  
  122. and you should be able to perform your processing with some useful
  123. methods
  124. """
  125.  
  126. def __init__(self, username, password, filename = None, group = None):
  127. """Perform some heuristic to determine the filetype,
  128. filename, groups and similar"""
  129.  
  130. # Set username and password
  131. self._username = username
  132. self._password = password
  133.  
  134. self._filename = filename
  135. self._group = group
  136.  
  137. # If filename is not set then we can recover it
  138. # from the environment variables.
  139. if self._filename is None:
  140. self._filename = os.getenv("FILENAME")
  141.  
  142.  
  143. # Recover other data from environment
  144. if not self._group:
  145. self._group = os.getenv("FILE_GROUP")
  146.  
  147. self._owner = os.getenv("FILE_OWNER")
  148. self._incoming = os.getenv("INCOMING")
  149.  
  150. self._user_email = os.getenv("USER_EMAIL")
  151.  
  152. # The file is not yet committed. You will need to commit it
  153. # before trying to move it.
  154. self._committed = False
  155.  
  156. # Construct the path of the file; this will be the real
  157. # path after it will be committed!
  158. self._dest_path = self._incoming
  159. if not self._dest_path.endswith(os.path.sep):
  160. self._dest_path += os.path.sep
  161. self._dest_path += self._filename
  162.  
  163. self._type = FileType(self._filename)
  164.  
  165.  
  166. def __repr__(self):
  167. return "<Download '%s'>" % self._filename
  168.  
  169. def _authentication_command (self):
  170. return "auth %s %s" % (self._username, self._password)
  171.  
  172. def commit(self):
  173. """Commit the file, i.e. save it to the hard disk
  174. in its final position. This should be the first
  175. thing you do"""
  176.  
  177. commands = [ self._authentication_command (),
  178. "commit" ]
  179. self.send_command (commands)
  180. self._committed = True
  181.  
  182.  
  183.  
  184. def send_command(self, command_list):
  185. """You can send a command, or a list of command
  186. to the daemon. Note that the every call to this
  187. function will open a connection to the daemon, so
  188. you will need to authenticate every time.
  189. """
  190. if isinstance(command_list, str):
  191. command_list = [ command_list ]
  192.  
  193. # Open the connection
  194. try:
  195. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  196. s.connect(("localhost", 4000))
  197. except Exception, e:
  198. self.notify_error("Unable to connect to mldonkey daemon: %s" % e)
  199.  
  200. # Costruct the command line
  201. command_line = "\n".join(command_list)
  202. # and execute it
  203. s.send(command_line + "\n")
  204.  
  205. # Cleanup
  206. s.send("quit\n")
  207. s.close ()
  208.  
  209. def move(self, destination_folder, filename = None):
  210. """Move the file to destination. destination_folder MUST be
  211. a folder. You could change the filename with the optional
  212. filename parameter"""
  213.  
  214. if not filename:
  215. filename = self._filename
  216.  
  217. # Assicuriamoci che il file sia stato creato
  218. if not self._committed:
  219. self.commit ()
  220.  
  221. # Be sure that this is a directory
  222. if not destination_folder.endswith(os.path.sep):
  223. destination_folder += os.path.sep
  224.  
  225. shutil.move (self._dest_path, destination_folder + filename)
  226.  
  227. # Update _dest_path
  228. self._dest_path = destination_folder + filename
  229.  
  230. def copy(self, destination, track = False):
  231. """
  232. Copy the file to another destination. Destination could be a folder
  233. to move the file in, or a complete path. The script will keep track
  234. only of the original file, i.e. if you call move() it will move the
  235. original file; if this is not what you want, move() the file to the
  236. right location and then copy() it around."""
  237.  
  238. if not self._committed:
  239. self.commit()
  240. shutil.copy(self._dest_path, destination)
  241.  
  242.  
  243. def rsync(self, remote_destination):
  244. """Rsync the file to the remote destination. There must be an ssh key
  245. in the remote server otherwise nothing will happen. The script will
  246. automatically try a bunch of time to retransfer the file if
  247. the connection fail."""
  248. if not self._committed:
  249. self.commit ()
  250.  
  251. # Initialize internal counter of the times we have tried to move the file
  252. self._rsync_counter = 0
  253. s = subprocess.Popen("rsync --partial -az --compress-level=9 \"%s\" \"%s\"" % (self._dest_path,
  254. remote_destination),
  255. shell = True, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
  256. ret_code = s.wait ()
  257.  
  258. # If we fail call this funtion recursively to retry...wait for 60 seconds and then go (it could
  259. # be only a network problem)
  260. if ret_code != 0:
  261. self._rsync_counter += 1
  262. if self._rsync_counter < rsync_tries:
  263. time.sleep (60)
  264. self.rsync(remote_destination)
  265. else:
  266. self.notify_error("Rsync transfer of file %s failed more than 5 times, aborting" % self._filename)
  267.  
  268. def notify_error(self, message):
  269. """Notify error via email"""
  270. self._send_mail (error_recipients, "[mlmanager] An error occurred",
  271. message)
  272.  
  273. def notify_email(self, recipients, subject, message):
  274. """Notify something to some people via email"""
  275. self._send_email (recipients, subject, message)
  276.  
  277. def _send_email(self, recipients, subject, message):
  278. """Low level function to send an e-mail."""
  279.  
  280. msg = MIMEText(message)
  281. msg.set_charset ("utf-8")
  282. msg['From'] = from_addr
  283.  
  284. # If recipients is a string make it a list
  285. if isinstance(recipients, str):
  286. recipients = [ recipients ]
  287.  
  288. # Add user email if requested
  289. if "owner" in recipients:
  290. recipients.remove("owner")
  291. recipients.append(self._user_email)
  292.  
  293. msg['To'] = ", ".join(to_addr)
  294. msg['Subject'] = subject
  295.  
  296. # Obtain message data
  297. data = msg.as_string ()
  298.  
  299. # Open a connection to the SMTP server
  300. try:
  301. s = smtplib.SMTP( host = mail_server )
  302. s.sendmail (from_addr, to_addr, data)
  303. s.quit ()
  304. except Exception, e:
  305. raise RuntimeError("Error while notifying you of an error: %s" % e)
  306.  
  307. def is_in_group(self, group):
  308. """Return True if file is part of the selected group,
  309. False otherwise"""
  310. return (self._group == group)
  311.  
  312.  
  313. def get_type(self):
  314. """
  315. Return the type of the selected file, it could be
  316. video, audio, image, cdimage, archive or other, if none matches.
  317. """
  318. return str(self._type)
  319.  
  320.