Ignore parameters in PKL type strings when checking them.
[libdcp.git] / test / ref / make.py
1 #!/usr/bin/python
2
3 #
4 # This slightly ridiculous script gets OpenDCP to generate
5 # a DCP using out test reference data (in j2c/ and wav/)
6 # and then adjusts its XML output to account for the fact
7 # that OpenDCP will generate its own random UUIDs (and use
8 # current timestamps).  We set UUIDs and timestamps back
9 # to what our test suite will expect.
10 #
11 # The output of this script is checked into git, so
12 # there's normally no need to run it.
13 #
14 # If you do run it, the XML should be right but the 
15 # MXFs that OpenDCP generates will not be quite what
16 # we expect, as they also contain random UUIDs.  I don't
17 # think there's an easy way round that, so after running
18 # this script you will need to check that the libdcp
19 # test code generates correct MXFs (by verification on
20 # a projector, probably), and then copy those MXFs into the
21 # test/ref/DCP directory.
22 #
23
24 import os
25 import sys
26 import fileinput
27 from lxml import etree
28
29 # Namespaces for the various XML files
30 assetmap_namespace = 'http://www.smpte-ra.org/schemas/429-9/2007/AM'
31 cpl_namespace = 'http://www.smpte-ra.org/schemas/429-7/2006/CPL'
32 pkl_namespace = 'http://www.smpte-ra.org/schemas/429-8/2007/PKL'
33
34 # The UUIDs of things that we want to put into the
35 # OpenDCP-generated XML
36 wanted_cpl_id = '9892e944-5046-4dbb-af7c-f50742f62fc2'
37 wanted_pkl_id = 'df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb'
38 wanted_assetmap_id = 'b135d5cf-d180-43d8-b0b3-7373737b73bf'
39 wanted_video_mxf_id = '81fb54df-e1bf-4647-8788-ea7ba154375b'
40 wanted_audio_mxf_id = '67b9341e-cadd-4dac-9d5c-f5a1d59f2d06'
41 wanted_reel_id = '379fa64c-ad71-46cf-bef7-b45624006610'
42
43 # The hashes of the assets: first is the video MXF, second the audio MXF and third the CPL.
44 wanted_asset_hashes = ['VB9LCTmiD9OLlw4SvrEWUm5d67Q=', 'HapNpn7MjiJLa1OHRI61Rx8N/is=', 'PbXuvpUOKccTLMxg/lEbaXvNCT4=']
45
46 # The issue date that we want to use
47 wanted_issue_date = '2012-07-17T04:45:18+00:00'
48
49 # Get OpenDCP to make the DCP
50 os.system('rm -rf DCP')
51 os.mkdir('DCP')
52 os.system('opendcp_mxf -i j2c -o DCP/video.mxf -r 24')
53 os.system('opendcp_mxf -i wav -o DCP/audio.mxf -r 24')
54 os.system('opendcp_xml --reel DCP/video.mxf DCP/audio.mxf -k feature -t "A Test DCP" -a "A Test DCP"')
55 os.system('mv *.xml DCP')
56
57 # Find what IDs it used
58 cpl_id = None
59 pkl_id = None
60 assetmap_id = None
61 video_mxf_id = None
62 audio_mxf_id = None
63 reel_id = None
64
65 for r, d, f in os.walk('DCP'):
66     for n in f:
67         if n.endswith('cpl.xml'):
68             cpl_id = n[0:-8]
69         elif n.endswith('pkl.xml'):
70             pkl_id = n[0:-8]
71
72 # (along the way, rename the CPL/PKL files)
73 os.rename('DCP/%s_cpl.xml' % cpl_id, 'DCP/%s_cpl.xml' % wanted_cpl_id)
74 os.rename('DCP/%s_pkl.xml' % pkl_id, 'DCP/%s_pkl.xml' % wanted_pkl_id)
75
76 xml = etree.parse('DCP/ASSETMAP.xml')
77 assetmap_id = xml.getroot().find('{%s}Id' % assetmap_namespace).text
78 assetmap_id = assetmap_id.replace('urn:uuid:', '')
79
80 def cpl_name(s):
81     return '{%s}%s' % (cpl_namespace, s)
82
83 xml = etree.parse('DCP/%s_cpl.xml' % wanted_cpl_id)
84
85 video_mxf_id = xml.getroot().find(cpl_name('ReelList')).    \
86                              find(cpl_name('Reel')).        \
87                              find(cpl_name('AssetList')).   \
88                              find(cpl_name('MainPicture')). \
89                              find(cpl_name('Id')).text
90 video_mxf_id = video_mxf_id.replace('urn:uuid:', '')
91
92 audio_mxf_id = xml.getroot().find(cpl_name('ReelList')).    \
93                              find(cpl_name('Reel')).        \
94                              find(cpl_name('AssetList')).   \
95                              find(cpl_name('MainSound')). \
96                              find(cpl_name('Id')).text
97 audio_mxf_id = audio_mxf_id.replace('urn:uuid:', '')
98
99 reel_id =      xml.getroot().find(cpl_name('ReelList')).    \
100                              find(cpl_name('Reel')).        \
101                              find(cpl_name('Id')).text
102 reel_id = reel_id.replace('urn:uuid:', '')
103
104 def pkl_name(s):
105     return '{%s}%s' % (pkl_namespace, s)
106
107 xml = etree.parse('DCP/%s_pkl.xml' % wanted_pkl_id)
108
109 asset_list =   xml.getroot().find(pkl_name('AssetList'))
110 asset_hashes = []
111 for a in asset_list.iter():
112     if a.tag == "{%s}Hash" % pkl_namespace:
113         asset_hashes.append(a.text)
114
115 issue_date =    xml.getroot().find(pkl_name('IssueDate')).text
116
117 # Now run through the XML files doing the replacements
118 for r, d, f in os.walk('DCP'):
119     for n in f:
120         if n.endswith('.xml'):
121             for line in fileinput.input(os.path.join(r, n), inplace = 1):
122                 line = line.replace(cpl_id, wanted_cpl_id)
123                 line = line.replace(pkl_id, wanted_pkl_id)
124                 line = line.replace(assetmap_id, wanted_assetmap_id)
125                 line = line.replace(video_mxf_id, wanted_video_mxf_id)
126                 line = line.replace(audio_mxf_id, wanted_audio_mxf_id)
127                 line = line.replace(reel_id, wanted_reel_id)
128                 line = line.replace(issue_date, wanted_issue_date)
129                 for i in range(0, len(asset_hashes)):
130                     line = line.replace(asset_hashes[i], wanted_asset_hashes[i])
131                 print line,
132                 
133