Skip to content

Commit c66679c

Browse files
committed
Fix Pretest Static Analysis found formats
1 parent fffeda1 commit c66679c

File tree

12 files changed

+173
-157
lines changed

12 files changed

+173
-157
lines changed

sonic_installer/main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ def migrate_sonic_packages(bootloader, binary_image_version):
375375
docker_started = True
376376
run_command_or_raise(["cp", packages_path, os.path.join(new_image_mount, TMP_DIR, packages_file)])
377377
run_command_or_raise(["mkdir", "-p", custom_manifests_path])
378-
run_command_or_raise(["cp", "-arf", custom_manifests_path , new_image_package_directory_path])
378+
run_command_or_raise(["cp", "-arf", custom_manifests_path, new_image_package_directory_path])
379379
run_command_or_raise(["touch", os.path.join(new_image_mount, "tmp", DOCKERD_SOCK)])
380380
run_command_or_raise(["mount", "--bind",
381381
os.path.join(VAR_RUN_PATH, DOCKERD_SOCK),

sonic_package_manager/main.py

+20-18
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
import click
1010
import click_log
1111
import tabulate
12-
from urllib.parse import urlparse
13-
import paramiko
14-
import requests
15-
import getpass
1612
from natsort import natsorted
1713

1814
from sonic_package_manager.database import PackageEntry, PackageDatabase
@@ -166,7 +162,7 @@ def repository(ctx):
166162
@click.pass_context
167163
def manifests(ctx):
168164
""" Custom local Manifest management commands. """
169-
165+
170166
pass
171167

172168
@cli.group()
@@ -292,13 +288,12 @@ def changelog(ctx,
292288
exit_cli(f'Failed to print package changelog: {err}', fg='red')
293289

294290

295-
296291
@manifests.command('create')
297292
@click.pass_context
298293
@click.argument('name', type=click.Path())
299294
@click.option('--from-json', type=str, help='specify manifest json file')
300295
@root_privileges_required
301-
def create(ctx, name, from_json):
296+
def create_manifest(ctx, name, from_json):
302297
"""Create a new custom local manifest file."""
303298

304299
manager: PackageManager = ctx.obj
@@ -307,17 +302,14 @@ def create(ctx, name, from_json):
307302
except Exception as e:
308303
click.echo("Error: Manifest {} creation failed - {}".format(name, str(e)))
309304
return
310-
311-
#At the end of sonic-package-manager install, a new manifest file is created with the name.
312-
#At the end of sonic-package-manager uninstall name, this manifest file name and name.edit will be deleted.
313-
#At the end of sonic-package-manager update, we need to mv maniests name.edit to name in case of success, else keep it as such.
314-
#So during sonic-package-manager update, we could take old package from name and new package from edit and at the end, follow 3rd point
305+
306+
315307
@manifests.command('update')
316308
@click.pass_context
317309
@click.argument('name', type=click.Path())
318310
@click.option('--from-json', type=str, required=True)
319311
@root_privileges_required
320-
def update(ctx, name, from_json):
312+
def update_manifest(ctx, name, from_json):
321313
"""Update an existing custom local manifest file with new one."""
322314

323315
manager: PackageManager = ctx.obj
@@ -327,11 +319,12 @@ def update(ctx, name, from_json):
327319
click.echo(f"Error occurred while updating manifest '{name}': {e}")
328320
return
329321

322+
330323
@manifests.command('delete')
331324
@click.pass_context
332325
@click.argument('name', type=click.Path())
333326
@root_privileges_required
334-
def delete(ctx, name):
327+
def delete_manifest(ctx, name):
335328
"""Delete a custom local manifest file."""
336329
manager: PackageManager = ctx.obj
337330
try:
@@ -352,6 +345,7 @@ def show_manifest(ctx, name):
352345
except FileNotFoundError:
353346
click.echo("Manifest file '{}' not found.".format(name))
354347

348+
355349
@manifests.command('list')
356350
@click.pass_context
357351
@root_privileges_required
@@ -421,8 +415,8 @@ def remove(ctx, name):
421415
help='Use locally created custom manifest file. ',
422416
hidden=True)
423417
@click.option('--name',
424-
type=str,
425-
help='custom name for the package')
418+
type=str,
419+
help='custom name for the package')
426420
@add_options(PACKAGE_SOURCE_OPTIONS)
427421
@add_options(PACKAGE_COMMON_OPERATION_OPTIONS)
428422
@add_options(PACKAGE_COMMON_INSTALL_OPTIONS)
@@ -471,13 +465,13 @@ def install(ctx,
471465

472466
if use_local_manifest:
473467
if not name:
474-
click.echo(f'name argument is not provided to use local manifest')
468+
click.echo('name argument is not provided to use local manifest')
475469
return
476470
original_file = os.path.join(MANIFESTS_LOCATION, name)
477471
if not os.path.exists(original_file):
478472
click.echo(f'Local Manifest file for {name} does not exists to install')
479473
return
480-
474+
481475
try:
482476
manager.install(package_expr,
483477
from_repository,
@@ -490,6 +484,14 @@ def install(ctx,
490484
except KeyboardInterrupt:
491485
exit_cli('Operation canceled by user', fg='red')
492486

487+
# At the end of sonic-package-manager install, a new manifest file is created with the name.
488+
# At the end of sonic-package-manager uninstall name,
489+
# this manifest file name and name.edit will be deleted.
490+
# At the end of sonic-package-manager update,
491+
# we need to mv maniests name.edit to name in case of success, else keep it as such.
492+
# So during sonic-package-manager update,
493+
# we could take old package from name and new package from edit and at the end, follow 3rd point
494+
493495

494496
@cli.command()
495497
@add_options(PACKAGE_COMMON_OPERATION_OPTIONS)

sonic_package_manager/manager.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,13 @@
6767
)
6868
import click
6969
import json
70-
import os
7170
import requests
7271
import getpass
7372
import paramiko
7473
import urllib.parse
7574
from scp import SCPClient
7675
from sonic_package_manager.manifest import Manifest, MANIFESTS_LOCATION, DEFAULT_MANIFEST_FILE
77-
LOCAL_JSON="/tmp/local_json"
76+
LOCAL_JSON = "/tmp/local_json"
7877

7978
@contextlib.contextmanager
8079
def failure_ignore(ignore: bool):
@@ -458,9 +457,9 @@ def install_from_source(self,
458457
self.database.commit()
459458

460459
@under_lock
461-
def update(self,
462-
name: str,
463-
**kwargs):
460+
def update(self,
461+
name: str,
462+
**kwargs):
464463
""" Update SONiC Package referenced by name. The update
465464
can be forced if force argument is True.
466465
@@ -481,7 +480,6 @@ def update(self,
481480
click.echo("Package {} is not installed".format(name))
482481
return
483482

484-
485483
def remove_unused_docker_image(self, package):
486484
image_id_used = any(entry.image_id == package.image_id for entry in self.database if entry.name != package.name)
487485
if not image_id_used:
@@ -579,7 +577,7 @@ def upgrade_from_source(self,
579577
Raises:
580578
PackageManagerError
581579
"""
582-
580+
583581
if update_only:
584582
new_package = self.get_installed_package(name, use_edit=True)
585583
else:
@@ -679,10 +677,10 @@ def upgrade_from_source(self,
679677
if not skip_host_plugins:
680678
self._install_cli_plugins(new_package)
681679
exits.callback(rollback(self._uninstall_cli_plugin, new_package))
682-
680+
683681
if old_package.image_id != new_package.image_id:
684682
self.remove_unused_docker_image(old_package)
685-
683+
686684
exits.pop_all()
687685
except Exception as err:
688686
raise PackageUpgradeError(f'Failed to upgrade {new_package.name}: {err}')
@@ -697,7 +695,7 @@ def upgrade_from_source(self,
697695
if update_only:
698696
manifest_path = os.path.join(MANIFESTS_LOCATION, name)
699697
edit_path = os.path.join(MANIFESTS_LOCATION, name + ".edit")
700-
os.rename(edit_path,manifest_path)
698+
os.rename(edit_path, manifest_path)
701699

702700
@under_lock
703701
@opt_check
@@ -820,9 +818,9 @@ def migrate_package(old_package_entry,
820818
new_package.version = old_package.version
821819
migrate_package(old_package, new_package)
822820
else:
823-
#self.install(f'{new_package.name}={new_package_default_version}')
824-
repo_tag_formed="{}:{}".format(new_package.repository, new_package.default_reference)
825-
self.install(None,repo_tag_formed,name=new_package.name)
821+
# self.install(f'{new_package.name}={new_package_default_version}')
822+
repo_tag_formed = "{}:{}".format(new_package.repository, new_package.default_reference)
823+
self.install(None, repo_tag_formed, name=new_package.name)
826824
else:
827825
# No default version and package is not installed.
828826
# Migrate old package same version.
@@ -1108,12 +1106,12 @@ def download_file(self, url, local_path):
11081106
remote_path = parsed_url.path
11091107
supported_protocols = ['http', 'https', 'scp', 'sftp']
11101108

1111-
#clear the temporary local file
1109+
# clear the temporary local file
11121110
if os.path.exists(local_path):
11131111
os.remove(local_path)
11141112

11151113
if not protocol:
1116-
#check for local file
1114+
# check for local file
11171115
if os.path.exists(url):
11181116
os.rename(url, local_path)
11191117
return True
@@ -1191,7 +1189,6 @@ def create_package_manifest(self, name, from_json):
11911189
click.echo("Error: Manifest file '{}' already exists.".format(name))
11921190
return
11931191

1194-
11951192
if from_json:
11961193
ret = self.download_file(from_json, LOCAL_JSON)
11971194
if ret is False:
@@ -1202,18 +1199,17 @@ def create_package_manifest(self, name, from_json):
12021199
data = {}
12031200
with open(from_json, 'r') as file:
12041201
data = json.load(file)
1205-
#Validate with manifest scheme
1202+
# Validate with manifest scheme
12061203
Manifest.marshal(data)
12071204

1208-
#Make sure the 'name' is overwritten into the dict
1205+
# Make sure the 'name' is overwritten into the dict
12091206
data['package']['name'] = name
12101207
data['service']['name'] = name
12111208

12121209
with open(mfile_name, 'w') as file:
12131210
json.dump(data, file, indent=4)
12141211
click.echo(f"Manifest '{name}' created successfully.")
12151212

1216-
12171213
def update_package_manifest(self, name, from_json):
12181214
if name == "default_manifest":
12191215
click.echo("Default Manifest updation is not allowed")
@@ -1223,7 +1219,7 @@ def update_package_manifest(self, name, from_json):
12231219
if not os.path.exists(original_file):
12241220
click.echo(f'Local Manifest file for {name} does not exists to update')
12251221
return
1226-
#download json file from remote/local path
1222+
# download json file from remote/local path
12271223
ret = self.download_file(from_json, LOCAL_JSON)
12281224
if ret is False:
12291225
return
@@ -1232,10 +1228,10 @@ def update_package_manifest(self, name, from_json):
12321228
with open(from_json, 'r') as file:
12331229
data = json.load(file)
12341230

1235-
#Validate with manifest scheme
1231+
# Validate with manifest scheme
12361232
Manifest.marshal(data)
12371233

1238-
#Make sure the 'name' is overwritten into the dict
1234+
# Make sure the 'name' is overwritten into the dict
12391235
data['package']['name'] = name
12401236
data['service']['name'] = name
12411237

@@ -1246,8 +1242,8 @@ def update_package_manifest(self, name, from_json):
12461242
json.dump(data, edit_file, indent=4)
12471243
click.echo(f"Manifest '{name}' updated successfully.")
12481244
else:
1249-
#If package is not installed,
1250-
## update the name file directly
1245+
# If package is not installed,
1246+
# update the name file directly
12511247
with open(original_file, 'w') as orig_file:
12521248
json.dump(data, orig_file, indent=4)
12531249
click.echo(f"Manifest '{name}' updated successfully.")

sonic_package_manager/manifest.py

-1
Original file line numberDiff line numberDiff line change
@@ -289,4 +289,3 @@ def get_manifest_from_local_file(name):
289289
}
290290
}
291291
return desired_dict
292-

sonic_package_manager/metadata.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import json
66
import tarfile
77
from typing import Dict, List, Optional
8-
import os
98
from sonic_package_manager import utils
109
from sonic_package_manager.errors import MetadataError
1110
from sonic_package_manager.logger import log
@@ -64,7 +63,8 @@ def __init__(self, docker, registry_resolver):
6463
self.docker = docker
6564
self.registry_resolver = registry_resolver
6665

67-
def from_local(self, image: str, use_local_manifest: bool = False, name: Optional[str] = None, use_edit: bool = False) -> Metadata:
66+
def from_local(self, image: str, use_local_manifest: bool = False,
67+
name: Optional[str] = None, use_edit: bool = False) -> Metadata:
6868
""" Reads manifest from locally installed docker image.
6969
7070
Args:
@@ -88,7 +88,7 @@ def from_local(self, image: str, use_local_manifest: bool = False, name: Optiona
8888
if name:
8989
labels = Manifest.get_manifest_from_local_file(name)
9090
if labels is None:
91-
raise MetadataError('No manifest found in image labels')
91+
raise MetadataError('No manifest found in image labels')
9292
else:
9393
raise MetadataError('No manifest found in image labels')
9494

@@ -125,7 +125,7 @@ def from_registry(self,
125125
raise MetadataError('The name(custom) option is required as there is no metadata found in image labels')
126126
labels = Manifest.get_manifest_from_local_file(name)
127127
if labels is None:
128-
raise MetadataError('No manifest found in image labels')
128+
raise MetadataError('No manifest found in image labels')
129129
return self.from_labels(labels)
130130

131131
def from_tarball(self, image_path: str, use_local_manifest: bool = False, name: Optional[str] = None) -> Metadata:
@@ -149,10 +149,11 @@ def from_tarball(self, image_path: str, use_local_manifest: bool = False, name:
149149
labels = image_config['config'].get('Labels')
150150
if labels is None or len(labels) == 0 or 'com.azure.sonic.manifest' not in labels:
151151
if name is None:
152-
raise MetadataError('The name(custom) option is required as there is no metadata found in image labels')
152+
raise MetadataError('The name(custom) option is \
153+
required as there is no metadata found in image labels')
153154
labels = Manifest.get_manifest_from_local_file(name)
154155
if labels is None:
155-
raise MetadataError('No manifest found in image labels')
156+
raise MetadataError('No manifest found in image labels')
156157
return self.from_labels(labels)
157158

158159
@classmethod
@@ -202,5 +203,5 @@ def from_labels(cls, labels: Dict[str, str]) -> Metadata:
202203
log.debug(f"Found YANG modules: {labels_yang_modules.keys()}")
203204
else:
204205
log.debug("No YANG modules found")
205-
206+
206207
return Metadata(Manifest.marshal(manifest_dict), components, yang_modules)

sonic_package_manager/source.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ def __init__(self,
117117

118118
def get_metadata(self) -> Metadata:
119119
""" Returns manifest read from tarball. """
120-
return self.metadata_resolver.from_tarball(self.tarball_path, use_local_manifest=self.use_local_manifest, name=self.name)
120+
return self.metadata_resolver.from_tarball(self.tarball_path,
121+
use_local_manifest=self.use_local_manifest,
122+
name=self.name)
121123

122124
def install_image(self, package: Package):
123125
""" Installs image from local tarball source. """

0 commit comments

Comments
 (0)