py2app在构建期间拾取包的.git子目录 [英] py2app picking up .git subdir of a package during build

查看:105
本文介绍了py2app在构建期间拾取包的.git子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我们的工厂中广泛使用py2app来生成自包含的.app软件包,以便在内部进行轻松部署而不会出现依赖问题.我最近注意到的一件事,不知道它是如何开始的,那就是在构建.app时,py2app开始包含主库的.git目录.

We use py2app extensively at our facility to produce self contained .app packages for easy internal deployment without dependency issues. Something I noticed recently, and have no idea how it began, is that when building an .app, py2app started including the .git directory of our main library.

commonLib是我们的python根库程序包,它是一个git repo.该程序包下面是各种子程序包,例如数据库,实用程序等.

commonLib, for instance, is our root python library package, which is a git repo. Under this package are the various subpackages such as database, utility, etc.

commonLib/
    |- .git/ # because commonLib is a git repo
    |- __init__.py
    |- database/
        |- __init__.py
    |- utility/
        |- __init__.py
    # ... etc

在一个给定的项目中,例如Foo,我们将执行from commonLib import xyz这样的导入来使用我们的通用软件包.通过py2app构建看起来像:python setup.py py2app

In a given project, say Foo, we will do imports like from commonLib import xyz to use our common packages. Building via py2app looks something like: python setup.py py2app

因此,我最近看到的问题是,在为项目Foo构建应用程序时,我将看到它将commonLib/.git/中的所有内容都包含到应用程序中,这实在是一件大事. py2app有一个excludes选项,但这似乎仅适用于python模块.我无法弄清楚要排除.git子目录是什么,或者实际上是什么原因导致它被首先包含在内.

So the recent issue I am seeing is that when building an app for project Foo, I will see it include everything in commonLib/.git/ into the app, which is extra bloat. py2app has an excludes option but that only seems to be for python modules. I cant quite figure out what it would take to exclude the .git subdir, or in fact, what is causing it to be included in the first place.

有人在使用git repo的python包导入时遇到过这种情况吗? 每个项目的setup.py文件中没有任何更改,并且commonLib一直是git repo.因此,我唯一可以想到的是py2app的版本及其dep,它们显然已经随着时间的推移而升级.

Has anyone experienced this when using a python package import that is a git repo? Nothing has changed in our setup.py files for each project, and commonLib has always been a git repo. So the only thing I can think of being a variable is the version of py2app and its deps which have obviously been upgraded over time.

编辑

我目前正在使用最新的py2app 0.6.4.另外,我的setup.py最初是由py2applet生成的,但此后已进行了手动配置,并被复制为每个新项目的模板.我在这些项目中的每个项目上都使用PyQt4/sip,所以这也让我想知道它是否与某个食谱有关?

I'm using the latest py2app 0.6.4 as of right now. Also, my setup.py was first generated from py2applet a while back, but has been hand configured since and copied over as a template for every new project. I am using PyQt4/sip for every single one of these projects, so it also makes me wonder if its an issue with one of the recipes?

从第一个答案开始,我尝试使用exclude_package_data设置的各种组合来解决此问题.似乎没有什么可以迫使.git目录被排除在外.这是我的setup.py文件通常的样例:

From the first answer, I tried to fix this using various combinations of exclude_package_data settings. Nothing seems to force the .git directory to become excluded. Here is a sample of what my setup.py files generally look like:

from setuptools import setup
from myApp import VERSION

appname = 'MyApp'
APP = ['myApp.py']
DATA_FILES = []
OPTIONS = {
    'includes': 'atexit, sip, PyQt4.QtCore, PyQt4.QtGui',
    'strip': True, 
    'iconfile':'ui/myApp.icns', 
    'resources':['src/myApp.png'], 
    'plist':{
        'CFBundleIconFile':'ui/myApp.icns',
        'CFBundleIdentifier':'com.company.myApp',
        'CFBundleGetInfoString': appname,
        'CFBundleVersion' : VERSION,
        'CFBundleShortVersionString' : VERSION
        }
    }

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

我尝试过类似的事情:

setup(
    ...
    exclude_package_data = { 'commonLib': ['.git'] },
    #exclude_package_data = { '': ['.git'] },
    #exclude_package_data = { 'commonLib/.git/': ['*'] },
    #exclude_package_data = { '.git': ['*'] },
    ...
)

更新#2

我已经发布了自己的答案,该答案对distutils进行了猴子补丁.它很丑陋,不是首选,但是直到有人可以为我提供更好的解决方案之前,我想这就是我所拥有的.

Update #2

I have posted my own answer which does a monkeypatch on distutils. Its ugly and not preferred, but until someone can offer me a better solution, I guess this is what I have.

推荐答案

我正在为我自己的问题添加一个答案,以记录到目前为止我发现唯一可以解决的问题.我的方法是在创建目录或复制文件时,对distutils进行monkeypatch忽略某些模式.这确实不是我想要执行的操作,但是就像我说的那样,这是到目前为止唯一有效的方法.

I am adding an answer to my own question, to document the only thing I have found to work thus far. My approach was to monkeypatch distutils to ignore certain patterns when creating a directory or copying a file. This is really not what I wanted to do, but like I said, its the only thing that works so far.

## setup.py ##

import re

# file_util has to come first because dir_util uses it
from distutils import file_util, dir_util

def wrapper(fn):
    def wrapped(src, *args, **kwargs):
        if not re.search(r'/\.git/?', src):
            fn(src, *args, **kwargs) 
    return wrapped       

file_util.copy_file = wrapper(file_util.copy_file)
dir_util.mkpath = wrapper(dir_util.mkpath)

# now import setuptools so it uses the monkeypatched methods
from setuptools import setup

希望有人会对此发表评论,并告诉我可以避免这样做的更高层次的方法.但是到目前为止,我可能会将其包装到实用程序方法中,例如exclude_data_patterns(re_pattern),以便在我的项目中重复使用.

Hopefully someone will comment on this and tell me a higher level approach to avoid doing this. But as of now, I will probably wrap this into a utility method like exclude_data_patterns(re_pattern) to be reused in my projects.

这篇关于py2app在构建期间拾取包的.git子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆