specify 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. def new_yaml(fpath):
  24. from spectacle import dumper
  25. if not fpath.endswith('.yaml'):
  26. fpath += '.yaml'
  27. autokeys = list(specify.MAND_KEYS) + ['Sources', 'Requires', 'PkgBR', 'PkgConfigBR']
  28. items = []
  29. for key in autokeys:
  30. if key in specify.STR_KEYS:
  31. items.append((key, '^^^'))
  32. elif key in specify.LIST_KEYS:
  33. items.append((key, ['^^^']))
  34. dumper = dumper.SpectacleDumper(format='yaml', opath = fpath)
  35. dumper.dump(items)
  36. # append comments
  37. with open(fpath, 'a') as f:
  38. f.write("""# Please replace all "^^^" with valid values, or remove the unused keys.
  39. # And cleanup these comments lines for the best.
  40. """)
  41. logger.info('New spectacle yaml file created: %s' % fpath)
  42. if logger.ask('Continue to edit the new file?'):
  43. if 'EDITOR' in os.environ:
  44. editor = sys.environ['EDITOR']
  45. else:
  46. editor = 'vi'
  47. os.system('%s %s' % (editor, fpath))
  48. def new_subpkg(fpath, sub):
  49. def _has_subs(path):
  50. import re
  51. with open(path) as f:
  52. lines = f.read()
  53. if re.search('^SubPackages:', lines, re.M):
  54. return True
  55. else:
  56. return False
  57. if _has_subs(fpath):
  58. str_sub = '\n'
  59. else:
  60. str_sub = '\nSubPackages:\n'
  61. str_sub +=""" - Name: %s
  62. Summary: ^^^
  63. Group: ^^^
  64. # Please replace all "^^^" with valid values, and cleanup this comment line.
  65. """ % sub
  66. with open(fpath, 'a') as f:
  67. f.write(str_sub)
  68. def parse_options(args):
  69. import spectacle.__version__
  70. usage = "Usage: %prog [options] [yaml-path]"
  71. parser = optparse.OptionParser(usage, version=spectacle.__version__.VERSION)
  72. parser.add_option("-o", "--output", type="string",
  73. dest="outfile_path", default=None,
  74. help="Path of output spec file")
  75. parser.add_option("-s", "--skip-scm", action="store_true",
  76. dest="skip_scm", default=False,
  77. help="Skip to check upstream SCM when specified in YAML")
  78. parser.add_option("-N", "--not-download", action="store_true",
  79. dest="not_download", default=False,
  80. help="Do not try to download newer source files")
  81. parser.add_option("-n", "--non-interactive", action="store_true",
  82. dest="noninteractive", default=False,
  83. help="Non interactive running, to use default answers")
  84. parser.add_option("", "--new", type="string",
  85. dest="newyaml", default=None,
  86. help="Create a new yaml from template")
  87. parser.add_option("", "--newsub", type="string",
  88. dest="newsub", default=None,
  89. help="Append a new sub-package to current yaml")
  90. return parser.parse_args()
  91. if __name__ == '__main__':
  92. """ Main Function """
  93. (options, args) = parse_options(sys.argv[1:])
  94. if options.noninteractive:
  95. logger.set_mode(False)
  96. if options.newyaml:
  97. if glob.glob('*.yaml'):
  98. if not logger.ask('Yaml file found in current dir, continue to create a new one?', False):
  99. sys.exit(0)
  100. elif glob.glob('*.spec'):
  101. if not logger.ask('Spec file found in current dir, maybe you need spec2spectacle to convert it, continue?', False):
  102. sys.exit(0)
  103. new_yaml(options.newyaml)
  104. sys.exit(0)
  105. if not args:
  106. # no YAML-path specified, search in CWD
  107. yamlls = glob.glob('*.yaml')
  108. if not yamlls:
  109. logger.error('Cannot find valid spectacle file(*.yaml) in current directory, please specify one.')
  110. elif len(yamlls) > 1:
  111. logger.error('Find multiple spectacle files(*.yaml) in current directory, please specify one.')
  112. yaml_fpath = yamlls[0]
  113. else:
  114. yaml_fpath = args[0]
  115. # check if the input file exists
  116. if not os.path.isfile(yaml_fpath):
  117. # input file does not exist
  118. logger.error("%s: File does not exist" % yaml_fpath)
  119. if options.newsub:
  120. new_subpkg(yaml_fpath, options.newsub)
  121. logger.info('Yaml file: %s has been appended with new subpkg: %s' % (yaml_fpath, options.newsub))
  122. sys.exit(0)
  123. if options.outfile_path:
  124. if os.path.sep in options.outfile_path:
  125. out_fpath = os.path.abspath(options.outfile_path)
  126. else:
  127. out_fpath = options.outfile_path
  128. else:
  129. # %{name}.spec as the default if not specified
  130. out_fpath = None
  131. # check the working path
  132. if yaml_fpath.find(os.path.sep) != -1 and os.path.dirname(yaml_fpath) != os.path.curdir:
  133. wdir = os.path.dirname(yaml_fpath)
  134. logger.info('Changing to working dir: %s' % wdir)
  135. os.chdir(wdir)
  136. yaml_fname = os.path.basename(yaml_fpath)
  137. spec_fpath, newspec = specify.generate_rpm(yaml_fname, spec_fpath=out_fpath, download_new=not options.not_download, skip_scm=options.skip_scm)
  138. if newspec:
  139. logger.warning("NEW spec file created: %s, maybe customized spec content is needed!" % spec_fpath)
  140. else:
  141. logger.info("Old spec file exists, patching %s ..." % spec_fpath)