Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/js/src/make_umd.py
16337 views
1
###############################################################################
2
#
3
# IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
#
5
# By downloading, copying, installing or using the software you agree to this license.
6
# If you do not agree to this license, do not download, install,
7
# copy or use the software.
8
#
9
#
10
# License Agreement
11
# For Open Source Computer Vision Library
12
#
13
# Copyright (C) 2013, OpenCV Foundation, all rights reserved.
14
# Third party copyrights are property of their respective owners.
15
#
16
# Redistribution and use in source and binary forms, with or without modification,
17
# are permitted provided that the following conditions are met:
18
#
19
# * Redistribution's of source code must retain the above copyright notice,
20
# this list of conditions and the following disclaimer.
21
#
22
# * Redistribution's in binary form must reproduce the above copyright notice,
23
# this list of conditions and the following disclaimer in the documentation
24
# and/or other materials provided with the distribution.
25
#
26
# * The name of the copyright holders may not be used to endorse or promote products
27
# derived from this software without specific prior written permission.
28
#
29
# This software is provided by the copyright holders and contributors "as is" and
30
# any express or implied warranties, including, but not limited to, the implied
31
# warranties of merchantability and fitness for a particular purpose are disclaimed.
32
# In no event shall the Intel Corporation or contributors be liable for any direct,
33
# indirect, incidental, special, exemplary, or consequential damages
34
# (including, but not limited to, procurement of substitute goods or services;
35
# loss of use, data, or profits; or business interruption) however caused
36
# and on any theory of liability, whether in contract, strict liability,
37
# or tort (including negligence or otherwise) arising in any way out of
38
# the use of this software, even if advised of the possibility of such damage.
39
#
40
41
###############################################################################
42
# AUTHOR: Sajjad Taheri, University of California, Irvine. sajjadt[at]uci[dot]edu
43
#
44
# LICENSE AGREEMENT
45
# Copyright (c) 2015, 2015 The Regents of the University of California (Regents)
46
#
47
# Redistribution and use in source and binary forms, with or without
48
# modification, are permitted provided that the following conditions are met:
49
# 1. Redistributions of source code must retain the above copyright
50
# notice, this list of conditions and the following disclaimer.
51
# 2. Redistributions in binary form must reproduce the above copyright
52
# notice, this list of conditions and the following disclaimer in the
53
# documentation and/or other materials provided with the distribution.
54
# 3. Neither the name of the University nor the
55
# names of its contributors may be used to endorse or promote products
56
# derived from this software without specific prior written permission.
57
#
58
# THIS SOFTWARE IS PROVIDED BY COPYRIGHT HOLDERS AND CONTRIBUTORS ''AS IS'' AND ANY
59
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
60
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
61
# DISCLAIMED. IN NO EVENT SHALL COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY
62
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
63
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
64
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
65
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
66
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
67
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68
###############################################################################
69
import os, sys, re, json, shutil
70
from subprocess import Popen, PIPE, STDOUT
71
72
def make_umd(opencvjs, cvjs):
73
src = open(opencvjs, 'r+b')
74
dst = open(cvjs, 'w+b')
75
content = src.read()
76
dst.seek(0)
77
# inspired by https://github.com/umdjs/umd/blob/95563fd6b46f06bda0af143ff67292e7f6ede6b7/templates/returnExportsGlobal.js
78
dst.write(("""
79
(function (root, factory) {
80
if (typeof define === 'function' && define.amd) {
81
// AMD. Register as an anonymous module.
82
define(function () {
83
return (root.cv = factory());
84
});
85
} else if (typeof module === 'object' && module.exports) {
86
// Node. Does not work with strict CommonJS, but
87
// only CommonJS-like environments that support module.exports,
88
// like Node.
89
module.exports = factory();
90
} else {
91
// Browser globals
92
root.cv = factory();
93
}
94
}(this, function () {
95
%s
96
if (typeof Module === 'undefined')
97
Module = {};
98
return cv(Module);
99
}));
100
""" % (content)).lstrip())
101
102
if __name__ == "__main__":
103
if len(sys.argv) > 2:
104
opencvjs = sys.argv[1]
105
cvjs = sys.argv[2]
106
if not os.path.isfile(opencvjs):
107
print('opencv.js file not found! Have you compiled the opencv_js module?')
108
exit()
109
make_umd(opencvjs, cvjs);
110
111