combo-layer 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/usr/bin/env python
  2. # ex:ts=4:sw=4:sts=4:et
  3. # -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
  4. #
  5. # Copyright 2011 Intel Corporation
  6. # Authored-by: Yu Ke <ke.yu@intel.com>
  7. # Paul Eggleton <paul.eggleton@intel.com>
  8. # Richard Purdie <richard.purdie@intel.com>
  9. #
  10. # This program is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License version 2 as
  12. # published by the Free Software Foundation.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. import os, sys
  23. import optparse
  24. import logging
  25. import subprocess
  26. import ConfigParser
  27. __version__ = "0.1.0"
  28. def logger_create():
  29. logger = logging.getLogger("")
  30. loggerhandler = logging.StreamHandler()
  31. loggerhandler.setFormatter(logging.Formatter("[%(asctime)s] %(message)s","%H:%M:%S"))
  32. logger.addHandler(loggerhandler)
  33. logger.setLevel(logging.INFO)
  34. return logger
  35. logger = logger_create()
  36. class Configuration(object):
  37. """
  38. Manages the configuration
  39. A valid conf looks like:
  40. # component name
  41. [bitbake]
  42. # mandatory options
  43. # git upstream uri
  44. src_uri = git://git.openembedded.org/bitbake
  45. # the directory to clone the component repo
  46. local_repo_dir = ~/src/bitbake
  47. # the relative dir to commit the repo patch
  48. # use "." if it is root dir
  49. dest_dir = bitbake
  50. # the updated revision last time.
  51. # leave it empty if no commit updated yet, and then the tool
  52. # will start from the first commit
  53. last_revision =
  54. # optional options
  55. # file_filter: only include the interested file
  56. # file_filter = [path] [path] ...
  57. # example:
  58. # file_filter = src/ : only include the subdir src
  59. # file_filter = src/*.c : only include the src *.c file
  60. # file_filter = src/main.c src/Makefile.am : only include these two files
  61. [oe-core]
  62. src_uri = git://git.openembedded.org/openembedded-core
  63. local_repo_dir = ~/src/oecore
  64. dest_dir = .
  65. last_revision =
  66. # it's also possible to embed python code in the config values. Similar
  67. # to bitbake it considers every value starting with @ to be a python script.
  68. # So local_repo could be easily configured using an environment variable as:
  69. #
  70. # [bitbake]
  71. # local_repo = @os.getenv("LOCAL_REPO_DIR") + "/bitbake"
  72. #
  73. # more components ...
  74. """
  75. def __init__(self, options):
  76. for key, val in options.__dict__.items():
  77. setattr(self, key, val)
  78. self.parser = ConfigParser.ConfigParser()
  79. self.parser.readfp(open(self.conffile))
  80. self.repos = {}
  81. for repo in self.parser.sections():
  82. self.repos[repo] = {}
  83. for (name, value) in self.parser.items(repo):
  84. if value.startswith("@"):
  85. self.repos[repo][name] = eval(value.strip("@"))
  86. else:
  87. self.repos[repo][name] = value
  88. def update(self, repo, option, value):
  89. self.parser.set(repo, option, value)
  90. self.parser.write(open(self.conffile, "w"))
  91. def sanity_check(self):
  92. required_options=["src_uri", "local_repo_dir", "dest_dir", "last_revision"]
  93. msg = ""
  94. for name in self.repos:
  95. for option in required_options:
  96. if option not in self.repos[name]:
  97. msg = "%s\nOption %s is not defined for component %s" %(msg, option, name)
  98. if msg != "":
  99. logger.error("configuration file %s has the following error:%s" % (self.conffile,msg))
  100. sys.exit(1)
  101. # filterdiff is required by action_splitpatch, so check its availability
  102. if subprocess.call("which filterdiff &>/dev/null", shell=True) != 0:
  103. logger.error("ERROR: patchutils package is missing, please install it (e.g. # apt-get install patchutils)")
  104. sys.exit(1)
  105. def runcmd(cmd,destdir=None):
  106. """
  107. execute command, raise CalledProcessError if fail
  108. return output if succeed
  109. """
  110. logger.debug("run cmd '%s' in %s" % (cmd, os.getcwd() if destdir is None else destdir))
  111. out = os.tmpfile()
  112. try:
  113. subprocess.check_call(cmd, stdout=out, stderr=out, cwd=destdir, shell=True)
  114. except subprocess.CalledProcessError,e:
  115. out.seek(0)
  116. logger.error("%s" % out.read())
  117. raise e
  118. out.seek(0)
  119. output = out.read()
  120. logger.debug("output: %s" % output )
  121. return output
  122. def action_init(conf, args):
  123. """
  124. Clone component repositories
  125. Check git initialised and working tree is clean
  126. """
  127. for name in conf.repos:
  128. ldir = conf.repos[name]['local_repo_dir']
  129. if not os.path.exists(ldir):
  130. logger.info("cloning %s to %s" %(conf.repos[name]['src_uri'], ldir))
  131. subprocess.check_call("git clone %s %s" % (conf.repos[name]['src_uri'], ldir), shell=True)
  132. if not os.path.exists(".git"):
  133. runcmd("git init")
  134. def check_repo_clean(repodir):
  135. """
  136. check if the repo is clean
  137. exit if repo is dirty
  138. """
  139. try:
  140. runcmd("git diff --quiet", repodir)
  141. #TODO: also check the index using "git diff --cached"
  142. # but this will fail in just initialized git repo
  143. # so need figure out a way
  144. except:
  145. logger.error("git repo %s is dirty, please fix it first", repodir)
  146. sys.exit(1)
  147. def action_update(conf, args):
  148. """
  149. update the component repo
  150. generate the patch list
  151. apply the generated patches
  152. """
  153. # make sure all repos are clean
  154. for name in conf.repos:
  155. check_repo_clean(conf.repos[name]['local_repo_dir'])
  156. check_repo_clean(os.getcwd())
  157. import uuid
  158. patch_dir = "patch-%s" % uuid.uuid4()
  159. os.mkdir(patch_dir)
  160. for name in conf.repos:
  161. repo = conf.repos[name]
  162. ldir = repo['local_repo_dir']
  163. dest_dir = repo['dest_dir']
  164. repo_patch_dir = os.path.join(os.getcwd(), patch_dir, name)
  165. # Step 1: update the component repo
  166. logger.info("git pull for component repo %s in %s ..." % (name, ldir))
  167. output=runcmd("git pull", ldir)
  168. logger.info(output)
  169. # Step 2: generate the patch list and store to patch dir
  170. logger.info("generating patches for %s" % name)
  171. if dest_dir != ".":
  172. prefix = "--src-prefix=a/%s/ --dst-prefix=b/%s/" % (dest_dir, dest_dir)
  173. else:
  174. prefix = ""
  175. if repo['last_revision'] == "":
  176. logger.info("Warning: last_revision of component %s is not set, so start from the first commit" % name)
  177. patch_cmd_range = "--root master"
  178. rev_cmd_range = "master"
  179. else:
  180. patch_cmd_range = "%s..master" % repo['last_revision']
  181. rev_cmd_range = "%s..master" % repo['last_revision']
  182. file_filter = repo.get('file_filter',"")
  183. patch_cmd = "git format-patch -N %s --output-directory %s %s -- %s" % \
  184. (prefix,repo_patch_dir, patch_cmd_range, file_filter)
  185. output = runcmd(patch_cmd, ldir)
  186. logger.debug("generated patch set:\n%s" % output)
  187. patchlist = output.splitlines()
  188. rev_cmd = 'git log --pretty=format:"%H" ' + rev_cmd_range
  189. revlist = runcmd(rev_cmd, ldir).splitlines()
  190. # Step 3: Call repo specific hook to adjust patch
  191. if 'hook' in repo:
  192. # hook parameter is: ./hook patchpath revision reponame
  193. count=len(revlist)-1
  194. for patch in patchlist:
  195. runcmd("%s %s %s %s" % (repo['hook'], patch, revlist[count], name))
  196. count=count-1
  197. # Step 4: write patch list and revision list to file, for user to edit later
  198. patchlist_file = os.path.join(os.getcwd(), patch_dir, "patchlist-%s" % name)
  199. repo['patchlist'] = patchlist_file
  200. f = open(patchlist_file, 'w')
  201. count=len(revlist)-1
  202. for patch in patchlist:
  203. f.write("%s %s\n" % (patch, revlist[count]))
  204. count=count-1
  205. f.close()
  206. # Step 5: invoke bash for user to edit patch and patch list
  207. if conf.interactive:
  208. print 'Edit the patch and patch list in %s\n' \
  209. 'For example, remove the unwanted patch entry from patchlist-*, so that it will be not applied later\n' \
  210. 'After finish, press following command to continue\n' \
  211. ' exit 0 -- exit and continue to apply the patch\n' \
  212. ' exit 1 -- abort and not apply patch\n' % patch_dir
  213. ret = subprocess.call(["bash"], cwd=patch_dir)
  214. if ret != 0:
  215. print "Abort without applying patch"
  216. sys.exit(0)
  217. # Step 6: apply the generated and revised patch
  218. action_apply_patch(conf, args)
  219. runcmd("rm -rf %s" % patch_dir)
  220. def action_apply_patch(conf, args):
  221. """
  222. apply the generated patch list to combo repo
  223. """
  224. for name in conf.repos:
  225. repo = conf.repos[name]
  226. lastrev = repo["last_revision"]
  227. for line in open(repo['patchlist']):
  228. patchfile = line.split()[0]
  229. lastrev = line.split()[1]
  230. cmd = "git am --keep-cr -s -p1 %s" % patchfile
  231. logger.info("Apply %s" % patchfile )
  232. try:
  233. runcmd(cmd)
  234. except subprocess.CalledProcessError:
  235. logger.info('"git am --abort" is executed to cleanup repo')
  236. runcmd("git am --abort")
  237. logger.error('"%s" failed' % cmd)
  238. logger.info("please manually apply patch %s" % patchfile)
  239. logger.info("After applying, run this tool again to apply the rest patches")
  240. conf.update(name, "last_revision", lastrev)
  241. sys.exit(0)
  242. conf.update(name, "last_revision", lastrev)
  243. def action_splitpatch(conf, args):
  244. """
  245. generate the commit patch and
  246. split the patch per repo
  247. """
  248. logger.debug("action_splitpatch")
  249. if len(args) > 1:
  250. commit = args[1]
  251. else:
  252. commit = "HEAD"
  253. patchdir = "splitpatch-%s" % commit
  254. if not os.path.exists(patchdir):
  255. os.mkdir(patchdir)
  256. # filerange_root is for the repo whose dest_dir is root "."
  257. # and it should be specified by excluding all other repo dest dir
  258. # like "-x repo1 -x repo2 -x repo3 ..."
  259. filerange_root = ""
  260. for name in conf.repos:
  261. dest_dir = conf.repos[name]['dest_dir']
  262. if dest_dir != ".":
  263. filerange_root = '%s -x "%s/*"' % (filerange_root, dest_dir)
  264. for name in conf.repos:
  265. dest_dir = conf.repos[name]['dest_dir']
  266. patch_filename = "%s/%s.patch" % (patchdir, name)
  267. if dest_dir == ".":
  268. cmd = "git format-patch -n1 --stdout %s^..%s | filterdiff -p1 %s > %s" % (commit, commit, filerange_root, patch_filename)
  269. else:
  270. cmd = "git format-patch --no-prefix -n1 --stdout %s^..%s -- %s > %s" % (commit, commit, dest_dir, patch_filename)
  271. runcmd(cmd)
  272. # Detect empty patches (including those produced by filterdiff above
  273. # that contain only preamble text)
  274. if os.path.getsize(patch_filename) == 0 or runcmd("filterdiff %s" % patch_filename) == "":
  275. os.remove(patch_filename)
  276. logger.info("(skipping %s - no changes)", name)
  277. else:
  278. logger.info(patch_filename)
  279. def action_error(conf, args):
  280. logger.info("invalid action %s" % args[0])
  281. actions = {
  282. "init": action_init,
  283. "update": action_update,
  284. "splitpatch": action_splitpatch,
  285. }
  286. def main():
  287. parser = optparse.OptionParser(
  288. version = "Combo Layer Repo Tool version %s" % __version__,
  289. usage = """%prog [options] action
  290. Create and update a combination layer repository from multiple component repositories.
  291. Action:
  292. init initialise the combo layer repo
  293. update get patches from component repos and apply them to the combo repo
  294. splitpatch [commit] generate commit patch and split per component, default commit is HEAD""")
  295. parser.add_option("-c", "--conf", help = "specify the config file (conf/combo-layer.conf is the default).",
  296. action = "store", dest = "conffile", default = "conf/combo-layer.conf")
  297. parser.add_option("-i", "--interactive", help = "interactive mode, user can edit the patch list and patches",
  298. action = "store_true", dest = "interactive", default = False)
  299. parser.add_option("-D", "--debug", help = "output debug information",
  300. action = "store_true", dest = "debug", default = False)
  301. options, args = parser.parse_args(sys.argv)
  302. # Dispatch to action handler
  303. if len(args) == 1:
  304. logger.error("No action specified, exiting")
  305. parser.print_help()
  306. elif args[1] not in actions:
  307. logger.error("Unsupported action %s, exiting\n" % (args[1]))
  308. parser.print_help()
  309. elif not os.path.exists(options.conffile):
  310. logger.error("No valid config file, exiting\n")
  311. parser.print_help()
  312. else:
  313. if options.debug:
  314. logger.setLevel(logging.DEBUG)
  315. confdata = Configuration(options)
  316. confdata.sanity_check()
  317. actions.get(args[1], action_error)(confdata, args[1:])
  318. if __name__ == "__main__":
  319. try:
  320. ret = main()
  321. except Exception:
  322. ret = 1
  323. import traceback
  324. traceback.print_exc(5)
  325. sys.exit(ret)