Make -S parameter to dcpomatic2_kdm_cli also filter screens (#2324).
authorCarl Hetherington <cth@carlh.net>
Thu, 8 Sep 2022 23:48:52 +0000 (01:48 +0200)
committerCarl Hetherington <cth@carlh.net>
Thu, 8 Sep 2022 23:54:38 +0000 (01:54 +0200)
src/lib/kdm_cli.cc
test/kdm_cli_test.cc

index 56977b52160811b20c966ca60232ed945abc8de7..79e65f9de4a2dcba16324aac9779d7357df13171 100644 (file)
@@ -423,7 +423,7 @@ try
        optional<string> cinema_name;
        shared_ptr<Cinema> cinema;
        optional<boost::filesystem::path> certificate;
-       string screen;
+       optional<string> screen;
        list<shared_ptr<Screen>> screens;
        optional<dcp::EncryptedKDM> dkdm;
        optional<boost::posix_time::ptime> valid_from;
@@ -440,6 +440,9 @@ try
 
        program_name = argv[0];
 
+       /* Reset getopt() so we can call this method several times in one test process */
+       optind = 1;
+
        int option_index = 0;
        while (true) {
                static struct option long_options[] = {
@@ -535,6 +538,9 @@ try
                        cinema = make_shared<Cinema>(optarg, list<string>(), "", 0, 0);
                        break;
                case 'S':
+                       /* Similarly, this could be the name of a new (temporary) screen or the name of a screen
+                        * to search for.
+                        */
                        screen = optarg;
                        break;
                case 'C':
@@ -558,7 +564,7 @@ try
        if (certificate) {
                /* Make a new screen and add it to the current cinema */
                dcp::CertificateChain chain(dcp::file_to_string(*certificate));
-               auto screen_to_add = std::make_shared<Screen>(screen, "", chain.leaf(), boost::none, vector<TrustedDevice>());
+               auto screen_to_add = std::make_shared<Screen>(screen.get_value_or(""), "", chain.leaf(), boost::none, vector<TrustedDevice>());
                if (cinema) {
                        cinema->add_screen(screen_to_add);
                }
@@ -596,6 +602,9 @@ try
                }
 
                screens = find_cinema (*cinema_name)->screens ();
+               if (screen) {
+                       screens.erase(std::remove_if(screens.begin(), screens.end(), [&screen](shared_ptr<Screen> s) { return s->name != *screen; }), screens.end());
+               }
        }
 
        if (duration_string) {
index 52b0316235a77a6ee0ff82ba0da0bef132e52a36..107a6210adfcbd05a43fcc192f46cdfc2aad85d1 100644 (file)
 */
 
 
+#include "lib/cinema.h"
+#include "lib/config.h"
 #include "lib/kdm_cli.h"
+#include "lib/screen.h"
+#include "lib/trusted_device.h"
+#include "test.h"
 #include <boost/filesystem.hpp>
 #include <boost/test/unit_test.hpp>
 #include <iostream>
 
 
+using std::list;
 using std::string;
 using std::vector;
+using boost::optional;
+
+
 optional<string>
 run(vector<string> const& args, vector<string>& output)
 {
@@ -66,6 +75,100 @@ BOOST_AUTO_TEST_CASE (kdm_cli_test_certificate)
        BOOST_CHECK (!error);
 
        BOOST_CHECK(boost::filesystem::exists(kdm_filename));
+}
+
+
+static
+void
+setup_test_config()
+{
+       auto config = Config::instance();
+       auto const cert = dcp::Certificate(dcp::file_to_string("test/data/cert.pem"));
+
+       auto cinema_a = std::make_shared<Cinema>("Dean's Screens", list<string>(), "", 0, 0);
+       cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 1", "", cert, boost::none, std::vector<TrustedDevice>()));
+       cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 2", "", cert, boost::none, std::vector<TrustedDevice>()));
+       cinema_a->add_screen(std::make_shared<dcpomatic::Screen>("Screen 3", "", cert, boost::none, std::vector<TrustedDevice>()));
+       config->add_cinema(cinema_a);
+
+       auto cinema_b = std::make_shared<Cinema>("Floyd's Celluloid", list<string>(), "", 0, 0);
+       cinema_b->add_screen(std::make_shared<dcpomatic::Screen>("Foo", "", cert, boost::none, std::vector<TrustedDevice>()));
+       cinema_b->add_screen(std::make_shared<dcpomatic::Screen>("Bar", "", cert, boost::none, std::vector<TrustedDevice>()));
+       config->add_cinema(cinema_b);
+}
+
+
+BOOST_AUTO_TEST_CASE(kdm_cli_select_cinema)
+{
+       ConfigRestorer cr;
+
+       setup_test_config();
+
+       vector<boost::filesystem::path> kdm_filenames = {
+               "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Foo.xml",
+               "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Floyds_Celluloid_Bar.xml"
+       };
+
+       for (auto path: kdm_filenames) {
+               boost::system::error_code ec;
+               boost::filesystem::remove(path, ec);
+       }
+
+       vector<string> args = {
+               "kdm_cli",
+               "--verbose",
+               "--valid-from", "now",
+               "--valid-duration", "2 weeks",
+               "-c", "Floyd's Celluloid",
+               "-o", "build/test",
+               "test/data/dkdm.xml"
+       };
+
+       vector<string> output;
+       auto error = run(args, output);
+       BOOST_CHECK(!error);
 
+       BOOST_REQUIRE_EQUAL(output.size(), 2);
+       BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from"));
+       BOOST_CHECK_EQUAL(output[1], "Wrote 2 KDM files to build/test");
+
+       for (auto path: kdm_filenames) {
+               BOOST_CHECK(boost::filesystem::exists(path));
+       }
 }
 
+
+BOOST_AUTO_TEST_CASE(kdm_cli_select_screen)
+{
+       ConfigRestorer cr;
+
+       setup_test_config();
+
+       boost::filesystem::path kdm_filename = "build/test/KDM_Test_FTR-1_F-133_XX-XX_MOS_2K_20220109_SMPTE_OV_Deans_Screens_Screen_2.xml";
+
+       boost::system::error_code ec;
+       boost::filesystem::remove(kdm_filename, ec);
+
+       vector<string> args = {
+               "kdm_cli",
+               "--verbose",
+               "--valid-from", "now",
+               "--valid-duration", "2 weeks",
+               "-c", "Dean's Screens",
+               "-S", "Screen 2",
+               "-o", "build/test",
+               "test/data/dkdm.xml"
+       };
+
+       vector<string> output;
+       auto error = run(args, output);
+       BOOST_CHECK(!error);
+
+       BOOST_REQUIRE_EQUAL(output.size(), 2);
+       BOOST_CHECK(boost::algorithm::starts_with(output[0], "Making KDMs valid from"));
+       BOOST_CHECK_EQUAL(output[1], "Wrote 1 KDM files to build/test");
+
+       BOOST_CHECK(boost::filesystem::exists(kdm_filename));
+}
+
+