Category: py

  • 做新旧命令兼容的一种方式

    将无法解析的命令
    1. catch parse异常.
    2. 错误输出直接重定向/dev/null.避免干扰output

    #!/usr/bin/env python
    
    import sys
    import os
    import subprocess
    from optparse import OptionParser
    
    class redirect_stdout_stderr(object):
        def __init__(self, stream):
            # Save the old std streams
            self.old_stream = sys.stdout
            self.old_error_stream = sys.stderr
            self.fstream = stream
    
        def __enter__(self):
            # Change the std streams to your streams when entering
            sys.stdout = self.fstream
            sys.stderr = self.fstream
    
        def __exit__(self, exc_type, exc_value, exc_traceback):
            # Change the std streams back to the original streams while exiting
            sys.stdout = self.old_stream
            sys.stderr = self.old_error_stream
    
    if __name__ == "__main__":
        usage = "mdfscli [options]"
        parser = OptionParser(usage)
        #需要兼容的旧命令
        parser.add_option("--build-info", action="store_true", dest="cmd_build_info", \
                          default=False, help="Show build information")
        newappcli = ""
        try:
            # parse command line arguments
            with redirect_stdout_stderr(open(os.devnull, 'w')):
                (options, args) = parser.parse_args()
        except:
            newappcli = "/usr/share/yourapp/newappcli %s" % (" ".join(sys.argv[1:]))
        else:
            if options.cmd_build_info:
                newappcli = "/usr/share/yourapp/newappcli --build-info"
            else:
              newappcli = "/usr/share/yourapp/newappcli %s" % (" ".join(sys.argv[1:]))
    
        cmd = subprocess.Popen(newappcli, shell=True, stdout=sys.stdout, stderr=sys.stderr, close_fds=True)
        sys.exit(cmd.wait())
    
  • PYTHONPATH作用

    尝试使用命令行执行自己的一个小项目,抛出了异常

    (venv) [root@node2 hui-cache-test]# python system-info-data/src/main.py
    Traceback (most recent call last):
      File "system-info-data/src/main.py", line 1, in <module>
        from src.iostat.collect_op import *
    ModuleNotFoundError: No module named 'src'
    

    Google一番原来是需要配置$PYTHONPATH

    export PYTHONPATH=path-to-project-directory:$PYTHONPATH
    
    #export PYTHONPATH=/root/hui-cache-test/system-info-data:$PYTHONPATH
    
  • 使用virtualenv创建python项目环境

    virtualenv 能够创建干净的python库环境.避免污染python项目环境

    yum install -y virtualenv
    
    --no-site-packages参数是指不从全局的Python中携带任何第三方库
    [vvv@node1 test-workspace]$ virtualenv --no-site-packages test_env
    Using base prefix '/usr'
    New python executable in /home/vvv/test-workspace/test_env/bin/python3.6
    Also creating executable in /home/vvv/test-workspace/test_env/bin/python
    Installing setuptools, pip, wheel...done.
    
    [vvv@node1 test-workspace]$ cd test_env/
    [vvv@node1 test_env]$ source bin/activate
    (test_env) [vvv@node1 test_env]$ pip freeze
    
    # 指定python版本
        virtualenv --no-site-packages test_env --python=python3.6
    
    # 安装需要的库
    # pip install six
    # pip freeze > requirements.txt  # 导出已安装的库
    # pip install -r requirements.txt # 导入需要的库
    (test_env) [vvv@node1 test_env]$ deactivate
    

    全局级别安装库

    sudo pip3  install pyinstaller
    

    用户级安装库

    pip3  install pyinstaller --user
    

    支持sock5 proxy

     pip install pysocks
    

    使用代理

    pip install -r requirements.txt --proxy='socks5://192.168.8.73:61080'
    
  • svg to png or pdf

    将svg转化成png或者pdf.不过性能不好.如果svg有几十M,会很慢.

    # ! encoding:UTF-8
    import cairosvg
    import os
    
    
    def svg_to_png(from_dir, target_dir):
        """
        very slow
        :param from_dir:
        :param target_dir:
        :return:
        """
        return _export(from_dir, target_dir, "png")
    
    
    def _export(from_dir, target_dir, export_type):
        files = os.listdir(from_dir)
        for fileName in files:
            path = os.path.join(from_dir, fileName)
            if os.path.isfile(path) and fileName[-3:] == "svg":
                file_handle = open(path)
                svg = file_handle.read()
                file_handle.close()
                export_path = os.path.join(target_dir, fileName[:-3] + export_type)
                export_file_handle = open(export_path, 'w')
    
                if export_type == "png":
                    cairosvg.svg2png(bytestring=svg, write_to=export_path)
                elif export_type == "pdf":
                    cairosvg.svg2pdf(bytestring=svg, write_to=export_path)
    
                export_file_handle.close()