示例#1
0
def read_dict_info(conf, generate_api=True):
    """Parse dictionary meta data from the dictionary source and extract
    information about all released dictionaries. Return a list of dictionaries
    (Dictionary objects). generate_api is used to emit a message which is only
    useful if the API is generated."""
    dictionaries = []
    for dict_source in ['crafted', 'generated']:
        dict_source = get_path(conf[dict_source])
        print("Parsing meta data for all dictionaries from", dict_source)
        dictionaries.extend(metadata.get_meta_from_xml(dict_source))

    release_path = get_path(conf['release'])
    print("Parsing release information from", release_path)
    release_files = releases.get_all_downloads(release_path)
    for dict in dictionaries:
        name = dict.get_name()
        if not name in release_files:
            if generate_api:
                print("Skipping %s, no releases found." % name)
            continue
        try:
            version = releases.get_latest_version(release_files[name])
        except releases.ReleaseError as e:  # add file name to error
            raise releases.ReleaseError(list(e.args) + [name])
        for full_file, format, sha in release_files[name][version]:
            dict.add_download(
                dictionary.mklink(full_file, format, version, sha))
    return dictionaries
示例#2
0
def main():
    args = setup()
    try:  # load configuration
        conf = config.discover_and_load()
    except config.ConfigurationError as e:
        print(e)
        sys.exit(42)

    if args.print_api_path:
        print(config.get_path(conf['DEFAULT'], key='api_output_path'))
        sys.exit(0)
    elif args.print_release_path:
        print(config.get_path(conf['release'], key='local_path'))
        sys.exit(0)

    access_method = UnisonFileAccess()
    if conf['DEFAULT']['file_access_via'] == 'sshfs':
        access_method = SshfsAccess()

    release_directory = config.get_path(conf['release'])
    if not os.path.exists(release_directory):
        try:
            os.makedirs(release_directory)
        except OSError:
            # if the file does exist, but the fuse endpoint is _not_ connected,
            # we could try running fusermount -u:
            os.system('fusermount -u "%s"' % release_directory)

    ret = 0
    if args.make_available:
        for section in ('release', 'generated'):
            if conf[section].getboolean('skip'):
                print("Skipping", section)
                continue
            print('Making files for "%s" available...' % section)
            options = conf[section]
            target_path = config.get_path(options)
            ret = access_method.make_available(options['user'],
                                               options['server'],
                                               options['remote_path'],
                                               target_path)
    elif args.umount:
        for section in ('generated', 'release'):
            if conf[section].getboolean('skip'):
                print("Skipping", section)
                continue
            target_path = config.get_path(conf[section])
            try:
                access_method.make_unavailable(target_path)
            except OSError as e:
                print(e.args[0])
                continue
    sys.exit(ret)
示例#3
0
def main_body(args):
    parser = argparse.ArgumentParser(description='FreeDict API generator')
    parser.add_argument(
        "-n",
        "--need-update",
        dest="check_for_unreleased_dicts",
        action="store_true",
        default=False,
        help=
        "check for unreleased dictionaries instead of generating the API file")
    parser.add_argument(
        '-p',
        "--pre-exec-script",
        dest="prexec",
        metavar="PATH",
        help=
        ('script/command to execute before this script, e.g. to set up a sshfs '
         'connection to a remote server, or to invoke unison.'))
    parser.add_argument(
        '-o',
        "--post-exec-script",
        dest="postexc",
        metavar="PATH",
        help=("script/command to execute after this script is done, e.g. to "
              "umount mounted volumes."))

    args = parser.parse_args(args[1:])
    conf = config.discover_and_load()

    exec_or_fail(args.prexec)  # mount / synchronize release files

    dictionaries = read_dict_info(conf, not args.check_for_unreleased_dicts)

    if args.check_for_unreleased_dicts:
        outdated = find_outdated_releases(dictionaries)
        if not outdated:
            print("Everything up-to-date.")
        else:
            print("\nName      Source Version    Release Version")
            print("-------   ---------------   --------------------------")
            for data in sorted(outdated, key=lambda x: x[0]):
                name, v1, v2 = [str(e if e else 'unknown') for e in data]
                print('{}   {:<15}   {:<15}'.format(name, v1, v2))
    else:
        # remove dictionaries without download links
        dictionaries = sorted(
            (d for d in dictionaries if d.get_downloads() != []),
            key=lambda entry: entry.get_name())
        api_path = config.get_path(conf['DEFAULT'], key='api_output_path')
        xml_path = os.path.join(api_path, 'freedict-database.xml')
        json_path = os.path.join(api_path, 'freedict-database.json')
        if not os.path.exists(api_path):
            os.makedirs(os.path.dirname(api_path))
        print("Writing XML API file to", xml_path)
        xmlhandlers.write_freedict_database(xml_path, dictionaries)
        print("Writing JSON API file to", json_path)
        jsonhandlers.write_freedict_database(json_path, dictionaries)

    # if the files had been mounted with sshfs, it's a good idea to give it some
    # time to synchronize its state, otherwise umounting fails
    time.sleep(2)
    exec_or_fail(args.postexc)  # umount or unison files, if required