Package qm :: Package dist :: Package command :: Module install_extensions
[hide private]
[frames] | no frames]

Source Code for Module qm.dist.command.install_extensions

  1  ######################################################################## 
  2  # 
  3  # File:   install_extensions.py 
  4  # Author: Stefan Seefeld 
  5  # Date:   2005-11-16 
  6  # 
  7  # Contents: 
  8  #   Command to install qmtest extensions. 
  9  # 
 10  # Copyright (c) 2005 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  ######################################################################## 
 17  # Imports 
 18  ######################################################################## 
 19   
 20  import qm.xmlutil 
 21  from distutils.command.install_lib import install_lib 
 22  import os, dircache, xml 
 23   
 24  ######################################################################## 
 25  # Classes 
 26  ######################################################################## 
 27   
28 -def _compare_files(a, b):
29 """Compare the content of two files. 30 31 'a' -- Filename of first file to be compared. 32 33 'b' -- Filename of second file to be compared. 34 35 returns -- True if both files have the same content, False otherwise.""" 36 37 file_a, file_b = open(a, 'r'), open(b, 'r') 38 for line in file_a: 39 if line != file_b.readline(): 40 return False 41 if file_b.readline(): 42 return False 43 return True
44 45
46 -class install_extensions(install_lib):
47 """Install extension files.""" 48 49 description = "install qmtest extension classes." 50 51
52 - def finalize_options(self):
53 54 if not self.install_dir: 55 i = self.distribution.get_command_obj('install') 56 i.ensure_finalized() 57 prefix = i.root or i.prefix 58 self.install_dir = os.path.join(prefix, qm.extension_path) 59 install_lib.finalize_options(self) 60 61 b = self.distribution.get_command_obj('build_extensions') 62 b.ensure_finalized() 63 self.build_dir = b.build_dir
64 65
66 - def run(self):
67 68 # Make sure we have built everything we need first 69 self.run_command('build_extensions') 70 71 if not os.path.isdir(self.install_dir): 72 self.mkpath(self.install_dir) 73 if not os.path.exists(os.path.join(self.install_dir, 'classes.qmc')): 74 # This is the first time extensions are installed there. 75 self.copy_tree(self.build_dir, self.install_dir) 76 else: 77 # Test that to-be-copied modules don't overwrite existing ones: 78 old_files = [f for f in dircache.listdir(self.install_dir) 79 if f.endswith('.py')] 80 new_files = [f for f in dircache.listdir(self.build_dir) 81 if f.endswith('.py') and f != '__init__.py'] 82 overlap = [] 83 for f in new_files: 84 if f in old_files: 85 # Don't complain if both files are identical. 86 if not _compare_files(os.path.join(self.build_dir, f), 87 os.path.join(self.install_dir, f)): 88 overlap.append(f) 89 if overlap: 90 print "Error: The following extension files already exist:" 91 for o in overlap: 92 print " %s"%o 93 return 94 # Copy all modules to the install directory. 95 for f in new_files: 96 self.copy_file(os.path.join(self.build_dir, f), 97 os.path.join(self.install_dir, f), 98 preserve_mode=0) 99 100 # Carefully merge the new extensions into an existing repository. 101 old_qmc = qm.xmlutil.load_xml_file(os.path.join(self.install_dir, 102 'classes.qmc')) 103 old_root = old_qmc.documentElement 104 105 new_qmc = qm.xmlutil.load_xml_file(os.path.join(self.build_dir, 106 'classes.qmc')) 107 new_root = new_qmc.documentElement 108 109 for ext in new_root.getElementsByTagName("class"): 110 # If this entry already exists in the repository, skip it. 111 name = ext.getAttribute("name") 112 entries = [c for c in old_root.childNodes 113 if c.nodeType == xml.dom.Node.ELEMENT_NODE and 114 c.tagName == "class" and 115 c.getAttribute("name") == name] 116 if not entries: 117 old_root.appendChild(ext) 118 old_root.appendChild(old_qmc.createTextNode('\n')) 119 # Write new repository file. 120 old_qmc.writexml(open(os.path.join(self.install_dir, 121 'tmp-classes.qmc'), 'w')) 122 # If that worked, update the original one. 123 os.rename(os.path.join(self.install_dir, 'tmp-classes.qmc'), 124 os.path.join(self.install_dir, 'classes.qmc'))
125