Skip to content

Instantly share code, notes, and snippets.

@cscutcher
Forked from wassname/1.generate_dockerignore.py
Last active May 17, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cscutcher/6910637fbe1d07a6c3f5c8597ff4bda2 to your computer and use it in GitHub Desktop.
Save cscutcher/6910637fbe1d07a6c3f5c8597ff4bda2 to your computer and use it in GitHub Desktop.
Convert .gitignore to .dockerignore: quick and dirty.
"""
Convert .gitignore to .dockerignore: quick and dirty.
This is a quick and dirty script to convert this:
`__pycache__/`
Into this:
```
__pycache__
*/__pycache__
*/*/__pycache__
*/*/*/__pycache__
```
Specifically [gitignores](https://git-scm.com/docs/gitignore) use a mixture of
fname, and shell glob matching with lots of rules and the dockeringore
files use simple
[golang matching](https://golang.org/pkg/path/filepath/#Match).
Forked from https://gist.github.com/wassname/b25471b0f3bb2f9ff81f
"""
def add_subdirs(pattern, max_depth=4):
"""
Replace start of glob with multiple levels
of path seperators.
*.pyc becomes *.pyc */*.pyc */*/*.pyc etc
"""
for i in range(max_depth):
yield pattern
pattern = '*/%s' % pattern
# and for a seperator line
yield ""
def generate_dockerignore(
gitignore, max_depth=5, prefix_lines=[], comment=True):
"""Pass in array of gitignore lines"""
di = [] # docker ignore lines
# add comment and extra lines
if comment:
prefix_lines = [
'# converted by generate_dockerignore.py\n'] + prefix_lines
for line in prefix_lines+gitignore:
# these slashes are ignored by dockerignore
line = line.rstrip('/')
# ingore whitespace and comments
if line.strip() == '':
di.append(line)
continue
elif line.startswith('#'):
di.append(line)
continue
# deal with **
elif '**' in line:
# replace wildcard glob **
# with multiple levels of seperators
# replace ** with *, */*, */*/*, etc
line_tmpl = line.replace('**', '{}')
replacement = '*'
for i in range(max_depth):
pattern = line_tmpl.format(replacement)
replacement = '*/' + replacement
gitignore = open('.gitignore').readlines()
# now put * */, */*, */*/* on the beginning to match subdirs
if line_tmpl.startswith('/'):
di += list(add_subdirs(pattern))
else:
di.append(pattern)
# deal with lines that don't start with a slash
elif not line.startswith('/'):
# shell glob
di += list(add_subdirs(line))
return di
if __name__ == "__main__":
git_ignore = open('.gitignore', 'r').readlines()
docker_ignore = generate_dockerignore(git_ignore)
with open('.dockerignore', 'w') as output:
for l in docker_ignore:
output.write(l)
# converted by generate_dockerignore.py
*.log
*/*.log
*/*/*.log
*/*/*/*.log
*.conf
*/*.conf
*/*/*.conf
*/*/*/*.conf
# Django specific
migrations/
*/migrations/
*/*/migrations/
*/*/*/migrations/
*.db
*/*.db
*/*/*.db
*/*/*/*.db
# From the excellent;
# https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*/__pycache__/
*/*/__pycache__/
*/*/*/__pycache__/
*.py[cod]
*/*.py[cod]
*/*/*.py[cod]
*/*/*/*.py[cod]
*$py.class
*/*$py.class
*/*/*$py.class
*/*/*/*$py.class
# C extensions
*.so
*/*.so
*/*/*.so
*/*/*/*.so
# Distribution / packaging
.Python
*/.Python
*/*/.Python
*/*/*/.Python
build/
*/build/
*/*/build/
*/*/*/build/
develop-eggs/
*/develop-eggs/
*/*/develop-eggs/
*/*/*/develop-eggs/
dist/
*/dist/
*/*/dist/
*/*/*/dist/
downloads/
*/downloads/
*/*/downloads/
*/*/*/downloads/
eggs/
*/eggs/
*/*/eggs/
*/*/*/eggs/
.eggs/
*/.eggs/
*/*/.eggs/
*/*/*/.eggs/
lib/
*/lib/
*/*/lib/
*/*/*/lib/
lib64/
*/lib64/
*/*/lib64/
*/*/*/lib64/
parts/
*/parts/
*/*/parts/
*/*/*/parts/
sdist/
*/sdist/
*/*/sdist/
*/*/*/sdist/
var/
*/var/
*/*/var/
*/*/*/var/
wheels/
*/wheels/
*/*/wheels/
*/*/*/wheels/
*.egg-info/
*/*.egg-info/
*/*/*.egg-info/
*/*/*/*.egg-info/
.installed.cfg
*/.installed.cfg
*/*/.installed.cfg
*/*/*/.installed.cfg
*.egg
*/*.egg
*/*/*.egg
*/*/*/*.egg
MANIFEST
*/MANIFEST
*/*/MANIFEST
*/*/*/MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*/*.manifest
*/*/*.manifest
*/*/*/*.manifest
*.spec
*/*.spec
*/*/*.spec
*/*/*/*.spec
# Installer logs
pip-log.txt
*/pip-log.txt
*/*/pip-log.txt
*/*/*/pip-log.txt
pip-delete-this-directory.txt
*/pip-delete-this-directory.txt
*/*/pip-delete-this-directory.txt
*/*/*/pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
*/htmlcov/
*/*/htmlcov/
*/*/*/htmlcov/
.tox/
*/.tox/
*/*/.tox/
*/*/*/.tox/
.coverage
*/.coverage
*/*/.coverage
*/*/*/.coverage
.coverage.*
*/.coverage.*
*/*/.coverage.*
*/*/*/.coverage.*
.cache
*/.cache
*/*/.cache
*/*/*/.cache
nosetests.xml
*/nosetests.xml
*/*/nosetests.xml
*/*/*/nosetests.xml
coverage.xml
*/coverage.xml
*/*/coverage.xml
*/*/*/coverage.xml
*.cover
*/*.cover
*/*/*.cover
*/*/*/*.cover
.hypothesis/
*/.hypothesis/
*/*/.hypothesis/
*/*/*/.hypothesis/
.pytest_cache/
*/.pytest_cache/
*/*/.pytest_cache/
*/*/*/.pytest_cache/
# Translations
*.mo
*/*.mo
*/*/*.mo
*/*/*/*.mo
*.pot
*/*.pot
*/*/*.pot
*/*/*/*.pot
# Django stuff:
*.log
*/*.log
*/*/*.log
*/*/*/*.log
local_settings.py
*/local_settings.py
*/*/local_settings.py
*/*/*/local_settings.py
db.sqlite3
*/db.sqlite3
*/*/db.sqlite3
*/*/*/db.sqlite3
# Flask stuff:
instance/
*/instance/
*/*/instance/
*/*/*/instance/
.webassets-cache
*/.webassets-cache
*/*/.webassets-cache
*/*/*/.webassets-cache
# Scrapy stuff:
.scrapy
*/.scrapy
*/*/.scrapy
*/*/*/.scrapy
# Sphinx documentation
docs/_build/
*/docs/_build/
*/*/docs/_build/
*/*/*/docs/_build/
# PyBuilder
target/
*/target/
*/*/target/
*/*/*/target/
# Jupyter Notebook
.ipynb_checkpoints
*/.ipynb_checkpoints
*/*/.ipynb_checkpoints
*/*/*/.ipynb_checkpoints
# pyenv
.python-version
*/.python-version
*/*/.python-version
*/*/*/.python-version
# celery beat schedule file
celerybeat-schedule
*/celerybeat-schedule
*/*/celerybeat-schedule
*/*/*/celerybeat-schedule
# SageMath parsed files
*.sage.py
*/*.sage.py
*/*/*.sage.py
*/*/*/*.sage.py
# Environments
.env
*/.env
*/*/.env
*/*/*/.env
.venv
*/.venv
*/*/.venv
*/*/*/.venv
env/
*/env/
*/*/env/
*/*/*/env/
venv/
*/venv/
*/*/venv/
*/*/*/venv/
ENV/
*/ENV/
*/*/ENV/
*/*/*/ENV/
env.bak/
*/env.bak/
*/*/env.bak/
*/*/*/env.bak/
venv.bak/
*/venv.bak/
*/*/venv.bak/
*/*/*/venv.bak/
# Spyder project settings
.spyderproject
*/.spyderproject
*/*/.spyderproject
*/*/*/.spyderproject
.spyproject
*/.spyproject
*/*/.spyproject
*/*/*/.spyproject
# Rope project settings
.ropeproject
*/.ropeproject
*/*/.ropeproject
*/*/*/.ropeproject
# mkdocs documentation
# mypy
.mypy_cache/
*/.mypy_cache/
*/*/.mypy_cache/
*/*/*/.mypy_cache/
*.log
*.conf
# Django specific
migrations/
*.db
# From the excellent;
# https://raw.githubusercontent.com/github/gitignore/master/Python.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
target/
# Jupyter Notebook
.ipynb_checkpoints
# pyenv
.python-version
# celery beat schedule file
celerybeat-schedule
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment