Rev 16: Start to simplify flush_range. in http://people.ubuntu.com/~robertc/baz2.0/plugins/groupcompress/trunk

Robert Collins robertc at robertcollins.net
Thu Jul 24 03:09:31 BST 2008


At http://people.ubuntu.com/~robertc/baz2.0/plugins/groupcompress/trunk

------------------------------------------------------------
revno: 16
revision-id: robertc at robertcollins.net-20080724020930-wnm0fmro7lari347
parent: robertc at robertcollins.net-20080724015151-vul2s7bru8fqytjl
committer: Robert Collins <robertc at robertcollins.net>
branch nick: trunk
timestamp: Thu 2008-07-24 12:09:30 +1000
message:
  Start to simplify flush_range.
modified:
  groupcompress.py               groupcompress.py-20080705181503-ccbxd6xuy1bdnrpu-8
  setup.py                       setup.py-20080705181503-ccbxd6xuy1bdnrpu-9
=== modified file 'groupcompress.py'
--- a/groupcompress.py	2008-07-24 01:51:51 +0000
+++ b/groupcompress.py	2008-07-24 02:09:30 +0000
@@ -137,7 +137,6 @@
         line_locations = self.line_locations
         range_len = 0
         range_start = 0
-        flush_range = self.flush_range
         copy_ends = None
         # We either copy a range (while there are reusable lines) or we 
         # insert new lines. To find reusable lines we traverse 
@@ -209,13 +208,13 @@
         for old_start, new_start, range_len in blocks:
             if new_start != current_pos:
                 # non-matching region
-                flush_range(False, current_pos, None, new_start - current_pos,
+                flush_range(current_pos, None, new_start - current_pos,
                     lines, new_lines, index_lines)
             current_pos = new_start + range_len
             if not range_len:
                 continue
-            flush_range(True, new_start, [old_start + range_len], range_len,
-                lines, new_lines, index_lines)
+            flush_range(new_start, old_start, range_len, lines,
+                new_lines, index_lines)
         delta_start = (self.endpoint, len(self.lines))
         self.output_lines(new_lines, index_lines)
         trim_encoding_newline(lines)
@@ -240,13 +239,12 @@
         sha1 = sha_strings(lines)
         return lines, sha1
 
-    def flush_range(self, copying, range_start, copy_ends, range_len, lines, new_lines, index_lines):
+    def flush_range(self, range_start, copy_start, range_len, lines, new_lines, index_lines):
         if not range_len:
             return
         insert_instruction = "i,%d\n" % range_len
-        if copying:
+        if copy_start is not None:
             # range stops, flush and start a new copy range
-            copy_start = min(copy_ends) - range_len
             stop_byte = self.line_offsets[copy_start + range_len - 1]
             if copy_start == 0:
                 start_byte = 0
@@ -263,7 +261,7 @@
         new_lines.append(insert_instruction)
         new_lines.extend(lines[range_start:range_start+range_len])
         index_lines.append(False)
-        index_lines.extend([not copying]*range_len)
+        index_lines.extend([copy_start is None]*range_len)
 
     def output_lines(self, new_lines, index_lines):
         """Output some lines.

=== modified file 'setup.py'
--- a/setup.py	2008-07-05 18:15:40 +0000
+++ b/setup.py	2008-07-24 02:09:30 +0000
@@ -1,10 +1,81 @@
 #!/usr/bin/env python
+import os
 from distutils.core import setup
 
 bzr_plugin_name = 'groupcompress'
 
 bzr_plugin_version = (1, 6, 0, 'dev', 0)
 
+from distutils import log
+from distutils.errors import CCompilerError, DistutilsPlatformError
+from distutils.extension import Extension
+ext_modules = []
+try:
+    from Pyrex.Distutils import build_ext
+except ImportError:
+    have_pyrex = False
+    # try to build the extension from the prior generated source.
+    print
+    print ("The python package 'Pyrex' is not available."
+           " If the .c files are available,")
+    print ("they will be built,"
+           " but modifying the .pyx files will not rebuild them.")
+    print
+    from distutils.command.build_ext import build_ext
+else:
+    have_pyrex = True
+
+
+class build_ext_if_possible(build_ext):
+
+    def run(self):
+        try:
+            build_ext.run(self)
+        except DistutilsPlatformError, e:
+            log.warn(str(e))
+            log.warn('Extensions cannot be built, '
+                     'will use the Python versions instead')
+
+    def build_extension(self, ext):
+        try:
+            build_ext.build_extension(self, ext)
+        except CCompilerError:
+            log.warn('Building of "%s" extension failed, '
+                     'will use the Python version instead' % (ext.name,))
+
+
+# Override the build_ext if we have Pyrex available
+unavailable_files = []
+
+
+def add_pyrex_extension(module_name, **kwargs):
+    """Add a pyrex module to build.
+
+    This will use Pyrex to auto-generate the .c file if it is available.
+    Otherwise it will fall back on the .c file. If the .c file is not
+    available, it will warn, and not add anything.
+
+    You can pass any extra options to Extension through kwargs. One example is
+    'libraries = []'.
+
+    :param module_name: The python path to the module. This will be used to
+        determine the .pyx and .c files to use.
+    """
+    path = module_name.replace('.', '/')
+    pyrex_name = path + '.pyx'
+    c_name = path + '.c'
+    # Manually honour package_dir :(
+    module_name = 'bzrlib.plugins.index2.' + module_name
+    if have_pyrex:
+        ext_modules.append(Extension(module_name, [pyrex_name]))
+    else:
+        if not os.path.isfile(c_name):
+            unavailable_files.append(c_name)
+        else:
+            ext_modules.append(Extension(module_name, [c_name]))
+
+add_pyrex_extension('_groupcompress_c')
+
 
 if __name__ == '__main__':
     setup(name="bzr groupcompress",
@@ -18,4 +89,6 @@
                     'bzrlib.plugins.groupcompress.tests',
                     ],
           package_dir={'bzrlib.plugins.groupcompress': '.'},
+          cmdclass={'build_ext': build_ext_if_possible},
+          ext_modules=ext_modules,
           }




More information about the bazaar-commits mailing list