예제 #1
0
파일: launcher.py 프로젝트: pwr/Sigil
def main(argv=unicode_argv()):

    if len(argv) != 5:
        failed(None, msg="Launcher: improper number of arguments passed to launcher.py")
        return -1

    ebook_root = argv[1]
    outdir = argv[2]
    script_type = argv[3]
    target_file = argv[4]
    script_home = os.path.dirname(target_file)
    script_module = os.path.splitext(os.path.basename(target_file))[0]

    # do basic sanity checking anyway
    if script_type not in SUPPORTED_SCRIPT_TYPES:
        failed(None, msg="Launcher: script type %s is not supported" % script_type)
        return -1

    ok = unipath.exists(ebook_root) and unipath.isdir(ebook_root)
    ok = ok and unipath.exists(outdir) and unipath.isdir(outdir)
    ok = ok and unipath.exists(script_home) and unipath.isdir(script_home)
    ok = ok and unipath.exists(target_file) and unipath.isfile(target_file)
    if not ok:
        failed(None, msg="Launcher: missing or incorrect paths passed in")
        return -1

    # update sys with path to target module home directory
    if script_home not in sys.path:
        sys.path.append(script_home)

    # load and parse opf if present
    op = None
    opf_path = os.path.join(ebook_root, "OEBPS", "content.opf")
    if unipath.exists(opf_path) and unipath.isfile(opf_path):
        op = Opf_Parser(opf_path)

    # create a wrapper for record keeping and safety
    rk = Wrapper(ebook_root, outdir, op)

    # get the correct container
    if script_type == "edit":
        bc = BookContainer(rk)
    elif script_type == "input":
        bc = InputContainer(rk)
    else:
        bc = OutputContainer(rk)

    # start the target script
    ps = ProcessScript(script_type, script_module, bc)
    ps.launch()

    # get standard error and standard out from the target script
    successmsg = ""
    for data in ps.stdouttext:
        successmsg += unicode_str(data)
    successmsg = escapeit(successmsg)
    errorlog = ""
    for data in ps.stderrtext:
        errorlog += unicode_str(data)
    errorlog = escapeit(errorlog)

    # get the target's script wrapper xml
    resultxml = "".join(ps.wrapout)
    resultxml += "<msg>\n"
    if ps.exitcode == 0:
        resultxml += successmsg
        if _DEBUG:
            resultxml += errorlog
    else:
        if _DEBUG:
            resultxml += successmsg
        resultxml += errorlog
    resultxml += "</msg>\n</wrapper>\n"

    # write it to stdout and exit
    if PY3:
        sys.stdout.buffer.write(utf8_str(resultxml))
    else:
        sys.stdout.write(utf8_str(resultxml))
    return 0
예제 #2
0
def main(argv=unicode_argv()):
    global DUMP
    global WRITE_RAW_DATA
    global SPLIT_COMBO_MOBIS
    global UPDATED_TITLE
    global OUTPUT_EPUB

    print("KindleUnpack v0.82")
    print("   Based on initial mobipocket version Copyright © 2009 Charles M. Hannum <*****@*****.**>")
    print("   Extensive Extensions and Improvements Copyright © 2009-2014 ")
    print("       by:  P. Durrant, K. Hendricks, S. Siebert, fandrieu, DiapDealer, nickredding, tkeo.")
    print("   This program is free software: you can redistribute it and/or modify")
    print("   it under the terms of the GNU General Public License as published by")
    print("   the Free Software Foundation, version 3.")

    progname = os.path.basename(argv[0])
    try:
        opts, args = getopt.getopt(argv[1:], "dhirspt:", ['epub_version='])
    except getopt.GetoptError as err:
        print(str(err))
        usage(progname)
        sys.exit(2)

    if len(args)<1:
        usage(progname)
        sys.exit(2)

    apnxfile = None
    epubver = '2'
    use_hd = False
    UPDATED_TITLE = False
    COMPRESS_ZIP = False
    OUTPUT_ZIP = True
    OUTPUT_EPUB = False
    OUTPUT_IMAGES = False

    for o, a in opts:
        if o == "-h":
            usage(progname)
            sys.exit(0)
        if o == "-i":
            use_hd = True
        if o == "-d":
            DUMP = True
        if o == "-r":
            WRITE_RAW_DATA = True
        if o == "-s":
            SPLIT_COMBO_MOBIS = True
        if o == "-t":
            UPDATED_TITLE = True
        if o == "-c":
            COMPRESS_ZIP = True
        if o == "-z":
            OUTPUT_ZIP = True
        if o == "-e":
            OUTPUT_EPUB = True
        if o == "-f":
            OUTPUT_IMAGES = True
        if o == "-p":
            apnxfile = a
        if o == "--epub_version":
            epubver = a

    if len(args) > 1:
        infile, outdir = args
    else:
        infile = args[0]
        outdir = os.path.splitext(infile)[0]

    infileext = os.path.splitext(infile)[1].upper()
    if infileext not in ['.MOBI', '.PRC', '.AZW', '.AZW3', '.AZW4']:
        print("Error: first parameter must be a Kindle/Mobipocket ebook or a Kindle/Print Replica ebook.")
        return 1

    try:
        print('Unpacking Book...')
        unpackBook(infile, outdir, apnxfile, epubver, use_hd)
        print('Completed')

    except ValueError as e:
        print("Error: %s" % e)
        print(traceback.format_exc())
        return 1

    return 0
예제 #3
0
def main(argv=unicode_argv()):

    if len(argv) != 5:
        failed(
            None,
            msg="Launcher: improper number of arguments passed to launcher.py")
        return -1

    ebook_root = argv[1]
    outdir = argv[2]
    script_type = argv[3]
    target_file = argv[4]
    script_home = os.path.dirname(target_file)
    plugin_name = os.path.split(script_home)[-1]
    plugin_dir = os.path.dirname(script_home)
    script_module = os.path.splitext(os.path.basename(target_file))[0]

    # do basic sanity checking anyway
    if script_type not in SUPPORTED_SCRIPT_TYPES:
        failed(None,
               msg="Launcher: script type %s is not supported" % script_type)
        return -1

    ok = unipath.exists(ebook_root) and unipath.isdir(ebook_root)
    ok = ok and unipath.exists(outdir) and unipath.isdir(outdir)
    ok = ok and unipath.exists(script_home) and unipath.isdir(script_home)
    ok = ok and unipath.exists(target_file) and unipath.isfile(target_file)
    if not ok:
        failed(None, msg="Launcher: missing or incorrect paths passed in")
        return -1

    # update sys with path to target module home directory
    sys.path.append(script_home)

    # load and parse opf if present
    op = None
    opf_path = os.path.join(ebook_root, 'OEBPS', 'content.opf')
    if unipath.exists(opf_path) and unipath.isfile(opf_path):
        op = Opf_Parser(opf_path)

    # create a wrapper for record keeping and safety
    rk = Wrapper(ebook_root, outdir, op, plugin_dir, plugin_name)

    # get the correct container
    if script_type == 'edit':
        bc = BookContainer(rk)
    elif script_type == 'input':
        bc = InputContainer(rk)
    elif script_type == 'validation':
        bc = ValidationContainer(rk)
    else:
        bc = OutputContainer(rk)

    # start the target script
    ps = ProcessScript(script_type, script_module, bc)
    ps.launch()

    # get standard error and standard out from the target script
    successmsg = ''
    for data in ps.stdouttext:
        successmsg += unicode_str(data)
    successmsg = escapeit(successmsg)
    errorlog = ''
    for data in ps.stderrtext:
        errorlog += unicode_str(data)
    errorlog = escapeit(errorlog)

    # get the target's script wrapper xml
    resultxml = "".join(ps.wrapout)
    resultxml += "<msg>\n"
    if ps.exitcode == 0:
        resultxml += successmsg
        if _DEBUG:
            resultxml += errorlog
    else:
        if _DEBUG:
            resultxml += successmsg
        resultxml += errorlog
    resultxml += '</msg>\n</wrapper>\n'

    # write it to stdout and exit
    if PY3:
        sys.stdout.buffer.write(utf8_str(resultxml))
    else:
        sys.stdout.write(utf8_str(resultxml))
    return 0
예제 #4
0
파일: azw2zip.py 프로젝트: junk2ool/azw2zip
def main(argv=unicode_argv()):
    progname = os.path.splitext(os.path.basename(argv[0]))[0]
    azw2zip_dir = os.path.dirname(os.path.abspath(argv[0]))

    print(u"{0:} v.{1:s}\nCopyright (C) 2020 junk2ool".format(
        progname, __version__))
    print(u"")

    try:
        opts, args = getopt.getopt(argv[1:], "zefptscomd")
    except getopt.GetoptError as err:
        print(str(err))
        usage(progname)
        sys.exit(2)

    if len(args) < 1:
        usage(progname)
        sys.exit(2)

    cfg = azw2zipConfig()
    cfg.load(os.path.join(azw2zip_dir, 'azw2zip.json'))

    updated_title = cfg.isUpdatedTitle()
    authors_sort = cfg.isAuthorsSort()
    compress_zip = cfg.isCompressZip()
    over_write = cfg.isOverWrite()
    output_thumb = cfg.isOutputThumb()
    output_zip = cfg.isOutputZip()
    output_epub = cfg.isOutputEpub()
    output_images = cfg.isOutputImages()
    output_pdf = cfg.isOutputPdf()
    debug_mode = cfg.isDebugMode()

    # オプション解析
    for o, a in opts:
        if o == "-t":
            updated_title = True
        if o == "-s":
            authors_sort = True
        if o == "-c":
            compress_zip = True
        if o == "-o":
            over_write = True
        if o == "-m":
            output_thumb = True
        if o == "-z":
            output_zip = True
        if o == "-e":
            output_epub = True
        if o == "-f":
            output_images = True
        if o == "-p":
            output_pdf = True
        if o == "-d":
            debug_mode = True
    if not output_zip and not output_epub and not output_images and not output_pdf:
        output_zip = True
    cfg.setOptions(updated_title, authors_sort, compress_zip, over_write,
                   output_thumb, debug_mode)
    cfg.setOutputFormats(output_zip, output_epub, output_images, output_pdf)

    # k4i ディレクトリはスクリプトのディレクトリ
    k4i_dir = cfg.getk4iDirectory()
    if not k4i_dir:
        k4i_dir = azw2zip_dir
    print(u"k4iディレクトリ: {}".format(k4i_dir))
    cfg.setk4iDirectory(k4i_dir)
    k4i_files = glob.glob(os.path.join(k4i_dir, '*.k4i'))
    if not len(k4i_files):
        # k4iがなければ作成
        if not sys.platform.startswith('win') and not sys.platform.startswith(
                'darwin'):
            # k4iはWindowsかMacでしか作成できない
            print(u"エラー : k4iファイルが見つかりません: {}".format(k4i_dir))
            exit(1)

        print(u"k4i作成: 開始: {}".format(k4i_dir))
        if debug_mode:
            kindlekey.getkey(k4i_dir)
        else:
            with redirect_stdout(open(os.devnull, 'w')):
                kindlekey.getkey(k4i_dir)
        k4i_files = glob.glob(os.path.join(k4i_dir, '*.k4i'))
        print(u"k4i作成: 完了: {}".format(k4i_files[0]))
    else:
        for k4i_fpath in k4i_files:
            print(u"k4i: {}".format(k4i_fpath))

    # 変換ディレクトリ
    in_dir = args[0]
    if not os.path.isabs(in_dir):
        in_dir = os.path.abspath(in_dir)
    if not in_dir:
        in_dir = os.getcwd()
    in_dir = os.path.realpath(os.path.normpath(in_dir))
    if (os.path.isfile(in_dir)):
        in_dir = os.path.dirname(in_dir)
    print(u"変換ディレクトリ: {}".format(in_dir))

    # 出力ディレクトリ作成
    out_dir = cfg.getOutputDirectory()
    if len(args) > 1:
        out_dir = args[1]
        if not os.path.isabs(out_dir):
            out_dir = os.path.abspath(out_dir)
    if not out_dir:
        out_dir = azw2zip_dir  #os.getcwd()
    out_dir = os.path.realpath(os.path.normpath(out_dir))
    cfg.setOutputDirectory(out_dir)

    print(u"出力ディレクトリ: {}".format(out_dir))
    if not unipath.exists(out_dir):
        unipath.mkdir(out_dir)
        print(u"出力ディレクトリ: 作成: {}".format(out_dir))

    output_zip_org = output_zip
    output_epub_org = output_epub
    output_images_org = output_images
    output_pdf_org = output_pdf

    # 処理ディレクトリのファイルを再帰走査
    for azw_fpath in find_all_files(in_dir):
        # ファイルでなければスキップ
        if not os.path.isfile(azw_fpath):
            continue
        # .azwファイルでなければスキップ
        fext = os.path.splitext(azw_fpath)[1].upper()
        if fext not in ['.AZW', '.AZW3']:
            continue

        output_zip = output_zip_org
        output_epub = output_epub_org
        output_images = output_images_org
        output_pdf = output_pdf_org

        output_format = [
            [output_zip, u"zip", u".zip"],
            [output_epub, u"epub", u".epub"],
            [output_images, u"Images", u""],
            [output_pdf, u"pdf", u".*.pdf"],
        ]

        print("")
        azw_dir = os.path.dirname(azw_fpath)
        print(u"変換開始: {}".format(azw_dir))

        # 上書きチェック
        a2z = azw2zip()
        over_write_flag = over_write
        try:
            if a2z.load(azw_fpath, '', debug_mode) != 0:
                over_write_flag = True
        except azw2zipException as e:
            print(str(e))
            over_write_flag = True

        cfg.setPrintReplica(a2z.is_print_replica())

        if not over_write_flag:
            fname_txt = cfg.makeOutputFileName(a2z.get_meta_data())
            for format in output_format:
                if format[0]:
                    output_fpath = os.path.join(out_dir, fname_txt + format[2])
                    output_files = glob.glob(output_fpath.replace('[', '[[]'))
                    if (len(output_files)):
                        format[0] = False
                        try:
                            print(u" {}変換: パス: {}".format(
                                format[1], output_files[0]))
                        except UnicodeEncodeError:
                            print(u" {}変換: パス: {}".format(
                                format[1], output_files[0].encode(
                                    'cp932', 'replace').decode('cp932')))
                    else:
                        over_write_flag = True

        if not over_write_flag:
            # すべてパス
            print(u"変換完了: {}".format(azw_dir))
            continue

        cfg.setOutputFormats(output_zip, output_epub, output_images,
                             output_pdf)

        # 作業ディレクトリ作成
        # ランダムな8文字のディレクトリ名
        source = string.ascii_letters + string.digits
        random_str = ''.join([random.choice(source) for _ in range(8)])
        temp_dir = os.path.join(out_dir, "Temp_" + random_str)
        book_fname = os.path.basename(os.path.dirname(azw_fpath))
        #temp_dir = os.path.join(out_dir, book_fname)
        print(u" 作業ディレクトリ: 作成: {}".format(temp_dir))
        if not unipath.exists(temp_dir):
            unipath.mkdir(temp_dir)

        cfg.setTempDirectory(temp_dir)

        # HD画像(resファイル)があれば展開
        res_files = glob.glob(os.path.join(os.path.dirname(azw_fpath),
                                           '*.res'))
        for res_fpath in res_files:
            print(u"  HD画像展開: 開始: {}".format(res_fpath))

            if debug_mode:
                DumpAZW6_v01.DumpAZW6(res_fpath, temp_dir)
            else:
                with redirect_stdout(open(os.devnull, 'w')):
                    DumpAZW6_v01.DumpAZW6(res_fpath, temp_dir)

            print(u"  HD画像展開: 完了: {}".format(
                os.path.join(temp_dir, 'azw6_images')))

        # azwならDRM解除
        DeDRM_path = ""
        if fext in ['.AZW']:
            print(u"  DRM解除: 開始: {}".format(azw_fpath))

            if debug_mode:
                decryptk4mobi(azw_fpath, temp_dir, k4i_dir)
            else:
                with redirect_stdout(open(os.devnull, 'w')):
                    decryptk4mobi(azw_fpath, temp_dir, k4i_dir)

            DeDRM_files = glob.glob(
                os.path.join(temp_dir, book_fname + '*.azw?'))
            if len(DeDRM_files) > 0:
                DeDRM_path = DeDRM_files[0]
                print(u"  DRM解除: 完了: {}".format(DeDRM_path))
            else:
                print(u"  DRM解除: 失敗:")
        elif fext in ['.AZW3']:
            DeDRM_path = azw_fpath

        if DeDRM_path and unipath.exists(DeDRM_path):
            # 書籍変換
            print(u"  書籍変換: 開始: {}".format(DeDRM_path))

            #unpack_dir = os.path.join(temp_dir, os.path.splitext(os.path.basename(DeDRM_path))[0])
            unpack_dir = temp_dir
            if debug_mode:
                kindleunpack.kindleunpack(DeDRM_path, unpack_dir, cfg)
            else:
                with redirect_stdout(open(os.devnull, 'w')):
                    kindleunpack.kindleunpack(DeDRM_path, unpack_dir, cfg)

            # 作成したファイル名を取得
            fname_path = os.path.join(temp_dir, "fname.txt")
            if unipath.exists(fname_path):
                fname_file = codecs.open(fname_path, 'r', 'utf-8')
                fname_txt = fname_file.readline().rstrip()
                fname_file.close()

                for format in output_format:
                    if format[0]:
                        output_fpath = os.path.join(out_dir,
                                                    fname_txt + format[2])
                        output_files = glob.glob(
                            output_fpath.replace('[', '[[]'))
                        if (len(output_files)):
                            try:
                                print(u"  {}変換: 完了: {}".format(
                                    format[1], output_files[0]))
                            except UnicodeEncodeError:
                                print(u"  {}変換: 完了: {}".format(
                                    format[1], output_files[0].encode(
                                        'cp932', 'replace').decode('cp932')))
            else:
                print(u"  書籍変換: 失敗:")
        else:
            print(u"  DRM解除: 失敗:")

        if not debug_mode:
            shutil.rmtree(temp_dir)
            print(u" 作業ディレクトリ: 削除: {}".format(temp_dir))

        print(u"変換完了: {}".format(azw_dir))

    return 0