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