Exemple #1
0
def update_file(file_disk_path, file_download_path):
    if os.path.isfile(file_disk_path):
        file_disk_md5 = util.get_file_md5(file_disk_path)
        file_download_md5 = util.get_file_md5(file_download_path)
        if file_disk_md5 != file_download_md5:
            shutil.copy(file_download_path, file_disk_path)
    else:
        file_disk_dir = os.path.dirname(file_disk_path)
        if not os.path.isdir(file_disk_dir):
            util.mkdir_p(file_disk_dir)
        shutil.copy(file_download_path, file_disk_path)
Exemple #2
0
 def watch_update(self):
     force, new_version, update_fn, fn_md5 = unpackb(self.sock.recv(), encoding='utf-8')
     cur_version = read_config()['version']
     if force == 0 and new_version == cur_version:
         log.logger.info('current version is the latest version')
         return
     retry = 5
     index = 1
     while index <= retry:
         # wget update package and check md5
         ##
         ##
         try:
             shutil.copy(update_fn, os.path.join(prog_dir(), '..'))
             log.logger.info('copy update file done')
         except Exception, e:
             log.logger.error(e)
             index += 1
             continue
         if fn_md5 != get_file_md5(os.path.join(prog_dir(), '..', os.path.basename(update_fn))):
             log.logger.error("file md5 no match")
             index += 1
             continue
         else:
             break
Exemple #3
0
 def play_sound(self, file_path):
     '''播放语音
     
     :param file_path: 音频文件路径
     '''
     from util import get_file_md5
     self.set_volume(50)  # 先设置音量
     file_ext = os.path.splitext(file_path)[-1]
     dst_path = '/data/local/tmp/%s%s' % (get_file_md5(file_path), file_ext)
     self.push_file(file_path, dst_path)
     return self._send_command('PlaySound', FilePath=dst_path)
Exemple #4
0
def reload_config(signum, facility_db, writer):
    '''Reloads process configuration if possible.'''

    logger = logging.getLogger()
    logger.info('Recevied signal %d.', signum)

    if get_file_md5(options.main.config) != cached_config_md5:
        logger.warning('The main config file ({0}) has changed.'.format(options.main.config))
        logger.warning('Online reloading of the config file is not supported.')
        logger.warning('Please restart the process instead.')
        return

    facility_db.reload()
    writer.reload()
    logger.info('Reload complete.')
Exemple #5
0
 def install_package(self, pkg_path, overwrite=False):
     '''安装应用
     '''
     from util import get_file_md5
     if not os.path.exists(pkg_path):
         raise RuntimeError('APK: %r not exist' % pkg_path)
     
     pkg_size = os.path.getsize(pkg_path)
     pkg_md5 = get_file_md5(pkg_path)
     pkg_name = ADB._get_package_name(pkg_path)
     if self.is_package_installed(pkg_name, pkg_size, pkg_md5):
         logger.info('APP %s [%d]%s is installed' % (pkg_name, pkg_size, pkg_md5))
         return True
     
     self.adb.install_apk(pkg_path, overwrite)
     return True
Exemple #6
0
def reload_config(signum, facility_db, writer):
    '''Reloads process configuration if possible.'''

    logger = logging.getLogger()
    logger.info('Recevied signal %d.', signum)

    if get_file_md5(options.main.config) != cached_config_md5:
        logger.warning('The main config file ({0}) has changed.'.format(
            options.main.config))
        logger.warning('Online reloading of the config file is not supported.')
        logger.warning('Please restart the process instead.')
        return

    facility_db.reload()
    writer.reload()
    logger.info('Reload complete.')
Exemple #7
0
def process(rep):
    res = 0
    while True:
        delete()
        res += 1
        if res > 20:
            return False
        run(rep["durl"])
        logger.info("saving done")
        file_path = "download.pdf"
        if not os.path.isfile(file_path):
            return False

        # logger.info(file_path)
        # try:
        #     fp = open(file_path, "rb")
        #     pdfReader = PdfFileReader(fp)
        #     logger.info("read done")
        #     if pdfReader.isEncrypted:
        #         return False
        #
        # except:
        #     continue
        # pages = pdfReader.getNumPages()
        pages, pdfcreationDate = getPage(file_path)
        if pdfcreationDate is None:
            return False
        # fp.close()
        size = os.path.getsize(file_path)

        md5 = util.get_file_md5(file_path)

        if check_file_exists(md5, rep["title"]):
            return False

        fileid = util.get_uuid()
        logger.info("%s, %s, %s, %s, %s, %s", rep["title"], size,
                    pdfcreationDate, pages, md5, fileid)

        oss = oss2_helper.Oss2Helper("xiniudata-report")
        fp = file(file_path, "rb")
        oss.put(fileid,
                fp,
                headers={
                    "Content-Type": "application/pdf",
                    "x-oss-meta-filename": rep["filename"]
                })
        fp.close()

        mongo = db.connect_mongo()
        mongo.article.report.insert_one({
            "source":
            rep["source"],
            "description":
            None,
            "title":
            rep["title"],
            "filename":
            rep["filename"],
            "size":
            size,
            "pdfCreationDate":
            pdfcreationDate,
            "pages":
            pages,
            "md5":
            md5,
            "fileid":
            fileid,
            "createTime":
            datetime.datetime.now() - datetime.timedelta(hours=8),
            "modifyTime":
            datetime.datetime.now() - datetime.timedelta(hours=8),
            "type":
            78001
        })
        mongo.close()
        return True
Exemple #8
0
    def create(cls, user_id, name, image):
        """
        @param user_id: Foreign key to user
        @param name: Image name, including extension
        @param image: Actual image file
        """

        user = User.get_from_id(user_id)
        if user:
            photos = user.photos.all()
            # Order is zero-based index
            order = (max([x.order for x in photos]) + 1) if (photos and len(photos) > 0) else 0

            tmp_root = os.path.join(settings.TMP_MEDIA_ROOT, "users", user.nick)
            if not os.path.exists(tmp_root):
                os.makedirs(tmp_root)
            tmp_path = os.path.join(tmp_root, name)

            fd = None
            try:
                fd = open(tmp_path, "wb")
                for chunk in image.chunks():
                    fd.write(chunk)
            except IOError:
                return None, "Unable to save image"
            finally:
                if fd:
                    fd.close()

            try:
                img = Image.open(tmp_path)
                width, height = img.size

                print("Width: %d, Height: %d" % (width, height))

                # Want to keep a 4:3 frame ratio
                if width < 200 or height < 266:
                    raise ImageDimensionException("Dimensions are too small")

                hash_md5 = get_file_md5(tmp_path)
                size = get_file_size_bytes(tmp_path)

                # TODO, what if the user has a photo with the same size and hash?

                # Copy the image from the temp folder to the final folder
                root = os.path.join(settings.MEDIA_ROOT, "users", user.nick)
                if not os.path.exists(root):
                    os.makedirs(root)
                path = os.path.join(root, name)
                shutil.copy2(tmp_path, path)

                # Scale the image in 4 different sizes
                # xs 50x50 used in suggestions, chat, and user "profile" settings link
                # s 120x120 used in sliders
                # m 200x266 used in search to have 4 results per row
                # l 300x400 used in profile's main picture
                scale_img_and_save(img, 50.0, 50.0, name, "xs", root)
                scale_img_and_save(img, 120.0, 120.0, name, "s", root)
                scale_img_and_save(img, 200.0, 266.0, name, "m", root, crop=False)
                scale_img_and_save(img, 300.0, 400.0, name, "l", root, crop=False)

                # Must garbage collect once done using in order to be able to delete
                del img

                photo = UserPhoto(user_id=user_id, name=name, path=path, bytes=size, size="regular",
                                  hash_md5=hash_md5, order=order)
                photo.save()
                return photo, ""
            except Exception, err:
                print("Original exception: %s" % str(err))
                error = "Unable to save image"

                if type(err).__name__ == ImageDimensionException.__name__:
                    print("Dimensions are too small")
                    error = err
                return None, error
            finally:
Exemple #9
0
def process(dir_path, filename):
    # logger.info(filename)
    file_path = os.path.join(dir_path, filename)
    if not os.path.isfile(file_path):
        return False
    if not filename.lower().endswith(".pdf"):
        return False
    # logger.info(file_path)

    fp = file(file_path, "rb")
    pdfReader = PdfFileReader(fp)
    if pdfReader.isEncrypted:
        fp.close()
        logger.info("File encrypted! filename: %s", filename)
        decrypt_pdf(file_path)
        fp = file(file_path, "rb")
        pdfReader = PdfFileReader(fp)

    # creationDate = pdfReader.documentInfo.get("/CreationDate")
    # if not isinstance(creationDate, str):
    #     try:
    #         creationDate = creationDate.getObject()
    #     except:
    #         traceback.print_exc()
    #         return False

    pages = pdfReader.getNumPages()
    fp.close()

    # try:
    #     datestring = creationDate[2:-7]
    #     ts = strptime(datestring, "%Y%m%d%H%M%S")
    # except:
    #     traceback.print_exc()
    #     return False
    # dt = datetime.fromtimestamp(mktime(ts)) - timedelta(hours=8)
    ts = os.path.getctime(file_path)
    dt = datetime.fromtimestamp(ts) - timedelta(hours=8)

    size = os.path.getsize(file_path)
    title = filename[0:-4].strip()
    source = None
    if u":" in title:
        strs = title.split(u":", 1)
        source = strs[0]
        title = strs[1]

    md5 = util.get_file_md5(file_path)

    if check_file_exists(md5, title):
        return True

    fileid = util.get_uuid()
    logger.info("%s, %s, %s, %s, %s, %s", title, size, dt, pages, md5, fileid)

    oss = oss2_helper.Oss2Helper("xiniudata-report")
    fp = file(file_path, "rb")
    oss.put(fileid,
            fp,
            headers={
                "Content-Type": "application/pdf",
                "x-oss-meta-filename": filename.strip()
            })
    fp.close()

    save(source, filename, title, size, dt, pages, md5, fileid)
    return True
Exemple #10
0
def cache_config_checksum():
    '''Cached the checksum of the config file in case we receive SIGHUP.'''

    global cached_config_md5
    cached_config_md5 = get_file_md5(options.main.config)
Exemple #11
0
def cache_config_checksum():
    '''Cached the checksum of the config file in case we receive SIGHUP.'''

    global cached_config_md5
    cached_config_md5 = get_file_md5(options.main.config)