Esempio n. 1
0
def main(verbose, exclusive_mode, sort_album_by_name, sanitycheck, link, updatedb26, imagedirpath, lycheepath,
         confpath):
    """Lycheesync

    A script to synchronize any directory containing photos with Lychee.
    Source directory should be on the same host than Lychee's
    """

    if sys.version_info.major == 2:
        imagedirpath = imagedirpath.decode('UTF-8')
        lycheepath = lycheepath.decode('UTF-8')
        confpath = confpath.decode('UTF-8')

    conf_data = {'verbose': verbose, "srcdir": imagedirpath, "lycheepath": lycheepath, 'confpath': confpath,
                 "dropdb": False, "replace": False, "normal": False, "watch": True}

    if exclusive_mode == "delete":
        conf_data["dropdb"] = True
        conf_data["watch"] = False
    elif exclusive_mode == "replace":
        conf_data["replace"] = True
        conf_data["watch"] = False
    elif exclusive_mode == "normal":
        conf_data["normal"] = True
        conf_data["watch"] = False

    conf_data["user"] = None
    conf_data["group"] = None
    conf_data["uid"] = None
    conf_data["gid"] = None
    conf_data["sort"] = sort_album_by_name
    if sanitycheck:
        logger.info("!!!!!!!!!!!!!!!! SANITY ON")
    else:
        logger.info("!!!!!!!!!!!!!!!! SANITY OFF")
    conf_data["sanity"] = sanitycheck
    conf_data["link"] = link
    # if conf_data["dropdb"]:
    #    conf_data["sort"] = True

    # read permission of the lycheepath directory to apply it to the uploade photos
    img_path = os.path.join(conf_data["lycheepath"], "uploads", "big")
    stat_info = os.stat(img_path)
    uid = stat_info.st_uid
    gid = stat_info.st_gid

    user = pwd.getpwuid(uid)[0]
    group = grp.getgrgid(gid)[0]

    conf_data["user"] = user
    conf_data["group"] = group
    conf_data["uid"] = uid
    conf_data["gid"] = gid

    script_init(conf_data)

    # DB update
    if updatedb26:
        inf_to_lychee_2_6_2.updatedb(conf_data)

    logger.info("=================== start adding to lychee ==================")
    try:

        # DELEGATE WORK TO LYCHEESYNCER
        s = LycheeSyncer()
        s.sync()

    except Exception:
        logger.exception('Failed to run batch')
        logger.error("=================== script ended with errors ==================")

    else:
        logger.info("=================== script successfully ended ==================")
Esempio n. 2
0
def updatedb(conf_data):
    print("updatedb")

    # read permission of the lycheepath directory to apply it to the uploade photos
    upload_dir = os.path.join(conf_data["lycheepath"], "uploads")
    stat_info = os.stat(upload_dir)
    uid = stat_info.st_uid
    gid = stat_info.st_gid

    user = pwd.getpwuid(uid)[0]
    group = grp.getgrgid(gid)[0]

    conf_data["user"] = user
    conf_data["group"] = group
    conf_data["uid"] = uid
    conf_data["gid"] = gid

    syncer = LycheeSyncer(conf_data)

    # for each file in upload fix the permissions
    for root, dirs, files in os.walk(upload_dir):
        for f in files:
            if syncer.isAPhoto(f):
                # adjust permissions
                filepath = os.path.join(root, f)
                os.chown(filepath, int(uid), int(gid))
                st = os.stat(filepath)
                os.chmod(filepath, st.st_mode | stat.S_IRWXU | stat.S_IRWXG)
                print("Changed permission for " + str(f))

    # connect to db
    try:
        db = pymysql.connect(host=conf_data["dbHost"],
                             user=conf_data["dbUser"],
                             passwd=conf_data["dbPassword"],
                             db=conf_data["db"])

        # get photo list
        cur = db.cursor()
        cur.execute("SELECT id, url from lychee_photos")
        rows = cur.fetchall()
        for row in rows:

            pid = row[0]
            url = row[1]

            photo_path = os.path.join(upload_dir, "big", url)
            chksum = __generateHash(photo_path)
            # for each photo in db recalculate checksum
            qry = "update lychee_photos set checksum= '" + chksum + "' where id=" + str(
                pid)
            try:
                cur = db.cursor()
                cur.execute(qry)
                db.commit()
                print("INFO photo checksum changed to: ", chksum)
            except Exception:
                print("checksum modification failed for photo:" + id,
                      Exception)
                traceback.print_exc()

        print("******************************")
        print("SUCCESS")
        print("******************************")
    except Exception:
        traceback.print_exc()
    finally:
        db.close()
def updatedb(conf_data):
    print("updatedb")

    # read permission of the lycheepath directory to apply it to the uploade photos
    upload_dir = os.path.join(conf_data["lycheepath"], "uploads")
    stat_info = os.stat(upload_dir)
    uid = stat_info.st_uid
    gid = stat_info.st_gid

    user = pwd.getpwuid(uid)[0]
    group = grp.getgrgid(gid)[0]

    conf_data["user"] = user
    conf_data["group"] = group
    conf_data["uid"] = uid
    conf_data["gid"] = gid

    syncer = LycheeSyncer(conf_data)

    # for each file in upload fix the permissions
    for root, dirs, files in os.walk(upload_dir):
        for f in files:
            if syncer.isAPhoto(f):
                # adjust permissions
                filepath = os.path.join(root, f)
                os.chown(filepath, int(uid), int(gid))
                st = os.stat(filepath)
                os.chmod(filepath, st.st_mode | stat.S_IRWXU | stat.S_IRWXG)
                print("Changed permission for " + str(f))

    # connect to db
    try:
        db = pymysql.connect(host=conf_data["dbHost"],
                             user=conf_data["dbUser"],
                             passwd=conf_data["dbPassword"],
                             db=conf_data["db"])

        # get photo list
        cur = db.cursor()
        cur.execute("SELECT id, url from lychee_photos")
        rows = cur.fetchall()
        for row in rows:

            pid = row[0]
            url = row[1]

            photo_path = os.path.join(upload_dir, "big", url)
            chksum = __generateHash(photo_path)
            # for each photo in db recalculate checksum
            qry = "update lychee_photos set checksum= '" + chksum + "' where id=" + str(pid)
            try:
                cur = db.cursor()
                cur.execute(qry)
                db.commit()
                print("INFO photo checksum changed to: ", chksum)
            except Exception:
                print("checksum modification failed for photo:" + id, Exception)
                traceback.print_exc()

        print("******************************")
        print("SUCCESS")
        print("******************************")
    except Exception:
        traceback.print_exc()
    finally:
        db.close()