[MQ-PATCH] ditrack: dt-createdb-use-optparse

Vlad Skvortsov vss at 73rus.com
Tue Jul 17 13:40:53 PDT 2007


Applied modified version of this patch in r1722. Thanks, Stefan!


Stefan Reichör wrote:
> Hi!
>
> Here is another patch.
> It uses the optparse module for dt-createdb.
>
> The benefits are:
>  * dt-createdb handles the --help switch
>  * there is another switch: -n for a dry-run
>  * It is easy to add more switches if needed
>
> Stefan.
>
>
>   
> ------------------------------------------------------------------------
>
> diff -r 1700c06b9e41 dt-createdb
> --- a/dt-createdb	Wed Jul 11 21:53:55 2007 +0200
> +++ b/dt-createdb	Wed Jul 11 22:21:52 2007 +0200
> @@ -30,10 +30,34 @@
>  #
>  
>  import os
> +import optparse
>  import sys
>  
>  # A list of supported backends. The first one is the default.
>  supported_backends = ["none", "svn"]
> +
> +parser = optparse.OptionParser(usage="""usage: %prog [options] <issue-db-dir> [<ditrack-backend-type>]""")
> +
> +backends = supported_backends
> +backends[0] += " (default)"
> +backends.sort()
> +extended_usage = """
> +Create a ditrack issue database in <issue-db-dir>.
> +The <issue-db-dir> will be created by this command, it must not exist before.
> +
> +<ditrack-backend-type> is the backend database type, which can be one of:
> +
> +%s
> +
> +""" % ("\n".join(map(lambda x: "    " + x, backends)))
> +
> +parser.add_option("-n", "--dry-run", action="store_true", dest="dry_run", default=False, help="Show what would be done, but don't create the database")
> +(options, args) = parser.parse_args()
> +
> +def show_usage():
> +    parser.print_help()
> +    print extended_usage
> +
>  
>  def mkdir(path):
>      try:
> @@ -91,34 +115,14 @@ def ditrack_add_to_svn(dbdir):
>  # ENTRY POINT                                                               #
>  #############################################################################
>  
> -if len(sys.argv) not in [2, 3]:
> -
> -    backends = supported_backends
> -    backends[0] += " (default)"
> -    backends.sort()
> -
> -    sys.stdout.write("""Syntax:
> -%s <issue-db-dir> [<ditrack-backend-type>]
> -
> -Create a ditrack issue database in <issue-db-dir>.
> -The <issue-db-dir> will be created by this command, it must not exist before.
> -
> -<ditrack-backend-type> is the backend database type, which can be one of:
> -
> -%s
> -
> -""" 
> -        % (
> -            os.path.basename(sys.argv[0]),
> -            "\n".join(map(lambda x: "    " + x, backends))
> -        )
> -    )
> +if len(args) not in [1, 2]:
> +    show_usage()
>      sys.exit(1)
>  
> -dbdir = sys.argv[1]
> +dbdir = args[0]
>  
> -if len(sys.argv) == 3:
> -    backend_type = sys.argv[2]
> +if len(args) == 2:
> +    backend_type = args[1]
>  else:
>      backend_type = supported_backends[0]
>  
> @@ -134,19 +138,20 @@ if os.path.exists(dbdir):
>      sys.stdout.write("'%s' already exists - aborting\n" % dbdir)
>      sys.exit(1)
>  
> -init_ditrack_db(dbdir)
> +if options.dry_run:
> +    sys.stdout.write("Would create ditrack database at '%s'\n" % dbdir)
> +else:
> +    init_ditrack_db(dbdir)
>  
>  if backend_type == "svn":
>      # XXX: we can't add to SVN, since that's not a working copy.
> +    # we could check for a .svn folder -> if one is present, this is a working copy...
>      #ditrack_add_to_svn(dbdir)
>      pass
>  
> -sys.stdout.write("Empty ditrack issue database created in '%s'\n" % dbdir)
> +if not options.dry_run:
> +    sys.stdout.write("Empty ditrack issue database created in '%s'\n" % dbdir)
>  
> -if backend_type == "svn":
> -    sys.stdout.write("""
> -Now you should probably type something like:
> -
> -svn commit %s
> -
> -""" % dbdir)
> +    if backend_type == "svn":
> +        sys.stdout.write("\nNow you should probably type something like:\n"
> +                         "svn commit %s\n\n" % dbdir)
>   
> ------------------------------------------------------------------------
>
> _______________________________________________
> Dev mailing list
> Dev at lists.ditrack.org
> http://lists.ditrack.org/mailman/listinfo/dev
>   


-- 
Vlad Skvortsov, vss at 73rus.com, http://vss.73rus.com



More information about the Dev mailing list