Skip to content

Commit 0682e74

Browse files
Lenrys29ylegal
authored andcommitted
Add output feature
Author: lenrys29 <lenrys29@gmail.com>
1 parent 9a0f815 commit 0682e74

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

docker_pull.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
urllib3.disable_warnings()
1313

1414
# Graphical needs for drawing full line
15-
console_rows, console_columns = os.popen('stty size', 'r').read().split()
15+
try:
16+
console_rows, console_columns = os.popen('stty size', 'r').read().split()
17+
except:
18+
console_rows, console_columns = 20 , 20
1619

1720
############# DEFAULTs VAR
1821

@@ -23,6 +26,7 @@
2326

2427
username = ""
2528
password = ""
29+
output_path = "."
2630

2731
json_manifest_type='application/vnd.docker.distribution.manifest.v2+json'
2832
json_manifest_type_bis='application/vnd.docker.distribution.manifest.list.v2+json'
@@ -101,7 +105,10 @@ def progress_bar(ublob, nb_traits):
101105
############## Check if args < 2
102106

103107
if len(sys.argv) < 2 :
104-
print('Usage:\n\tdocker_pull.py [registry/][repository/]image[:tag|@digest] [username] [password]\n')
108+
print ('Usage:')
109+
print ('\t docker_pull.py [registry/][repository/]image[:tag|@digest] ')
110+
print ('\t docker_pull.py [registry/][repository/]image[:tag|@digest] output_path')
111+
print ('\t docker_pull.py [registry/][repository/]image[:tag|@digest] username password output_path\n')
105112
exit(1)
106113

107114
############## Get info from arg
@@ -113,6 +120,12 @@ def progress_bar(ublob, nb_traits):
113120
username = sys.argv[2]
114121
password = sys.argv[3]
115122

123+
if len(sys.argv) == 3:
124+
output_path = sys.argv[2]
125+
126+
if len(sys.argv) == 5:
127+
output_path = sys.argv[4]
128+
116129
############## Get repository url + registry url for auth
117130

118131
if len(imgparts) > 1 and ('.' in imgparts[0] or ':' in imgparts[0]):
@@ -183,7 +196,7 @@ def progress_bar(ublob, nb_traits):
183196
layers = resp.json()['layers']
184197

185198
# Create tmp folder that will hold the image
186-
imgdir = '/tmp/tmp_{}_{}'.format(img.replace('/', '.'), tag)
199+
imgdir = output_path + '/tmp_{}'.format(sys.argv[1].replace('/', '.').replace(':','@'))
187200

188201
if os.path.exists(imgdir):
189202
shutil.rmtree(imgdir)
@@ -305,7 +318,7 @@ def progress_bar(ublob, nb_traits):
305318
file.close()
306319

307320
# Create image tar and clean tmp folder
308-
docker_tar = repository.replace('/', '_') + '_' + img.replace('/', '_') + '.tar'
321+
docker_tar = output_path + "/" + sys.argv[1].replace('/', '_').replace(':','@') + '.tar'
309322
sys.stdout.write("Creating archive...")
310323
sys.stdout.flush()
311324
tar = tarfile.open(docker_tar, "w")

0 commit comments

Comments
 (0)