Windows Python 跨进程文件访问

OS: windows 7 python: 2.7

最近在把Android OTA的整套东西移植到windows上,涉及了很多python脚本,要疯了。暂不说没写过一行python了,就是windows系统也有很多年头没动过了,这个牛X的系统,居然还是那么难用。

问题描述:

temp_zip_file = tempfile.NamedTemporaryFile()

#同一线程访问temp_zip_file

#不同线程访问temp_zip_file

temp_zip_file.close()

运行时提示错误:

另一个程序正在使用此文件,进程无法访问。

靠啊,这么简单的一条语句还能出啥问题啊,在Linux上也是没有问题的,查查文档,发现下面这句话:

tempfile.NamedTemporaryFile([mode='w+b'[, bufsize=-1[, suffix=''[, prefix='tmp'[, dir=None[, delete=True]]]]]])

This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from the name attribute of the file object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT or later). If delete is true (the default), the file is deleted as soon as it is closed.
The returned object is always a file-like object whose file attribute is the underlying true file object. This file-like object can be used in a with statement, just like a normal file.

一股浓烈的鄙视之情不由自主的直往上冒。。。。。。。

解决方法:

NamedTemporaryFile默认情况下在被调用close()后会自动删除掉(跟linux的unlink差不多),加上delete=False后可以改变这个行为,但是最后要记得手工去删除这个临时文件。最后代码就变成了这个样子:

temp_zip_file = tempfile.NamedTemporaryFile(delete = False)

#同一线程访问temp_zip_file后需要关闭该文件
temp_zip_file.close()

#不同线程访问temp_zip_file
temp_zip_file = open(temp_zip_file.name, 'rb')

#最后删除文件
os.remove(temp_zip_file.name)

ref: http://bugs.python.org/issue14243#msg164504 ref: https://docs.python.org/2/library/tempfile.html ref: http://stackoverflow.com/questions/15169101/how-to-create-a-temporary-file-that-can-be-read-by-a-subprocess/15235559#15235559

Java 判断符号链接

  /**
     * Determines whether the specified file is a Symbolic Link rather than an actual file.
     * <p>
     * Will not return true if there is a Symbolic Link anywhere in the path,
     * only if the specific file is.
     * <p>
     * <b>Note:</b> the current implementation always returns {@code false} if the system
     * is detected as Windows using {@link FilenameUtils#isSystemWindows()}
     * <p>
     * For code that runs on Java 1.7 or later, use the following method instead:
     * <br>
     * {@code boolean java.nio.file.Files.isSymbolicLink(Path path)}
     * @param file the file to check
     * @return true if the file is a Symbolic Link
     * @throws IOException if an IO error occurs while checking the file
     * @since 2.0
     */
    public static boolean isSymlink(final File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        if (FilenameUtils.isSystemWindows()) {
            return false;
        }
        File fileInCanonicalDir = null;
        if (file.getParent() == null) {
            fileInCanonicalDir = file;
        } else {
            final File canonicalDir = file.getParentFile().getCanonicalFile();
            fileInCanonicalDir = new File(canonicalDir, file.getName());
        }

        if (fileInCanonicalDir.getCanonicalFile().equals(fileInCanonicalDir.getAbsoluteFile())) {
            return false;
        } else {
            return true;
        }
    }

ref: http://svn.apache.org/viewvc/commons/proper/io/trunk/src/main/java/org/apache/commons/io/FileUtils.java?view=markup

Android LinearGradient

Bitmap    mBitmap;
Matrix    mShadowPaint;
Matrix    mLinearGradientMatrix;
LinearGradient    mLinearGradient;

mBitmap = BitmapFactory.decodeResource(getResource, R.drawable.jobs);

mShadowPaint = new Paint();
mShadowPaint.setStyle(Style.FILL);
mShadowPaint.setAlpha((int)0.9 * 255);

//渐变范围从(0, 0)到(1.0, 0)
//渐变颜色从红色到透明
//TileMode.CLAMP重复边缘色,这里实际是拉伸透明
mLinearGradient = new LinearGradient(0, 0, 1.0f, 0, Color.red, Color.TRANSPARENT, TileMode.CLAMP); 

mLinearGradientMatrix = new Matrix();
mLinearGradientMatrix.setScale(mBitmap.getWidth(), 1);
//把线性渐变的范围扩大到整张图片
mLinearGradient.setLocalMatrix(mLinearGradientMatrix);

mShadowPaint.setShader(mLinearGradient);

canvas.drawBitmap(mBitmap, 0, 0, null);
canvas.drawRect(0, 0, mBitmap.getWidth(), mBitmap.getHeight(), mShadowPaint);

Apache VirtualHost 配置

OS: ubuntu 12.04 Apache: 2.2.22

Configure file: /etc/apache2/sites-available/default

NameVirtualHost [IP Address]

<VirtualHost [IP Address]>
        ServerName www.asite.com
        ServerAdmin [email protected]

        DocumentRoot /var/www/asite

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /var/www/asite>
                #Options Indexes FollowSymLinks MultiViews
                Options -Indexes
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride All
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


<VirtualHost [IP Address]>
        ServerName www.bsite.com
        ServerAdmin [email protected]

        DocumentRoot /var/www/bsite

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /var/www/bsite>
                #Options Indexes FollowSymLinks MultiViews
                Options -Indexes 
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
        <Directory "/usr/lib/cgi-bin">
                AllowOverride All
                Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                Order allow,deny
                Allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

配置ibus使支持skype

编辑配置文件/etc/X11/xinit/xinput.d/default

XIM=ibus                           
XIM_PROGRAM=/usr/bin/ibus-daemon
XIM_ARGS="--xim"                   
XIM_PROGRAM_XTRA=                  
XIM_PROGRAM_SETS_ITSELF_AS_DAEMON=
                                 
GTK_IM_MODULE=ibus                 
QT_IM_MODULE=ibus                  
                                                                    
DEPENDS="ibus, ibus-gtk, ibu-qt4"