Skip to content

Commit 210cdc8

Browse files
committed
run sh without provide dst-path; add build_time_prefix;
1 parent a47dcf9 commit 210cdc8

File tree

2 files changed

+89
-7
lines changed

2 files changed

+89
-7
lines changed

pydocker.py

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ class DockerFile(pydocker.DockerFile):
7575
v1.0.5 Sat Jun 15 11:21:02 UTC 2019 jen
7676
- add error trace for RUN_bash_script
7777
78+
v1.0.6 Mon Jan 3 22:35:21 UTC 2022 jen
79+
- add DockerFileV105a
80+
- allow run sh script without provide dst-path
81+
- unique tag - useful for development new dockerfile
82+
83+
- add generate_img_name
84+
7885
7986
--------------------------------------------------------------------------------
8087
contributors:
@@ -105,6 +112,8 @@ class DockerFile(pydocker.DockerFile):
105112
import re
106113
import json
107114
import subprocess
115+
import hashlib
116+
import datetime
108117

109118
import logging
110119

@@ -114,7 +123,7 @@ class DockerFile(pydocker.DockerFile):
114123

115124

116125
# ############################################################################ #
117-
class DockerFile(object):
126+
class DockerFileV105(object):
118127
# https://docs.docker.com/engine/reference/builder/
119128

120129
FROM, LABEL, COPY, RUN, WORKDIR, ENV, SHELL, EXPOSE, ENTRYPOINT, CMD, \
@@ -149,7 +158,7 @@ def get_img_name(self):
149158

150159
def __setattr__(self, key, value):
151160
if key.startswith('_'):
152-
return super(DockerFile, self).__setattr__(key, value)
161+
return super(DockerFileV105, self).__setattr__(key, value)
153162
#
154163
if not isinstance(value, str):
155164
value = json.dumps(value)
@@ -298,6 +307,79 @@ def build_img(self, remove_out_files=True):
298307

299308
# ############################################################################ #
300309

310+
class DockerFileV106(DockerFileV105):
311+
"""
312+
add features:
313+
- allow run sh script without provide dst-path
314+
- unique tag - useful for development new dockerfile
315+
316+
- add generate_img_name
317+
"""
318+
def __init__(self, base_img, name, use_build_time_prefix=False):
319+
super(DockerFileV106, self).__init__(base_img, name)
320+
self._use_build_time_prefix = use_build_time_prefix
321+
322+
def RUN_bash_script(self, dst_path: str, content=None, keep_file=False):
323+
if content is None:
324+
# feature - allow run sh script without provide dst-path
325+
assert keep_file is False
326+
content = dst_path
327+
_name_sold = hashlib.sha256(
328+
content.encode()
329+
).hexdigest()[:24]
330+
_counter = getattr(self, '_counter', 0)
331+
_counter += 1
332+
setattr(self, '_counter', _counter)
333+
dst_path = f'/opt/random_{_counter:02d}_{_name_sold}.sh'
334+
#
335+
return super(DockerFileV106, self).RUN_bash_script(dst_path, content, keep_file)
336+
337+
def get_img_name(self):
338+
_img_name = getattr(self, '_img_name', None)
339+
if _img_name is None:
340+
# use cashing for ime_prefix feature
341+
_img_name = self.generate_img_name()
342+
setattr(self, '_img_name', _img_name)
343+
#
344+
return _img_name
345+
346+
def generate_img_name(self):
347+
if self._use_build_time_prefix is True:
348+
# feature - unique tag - useful for development new dockerfile
349+
# need to check if docker-file has new changes
350+
_build_time_id = f'{datetime.datetime.utcnow():%y%m%d%H%M%S}'
351+
return f'{self._namespace}/{self._name}' \
352+
f':' \
353+
f'{self._version}-build-{_build_time_id}'
354+
#
355+
return super(DockerFileV106, self).get_img_name()
356+
357+
# ############################################################################ #
358+
359+
360+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
361+
DockerFileLatestStable = DockerFileV105 # stable using for long time
362+
DockerFileLatestAlpha = DockerFileV106 # newest version for more features
363+
364+
DockerFile = DockerFileLatestStable # by default - stable
365+
366+
# it's allow you to use more appropriate release in your code
367+
# you can user full compatibility version DockerFileLatestStable
368+
# or maximum feature DockerFileLatestAlpha
369+
# or take direct DockerFileV106 for fixation any api changes in feature
370+
371+
372+
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
373+
# aliases - using naming Dockerfile instead DockerFile
374+
DockerfileV105 = DockerFileV105
375+
DockerfileV106 = DockerFileV106
376+
377+
DockerfileLatestStable = DockerFileLatestStable
378+
DockerfileLatestAlpha = DockerFileLatestAlpha
379+
Dockerfile = DockerFile
380+
381+
382+
383+
301384

302-
Dockerfile = DockerFile # alias
303385

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
# https://test.pypi.org/project/pydocker
3434
# pip install --no-cache-dir -U -i https://test.pypi.org/pypi pydocker
3535
36-
# git tag -a v1.0.5 -m 'version 1.0.5'
36+
# git tag -a v1.0.6 -m 'version 1.0.6'
3737
# git push origin --tags
3838
3939
# ------------------------------------------------------------------------------
@@ -43,7 +43,7 @@
4343
python3.4 setup.py sdist # EGG
4444
python3.4 -m twine upload dist/* -r pypi
4545
46-
# pip install --no-cache-dir -U pydocker==1.0.5
46+
# pip install --no-cache-dir -U pydocker==1.0.6
4747
# ls -lah /usr/local/lib/python2.7/dist-packages | grep pydocker
4848
4949
# ------------------------------------------------------------------------------
@@ -59,7 +59,7 @@
5959

6060
setup(
6161
name='pydocker',
62-
version='1.0.5',
62+
version='1.0.6',
6363

6464
description='Easy generator Dockerfile for humans.',
6565
long_description=long_description,
@@ -74,7 +74,7 @@
7474
maintainer_email='[email protected]',
7575

7676
platforms=['any', ],
77-
download_url='https://github.com/jen-soft/pydocker/archive/v1.0.5.zip',
77+
download_url='https://github.com/jen-soft/pydocker/archive/v1.0.6.zip',
7878

7979
# packages=['pydocker', ],
8080
py_modules=['pydocker', ],

0 commit comments

Comments
 (0)