Support for bzr inventory operations
authorAaron Bentley <aaron.bentley@utoronto.ca>
Wed, 4 May 2005 10:22:12 +0000 (10:22 +0000)
committerAaron Bentley <aaron.bentley@utoronto.ca>
Wed, 4 May 2005 10:22:12 +0000 (10:22 +0000)
libbe/bugdir.py
libbe/bzr.py [new file with mode: 0644]
libbe/rcs.py

index ab39a18d5c9c11e14c5565a764ad9fc41914017d..61f5e303e76537f1d0de865f5a7cac96eafaf385 100644 (file)
@@ -82,7 +82,7 @@ class BugDir:
         except NoSuchFile:
             self.settings = {"rcs_name": "None"}
 
-    rcs_name = setting_property("rcs_name", ("None", "Arch"))
+    rcs_name = setting_property("rcs_name", ("None", "bzr", "Arch"))
     _rcs = None
 
     target = setting_property("target")
@@ -312,10 +312,11 @@ def pyname_to_header(name):
     
 def map_save(rcs, path, map):
     """Save the map as a mapfile to the specified path"""
-    if not os.path.exists(path):
-        rcs.add_id(path)
+    add = not os.path.exists(path)
     output = file(path, "wb")
     mapfile.generate(output, map)
+    if add:
+        rcs.add_id(path)
 
 class NoSuchFile(Exception):
     def __init__(self, pathname):
diff --git a/libbe/bzr.py b/libbe/bzr.py
new file mode 100644 (file)
index 0000000..350e86b
--- /dev/null
@@ -0,0 +1,65 @@
+from popen2 import Popen3
+import os
+import config
+
+def invoke(args):
+    q=Popen3(args, True)
+    output = q.fromchild.read()
+    error = q.childerr.read()
+    status = q.wait()
+    if os.WIFEXITED(status):
+        return os.WEXITSTATUS(status), output, error
+    raise Exception("Command failed: %s" % error)
+
+def invoke_client(*args, **kwargs):
+    cl_args = ["bzr"]
+    cl_args.extend(args)
+    status,output,error = invoke(cl_args)
+    if status not in (0,):
+        raise Exception("Command failed: %s" % error)
+    return output
+
+def add_id(filename):
+    invoke_client("add", filename)
+
+def delete_id(filename):
+    invoke_client("remove", filename)
+
+def mkdir(path):
+    os.mkdir(path)
+    add_id(path)
+
+def set_file_contents(path, contents):
+    add = not os.path.exists(path)
+    file(path, "wb").write(contents)
+    if add:
+        add_id(path)
+
+
+def path_in_reference(bug_dir, spec):
+    if spec is not None:
+        return invoke_client("file-find", bug_dir, spec).rstrip('\n')
+    return invoke_client("file-find", bug_dir).rstrip('\n')
+
+
+def unlink(path):
+    try:
+        os.unlink(path)
+        delete_id(path)
+    except OSError, e:
+        if e.errno != 2:
+            raise
+
+
+def detect(path):
+    """Detect whether a directory is revision-controlled using bzr"""
+    path = os.path.realpath(path)
+    while True:
+        if os.path.exists(os.path.join(path, ".bzr")):
+            return True
+        if path == "/":
+            return False
+        path = os.path.dirname(path)
+
+
+name = "bzr"
index 6dba51a9a510bf52cc53856556a89ad482c6b5ef..fac4b0b9b8cfe0487d65b779a7fba7f40bb98faf 100644 (file)
@@ -3,6 +3,9 @@ def rcs_by_name(rcs_name):
     if rcs_name == "Arch":
         import arch
         return arch
+    elif rcs_name == "bzr":
+        import bzr
+        return bzr
     elif rcs_name == "None":
         import no_rcs
         return no_rcs
@@ -10,7 +13,10 @@ def rcs_by_name(rcs_name):
 def detect(dir):
     """Return the module for the rcs being used in this directory"""
     import arch
+    import bzr
     if arch.detect(dir):
         return arch
+    elif bzr.detect(dir):
+        return bzr
     import no_rcs
     return no_rcs