specify 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #!/usr/bin/python -tt
  2. # vim: ai ts=4 sts=4 et sw=4
  3. # Copyright (c) 2009 Intel Corporation
  4. #
  5. # This program is free software; you can redistribute it and/or modify it
  6. # under the terms of the GNU General Public License as published by the Free
  7. # Software Foundation; version 2 of the License
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. # for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along
  15. # with this program; if not, write to the Free Software Foundation, Inc., 59
  16. # Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. from __future__ import with_statement
  18. import os,sys
  19. import optparse
  20. import glob
  21. from spectacle import specify
  22. from spectacle import logger
  23. MF_TEMPLATE = """PKG_NAME := %s
  24. SPECFILE = $(addsuffix .spec, $(PKG_NAME))
  25. YAMLFILE = $(addsuffix .yaml, $(PKG_NAME))
  26. include /usr/share/packaging-tools/Makefile.common
  27. """
  28. def new_yaml(fpath):
  29. from spectacle import dumper
  30. if not fpath.endswith('.yaml'):
  31. fpath += '.yaml'
  32. autokeys = list(specify.MAND_KEYS) + ['Sources', 'Requires', 'PkgBR', 'PkgConfigBR']
  33. items = []
  34. for key in autokeys:
  35. if key in specify.STR_KEYS:
  36. items.append((key, '^^^'))
  37. elif key in specify.LIST_KEYS:
  38. items.append((key, ['^^^']))
  39. dumper = dumper.SpectacleDumper(format='yaml', opath = fpath)
  40. dumper.dump(items)
  41. # append comments
  42. with open(fpath, 'a') as f:
  43. f.write("""# Please replace all "^^^" with valid values, or remove the unused keys.
  44. # And cleanup these comments lines for the best.
  45. """)
  46. logger.info('New spectacle yaml file created: %s' % fpath)
  47. if logger.ask('Continue to edit the new file?'):
  48. if 'EDITOR' in os.environ:
  49. editor = sys.environ['EDITOR']
  50. else:
  51. editor = 'vi'
  52. os.system('%s %s' % (editor, fpath))
  53. def new_subpkg(fpath, sub):
  54. def _has_subs(path):
  55. import re
  56. with open(path) as f:
  57. lines = f.read()
  58. if re.search('^SubPackages:', lines, re.M):
  59. return True
  60. else:
  61. return False
  62. if _has_subs(fpath):
  63. str_sub = '\n'
  64. else:
  65. str_sub = '\nSubPackages:\n'
  66. str_sub +=""" - Name: %s
  67. Summary: ^^^
  68. Group: ^^^
  69. # Please replace all "^^^" with valid values, and cleanup this comment line.
  70. """ % sub
  71. with open(fpath, 'a') as f:
  72. f.write(str_sub)
  73. def parse_options(args):
  74. import spectacle.__version__
  75. usage = "Usage: %prog [options] [yaml-path]"
  76. parser = optparse.OptionParser(usage, version=spectacle.__version__.VERSION)
  77. parser.add_option("-o", "--output", type="string",
  78. dest="outfile_path", default=None,
  79. help="Path of output spec file")
  80. parser.add_option("-s", "--skip-scm", action="store_true",
  81. dest="skip_scm", default=False,
  82. help="Skip to check upstream SCM when specified in YAML")
  83. parser.add_option("-N", "--not-download", action="store_true",
  84. dest="not_download", default=False,
  85. help="Do not try to download newer source files")
  86. parser.add_option("-n", "--non-interactive", action="store_true",
  87. dest="noninteractive", default=False,
  88. help="Non interactive running, to use default answers")
  89. parser.add_option("", "--new", type="string",
  90. dest="newyaml", default=None,
  91. help="Create a new yaml from template")
  92. parser.add_option("", "--newsub", type="string",
  93. dest="newsub", default=None,
  94. help="Append a new sub-package to current yaml")
  95. return parser.parse_args()
  96. if __name__ == '__main__':
  97. """ Main Function """
  98. (options, args) = parse_options(sys.argv[1:])
  99. if options.noninteractive:
  100. logger.set_mode(False)
  101. if options.newyaml:
  102. if glob.glob('*.yaml'):
  103. if not logger.ask('Yaml file found in current dir, continue to create a new one?', False):
  104. sys.exit(0)
  105. elif glob.glob('*.spec'):
  106. if not logger.ask('Spec file found in current dir, maybe you need spec2spectacle to convert it, continue?', False):
  107. sys.exit(0)
  108. new_yaml(options.newyaml)
  109. sys.exit(0)
  110. if not args:
  111. # no YAML-path specified, search in CWD
  112. yamlls = glob.glob('*.yaml')
  113. if not yamlls:
  114. logger.error('Cannot find valid spectacle file(*.yaml) in current directory, please specify one.')
  115. elif len(yamlls) > 1:
  116. logger.error('Find multiple spectacle files(*.yaml) in current directory, please specify one.')
  117. yaml_fpath = yamlls[0]
  118. else:
  119. yaml_fpath = args[0]
  120. # check if the input file exists
  121. if not os.path.isfile(yaml_fpath):
  122. # input file does not exist
  123. logger.error("%s: File does not exist" % yaml_fpath)
  124. if options.newsub:
  125. new_subpkg(yaml_fpath, options.newsub)
  126. logger.info('Yaml file: %s has been appended with new subpkg: %s' % (yaml_fpath, options.newsub))
  127. sys.exit(0)
  128. if options.outfile_path:
  129. if os.path.sep in options.outfile_path:
  130. out_fpath = os.path.abspath(options.outfile_path)
  131. else:
  132. out_fpath = options.outfile_path
  133. else:
  134. # %{name}.spec as the default if not specified
  135. out_fpath = None
  136. # check the working path
  137. if yaml_fpath.find(os.path.sep) != -1 and os.path.dirname(yaml_fpath) != os.path.curdir:
  138. wdir = os.path.dirname(yaml_fpath)
  139. logger.info('Changing to working dir: %s' % wdir)
  140. os.chdir(wdir)
  141. yaml_fname = os.path.basename(yaml_fpath)
  142. # check Makefile from packaging-tools
  143. if not os.path.exists('Makefile'):
  144. logger.warning('There is no "Makefile" for this package, please update it using packaging-tools')
  145. if logger.ask('Need to create Makefile from default template?', False):
  146. mf = open('Makefile', 'w')
  147. mf.write(MF_TEMPLATE % os.path.basename(yaml_fname).rstrip('.yaml'))
  148. mf.close()
  149. spec_fpath, newspec = specify.generate_rpm(yaml_fname, spec_fpath=out_fpath, download_new=not options.not_download, skip_scm=options.skip_scm)
  150. if newspec:
  151. logger.warning("NEW spec file created: %s, maybe customized spec content is needed!" % spec_fpath)
  152. else:
  153. logger.info("Old spec file exists, patching %s ..." % spec_fpath)