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/python
  2. #
  3. #
  4.  
  5. import tarfile
  6. import tempfile
  7. import json
  8. import os
  9. import sys
  10. import base64
  11. import shutil
  12. import subprocess
  13.  
  14. class Bundle():
  15. """This class represent a bundle of an application
  16. that can be easily moved around."""
  17.  
  18. def __init__(self, appname = None):
  19. """
  20. :param appname The name of the application
  21. whose this bundle refers to
  22. """
  23. self._appname = appname
  24. self._bundle = None
  25. self._appcmd = None
  26.  
  27.  
  28. def bundle_app (self, appdir, appcmd):
  29. """Bundle an application and prepare it for
  30. distribution."""
  31. if self._bundle is not None:
  32. raise RuntimeError ("Only a single application can be bundled")
  33. self._appcmd = appcmd
  34. self._appdir = appdir
  35. if (self._appdir.endswith ("/")):
  36. self._appdir = appdir[:-1]
  37.  
  38. # Create the tar archive to be packed
  39. tmp_f = tempfile.NamedTemporaryFile (delete = False)
  40. tar = tarfile.TarFile (fileobj = tmp_f, mode = 'w')
  41.  
  42. # Create packing information
  43. package_info = {
  44. 'name': self._appname,
  45. 'cmd': self._appcmd,
  46. 'dir': os.path.basename (self._appdir),
  47. }
  48.  
  49. # Add the directory that needs to be packed together
  50. # with the info on how to run the package. We need to
  51. # chdir to the right place to have directory structure
  52. # packed right
  53. old_dir = os.getcwd ()
  54. os.chdir (os.path.dirname (self._appdir))
  55. tar.add (os.path.basename (self._appdir))
  56.  
  57. # Create the info file
  58. with open ("info.bundle", "w") as handle:
  59. handle.write (json.dumps (package_info))
  60. tar.add ("info.bundle")
  61.  
  62. # Close the tar file and copy the tar in my home
  63. tar.close ()
  64. tmp_f.close ()
  65.  
  66. # Read the file
  67. with open (tmp_f.name, "rb") as handle:
  68. self._bundle = handle.read ()
  69.  
  70. # Remove the file since we don't need it anymore
  71. os.remove (tmp_f.name)
  72.  
  73. # Pack the application with this python code to make
  74. # it unbundlable.
  75. os.chdir (old_dir)
  76. with open(__file__, "rb") as handle:
  77. file_content = handle.read ()
  78.  
  79. file_content += b"\n\n\n" + 15 * b"#" + base64.b64encode (self._bundle)
  80.  
  81. # Dump file_content
  82. self._bundle_path = self._appname + ".bundle"
  83. if os.path.exists (self._bundle_path):
  84. print_msg ("Not overwriting %s, please remove it manually" % self._bundle_path)
  85. return None
  86. with open(self._bundle_path, "w") as handle:
  87. handle.write (file_content.decode ("utf-8"))
  88. return self._bundle_path
  89.  
  90. def tar_filter (self, tarinfo):
  91. """Check if a file has to be packed or not,
  92. and skips files such as version control logs
  93. and source files"""
  94. pass
  95.  
  96. def unbundle_app (self):
  97. """Unbundle app assuming that it is contained in this file"""
  98. with open(__file__, "rb") as handle:
  99. self._bundle = handle.read ().decode ("utf-8")
  100.  
  101. # Decode the content of the file
  102. start = self._bundle.index(15 * "#")
  103. self._bundle = base64.b64decode (self._bundle[start + 14:].encode ("utf-8"))
  104.  
  105. tmp_d = tempfile.mkdtemp ()
  106. os.chdir (tmp_d)
  107.  
  108. with open ("bundle.tar", "wb") as handle:
  109. handle.write (self._bundle)
  110. tar = tarfile.open ("bundle.tar")
  111.  
  112. tar.extractall ()
  113. tar.close ()
  114.  
  115. package_info = json.load (open ("info.bundle", "r"))
  116. print_msg ("Launching %s" % package_info["name"])
  117. os.chdir (package_info["dir"])
  118.  
  119. # Executing the program
  120. p = subprocess.Popen (package_info["cmd"], shell = True)
  121. p.wait ()
  122.  
  123. # Remove extracted files
  124. shutil.rmtree (tmp_d)
  125.  
  126. def print_msg (msg):
  127. print ("\033[31;1m> \033[0m%s" % msg)
  128.  
  129. def print_help ():
  130. help_text = """
  131. Bundle.py v0.1 --- A simple framework to pack applications
  132.  
  133. bundle.py is a simple python script that packs compiled application
  134. in a single autoextracting python file that can be executed and
  135. runs the application.
  136.  
  137. To pack your application, simply create an application.info file
  138. similar to this (JSON coded dictionary)
  139.  
  140. {
  141. "name": "My beatiful application",
  142. "path": "/home/me/dev/my_app",
  143. "cmd": "./src/my_app"
  144. }
  145.  
  146. and then run ./bundle.py application.info
  147. """
  148. print (help_text)
  149. sys.exit (0)
  150.  
  151. if __name__ == "__main__":
  152.  
  153. # Check if this is the bundle.py source file
  154. # and if it does not contain a bundle
  155. with open (__file__, "r") as handle:
  156. if len(sys.argv) <= 1 and not (15 * "#") in handle.read ():
  157. print_help ()
  158.  
  159. print_msg ("Starting bundle framework")
  160.  
  161. if len (sys.argv) <= 1:
  162. print_msg ("Entering unbundle mode")
  163. bundle = Bundle ()
  164. bundle.unbundle_app ()
  165. else:
  166. print_msg ("Entering bundle mode")
  167. package_info = json.load (open (sys.argv[1], "r"))
  168. bundle = Bundle (package_info["name"])
  169. b_path = bundle.bundle_app (package_info["path"],
  170. package_info["cmd"])
  171.  
  172. if b_path is not None:
  173. print_msg ("Bundle created correctly as %s" % b_path)
  174. else:
  175. print_msg ("Bundle creation canceled")