From 1c76cd0c0f6f5ed506f21e2fb6684c5f0276ab2c Mon Sep 17 00:00:00 2001 From: Yuming Zhu Date: Fri, 22 Jun 2018 13:42:47 +0800 Subject: [PATCH] cli: rename _unique_path to unique_path, and deprecate the old one --- cli/koji_cli/commands.py | 18 +++++------ cli/koji_cli/lib.py | 8 ++++- docs/source/writing_koji_code.rst | 2 +- tests/test_cli/test_build.py | 32 +++++++++---------- tests/test_cli/test_dist_repo.py | 2 +- tests/test_cli/test_image_build.py | 2 +- .../test_cli/test_image_build_indirection.py | 2 +- tests/test_cli/test_import.py | 8 ++--- tests/test_cli/test_import_cg.py | 2 +- tests/test_cli/test_spin_commands.py | 2 +- tests/test_cli/test_unique_path.py | 8 ++--- 11 files changed, 46 insertions(+), 40 deletions(-) diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index cdd4e9f3..77bc3421 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -39,7 +39,7 @@ from koji_cli.lib import _, OptionParser, activate_session, parse_arches, \ _unique_path, _running_in_bg, _progress_callback, watch_tasks, \ arg_filter, linked_upload, list_task_output_all_volumes, \ print_task_headers, print_task_recurse, download_file, watch_logs, \ - error, greetings, _list_tasks + error, greetings, _list_tasks, unique_path def _printable_unicode(s): @@ -485,7 +485,7 @@ def handle_build(options, session, args): #treat source as an srpm and upload it if not build_opts.quiet: print("Uploading srpm: %s" % source) - serverdir = _unique_path('cli-build') + serverdir = unique_path('cli-build') if _running_in_bg() or build_opts.noprogress or build_opts.quiet: callback = None else: @@ -1179,7 +1179,7 @@ def handle_import(goptions, session, args): if options.test: print(_("Test mode -- skipping import for %s") % path) return - serverdir = _unique_path('cli-import') + serverdir = unique_path('cli-import') if options.link: linked_upload(path, serverdir) else: @@ -1299,7 +1299,7 @@ def handle_import_cg(goptions, session, args): # get upload path # XXX - need a better way - serverdir = _unique_path('cli-import') + serverdir = unique_path('cli-import') for localpath, info in to_upload: relpath = os.path.join(serverdir, info.get('relpath', '')) @@ -2159,7 +2159,7 @@ def handle_import_archive(options, session, args): for filepath in args[1:]: filename = os.path.basename(filepath) print("Uploading archive: %s" % filename) - serverdir = _unique_path('cli-import') + serverdir = unique_path('cli-import') if _running_in_bg() or suboptions.noprogress: callback = None else: @@ -5594,7 +5594,7 @@ def _build_image_indirection(options, task_opts, session, args): # only scratch builds can omit indirection_template_url raise koji.GenericError(_("Non-scratch builds must provide a URL for the indirection template")) templatefile = task_opts.indirection_template - serverdir = _unique_path('cli-image-indirection') + serverdir = unique_path('cli-image-indirection') session.uploadWrapper(templatefile, serverdir, callback=callback) task_opts.indirection_template = os.path.join('work', serverdir, os.path.basename(templatefile)) @@ -5791,7 +5791,7 @@ def _build_image(options, task_opts, session, args, img_type): ksfile = args[4] if not task_opts.ksurl: - serverdir = _unique_path('cli-' + img_type) + serverdir = unique_path('cli-' + img_type) session.uploadWrapper(ksfile, serverdir, callback=callback) ksfile = os.path.join(serverdir, os.path.basename(ksfile)) print('') @@ -5867,7 +5867,7 @@ def _build_image_oz(options, task_opts, session, args): # only scratch builds can omit ksurl raise koji.GenericError(_("Non-scratch builds must provide ksurl")) ksfile = task_opts.kickstart - serverdir = _unique_path('cli-image') + serverdir = unique_path('cli-image') session.uploadWrapper(ksfile, serverdir, callback=callback) task_opts.kickstart = os.path.join('work', serverdir, os.path.basename(ksfile)) @@ -6975,7 +6975,7 @@ def handle_dist_repo(options, session, args): parser.error(_('allow_missing_signatures and skip_missing_signatures ' 'are mutually exclusive')) activate_session(session, options) - stuffdir = _unique_path('cli-dist-repo') + stuffdir = unique_path('cli-dist-repo') if task_opts.comps: if not os.path.exists(task_opts.comps): parser.error(_('could not find %s') % task_opts.comps) diff --git a/cli/koji_cli/lib.py b/cli/koji_cli/lib.py index 341cecdc..0bc86b0d 100644 --- a/cli/koji_cli/lib.py +++ b/cli/koji_cli/lib.py @@ -402,7 +402,7 @@ def list_task_output_all_volumes(session, task_id): return dict([fn, ['DEFAULT']] for fn in output) -def _unique_path(prefix): +def unique_path(prefix): """Create a unique path fragment by appending a path component to prefix. The path component will consist of a string of letter and numbers that is unlikely to be a duplicate, but is not guaranteed to be unique.""" @@ -414,6 +414,12 @@ def _unique_path(prefix): ''.join([random.choice(string.ascii_letters) for i in range(8)])) +def _unique_path(prefix): + koji.util.deprecated('_unique_path is deprecated, use unique_path instead.' + ' See: https://pagure.io/koji/issue/975') + return unique_path(prefix) + + def _format_size(size): if (size / 1073741824 >= 1): return "%0.2f GiB" % (size / 1073741824.0) diff --git a/docs/source/writing_koji_code.rst b/docs/source/writing_koji_code.rst index f3e4ed60..27ea8e60 100644 --- a/docs/source/writing_koji_code.rst +++ b/docs/source/writing_koji_code.rst @@ -146,7 +146,7 @@ You can create progress bars as necessary with this snippet: callback = None else: callback = _progress_callback - serverdir = _unique_path('cli-image') # create a unique path on the hub + serverdir = unique_path('cli-image') # create a unique path on the hub session.uploadWrapper(somefile, serverdir, callback=callback) Task Arguments diff --git a/tests/test_cli/test_build.py b/tests/test_cli/test_build.py index e4a601c7..86392f77 100644 --- a/tests/test_cli/test_build.py +++ b/tests/test_cli/test_build.py @@ -25,7 +25,7 @@ class TestBuild(unittest.TestCase): @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_from_srpm( @@ -77,7 +77,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_from_scm( @@ -127,7 +127,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_no_arg( @@ -173,7 +173,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_help( @@ -232,7 +232,7 @@ Options: @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_arch_override_denied( @@ -280,7 +280,7 @@ Options: @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_none_tag( @@ -326,7 +326,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_target_not_found( @@ -373,7 +373,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_dest_tag_not_found( @@ -424,7 +424,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stderr', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_dest_tag_locked( @@ -475,7 +475,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_arch_override( @@ -531,7 +531,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_background( @@ -580,7 +580,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=True) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_running_in_bg( @@ -631,7 +631,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_noprogress( @@ -684,7 +684,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_quiet( @@ -734,7 +734,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_wait( @@ -788,7 +788,7 @@ Task info: weburl/taskinfo?taskID=1 @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands.activate_session') - @mock.patch('koji_cli.commands._unique_path', return_value='random_path') + @mock.patch('koji_cli.commands.unique_path', return_value='random_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.watch_tasks', return_value=0) def test_handle_build_nowait( diff --git a/tests/test_cli/test_dist_repo.py b/tests/test_cli/test_dist_repo.py index 0f3fcd3a..0ef6d553 100644 --- a/tests/test_cli/test_dist_repo.py +++ b/tests/test_cli/test_dist_repo.py @@ -53,7 +53,7 @@ class TestDistRepo(utils.CliTestCase): self.setUpMocks() def setUpMocks(self): - self.unique_path = mock.patch('koji_cli.commands._unique_path').start() + self.unique_path = mock.patch('koji_cli.commands.unique_path').start() self.unique_path.return_value = '/path/to/cli-dist-repo' self.activate_session = mock.patch('koji_cli.commands.activate_session').start() diff --git a/tests/test_cli/test_image_build.py b/tests/test_cli/test_image_build.py index 38a69b5a..15f240ad 100644 --- a/tests/test_cli/test_image_build.py +++ b/tests/test_cli/test_image_build.py @@ -113,7 +113,7 @@ class TestBuildImageOz(utils.CliTestCase): # mocks self.activate_session = mock.patch('koji_cli.commands.activate_session').start() self.watch_tasks = mock.patch('koji_cli.commands.watch_tasks').start() - self.unique_path = mock.patch('koji_cli.commands._unique_path').start() + self.unique_path = mock.patch('koji_cli.commands.unique_path').start() self.unique_path.return_value = '/path/to/cli-image' self.running_in_bg = mock.patch('koji_cli.commands._running_in_bg').start() self.running_in_bg.return_value = False diff --git a/tests/test_cli/test_image_build_indirection.py b/tests/test_cli/test_image_build_indirection.py index eb4c8536..429b3b06 100644 --- a/tests/test_cli/test_image_build_indirection.py +++ b/tests/test_cli/test_image_build_indirection.py @@ -53,7 +53,7 @@ class TestBuildImageIndirection(utils.CliTestCase): self.options.weburl = self.weburl self.session = mock.MagicMock() self.activate_session = mock.patch('koji_cli.commands.activate_session').start() - self.unique_path = mock.patch('koji_cli.commands._unique_path').start() + self.unique_path = mock.patch('koji_cli.commands.unique_path').start() self.task_opts = Options(TASK_OPTIONS) diff --git a/tests/test_cli/test_import.py b/tests/test_cli/test_import.py index 24bce3a6..d1984179 100644 --- a/tests/test_cli/test_import.py +++ b/tests/test_cli/test_import.py @@ -95,7 +95,7 @@ class TestImport(utils.CliTestCase): upload_rpm_mock = kwargs.get('upload_rpm_mock', session.uploadWrapper) with mock.patch('koji.get_header_fields') as get_header_fields_mock: - with mock.patch('koji_cli.commands._unique_path') as unique_path_mock: + with mock.patch('koji_cli.commands.unique_path') as unique_path_mock: with mock.patch('koji_cli.commands.activate_session') as activate_session_mock: with mock.patch('sys.stdout', new_callable=six.StringIO) as stdout: with upload_rpm_mock: @@ -475,7 +475,7 @@ class TestImport(utils.CliTestCase): @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji.get_header_fields') - @mock.patch('koji_cli.commands._unique_path') + @mock.patch('koji_cli.commands.unique_path') @mock.patch('koji_cli.commands.activate_session') def test_handle_import_with_test_option( self, @@ -524,7 +524,7 @@ class TestImport(utils.CliTestCase): expected = "Test mode -- would have created empty build: %s\n" % nvr() expected += "Test mode -- skipping import for %s\n" % arguments[0] - with mock.patch('koji_cli.commands._unique_path') as unique_path_mock: + with mock.patch('koji_cli.commands.unique_path') as unique_path_mock: handle_import(options, session, arguments) self.assert_console_message(stdout, expected) unique_path_mock.assert_not_called() @@ -533,7 +533,7 @@ class TestImport(utils.CliTestCase): @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji.get_header_fields') - @mock.patch('koji_cli.commands._unique_path') + @mock.patch('koji_cli.commands.unique_path') @mock.patch('koji_cli.commands.activate_session') def test_handle_import_with_epoch_option( self, diff --git a/tests/test_cli/test_import_cg.py b/tests/test_cli/test_import_cg.py index 328e5311..10083716 100644 --- a/tests/test_cli/test_import_cg.py +++ b/tests/test_cli/test_import_cg.py @@ -31,7 +31,7 @@ class TestImportCG(utils.CliTestCase): @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji_cli.commands._progress_callback') - @mock.patch('koji_cli.commands._unique_path') + @mock.patch('koji_cli.commands.unique_path') @mock.patch('koji_cli.commands._running_in_bg', return_value=False) @mock.patch('koji_cli.commands.linked_upload') @mock.patch('koji_cli.commands.activate_session') diff --git a/tests/test_cli/test_spin_commands.py b/tests/test_cli/test_spin_commands.py index e8d88478..65786884 100644 --- a/tests/test_cli/test_spin_commands.py +++ b/tests/test_cli/test_spin_commands.py @@ -77,7 +77,7 @@ class TestBuildImage(utils.CliTestCase): self.session = mock.MagicMock() self.arguments = ['test-image', '1', 'target', 'x86_64', 'image.ks'] self.activate_session = mock.patch('koji_cli.commands.activate_session').start() - self.unique_path = mock.patch('koji_cli.commands._unique_path').start() + self.unique_path = mock.patch('koji_cli.commands.unique_path').start() self.running_in_bg = mock.patch('koji_cli.commands._running_in_bg').start() self.watch_tasks = mock.patch('koji_cli.commands.watch_tasks').start() diff --git a/tests/test_cli/test_unique_path.py b/tests/test_cli/test_unique_path.py index 6fa0db81..a223b9a1 100644 --- a/tests/test_cli/test_unique_path.py +++ b/tests/test_cli/test_unique_path.py @@ -6,17 +6,17 @@ except ImportError: from six.moves import range -from koji_cli.lib import _unique_path +from koji_cli.lib import unique_path class TestUniquePath(unittest.TestCase): def test_unique_path(self): for i in range(1000): self.assertNotEqual( - _unique_path('prefix'), - _unique_path('prefix')) + unique_path('prefix'), + unique_path('prefix')) self.assertRegexpMatches( - _unique_path('prefix'), + unique_path('prefix'), '^prefix/\d{10}\.\d{1,7}\.[a-zA-Z]{8}$') if __name__ == '__main__':