workdir: cleanup the created tmp file
[yap.git] / plugins / workdir.py
blob1c6eb46f6d2b3d75722e51b8f3abb4027df9c583
2 from yap.yap import YapCore, YapError
3 from yap.util import get_output, takes_options, run_safely
5 import os
6 import tempfile
8 class WorkdirPlugin(YapCore):
9 "Create extra work directories of a repository"
11 def __init__(self, *args, **flags):
12 super(WorkdirPlugin, self).__init__(*args, **flags)
14 def _unlock_branch(self, branch):
15 repo = get_output('git rev-parse --git-dir')[0]
16 dir = os.path.join(repo, 'yap', 'lock')
17 try:
18 os.mkdir(dir)
19 except OSError:
20 pass
22 lockfile = os.path.join(dir, branch.replace('/', '\/'))
24 try:
25 os.unlink(lockfile)
26 except OSError:
27 pass
29 def _lock_branch(self, branch, locked_by):
30 repo = get_output('git rev-parse --git-dir')[0]
31 dir = os.path.join(repo, 'yap', 'lock')
32 try:
33 os.mkdir(dir)
34 except OSError:
35 pass
37 fd, tmplock = tempfile.mkstemp("yap", dir=dir)
38 try:
39 os.write(fd, locked_by)
40 os.close(fd)
41 while True:
42 lockfile = os.path.join(dir, branch.replace('/', '\/'))
43 try:
44 os.link(tmplock, lockfile)
45 break
46 except OSError, e:
47 try:
48 fd = file(lockfile)
49 except:
50 raise e
51 user = fd.readline()
52 # If the workdir has been deleted, break his lock
53 if os.access(user, os.R_OK):
54 raise YapError("That branch is being used by an existing workdir")
55 os.unlink(lockfile)
56 continue
57 finally:
58 os.unlink(tmplock)
60 def cmd_workdir(self, branch, workdir=None):
61 "<branch> [workdir]"
63 self._check_git()
65 branches = get_output("git for-each-ref --format='%(refname)' 'refs/heads'")
66 if 'refs/heads/%s' % branch not in branches:
67 raise YapError("Not a branch: %s" % branch)
69 current = get_output("git symbolic-ref HEAD")[0]
71 repo = get_output('git rev-parse --git-dir')[0]
72 repo = os.path.join(os.getcwd(), repo)
73 repodir = os.path.dirname(repo)
74 if workdir is None:
75 repoparent, reponame = os.path.split(repodir)
76 workdir = os.path.join(repoparent, "%s-%s" % (reponame, branch))
78 # Make sure the current branch is locked
79 try:
80 self._lock_branch(current.replace('refs/heads/', ''), repodir)
81 except:
82 pass
84 self._lock_branch(branch, workdir)
86 try:
87 os.mkdir(workdir)
88 except OSError, e:
89 raise YapError("Can't create new workdir: %s (%s)" % (workdir, e))
91 os.chdir(workdir)
92 os.mkdir(".git")
93 os.chdir(".git")
95 for x in ["config", "refs", "logs/refs", "objects", "info",
96 "hooks", "packed-refs", "remotes", "svn"]:
97 if os.path.dirname(x):
98 os.makedirs(os.path.dirname(x))
99 os.symlink(os.path.join(repo, x), x)
101 run_safely("cp %s HEAD" % os.path.join(repo, 'HEAD'))
102 os.chdir("..")
103 run_safely("git symbolic-ref HEAD refs/heads/%s" % branch)
104 self.cmd_revert(**{'-a': 1})
106 print "Workdir created at %s for branch %s" % (workdir, branch)
108 def cmd_switch(self, branch, *args, **flags):
109 self._check_git()
111 current = get_output("git symbolic-ref HEAD")[0]
113 repo = get_output('git rev-parse --git-dir')[0]
114 self._lock_branch(branch, repo)
116 try:
117 super(WorkdirPlugin, self).cmd_switch(branch, *args, **flags)
118 except:
119 self._unlock_branch(branch)
120 raise
122 self._unlock_branch(current.replace('refs/heads/', ''))