#!/usr/bin/env python

from __future__ import print_function

import os, sys
from subprocess import Popen, PIPE, check_call, CalledProcessError

def tar_file(dir, no_compress=False):
    """
    Create tar file from ``dir``.  If ``no_compress`` is True, don't
    compress; otherwise, compress using bzip2.

    If on a Mac, set the environment variable ``COPYFILE_DISABLE`` to
    True and use the subprocess module to call tar (to use the new
    environment variable).  Otherwise, use the tarfile module to
    create the tar file.
    """
    if sys.platform == 'darwin':
        # workaround OS X issue -- see trac #2522
        COPYFILE_DISABLE = True
        os.environ['COPYFILE_DISABLE'] = 'true'
        if no_compress:
            cmd = "tar -cf %s.spkg %s" % (dir, dir)
        else:
            cmd = "tar -cf - %s | bzip2 > %s.spkg" % (dir, dir)
        try:
            check_call(cmd, shell=True)
        except CalledProcessError:
            print("Package creation failed.")
            sys.exit(1)
    else:
        import tarfile
        if no_compress:
            mode = "w"
        else:
            mode = "w:bz2"
        try:
            tar = tarfile.open("%s.spkg" % dir, mode=mode)
            tar.add(dir, exclude=lambda f: f == ".DS_Store")
            tar.close()
        except tarfile.TarError:
            print("Package creation failed.")
            sys.exit(1)

def main():
    import re
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option("-n", "--no-compress", "--no_compress", "--nocompress",
                      dest="no_compress", action="store_true",
                      help="don't compress the spkg file")
    (options, args) = parser.parse_args()
    for path in args:
        print("Creating {1}Sage package from {0}".format(path,
                "uncompressed " if options.no_compress else ""))

        if not os.path.isdir(path):
            print("No directory {}".format(path))
            sys.exit(1)
        dir = os.path.basename(path.rstrip("/"))
        file = dir + ".spkg"
        s = dir.split("-", 1)
        try:
            name = s[0]
            version = s[1]
        except IndexError:
            name = dir
            version = ""

        if len(version) == 0:
            print("Warning: no version number detected")
        else:
            m = re.match(r"[0-9]+[-.0-9a-zA-Z]*(\.p[0-9]+)?$", version)
            if not m:
                print("Warning: version number ({}) not of the expected form."
                      .format(version))
                print("""The version number should start with a number and contain only numbers,
letters, periods, and hyphens.  It may optionally end with a string of
the form 'pNUM' where NUM is a non-negative integer.

Proceeding anyway...""")

        tar_file(dir, no_compress = options.no_compress)

        size = os.path.getsize(file)
        if size < 1024 * 1024:
            size = "{}K".format(size / 1024)
        else:
            size = "{:.1f}M".format(size / (1024 * 1024.0))

        if os.path.exists(os.path.join(dir, "SPKG.txt")):
            spkg_txt = "Good"
        else:
            spkg_txt = "File is missing"

        print("""
Created package {file}.

    NAME: {name}
 VERSION: {version}
    SIZE: {size}
SPKG.txt: {spkg}

Please test this package using

   sage -f {file}

immediately.""".format(file=file, name=name,  version=version, size=size,
                       spkg=spkg_txt), end=' ')

        if options.no_compress:
            print()
            print()
        else:
            print(""" Also, note that you can use

   sage -pkg_nc {}

to make an uncompressed version of the package (useful if the
package is full of compressed data).
""".format(dir))

if __name__ == "__main__":
    main()
