setup.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. #!/usr/bin/env python
  2. """
  3. script to install pwman3
  4. """
  5. import datetime
  6. from distutils.core import Command
  7. from distutils.errors import DistutilsOptionError
  8. from distutils.command.build import build
  9. import argparse
  10. from setuptools import setup
  11. from setuptools import find_packages
  12. import sys
  13. from setuptools.command.install import install
  14. import os
  15. from subprocess import Popen, PIPE
  16. import pwman
  17. # The BuildManPage code is distributed
  18. # under the same License of Python
  19. # Copyright (c) 2014 Oz Nahum Tiram <nahumoz@gmail.com>
  20. """
  21. Add a `build_manpage` command to your setup.py.
  22. To use this Command class import the class to your setup.py,
  23. and add a command to call this class::
  24. from build_manpage import BuildManPage
  25. ...
  26. ...
  27. setup(
  28. ...
  29. ...
  30. cmdclass={
  31. 'build_manpage': BuildManPage,
  32. )
  33. You can then use the following setup command to produce a man page::
  34. $ python setup.py build_manpage --output=prog.1
  35. --parser=yourmodule:argparser
  36. Alternatively, set the variable AUTO_BUILD to True, and just invoke::
  37. $ python setup.py build
  38. If automatically want to build the man page every time you invoke your build,
  39. add to your ```setup.cfg``` the following::
  40. [build_manpage]
  41. output = <appname>.1
  42. parser = <path_to_your_parser>
  43. """
  44. build.sub_commands.append(('build_manpage', None))
  45. class BuildManPage(Command):
  46. description = 'Generate man page from an ArgumentParser instance.'
  47. user_options = [
  48. ('output=', 'O', 'output file'),
  49. ('parser=', None, 'module path to an ArgumentParser instance'
  50. '(e.g. mymod:func, where func is a method or function which return'
  51. 'an arparse.ArgumentParser instance.'),
  52. ]
  53. def initialize_options(self):
  54. self.output = None
  55. self.parser = None
  56. def finalize_options(self):
  57. if self.output is None:
  58. raise DistutilsOptionError('\'output\' option is required')
  59. if self.parser is None:
  60. raise DistutilsOptionError('\'parser\' option is required')
  61. mod_name, func_name = self.parser.split(':')
  62. fromlist = mod_name.split('.')
  63. try:
  64. mod = __import__(mod_name, fromlist=fromlist)
  65. self._parser = getattr(mod, func_name)(
  66. formatter_class=ManPageFormatter)
  67. except ImportError as err:
  68. raise err
  69. self.announce('Writing man page %s' % self.output)
  70. self._today = datetime.date.today()
  71. def run(self):
  72. dist = self.distribution
  73. homepage = dist.get_url()
  74. appname = self._parser.prog
  75. sections = {'authors': ("pwman3 was originally written by Ivan Kelly "
  76. "<ivan@ivankelly.net>.\n pwman3 is now "
  77. "maintained "
  78. "by Oz Nahum <nahumoz@gmail.com>."),
  79. 'distribution': ("The latest version of {} may be "
  80. "downloaded from {}".format(appname,
  81. homepage))
  82. }
  83. dist = self.distribution
  84. mpf = ManPageFormatter(appname,
  85. desc=dist.get_description(),
  86. long_desc=dist.get_long_description(),
  87. ext_sections=sections)
  88. m = mpf.format_man_page(self._parser)
  89. with open(self.output, 'w') as f:
  90. f.write(m)
  91. class ManPageFormatter(argparse.HelpFormatter):
  92. """
  93. Formatter class to create man pages.
  94. This class relies only on the parser, and not distutils.
  95. The following shows a scenario for usage::
  96. from pwman import parser_options
  97. from build_manpage import ManPageFormatter
  98. # example usage ...
  99. dist = distribution
  100. mpf = ManPageFormatter(appname,
  101. desc=dist.get_description(),
  102. long_desc=dist.get_long_description(),
  103. ext_sections=sections)
  104. # parser is an ArgumentParser instance
  105. m = mpf.format_man_page(parsr)
  106. with open(self.output, 'w') as f:
  107. f.write(m)
  108. The last line would print all the options and help infomation wrapped with
  109. man page macros where needed.
  110. """
  111. def __init__(self,
  112. prog,
  113. indent_increment=2,
  114. max_help_position=24,
  115. width=None,
  116. section=1,
  117. desc=None,
  118. long_desc=None,
  119. ext_sections=None,
  120. authors=None,
  121. ):
  122. super(ManPageFormatter, self).__init__(prog)
  123. self._prog = prog
  124. self._section = 1
  125. self._today = datetime.date.today().strftime('%Y\\-%m\\-%d')
  126. self._desc = desc
  127. self._long_desc = long_desc
  128. self._ext_sections = ext_sections
  129. def _get_formatter(self, **kwargs):
  130. return self.formatter_class(prog=self.prog, **kwargs)
  131. def _markup(self, txt):
  132. return txt.replace('-', '\\-')
  133. def _underline(self, string):
  134. return "\\fI\\s-1" + string + "\\s0\\fR"
  135. def _bold(self, string):
  136. if not string.strip().startswith('\\fB'):
  137. string = '\\fB' + string
  138. if not string.strip().endswith('\\fR'):
  139. string = string + '\\fR'
  140. return string
  141. def _mk_synopsis(self, parser):
  142. self.add_usage(parser.usage, parser._actions,
  143. parser._mutually_exclusive_groups, prefix='')
  144. usage = self._format_usage(None, parser._actions,
  145. parser._mutually_exclusive_groups, '')
  146. usage = usage.replace('%s ' % self._prog, '')
  147. usage = '.SH SYNOPSIS\n \\fB%s\\fR %s\n' % (self._markup(self._prog),
  148. usage)
  149. return usage
  150. def _mk_title(self, prog):
  151. return '.TH {0} {1} {2}\n'.format(prog, self._section,
  152. self._today)
  153. def _make_name(self, parser):
  154. """
  155. this method is in consitent with others ... it relies on
  156. distribution
  157. """
  158. return '.SH NAME\n%s \\- %s\n' % (parser.prog,
  159. parser.description)
  160. def _mk_description(self):
  161. if self._long_desc:
  162. long_desc = self._long_desc.replace('\n', '\n.br\n')
  163. return '.SH DESCRIPTION\n%s\n' % self._markup(long_desc)
  164. else:
  165. return ''
  166. def _mk_footer(self, sections):
  167. if not hasattr(sections, '__iter__'):
  168. return ''
  169. footer = []
  170. for section, value in sections.items():
  171. part = ".SH {}\n {}".format(section.upper(), value)
  172. footer.append(part)
  173. return '\n'.join(footer)
  174. def format_man_page(self, parser):
  175. page = []
  176. page.append(self._mk_title(self._prog))
  177. page.append(self._mk_synopsis(parser))
  178. page.append(self._mk_description())
  179. page.append(self._mk_options(parser))
  180. page.append(self._mk_footer(self._ext_sections))
  181. return ''.join(page)
  182. def _mk_options(self, parser):
  183. formatter = parser._get_formatter()
  184. # positionals, optionals and user-defined groups
  185. for action_group in parser._action_groups:
  186. formatter.start_section(None)
  187. formatter.add_text(None)
  188. formatter.add_arguments(action_group._group_actions)
  189. formatter.end_section()
  190. # epilog
  191. formatter.add_text(parser.epilog)
  192. # determine help from format above
  193. return '.SH OPTIONS\n' + formatter.format_help()
  194. def _format_action_invocation(self, action):
  195. if not action.option_strings:
  196. metavar, = self._metavar_formatter(action, action.dest)(1)
  197. return metavar
  198. else:
  199. parts = []
  200. # if the Optional doesn't take a value, format is:
  201. # -s, --long
  202. if action.nargs == 0:
  203. parts.extend([self._bold(action_str) for action_str in
  204. action.option_strings])
  205. # if the Optional takes a value, format is:
  206. # -s ARGS, --long ARGS
  207. else:
  208. default = self._underline(action.dest.upper())
  209. args_string = self._format_args(action, default)
  210. for option_string in action.option_strings:
  211. parts.append('%s %s' % (self._bold(option_string),
  212. args_string))
  213. return ', '.join(parts)
  214. class ManPageCreator(object):
  215. """
  216. This class takes a little different approach. Instead of relying on
  217. information from ArgumentParser, it relies on information retrieved
  218. from distutils.
  219. This class makes it easy for package maintainer to create man pages in
  220. cases, that there is no ArgumentParser.
  221. """
  222. pass
  223. def _mk_name(self, distribution):
  224. """
  225. """
  226. return '.SH NAME\n%s \\- %s\n' % (distribution.get_name(),
  227. distribution.get_description())
  228. sys.path.insert(0, os.getcwd())
  229. def describe():
  230. des = Popen('git describe', shell=True, stdout=PIPE)
  231. ver = des.stdout.readlines()
  232. if ver:
  233. return ver[0].decode().strip()
  234. else:
  235. return pwman.version
  236. class PyCryptoInstallCommand(install):
  237. """
  238. A Custom command to download and install pycrypto26
  239. binary from voidspace. Not optimal, but it should work ...
  240. """
  241. description = ("A Custom command to download and install pycrypto26"
  242. "binary from voidspace.")
  243. def run(self):
  244. base_path = "http://www.voidspace.org.uk/downloads/pycrypto26"
  245. if 'win32' in sys.platform:
  246. if 'AMD64' not in sys.version:
  247. pycrypto = 'pycrypto-2.6.win32-py2.7.exe'
  248. else: # 'for AMD64'
  249. pycrypto = 'pycrypto-2.6.win-amd64-py2.7.exe'
  250. os.system('easy_install '+base_path+'/'+pycrypto)
  251. install.run(self)
  252. else:
  253. print(('Please use pip or your Distro\'s package manager '
  254. 'to install pycrypto ...'))
  255. setup(name=pwman.appname,
  256. version=describe(),
  257. description=pwman.description,
  258. long_description=pwman.long_description,
  259. author=pwman.author,
  260. author_email=pwman.authoremail,
  261. url=pwman.website,
  262. license="GNU GPL",
  263. packages=find_packages(exclude=['tests']),
  264. zip_safe=False,
  265. install_requires=['pycrypto>=2.6',
  266. 'colorama>=0.2.4'],
  267. keywords="password-manager crypto cli",
  268. classifiers=['Environment :: Console',
  269. 'Intended Audience :: End Users/Desktop',
  270. 'Intended Audience :: Developers',
  271. 'Intended Audience :: System Administrators',
  272. ('License :: OSI Approved :: GNU General Public License'
  273. ' v3 or later (GPLv3+)'),
  274. 'Operating System :: OS Independent',
  275. 'Programming Language :: Python',
  276. 'Programming Language :: Python :: 2.7',
  277. 'Programming Language :: Python :: 3',
  278. 'Programming Language :: Python :: 3.2',
  279. 'Programming Language :: Python :: 3.3',
  280. 'Programming Language :: Python :: 3.4',
  281. ],
  282. test_suite='tests.test_pwman.suite',
  283. cmdclass={
  284. 'install_pycrypto': PyCryptoInstallCommand,
  285. 'build_manpage': BuildManPage
  286. },
  287. entry_points={
  288. 'console_scripts': ['pwman3 = pwman.ui.cli:main']
  289. }
  290. )