17 from fnmatch
import fnmatch
18 from subprocess
import PIPE, 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 def cmt(*args, **kwargs):
34 return command(
"cmt", *args, **kwargs)
37 def cvs(*args, **kwargs):
38 return command(
"cvs", *args, **kwargs)
41 def svn(*args, **kwargs):
42 return command(
"svn", *args, **kwargs)
47 Find the local packages the current one depends on (using 'cmt broadcast').
48 Returns a list of pairs ("package name","path to the cmt directory").
51 if not sys.platform.startswith(
"win"):
53 "[\n" +
cmt(
"broadcast",
r'echo "(\"<package>\", \"$PWD\"),"')[0] +
"]"
58 +
cmt(
"broadcast",
r'echo ("<package>", r"%<package>root%\cmt"),')[0]
63 [l.strip()
for l
in pkg_dirs.splitlines()
if not l.startswith(
"#")]
70 Returns True if any of the specified glob patterns (list of strings) matched
71 the string 'filename'.
74 if fnmatch(filename, p):
75 logging.debug(
"Excluding file: %r", filename)
82 Replace the entries in files that correspond to directories with the list of
83 files in those directories.
86 lb = len(basepath) + 1
91 base = os.path.join(basepath, f)
92 if os.path.isdir(base):
93 for root, ds, fs
in os.walk(base):
95 newlist.append(os.path.join(root, ff)[lb:])
102 if os.path.isdir(os.path.join(cwd,
"CVS")):
103 return cvs(
"diff",
"-upN", cwd=cwd)
106 out, err =
svn(
"status", cwd=cwd)
108 newfiles = [l
for l
in out.splitlines()
if l.startswith(
"? ")]
109 out, err =
svn(
"diff", cwd=cwd)
111 out =
"\n".join(newfiles) +
"\n" + out
117 Return the patch data for a package.
119 rootdir = os.path.dirname(cmtdir)
122 new_files = [l.split(
None, 1)[1]
for l
in out.splitlines()
if l.startswith(
"? ")]
124 new_files = [f
for f
in new_files
if not matches(f, exclusions)]
127 logging.info(
"Added file %r", f)
131 out +=
"Index: %s\n" % f
132 out +=
"===================================================================\n"
133 out +=
command(
"diff",
"-upN",
"/dev/null", f, cwd=rootdir)[0]
135 removed_files = [l.split()[-1]
for l
in err.splitlines()
if "cannot find" in l]
136 removed_files = [f
for f
in removed_files
if not matches(f, exclusions)]
138 for f
in removed_files:
139 logging.info(
"Removed file %r", f)
141 orig =
cvs(
"up",
"-p", f, cwd=rootdir)[0]
142 out +=
"diff -u -p -N %s\n" % os.path.basename(f)
143 out +=
"--- %s\t1 Jan 1970 00:00:00 -0000\n" % f
144 out +=
"+++ /dev/null\t1 Jan 1970 00:00:00 -0000\n"
145 lines = orig.splitlines()
146 out +=
"@@ -1,%d +0,0 @@\n" % len(lines)
150 rex = re.compile(
r"^(Index: |\? |\+\+\+ |--- (?!/dev/null))", re.MULTILINE)
151 out = rex.sub(
r"\1%s/" % name, out)
156 from optparse
import OptionParser
158 parser = OptionParser(
159 description=
"Produce a patch file from a CMT project. "
160 "The patch contains the changes with respect "
161 "to the CVS repository, including new files "
162 "that are present only locally. Run the script "
163 "from the cmt directory of a package."
172 help=
"Pattern to exclude new files from the patch",
179 help=
"Name of the file to send the output to. Standard "
180 "output is used if not specified",
186 help=
"Print some progress information on standard error",
189 "--debug", action=
"store_true", help=
"Print debug information on standard error"
191 parser.set_defaults(exclusions=[])
193 opts, args = parser.parse_args()
196 logging.basicConfig(level=logging.DEBUG)
198 logging.basicConfig(level=logging.INFO)
211 "cmt/install*.history",
220 "i686-slc[34567]-[ig]cc*",
221 "i686-slc[34567]-clang*",
222 "x86_64-slc[34567]-[ig]cc*",
223 "x86_64-slc[34567]-clang*",
226 if "CMTCONFIG" in os.environ:
227 opts.exclusions.append(os.environ[
"CMTCONFIG"])
230 if not (os.path.basename(os.getcwd()) ==
"cmt" and os.path.exists(
"requirements")):
232 "This script must be executed from the cmt directory of a package."
241 for name, path
in pkgs:
244 "Processing %s from %s (%d/%d)",
246 os.path.dirname(path),
250 patch +=
diff_pkg(name, path, opts.exclusions)
252 if sys.platform.startswith(
"win"):
254 patch = patch.replace(
"\r",
"")
257 logging.info(
"Writing patch file %r", opts.output)
258 open(opts.output,
"w").write(patch)
260 sys.stdout.write(patch)
264 if __name__ ==
"__main__":