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. 'appcmd': self._appcmd,
  46. 'appdir': 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. r = ask ("%s already exists, overwrite it? [y/n]" % self._bundle_path)
  85. if (r != "y"):
  86. return None
  87. with open(self._bundle_path, "w") as handle:
  88. handle.write (file_content.decode ("utf-8"))
  89. return self._bundle_path
  90.  
  91.  
  92. def unbundle_app (self):
  93. """Unbundle app assuming that it is contained in this file"""
  94. with open(__file__, "rb") as handle:
  95. self._bundle = handle.read ()
  96.  
  97. # Decode the content of the file
  98. start = self._bundle.index(15 * "#")
  99. self._bundle = base64.b64decode (self._bundle[start + 14:])
  100.  
  101. tmp_d = tempfile.mkdtemp ()
  102. os.chdir (tmp_d)
  103.  
  104. with open (os.path.join (tmp_d, "bundle.tar"), "wb") as handle:
  105. handle.write (self._bundle)
  106. tar = tarfile.open (os.path.join (tmp_d, "bundle.tar"))
  107. tar.extractall ()
  108. tar.close ()
  109.  
  110. package_info = json.load (open ("info.bundle", "r"))
  111. os.chdir (package_info["appdir"])
  112. p = subprocess.Popen (package_info["appcmd"], shell = True)
  113. p.wait ()
  114. shutil.rmtree (tmp_d)
  115.  
  116. def ask (question):
  117. return raw_input ("\033[32;1m> \033[0m" + question + ": ")
  118.  
  119. def print_msg (msg):
  120. print ("\033[31;1m> \033[0m%s" % msg)
  121.  
  122. if __name__ == "__main__":
  123.  
  124. print_msg ("Starting bundle framework")
  125.  
  126. if len (sys.argv) <= 1:
  127. print_msg ("Entering unbundle mode")
  128. bundle = Bundle ()
  129. bundle.unbundle_app ()
  130. else:
  131. print_msg ("Entering bundle mode")
  132. appdir = sys.argv[1]
  133. appname = ask ("Enter bundle name")
  134. appcmd = ask ("Enter the command to run the application")
  135. bundle = Bundle (appname)
  136. b_path = bundle.bundle_app (appdir, appcmd)
  137.  
  138. if b_path is not None:
  139. print_msg ("Bundle created correctly as %s" % b_path)
  140. else:
  141. print_msg ("Bundle creation canceled")