17 from fnmatch
import fnmatch
18 from subprocess
import PIPE, STDOUT, Popen
23 Simple wrapper to execute a command and return standard output and standard error.
25 d = {
"stdout": PIPE,
"stderr": PIPE}
27 cmd = [cmd] + list(args)
28 logging.debug(
"Execute command: %r %r",
" ".join(cmd), kwargs)
29 proc = Popen(cmd, **d)
30 return proc.communicate()
33 cmt =
lambda *args, **kwargs:
command(
"cmt", *args, **kwargs)
34 cvs =
lambda *args, **kwargs:
command(
"cvs", *args, **kwargs)
35 svn =
lambda *args, **kwargs:
command(
"svn", *args, **kwargs)
40 Find the local packages the current one depends on (using 'cmt broadcast').
41 Returns a list of pairs ("package name","path to the cmt directory").
44 if not sys.platform.startswith(
"win"):
46 "[\n" +
cmt(
"broadcast",
r'echo "(\"<package>\", \"$PWD\"),"')[0] +
"]"
51 +
cmt(
"broadcast",
r'echo ("<package>", r"%<package>root%\cmt"),')[0]
56 [l.strip()
for l
in pkg_dirs.splitlines()
if not l.startswith(
"#")]
63 Returns True if any of the specified glob patterns (list of strings) matched
64 the string 'filename'.
67 if fnmatch(filename, p):
68 logging.debug(
"Excluding file: %r", filename)
75 Replace the entries in files that correspond to directories with the list of
76 files in those directories.
79 lb = len(basepath) + 1
84 base = os.path.join(basepath, f)
85 if os.path.isdir(base):
86 for root, ds, fs
in os.walk(base):
88 newlist.append(os.path.join(root, ff)[lb:])
95 if os.path.isdir(os.path.join(cwd,
"CVS")):
96 return cvs(
"diff",
"-upN", cwd=cwd)
99 out, err =
svn(
"status", cwd=cwd)
101 newfiles = [l
for l
in out.splitlines()
if l.startswith(
"? ")]
102 out, err =
svn(
"diff", cwd=cwd)
104 out =
"\n".join(newfiles) +
"\n" + out
110 Return the patch data for a package.
112 rootdir = os.path.dirname(cmtdir)
115 new_files = [l.split(
None, 1)[1]
for l
in out.splitlines()
if l.startswith(
"? ")]
117 new_files = [f
for f
in new_files
if not matches(f, exclusions)]
120 logging.info(
"Added file %r", f)
124 out +=
"Index: %s\n" % f
125 out +=
"===================================================================\n"
126 out +=
command(
"diff",
"-upN",
"/dev/null", f, cwd=rootdir)[0]
128 removed_files = [l.split()[-1]
for l
in err.splitlines()
if "cannot find" in l]
129 removed_files = [f
for f
in removed_files
if not matches(f, exclusions)]
131 for f
in removed_files:
132 logging.info(
"Removed file %r", f)
134 orig =
cvs(
"up",
"-p", f, cwd=rootdir)[0]
135 out +=
"diff -u -p -N %s\n" % os.path.basename(f)
136 out +=
"--- %s\t1 Jan 1970 00:00:00 -0000\n" % f
137 out +=
"+++ /dev/null\t1 Jan 1970 00:00:00 -0000\n"
138 lines = orig.splitlines()
139 out +=
"@@ -1,%d +0,0 @@\n" % len(lines)
143 rex = re.compile(
r"^(Index: |\? |\+\+\+ |--- (?!/dev/null))", re.MULTILINE)
144 out = rex.sub(
r"\1%s/" % name, out)
149 from optparse
import OptionParser
151 parser = OptionParser(
152 description=
"Produce a patch file from a CMT project. "
153 "The patch contains the changes with respect "
154 "to the CVS repository, including new files "
155 "that are present only locally. Run the script "
156 "from the cmt directory of a package."
165 help=
"Pattern to exclude new files from the patch",
172 help=
"Name of the file to send the output to. Standard "
173 "output is used if not specified",
179 help=
"Print some progress information on standard error",
182 "--debug", action=
"store_true", help=
"Print debug information on standard error"
184 parser.set_defaults(exclusions=[])
186 opts, args = parser.parse_args()
189 logging.basicConfig(level=logging.DEBUG)
191 logging.basicConfig(level=logging.INFO)
204 "cmt/install*.history",
213 "i686-slc[34567]-[ig]cc*",
214 "i686-slc[34567]-clang*",
215 "x86_64-slc[34567]-[ig]cc*",
216 "x86_64-slc[34567]-clang*",
219 if "CMTCONFIG" in os.environ:
220 opts.exclusions.append(os.environ[
"CMTCONFIG"])
223 if not (os.path.basename(os.getcwd()) ==
"cmt" and os.path.exists(
"requirements")):
225 "This script must be executed from the cmt directory of a package."
234 for name, path
in pkgs:
237 "Processing %s from %s (%d/%d)",
239 os.path.dirname(path),
243 patch +=
diff_pkg(name, path, opts.exclusions)
245 if sys.platform.startswith(
"win"):
247 patch = patch.replace(
"\r",
"")
250 logging.info(
"Writing patch file %r", opts.output)
251 open(opts.output,
"w").write(patch)
253 sys.stdout.write(patch)
257 if __name__ ==
"__main__":