summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rwxr-xr-xsrc/fedpkg.py49
-rw-r--r--src/pyfedpkg/__init__.py25
-rw-r--r--src/pyfedpkg/man_page.py162
3 files changed, 214 insertions, 22 deletions
diff --git a/src/fedpkg.py b/src/fedpkg.py
index 75770f5..fd16f49 100755
--- a/src/fedpkg.py
+++ b/src/fedpkg.py
@@ -569,10 +569,7 @@ def local(args):
arch = args.arch
try:
mymodule = pyfedpkg.PackageModule(args.path, args.dist)
- if args.md5:
- return mymodule.local(arch=arch, hashtype='md5')
- else:
- return mymodule.local(arch=arch)
+ return mymodule.local(arch=arch, hashtype=args.hashtype)
except pyfedpkg.FedpkgError, e:
log.error('Could not build locally: %s' % e)
sys.exit(1)
@@ -674,10 +671,8 @@ def srpm(args):
try:
mymodule = pyfedpkg.PackageModule(args.path, args.dist)
pyfedpkg.sources(args.path)
- if args.md5:
- mymodule.srpm('md5')
- else:
- mymodule.srpm()
+ mymodule.srpm(hashtype=args.hashtype,
+ fix_permissions=args.fix_permissions)
except pyfedpkg.FedpkgError, e:
log.error('Could not make an srpm: %s' % e)
sys.exit(1)
@@ -848,8 +843,10 @@ def verrel(args):
sys.exit(1)
print('%s-%s-%s' % (mymodule.module, mymodule.ver, mymodule.rel))
-# THe main code goes here
-if __name__ == '__main__':
+
+def parse_cmdline(generate_manpage = False):
+ """Parse the command line"""
+
# Create the parser object
parser = argparse.ArgumentParser(description = 'Fedora Packaging utility',
prog = 'fedpkg',
@@ -861,7 +858,8 @@ if __name__ == '__main__':
parser.add_argument('--dist', default=None,
help='Override the distribution, eg f15 or el6')
# Let somebody override the username found in fedora cert
- parser.add_argument('-u', '--user')
+ parser.add_argument('-u', '--user',
+ help = "Override the username found in the fedora cert")
# Let the user define which path to look at instead of pwd
parser.add_argument('--path', default = os.getcwd(),
help='Directory to interact with instead of current dir')
@@ -1079,7 +1077,8 @@ packages will be built sequentially.
help = 'Local test rpmbuild binary')
parser_local.add_argument('--arch', help = 'Build for arch')
# optionally define old style hashsums
- parser_local.add_argument('--md5', action = 'store_true',
+ parser_local.add_argument('--md5', action = 'store_const',
+ dest='hashtype', const='md5', default=None,
help = 'Use md5 checksums (for older rpm hosts)')
parser_local.set_defaults(command = local)
@@ -1157,8 +1156,12 @@ packages will be built sequentially.
parser_srpm = subparsers.add_parser('srpm',
help = 'Create a source rpm')
# optionally define old style hashsums
- parser_srpm.add_argument('--md5', action = 'store_true',
+ parser_srpm.add_argument('--md5', action = 'store_const',
+ dest='hashtype', const='md5', default=None,
help = 'Use md5 checksums (for older rpm hosts)')
+ parser_srpm.add_argument('--fix-permissions', action='store_true',
+ default=False,
+ help = 'Fix permissions of files to be put into .src.rpm file')
parser_srpm.set_defaults(command = srpm)
# switch branches
@@ -1238,8 +1241,24 @@ packages will be built sequentially.
' name-version-release')
parser_verrel.set_defaults(command = verrel)
- # Parse the args
- args = parser.parse_args()
+ if not generate_manpage:
+ # Parse the args
+ return parser.parse_args()
+ else:
+ # Generate the man page
+
+ # Use the "as man_page" part to avoid overwriting the pyfedpkg
+ # namespace, which would break all usage of pyfedpkg.* outside
+ # of this else branch.
+ import pyfedpkg.man_page as man_page
+ man_page.generate(parser, subparsers)
+ sys.exit(0)
+ # no return possible
+
+
+# The main code goes here
+if __name__ == '__main__':
+ args = parse_cmdline()
# setup the logger -- This logger will take things of INFO or DEBUG and
# log it to stdout. Anything above that (WARN, ERROR, CRITICAL) will go
diff --git a/src/pyfedpkg/__init__.py b/src/pyfedpkg/__init__.py
index 401f738..6b9682e 100644
--- a/src/pyfedpkg/__init__.py
+++ b/src/pyfedpkg/__init__.py
@@ -136,7 +136,7 @@ def _run_command(cmd, shell=False, env=None, pipe=[], cwd=None):
"""
- # Process any environment vairables.
+ # Process any environment variables.
environ = os.environ
if env:
for item in env.keys():
@@ -1075,12 +1075,12 @@ class PackageModule:
return subprocess.Popen(['rpm --eval %{_arch}'], shell=True,
stdout=subprocess.PIPE).communicate()[0].strip('\n')
- def __init__(self, path=None, dist=None):
+ def __init__(self, path, dist=None):
# Initiate a PackageModule object in a given path
# Set some global variables used throughout
- if not path:
- path = os.getcwd()
log.debug('Creating module object from %s' % path)
+ if not os.path.isdir(path):
+ raise FedpkgError('Module directory not found: %s' % path)
self.path = path
self.lookaside = LOOKASIDE
self.lookasidehash = LOOKASIDEHASH
@@ -1528,7 +1528,7 @@ class PackageModule:
_run_command(cmd, shell=True)
return
- def local(self, arch=None, hashtype='sha256'):
+ def local(self, arch=None, hashtype=None):
"""rpmbuild locally for given arch.
Takes arch to build for, and hashtype to build with.
@@ -1539,6 +1539,10 @@ class PackageModule:
"""
+ # Figure out which hashtype to use, if not provided one
+ if not hashtype:
+ hashtype = self.hashtype
+
# This could really use a list of arches to build for and loop over
# Get the sources
sources(self.path)
@@ -1725,7 +1729,7 @@ class PackageModule:
_run_command(cmd, shell=True)
return
- def srpm(self, hashtype=None):
+ def srpm(self, hashtype=None, fix_permissions=False):
"""Create an srpm using hashtype from content in the module
Requires sources already downloaded.
@@ -1743,11 +1747,18 @@ class PackageModule:
# srpm is newer, don't redo it
return
- cmd = ['rpmbuild']
+ if fix_permissions:
+ _run_command(cmd=['git', 'ls-files', '-z'],
+ pipe= ['xargs', '-0', 'chmod', 'a+r'],
+ shell=False)
+
+ cmd = ['fakeroot', 'rpmbuild']
cmd.extend(self.rpmdefines)
+
# Figure out which hashtype to use, if not provided one
if not hashtype:
hashtype = self.hashtype
+
# This may need to get updated if we ever change our checksum default
if not hashtype == 'sha256':
cmd.extend(["--define '_source_filedigest_algorithm %s'" % hashtype,
diff --git a/src/pyfedpkg/man_page.py b/src/pyfedpkg/man_page.py
new file mode 100644
index 0000000..47285f2
--- /dev/null
+++ b/src/pyfedpkg/man_page.py
@@ -0,0 +1,162 @@
+# Print a man page from the help texts.
+
+
+import argparse
+import sys
+import datetime
+
+
+# We could substitute the "" in .TH with the fedpkg version if we knew it
+man_header = """\
+.\" man page for fedpkg
+.TH fedpkg 1 "%(today)s" "" "fedora\-packager"
+.SH "NAME"
+fedpkg \- Fedora Packaging utility
+.SH "SYNOPSIS"
+.B "fedpkg"
+[
+.I global_options
+]
+.I "command"
+[
+.I command_options
+]
+[
+.I command_arguments
+]
+.br
+.B "fedpkg"
+.B "help"
+.br
+.B "fedpkg"
+.I "command"
+.B "\-\-help"
+.SH "DESCRIPTION"
+.B "fedpkg"
+is a script to interact with the Fedora Packaging system.
+"""
+
+man_footer = """\
+.SH "SEE ALSO"
+.UR "https://fedorahosted.org/fedora\-packager/"
+.BR "https://fedorahosted.org/fedora\-packager/"
+"""
+
+class ManFormatter(object):
+
+ def __init__(self, man):
+ self.man = man
+
+ def write(self, data):
+ #print "MF:", repr(data)
+ for line in data.split('\n'):
+ #print 'MFL:', line
+ self.man.write(' %s\n' % line)
+
+
+def strip_usage(s):
+ """Strip "usage: " string from beginning of string if present"""
+ if s.startswith('usage: '):
+ return s.replace('usage: ', '', 1)
+ else:
+ return s
+
+
+def man_constants():
+ """Global constants for man file templates"""
+ today = datetime.date.today()
+ today_manstr = today.strftime('%Y\-%m\-%d')
+ return {'today': today_manstr}
+
+
+def generate(parser, subparsers):
+ """\
+ Generate the man page on stdout
+
+ Given the argparse based parser and subparsers arguments, generate
+ the corresponding man page and write it to stdout.
+ """
+
+ # Not nice, but works: Redirect any print statement output to
+ # stderr to avoid clobbering the man page output on stdout.
+ man_file = sys.stdout
+ sys.stdout = sys.stderr
+
+ mf = ManFormatter(man_file)
+
+ choices = subparsers.choices
+ k = choices.keys()
+ k.sort()
+
+ man_file.write(man_header % man_constants())
+
+ helptext = parser.format_help()
+ helptext = strip_usage(helptext)
+ helptextsplit = helptext.split('\n')
+ helptextsplit = [ line for line in helptextsplit
+ if not line.startswith(' -h, --help') ]
+
+ man_file.write('.SS "%s"\n' % ("Global Options",))
+
+ outflag = False
+ for line in helptextsplit:
+ if line == "optional arguments:":
+ outflag = True
+ elif line == "":
+ outflag = False
+ elif outflag:
+ man_file.write("%s\n" % line)
+
+ help_texts = {}
+ for pa in subparsers._choices_actions:
+ help_texts[pa.dest] = getattr(pa, 'help', None)
+
+ if True: # Either kill THIS
+ # determine length of longest command and generate format string
+ commands = help_texts.keys()
+ commands.sort(lambda a,b: cmp(len(b), len(a)))
+ max_cmdlen = len(commands[0])
+ fmtstring = ' %%-%ds %%s\n' % (max_cmdlen,)
+
+ man_file.write('.SS "Commands"\n')
+
+ for command in k:
+ cmdparser = choices[command]
+ if not cmdparser.add_help:
+ continue
+ man_file.write(fmtstring % (command, help_texts[command]))
+
+ if True: # Or kill THIS
+ man_file.write('.SH "COMMAND OVERVIEW"\n')
+
+ for command in k:
+ cmdparser = choices[command]
+ if not cmdparser.add_help:
+ continue
+ usage = cmdparser.format_usage()
+ usage = strip_usage(usage)
+ usage = ''.join(usage.split('\n'))
+ usage = ' '.join(usage.split())
+ if help_texts[command]:
+ man_file.write('.TP\n.B "%s"\n%s\n' % (usage, help_texts[command]))
+ else:
+ man_file.write('.TP\n.B "%s"\n' % (usage))
+
+ man_file.write('.SH "COMMAND REFERENCE"\n')
+ for command in k:
+ cmdparser = choices[command]
+ if not cmdparser.add_help:
+ continue
+
+ man_file.write('.SS "%s"\n' % cmdparser.prog)
+
+ help = help_texts[command]
+ if help and not cmdparser.description:
+ if not help.endswith('.'): help = "%s." % help
+ cmdparser.description = help
+
+ formatter = cmdparser.formatter_class(cmdparser.prog)
+ h = cmdparser.format_help()
+ mf.write(h)
+
+ man_file.write(man_footer)