Commits

Jannis Leidel committed ec6d4ee

Initial checkin, version 0.1

Comments (0)

Files changed (3)

+Copyright (c) 2009, Jannis Leidel
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+    * Redistributions of source code must retain the above copyright
+      notice, this list of conditions and the following disclaimer.
+    * Redistributions in binary form must reproduce the above
+      copyright notice, this list of conditions and the following
+      disclaimer in the documentation and/or other materials provided
+      with the distribution.
+    * Neither the name of the author nor the names of other
+      contributors may be used to endorse or promote products derived
+      from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+from setuptools import setup
+
+long_desciption = """
+A plugin for setuptools to find files under Mercurial version control to be
+automatically included as package data.
+
+It works correctly with Mercurial 1.x and uses Mercurial's Python library by
+default. It will however fall back to use the command line programm hg(1) to
+determin the list of files.
+"""
+
+setup(
+    name="setuptools_hg",
+    version='0.1',
+    author="Jannis Leidel",
+    author_email="jannis@leidel.info",
+    url="http://bitbucket.org/jezdez/",
+    description="Setuptools plugin for finding files under Mercurial version control.",
+    long_desciption=long_desciption,
+    license="BSD License",
+    classifiers=[
+        "Development Status :: 4 - Beta",
+        "Programming Language :: Python",
+        "Intended Audience :: Developers",
+        "Operating System :: OS Independent",
+        "License :: OSI Approved :: BSD License",
+        "Topic :: Software Development :: Version Control"
+    ],
+    py_modules=["setuptools_hg"],
+    entry_points = {
+        "setuptools.file_finders": [
+            "hg = setuptools_hg:hg_file_finder"
+        ]
+    }
+)
+"""
+A plugin for setuptools to find files under the Mercurial version control
+system which uses the Python library by default and falls back to use the
+command line programm hg(1).
+"""
+__version__ = '0.1'
+__author__ = 'Jannis Leidel'
+__all__ = ['hg_file_finder']
+
+import os
+import subprocess
+
+try:
+    from mercurial import hg, ui, cmdutil
+    from mercurial.repo import RepoError
+except ImportError:
+    hg = None
+
+if os.getenv('HG_SETUPTOOLS_FORCE_CMD'):
+    hg = None
+
+def find_files_with_lib(dirname):
+    """
+    Use the Mercurial library to recursively find versioned files in dirname.
+    """
+    try:
+        repo = hg.repository(ui.ui(), dirname)
+    except RepoError:
+        return
+
+    # tuple of (modified, added, removed, deleted, unknown, ignored, clean)
+    modified, added, removed, deleted, unknown = repo.status()[:5]
+
+    # exclude all files that hg knows about, but haven't been added,
+    # or have been deleted, removed, or have an unknown status
+    excluded = removed + deleted + unknown
+
+    rev = None
+    match = cmdutil.match(repo, [], {}, default='relglob')
+    match.bad = lambda x, y: False
+    for abs in repo[rev].walk(match):
+        if not rev and abs not in repo.dirstate:
+            continue
+        if abs in excluded:
+            continue
+        yield abs
+
+def find_files_with_cmd(dirname="."):
+    """
+    Use the hg command to recursively find versioned files in dirname.
+    """
+    proc = subprocess.Popen(['hg', 'locate'],
+                            stdin=subprocess.PIPE,
+                            stdout=subprocess.PIPE,
+                            stderr=subprocess.PIPE,
+                            cwd=dirname)
+    stdout, stderr = proc.communicate()
+    return stdout.splitlines()
+
+def hg_file_finder(dirname="."):
+    """
+    Find the files in ``dirname`` under Mercurial version control.
+    """
+    if not dirname:
+        dirname = "."
+    if hg is None:
+        return find_files_with_cmd(dirname)
+    return find_files_with_lib(dirname)
+
+def main():
+    import sys
+    from pprint import pprint
+    try:
+        dirname = sys.argv[1]
+    except IndexError:
+        dirname = "."
+    pprint(list(hg_file_finder(dirname)))
+
+if __name__ == '__main__':
+    main()